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

usageFormat.test.ts

57 lines2.1 KB

1// @effect-diagnostics globalDate:off -- A fixed instant keeps calendar-window assertions deterministic.
2import { describe, expect, it } 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("enumerates 24 fixed buckets across a rolling window", () => {
14 const hours = enumerateHourStarts("2026-08-10T12:37:00.000Z", "2026-08-11T12:37:00.000Z");
15​
16 expect(hours).toHaveLength(24);
17 expect(hours[0]).toBe("2026-08-10T12:37:00.000Z");
18 expect(hours[23]).toBe("2026-08-11T11:37:00.000Z");
19 });
20​
21 it("formats rolling instants in the requested time zone", () => {
22 expect(formatHourShort("2026-08-11T00:37:00.000Z", "UTC")).toBe("12 AM");
23 expect(formatHourShort("2026-08-11T12:37:00.000Z", "UTC")).toBe("12 PM");
24 expect(formatDateTimeShort("2026-08-11T17:37:00.000Z", "UTC")).toBe("Aug 11, 5 PM");
25 });
26​
27 it("disambiguates repeated hours during a fall-back transition", () => {
28 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York")).toBe("1 AM EDT");
29 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
30 });
31​
32 it("makes hourly tooltip dates relative to the window in its requested time zone", () => {
33 const windowEnd = "2026-08-11T14:37:00.000Z";
34​
35 expect(formatRelativeHourShort("2026-08-10T17:37:00.000Z", windowEnd, "UTC")).toBe(
36 "5 PM yesterday",
37 );
38 expect(formatRelativeHourShort("2026-08-11T14:37:00.000Z", windowEnd, "UTC")).toBe(
39 "2 PM today",
40 );
41 expect(
42 formatRelativeHourShort(
43 "2026-08-11T01:37:00.000Z",
44 "2026-08-11T10:37:00.000Z",
45 "America/Los_Angeles",
46 ),
47 ).toBe("6 PM yesterday");
48 });
49​
50 it("builds an exact minute-aligned 24-hour request", () => {
51 const window = makeWindow(1, new Date("2026-08-11T12:37:42.123Z"), "hour");
52​
53 expect(window.resolution).toBe("hour");
54 expect(window.sinceTime).toBe("2026-08-10T12:37:00.000Z");
55 expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
56 });
57});