import { formatBytes, formatClock, formatDuration, formatMicros, formatTime } from "@/lib/format";

test("formatTime renders unix seconds in the given locale and zone", () => {
	// 2024-01-01T00:00:00Z; ICU emits U+202F before AM/PM in recent Node.
	expect(formatTime(1704067200, "en-US", "UTC").replace(/ /g, " ")).toBe("Jan 1, 2024, 12:00:00 AM");
});

test("formatClock states the time of day alone, for a stamp read against now", () => {
	expect(formatClock(Date.UTC(2026, 0, 1, 14, 5) / 1000, "en-GB", "UTC")).toBe("14:05");
	expect(formatClock(Date.UTC(2026, 0, 1, 9, 30) / 1000, "en-GB", "UTC")).toBe("09:30");
	// No date: the caller places it against now, and a date would be noise.
	expect(formatClock(Date.UTC(2026, 0, 1, 14, 5) / 1000, "en-GB", "UTC")).not.toMatch(/2026/);
});

test("formatBytes humanizes with binary units", () => {
	expect(formatBytes(0)).toBe("0 B");
	expect(formatBytes(1023)).toBe("1023 B");
	expect(formatBytes(1024)).toBe("1.0 KiB");
	expect(formatBytes(1536)).toBe("1.5 KiB");
	expect(formatBytes(5 * 1024 * 1024)).toBe("5.0 MiB");
	expect(formatBytes(3 * 1024 * 1024 * 1024)).toBe("3.0 GiB");
	expect(formatBytes(2 * 1024 ** 4)).toBe("2.0 TiB");
});

test("formatDuration steps up a unit at each boundary and truncates", () => {
	expect(formatDuration(0)).toBe("0s");
	expect(formatDuration(59)).toBe("59s");
	expect(formatDuration(60)).toBe("1m");
	expect(formatDuration(3599)).toBe("59m");
	expect(formatDuration(3600)).toBe("1h");
	expect(formatDuration(10800)).toBe("3h");
	expect(formatDuration(86399)).toBe("23h");
	expect(formatDuration(86400)).toBe("1d");
	expect(formatDuration(400000)).toBe("4d");
});

test("formatDuration is never negative", () => {
	// Clock skew between the server's timestamps and the browser's clock.
	expect(formatDuration(-5)).toBe("0s");
});

test("formatMicros renders milliseconds with one decimal", () => {
	expect(formatMicros(0)).toBe("0.0 ms");
	expect(formatMicros(1234)).toBe("1.2 ms");
	expect(formatMicros(999)).toBe("1.0 ms");
	expect(formatMicros(2_500_000)).toBe("2500.0 ms");
});
