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

usageFormat.test.ts

97 lines3.5 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 {
5 enumerateHourStarts,
6 formatDateTimeShort,
7 formatHourShort,
8 formatRelativeHourShort,
9 makeWindow,
10} from "./usageFormat.ts";
11​
12describe("hourly usage formatting", () => {
13 it("keeps requested zones separate when formatting repeated calls", () => {
14 const instant = "2026-08-11T12:37:00.000Z";
15 for (const zone of ["UTC", "America/New_York", "Asia/Kathmandu", "UTC"]) {
16 expect(formatHourShort(instant, zone)).toBe(
17 new Intl.DateTimeFormat("en-US", { timeZone: zone, hour: "numeric" }).format(
18 new Date(instant),
19 ),
20 );
21 }
22 expect(() => formatHourShort(instant, "Etc/Unknown")).toThrow(RangeError);
23 expect(formatHourShort("invalid", "UTC")).toBe("invalid");
24 });
25​
26 it("uses the current system zone when no zone is supplied", () => {
27 try {
28 vi.stubEnv("TZ", "UTC");
29 expect(formatHourShort("2026-08-11T12:37:00.000Z")).toBe("12 PM");
30 vi.stubEnv("TZ", "America/New_York");
31 expect(formatHourShort("2026-08-11T12:37:00.000Z")).toBe("8 AM");
32 } finally {
33 vi.unstubAllEnvs();
34 }
35 });
36​
37 it("enumerates 24 fixed buckets across a rolling window", () => {
38 const hours = enumerateHourStarts("2026-08-10T12:37:00.000Z", "2026-08-11T12:37:00.000Z");
39​
40 expect(hours).toHaveLength(24);
41 expect(hours[0]).toBe("2026-08-10T12:37:00.000Z");
42 expect(hours[23]).toBe("2026-08-11T11:37:00.000Z");
43 });
44​
45 it("formats rolling instants in the requested time zone", () => {
46 expect(formatHourShort("2026-08-11T00:37:00.000Z", "UTC")).toBe("12 AM");
47 expect(formatHourShort("2026-08-11T12:37:00.000Z", "UTC")).toBe("12 PM");
48 expect(formatDateTimeShort("2026-08-11T17:37:00.000Z", "UTC")).toBe("Aug 11, 5 PM");
49 });
50​
51 it("disambiguates repeated hours during a fall-back transition", () => {
52 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York")).toBe("1 AM EDT");
53 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
54 });
55​
56 it("makes hourly tooltip dates relative to the window in its requested time zone", () => {
57 const windowEnd = "2026-08-11T14:37:00.000Z";
58​
59 expect(formatRelativeHourShort("2026-08-10T17:37:00.000Z", windowEnd, "UTC")).toBe(
60 "5 PM yesterday",
61 );
62 expect(formatRelativeHourShort("2026-08-11T14:37:00.000Z", windowEnd, "UTC")).toBe(
63 "2 PM today",
64 );
65 expect(
66 formatRelativeHourShort(
67 "2026-08-11T01:37:00.000Z",
68 "2026-08-11T10:37:00.000Z",
69 "America/Los_Angeles",
70 ),
71 ).toBe("6 PM yesterday");
72 });
73​
74 it("builds an exact minute-aligned 24-hour request", () => {
75 const window = makeWindow(1, new Date("2026-08-11T12:37:42.123Z"), "hour");
76​
77 expect(window.resolution).toBe("hour");
78 expect(window.sinceTime).toBe("2026-08-10T12:37:00.000Z");
79 expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
80 });
81​
82 it("degrades an unknown resolved zone to UTC instead of crashing", () => {
83 const resolved = new Intl.DateTimeFormat().resolvedOptions();
84 const resolvedOptions = vi
85 .spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions")
86 .mockReturnValue({ ...resolved, timeZone: "Etc/Unknown" });
87​
88 try {
89 const now = new Date("2026-08-11T12:37:42.123Z");
90​
91 expect(makeWindow(1, now, "hour").timeZone).toBe("UTC");
92 expect(makeWindow(30, now).timeZone).toBe("UTC");
93 } finally {
94 resolvedOptions.mockRestore();
95 }
96 });
97});