Open History to see the last commit
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 formatPercent,
9 formatRelativeHourShort,
10 makeWindow,
11} from "./usageFormat.ts";
12
13describe("formatPercent", () => {
14 it("distinguishes a small positive share from zero", () => {
15 expect(formatPercent(0)).toBe("0.0%");
16 expect(formatPercent(0.0004)).toBe("<0.1%");
17 expect(formatPercent(0.0009)).toBe("<0.1%");
18 expect(formatPercent(0.001)).toBe("0.1%");
19 expect(formatPercent(0.023)).toBe("2.3%");
20 expect(formatPercent(0.00004, 2)).toBe("<0.01%");
21 });
22});
23
24describe("hourly usage formatting", () => {
25 it("keeps requested zones separate when formatting repeated calls", () => {
26 const instant = "2026-08-11T12:37:00.000Z";
27 for (const zone of ["UTC", "America/New_York", "Asia/Kathmandu", "UTC"]) {
28 expect(formatHourShort(instant, zone)).toBe(
29 new Intl.DateTimeFormat("en-US", { timeZone: zone, hour: "numeric" }).format(
30 new Date(instant),
31 ),
32 );
33 }
34 expect(() => formatHourShort(instant, "Etc/Unknown")).toThrow(RangeError);
35 expect(formatHourShort("invalid", "UTC")).toBe("invalid");
36 });
37
38 it("uses the current system zone when no zone is supplied", () => {
39 try {
40 vi.stubEnv("TZ", "UTC");
41 expect(formatHourShort("2026-08-11T12:37:00.000Z")).toBe("12 PM");
42 vi.stubEnv("TZ", "America/New_York");
43 expect(formatHourShort("2026-08-11T12:37:00.000Z")).toBe("8 AM");
44 } finally {
45 vi.unstubAllEnvs();
46 }
47 });
48
49 it("enumerates 24 fixed buckets across a rolling window", () => {
50 const hours = enumerateHourStarts("2026-08-10T12:37:00.000Z", "2026-08-11T12:37:00.000Z");
51
52 expect(hours).toHaveLength(24);
53 expect(hours[0]).toBe("2026-08-10T12:37:00.000Z");
54 expect(hours[23]).toBe("2026-08-11T11:37:00.000Z");
55 });
56
57 it("formats rolling instants in the requested time zone", () => {
58 expect(formatHourShort("2026-08-11T00:37:00.000Z", "UTC")).toBe("12 AM");
59 expect(formatHourShort("2026-08-11T12:37:00.000Z", "UTC")).toBe("12 PM");
60 expect(formatDateTimeShort("2026-08-11T17:37:00.000Z", "UTC")).toBe("Aug 11, 5 PM");
61 });
62
63 it("disambiguates repeated hours during a fall-back transition", () => {
64 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York")).toBe("1 AM EDT");
65 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
66 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York", false)).toBe(
67 "01:00 EDT",
68 );
69 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York", false)).toBe(
70 "01:00 EST",
71 );
72 });
73
74 it.each([
75 [true, "2 PM", "12 AM"],
76 [false, "14:00", "00:00"],
77 ])("formats hourly usage labels with hour12=%s", (hour12, afternoon, midnight) => {
78 const instant = "2026-08-11T14:37:00.000Z";
79
80 expect(formatHourShort(instant, "UTC", hour12)).toBe(afternoon);
81 expect(formatHourShort("2026-08-11T00:37:00.000Z", "UTC", hour12)).toBe(midnight);
82 expect(formatDateTimeShort(instant, "UTC", hour12)).toBe(`Aug 11, ${afternoon}`);
83 expect(formatRelativeHourShort(instant, instant, "UTC", hour12)).toBe(`${afternoon} today`);
84 expect(formatRelativeHourShort(instant, "2026-08-12T15:37:00.000Z", "UTC", hour12)).toBe(
85 `${afternoon} yesterday`,
86 );
87 expect(formatRelativeHourShort(instant, "2026-08-14T15:37:00.000Z", "UTC", hour12)).toBe(
88 `Aug 11, ${afternoon}`,
89 );
90 });
91
92 it("makes hourly tooltip dates relative to the window in its requested time zone", () => {
93 const windowEnd = "2026-08-11T14:37:00.000Z";
94
95 expect(formatRelativeHourShort("2026-08-10T17:37:00.000Z", windowEnd, "UTC")).toBe(
96 "5 PM yesterday",
97 );
98 expect(formatRelativeHourShort("2026-08-11T14:37:00.000Z", windowEnd, "UTC")).toBe(
99 "2 PM today",
100 );
101 expect(
102 formatRelativeHourShort(
103 "2026-08-11T01:37:00.000Z",
104 "2026-08-11T10:37:00.000Z",
105 "America/Los_Angeles",
106 ),
107 ).toBe("6 PM yesterday");
108 });
109
110 it("builds an exact minute-aligned 24-hour request", () => {
111 const window = makeWindow(1, new Date("2026-08-11T12:37:42.123Z"), "hour");
112
113 expect(window.resolution).toBe("hour");
114 expect(window.sinceTime).toBe("2026-08-10T12:37:00.000Z");
115 expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
116 });
117
118 it("degrades an unknown resolved zone to UTC instead of crashing", () => {
119 const resolved = new Intl.DateTimeFormat().resolvedOptions();
120 const resolvedOptions = vi
121 .spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions")
122 .mockReturnValue({ ...resolved, timeZone: "Etc/Unknown" });
123
124 try {
125 const now = new Date("2026-08-11T12:37:42.123Z");
126
127 expect(makeWindow(1, now, "hour").timeZone).toBe("UTC");
128 expect(makeWindow(30, now).timeZone).toBe("UTC");
129 } finally {
130 resolvedOptions.mockRestore();
131 }
132 });
133});