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

usageFormat.test.ts

201 lines7.3 KB

1// @effect-diagnostics globalDate:off -- A fixed instant keeps calendar-window assertions deterministic.
2import { describe, expect, it, vi } from "vite-plus/test";
3import type { UsageSource } from "@t3tools/contracts";
4​
5import {
6 formatDateTimeShort,
7 formatHourShort,
8 formatTokens,
9 makeWindow,
10 usageSourceMessages,
11} from "./usageFormat.ts";
12​
13function malformedSource(count: number, overrides: Partial<UsageSource> = {}): UsageSource {
14 return {
15 fingerprint: {
16 hostId: "demo-host",
17 provider: "codex",
18 resolvedHomePath: "/demo/codex",
19 volumeId: "demo-volume",
20 },
21 status: "partial",
22 scannedFiles: 1,
23 skippedFiles: 0,
24 malformedRecords: count,
25 distinctSessions: 1,
26 message: `${count} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
27 ...overrides,
28 };
30​
31describe("usage source messages", () => {
32 it("combines different skipped-record counts into one warning", () => {
33 const codex = malformedSource(1);
34 const claude = malformedSource(11, {
35 fingerprint: { ...codex.fingerprint, provider: "claude", resolvedHomePath: "/demo/claude" },
36 });
37​
38 expect(usageSourceMessages([codex, claude])).toEqual([
39 "12 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
40 ]);
41 });
42​
43 it.each([
44 { first: 1, second: 1, total: 2 },
45 { first: 1, second: 11, total: 12 },
46 ])(
47 "combines hosts reporting $first and $second skipped records once per source",
48 ({ first, second, total }) => {
49 const source = malformedSource(first);
50 const otherHost = malformedSource(second, {
51 fingerprint: { ...source.fingerprint, hostId: "other-demo-host" },
52 });
53​
54 expect(usageSourceMessages([source, source, otherHost, otherHost])).toEqual([
55 `${total} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
56 ]);
57 expect(usageSourceMessages([source, source])).toEqual([
58 `${first} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
59 ]);
60 expect(usageSourceMessages([otherHost, otherHost])).toEqual([
61 `${second} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
62 ]);
63 },
64 );
65​
66 it("keeps different machines with the same hostname and home path distinct", () => {
67 const source = malformedSource(1);
68 const otherMachine = malformedSource(11, {
69 fingerprint: { ...source.fingerprint, volumeId: "other-demo-volume" },
70 });
71​
72 expect(usageSourceMessages([source, otherMachine])).toEqual([
73 "12 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
74 ]);
75 });
76​
77 it("uses the largest count when connected environments report the same source", () => {
78 const source = malformedSource(1);
79​
80 expect(usageSourceMessages([source, malformedSource(11), source])).toEqual([
81 "11 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
82 ]);
83 });
84​
85 it("retains other diagnostics beside the combined warning", () => {
86 const source = malformedSource(1);
87 const readFailure = "2 transcript path(s) could not be read; usage may be incomplete.";
88 const cursor = malformedSource(0, {
89 fingerprint: { ...source.fingerprint, provider: "cursor" },
90 status: "ok",
91 message: "Cursor history is limited to the last 30 days.",
92 });
93​
94 expect(
95 usageSourceMessages([
96 { ...source, message: `${readFailure} ${source.message}` },
97 { ...source, malformedRecords: 0, message: readFailure },
98 cursor,
99 ]),
100 ).toEqual([
101 readFailure,
102 cursor.message,
103 "1 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
104 ]);
105 });
106​
107 it("includes cached diagnostics when a transcript directory is missing", () => {
108 const source = malformedSource(1);
109 const missing = malformedSource(2, {
110 fingerprint: { ...source.fingerprint, hostId: "other-demo-host" },
111 status: "missing",
112 scannedFiles: 0,
113 skippedFiles: 1,
114 distinctSessions: 0,
115 message:
116 "No transcript directory on this environment. 2 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
117 });
118​
119 expect(usageSourceMessages([source, missing, missing])).toEqual([
120 "No transcript directory on this environment.",
121 "3 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
122 ]);
123 });
124​
125 it("omits empty missing sources and healthy sources", () => {
126 expect(
127 usageSourceMessages([
128 malformedSource(0, { status: "missing" }),
129 malformedSource(1, { status: "ok" }),
130 ]),
131 ).toEqual([]);
132 expect(usageSourceMessages([])).toEqual([]);
133 });
134});
135​
136describe("hourly usage formatting", () => {
137 it("keeps requested zones separate when formatting repeated calls", () => {
138 const instant = "2026-08-11T12:37:00.000Z";
139 for (const zone of ["UTC", "America/New_York", "Asia/Kathmandu", "UTC"]) {
140 expect(formatHourShort(instant, zone)).toBe(
141 new Intl.DateTimeFormat("en-US", { timeZone: zone, hour: "numeric" }).format(
142 new Date(instant),
143 ),
144 );
145 }
146 expect(() => formatHourShort(instant, "Etc/Unknown")).toThrow(RangeError);
147 expect(formatHourShort("invalid", "UTC")).toBe("invalid");
148 });
149​
150 it("disambiguates repeated hours during a fall-back transition", () => {
151 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York")).toBe("1 AM EDT");
152 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
153 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York", false)).toBe(
154 "01:00 EDT",
155 );
156 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York", false)).toBe(
157 "01:00 EST",
158 );
159 });
160​
161 it("uses a 12-hour clock in locales that default to 24-hour", () => {
162 const instant = "2026-11-24T18:03:00.000Z";
163 for (const locale of ["fr", "de", "es"]) {
164 expect(formatHourShort(instant, "UTC", true, locale)).not.toContain("18");
165 }
166 expect(formatDateTimeShort(instant, "UTC", true, "fr")).not.toContain("18");
167 expect(formatHourShort(instant, "UTC", false, "es")).toBe("18:00");
168 });
169​
170 it("builds an exact minute-aligned 24-hour request", () => {
171 const window = makeWindow(1, new Date("2026-08-11T12:37:42.123Z"), "hour");
172​
173 expect(window.resolution).toBe("hour");
174 expect(window.sinceTime).toBe("2026-08-10T12:37:00.000Z");
175 expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
176 });
177​
178 it("degrades an unknown resolved zone to UTC instead of crashing", () => {
179 const resolved = new Intl.DateTimeFormat().resolvedOptions();
180 const resolvedOptions = vi
181 .spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions")
182 .mockReturnValue({ ...resolved, timeZone: "Etc/Unknown" });
183​
184 try {
185 const now = new Date("2026-08-11T12:37:42.123Z");
186​
187 expect(makeWindow(1, now, "hour").timeZone).toBe("UTC");
188 expect(makeWindow(30, now).timeZone).toBe("UTC");
189 } finally {
190 resolvedOptions.mockRestore();
191 }
192 });
193});
194​
195describe("token formatting", () => {
196 it("moves to the next unit when rounding reaches it", () => {
197 expect(formatTokens(999_999)).toBe("1M");
198 expect(formatTokens(999_999_999)).toBe("1B");
199 expect(formatTokens(-999_999)).toBe("-1M");
200 });
201});