/** * How the figures of an integration are written on the page — numbers, sizes, speeds, money — and how a change over a stretch is put into words. * * Run with: npm test --workspace server */ import { test, describe } from "node:test"; import assert from "node:assert/strict"; import { axisFigure, changeWords, formatBytes, formatFigure } from "../../web/src/utils/figureUnits.js"; describe("writing a figure", () => { test("sizes count in thousands, with a decimal only while it is small", () => { assert.equal(formatBytes(0), "0 B"); assert.equal(formatBytes(999), "999 B"); assert.equal(formatBytes(1_536), "1.5 kB"); assert.equal(formatBytes(15_360_000), "15 MB"); assert.equal(formatBytes(5e9), "5 GB"); assert.equal(formatBytes(2.5e12), "2.5 TB"); assert.equal(formatBytes(-2_000), "-2 kB"); assert.equal(formatBytes(Number.NaN), "—"); }); test("each unit reads the way it should", () => { assert.equal(formatFigure("count", 12345), "12,345"); assert.equal(formatFigure("count", 7.04), "7"); assert.equal(formatFigure("percent", 41.56), "41.6%"); assert.equal(formatFigure("ms", 11.5), "11.5 ms"); assert.equal(formatFigure("bytes", 5e9), "5 GB"); assert.equal(formatFigure("bytes_per_s", 5_000_000), "5 MB/s"); assert.equal(formatFigure("money:EUR", 42.4), "€42"); assert.equal(formatFigure("money:XXXX", 42.4), "42 XXXX", "an unknown currency is still a number"); assert.equal(formatFigure("count", Number.POSITIVE_INFINITY), "—"); }); }); describe("a figure in a chart's margin", () => { test("short, so it fits", () => { assert.equal(axisFigure("count", 12_500), "12.5K"); assert.equal(axisFigure("count", 950), "950"); assert.equal(axisFigure("percent", 41.6), "42%"); assert.equal(axisFigure("bytes", 340e9), "340 GB"); assert.equal(axisFigure("money:EUR", 1200), "EUR 1.2K"); assert.equal(axisFigure("count", Number.NaN), ""); }); }); describe("how a figure moved", () => { test("up, down and unchanged, in the unit's own terms", () => { assert.equal(changeWords("count", 100, 220), "up 120"); assert.equal(changeWords("count", 100, 99), "down 1"); assert.equal(changeWords("count", 100, 100.2), "unchanged"); assert.equal(changeWords("percent", 40, 43.5), "up 3.5 points"); assert.equal(changeWords("percent", 40, 39.98), "unchanged"); assert.equal(changeWords("bytes", 5e9, 5.5e9), "up 500 MB"); assert.equal(changeWords("bytes", 5e9, 5.00001e9), "unchanged", "a rounding-sized change in a big number is not a change"); assert.equal(changeWords("ms", 10, 7), "down 3 ms"); assert.equal(changeWords("count", null, 5), ""); assert.equal(changeWords("count", 5, null), ""); }); });