Files
HealthLog/src/lib/format/__tests__/bytes.test.ts
T
Marc-André Bombeck 56daa57350 refactor(format): one byte formatter for every surface
The document vault and the admin area each carried a formatBytes; the
same backup read 1.25 MB in one and 1.2 MB in the other, and only the
vault knew gigabytes. The vault rule (one decimal below 100, up to GB,
digits in the reader's locale) now lives in src/lib/format/bytes.ts and
takes the locale, so the server-rendered share page uses it too. A test
holds the definition to that one module.
2026-10-20 12:55:06 +02:00

54 lines
1.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { readFileSync, readdirSync, statSync } from "node:fs";
import { join } from "node:path";
import { formatBytes } from "../bytes";
describe("formatBytes", () => {
it("scales units and keeps one decimal below 100", () => {
expect(formatBytes(0, "en")).toBe("0 B");
expect(formatBytes(512, "en")).toBe("512 B");
expect(formatBytes(1_310_720, "en")).toBe("1.3 MB");
expect(formatBytes(26_214_400, "en")).toBe("25 MB");
expect(formatBytes(1_288_490_189, "en")).toBe("1.2 GB");
expect(formatBytes(150 * 1024 * 1024, "en")).toBe("150 MB");
});
it("prints the digits in the reader's locale", () => {
expect(formatBytes(1_288_490_189, "de")).toBe("1,2 GB");
expect(formatBytes(1_288_490_189, "de-DE")).toBe("1,2 GB");
expect(formatBytes(1_310_720, "fr")).toBe("1,3 MB");
});
it("never prints a negative size", () => {
expect(formatBytes(-5, "en")).toBe("0 B");
});
});
/**
* One byte formatter. A second definition is how the admin area and the
* document vault came to print the same size two ways.
*/
describe("a single byte formatter", () => {
it("is defined only in src/lib/format/bytes.ts", () => {
const root = join(__dirname, "../../..");
const hits: string[] = [];
const walk = (dir: string) => {
for (const name of readdirSync(dir)) {
if (name === "node_modules" || name === "generated") continue;
const path = join(dir, name);
if (statSync(path).isDirectory()) {
walk(path);
} else if (/\.tsx?$/.test(name) && !/\.test\.tsx?$/.test(name)) {
const source = readFileSync(path, "utf8");
if (/function\s+formatBytes\b|const\s+formatBytes\s*=/.test(source)) {
hits.push(path.slice(root.length + 1));
}
}
}
};
walk(root);
expect(hits).toEqual(["lib/format/bytes.ts"]);
});
});