Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageFormat.test.ts

54 lines2.1 KB

1// @effect-diagnostics globalDate:off -- A fixed instant keeps calendar-window assertions deterministic.
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4import { formatHourShort, makeWindow } from "./usageFormat.ts";
5​
6describe("hourly usage formatting", () => {
7 it("keeps requested zones separate when formatting repeated calls", () => {
8 const instant = "2026-08-11T12:37:00.000Z";
9 for (const zone of ["UTC", "America/New_York", "Asia/Kathmandu", "UTC"]) {
10 expect(formatHourShort(instant, zone)).toBe(
11 new Intl.DateTimeFormat("en-US", { timeZone: zone, hour: "numeric" }).format(
12 new Date(instant),
13 ),
14 );
15 }
16 expect(() => formatHourShort(instant, "Etc/Unknown")).toThrow(RangeError);
17 expect(formatHourShort("invalid", "UTC")).toBe("invalid");
18 });
19​
20 it("disambiguates repeated hours during a fall-back transition", () => {
21 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York")).toBe("1 AM EDT");
22 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
23 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York", false)).toBe(
24 "01:00 EDT",
25 );
26 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York", false)).toBe(
27 "01:00 EST",
28 );
29 });
30​
31 it("builds an exact minute-aligned 24-hour request", () => {
32 const window = makeWindow(1, new Date("2026-08-11T12:37:42.123Z"), "hour");
33​
34 expect(window.resolution).toBe("hour");
35 expect(window.sinceTime).toBe("2026-08-10T12:37:00.000Z");
36 expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
37 });
38​
39 it("degrades an unknown resolved zone to UTC instead of crashing", () => {
40 const resolved = new Intl.DateTimeFormat().resolvedOptions();
41 const resolvedOptions = vi
42 .spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions")
43 .mockReturnValue({ ...resolved, timeZone: "Etc/Unknown" });
44​
45 try {
46 const now = new Date("2026-08-11T12:37:42.123Z");
47​
48 expect(makeWindow(1, now, "hour").timeZone).toBe("UTC");
49 expect(makeWindow(30, now).timeZone).toBe("UTC");
50 } finally {
51 resolvedOptions.mockRestore();
52 }
53 });
54});