Files
kandev/apps/web/lib/utils/format-bytes.test.ts
T
João Salavisa b082d5e1b9 refactor(web): use the shared formatBytes in chat attachments (#2071)
Delete the two byte-identical local formatBytes copies from the chat-attachment
modules and repoint all call sites at the canonical lib/utils/format-bytes.ts,
which its own doc comment asks new code to use.

The copies had no upper clamp (>= 1 TB rendered "1 undefined") and no NaN or
negative guard (NaN rendered "NaN undefined"). Size labels now carry one
decimal ("10.0 MB" instead of "10 MB") because the canonical helper does not
strip a trailing zero; unit and e2e expectations updated to match.
2026-07-30 22:40:50 +01:00

42 lines
1.2 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { formatBytes } from "./format-bytes";
describe("formatBytes", () => {
it("returns '-' for nullish input", () => {
expect(formatBytes(null)).toBe("-");
expect(formatBytes(undefined)).toBe("-");
});
it("returns '0 B' for zero or negative input", () => {
expect(formatBytes(0)).toBe("0 B");
expect(formatBytes(-1)).toBe("0 B");
});
it("renders integer bytes below 1 KB", () => {
expect(formatBytes(1)).toBe("1 B");
expect(formatBytes(1023)).toBe("1023 B");
});
it("renders KB with one decimal", () => {
expect(formatBytes(1024)).toBe("1.0 KB");
expect(formatBytes(2048)).toBe("2.0 KB");
});
it("renders MB / GB / TB", () => {
expect(formatBytes(1024 * 1024)).toBe("1.0 MB");
expect(formatBytes(1024 * 1024 * 1024)).toBe("1.0 GB");
expect(formatBytes(1024 ** 4)).toBe("1.0 TB");
});
it("clamps to TB above the largest unit", () => {
expect(formatBytes(1024 ** 5)).toBe("1024.0 TB");
expect(formatBytes(1024 ** 6)).toBe("1048576.0 TB");
});
it("returns '-' for non-finite input", () => {
expect(formatBytes(NaN)).toBe("-");
expect(formatBytes(Infinity)).toBe("-");
expect(formatBytes(-Infinity)).toBe("-");
});
});