/** * The drawing of the small lines in the rows of a list: where the points go, that a gap is a gap, that a lone reading is a dot, the fixed scale of a percentage, and the sentence for a screen reader. * * Run with: npm test --workspace server */ import { test, describe } from "node:test"; import assert from "node:assert/strict"; import { sparkGeometry, sparkTicks, sparkWords } from "../../web/src/utils/sparkline.js"; describe("the points", () => { test("oldest at the left, newest at the right, the highest reading at the top; the last reading is marked", () => { const g = sparkGeometry([0, 50, 100], 100, 20, { min: 0, max: 100, pad: 2 }); assert.deepEqual(g.lines, ["M2 18 L50 10 L98 2"]); assert.deepEqual(g.last, { x: 98, y: 2 }); assert.deepEqual([g.min, g.max], [0, 100]); assert.deepEqual(g.dots, []); }); test("a gap in the data is a gap in the line, and a reading alone between gaps is a dot", () => { const g = sparkGeometry([10, 20, null, 40, null, 60, 70], 120, 20, { min: 0, max: 100 }); assert.equal(g.lines.length, 2, "two runs of two"); assert.equal(g.dots.length, 1, "and the lone 40"); assert.ok(g.lines[0].startsWith("M2 ")); assert.deepEqual(g.last, { x: 118, y: 6.8 }, "the last reading, 70 on a scale of 0 to 100"); assert.equal(sparkGeometry([null, null, null], 100, 20).last, null); assert.deepEqual(sparkGeometry([null, null], 100, 20).lines, []); }); test("the scale fits the data unless it is fixed; a flat line is drawn through the middle, not along an edge", () => { const fitted = sparkGeometry([100, 300], 100, 20); assert.deepEqual([fitted.min, fitted.max], [100, 300]); assert.equal(fitted.lines[0], "M2 18 L98 2"); const flat = sparkGeometry([5, 5, 5], 100, 20); assert.deepEqual(flat.lines, ["M2 10 L50 10 L98 10"]); // a fixed scale is not moved by the data, and a reading outside it stays inside the picture const percent = sparkGeometry([5, 5], 100, 20, { min: 0, max: 100 }); assert.equal(percent.lines[0], "M2 17.2 L98 17.2"); assert.equal(sparkGeometry([250], 100, 20, { min: 0, max: 100 }).dots[0].y, 2); }); test("one reading is one dot in the middle", () => { const g = sparkGeometry([42], 100, 20); assert.deepEqual(g.lines, []); assert.deepEqual(g.dots, [{ x: 50, y: 10 }]); }); }); describe("the ticks and the words", () => { test("a tick under each stretch that is marked, at the place of that stretch", () => { assert.deepEqual(sparkTicks([false, true, false, true], 100), [34, 98]); assert.deepEqual(sparkTicks([false, false], 100), []); }); test("the sentence says what it is, over what, where it stands now, the range, and the failures", () => { const pct = (v: number) => `${Math.round(v)}%`; assert.equal(sparkWords("CPU load", "last 24 hours", 12, 3, 40, pct), "CPU load, last 24 hours: 12% now, between 3% and 40%"); assert.equal(sparkWords("Disk", "last week", 50, 50, 50, pct), "Disk, last week: 50% now"); assert.equal(sparkWords("Wiki", "answer time, last 24 hours", 30, 20, 90, (v) => `${v} ms`, 2), "Wiki, answer time, last 24 hours: 30 ms now, between 20 ms and 90 ms, 2 stretches with a failure"); assert.equal(sparkWords("Wiki", "x", 30, 30, 30, pct, 1), "Wiki, x: 30% now, 1 stretch with a failure"); assert.equal(sparkWords("Up", "last week", null, null, null, pct), "Up, last week: nothing read"); }); });