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

usageFormat.test.ts

265 lines9.6 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 formatPercent,
9 formatTokens,
10 makeWindow,
11 usageSourceMessages,
12} from "./usageFormat.ts";
13import { usageSourceMessages as localizedUsageSourceMessages } from "./usageFormatCopy.ts";
14​
15function malformedSource(count: number, overrides: Partial<UsageSource> = {}): UsageSource {
16 return {
17 fingerprint: {
18 hostId: "demo-host",
19 provider: "codex",
20 resolvedHomePath: "/demo/codex",
21 volumeId: "demo-volume",
22 },
23 status: "partial",
24 scannedFiles: 1,
25 skippedFiles: 0,
26 malformedRecords: count,
27 distinctSessions: 1,
28 message: `${count} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
29 ...overrides,
30 };
32​
33describe("usage source messages", () => {
34 it("combines different skipped-record counts into one warning", () => {
35 const codex = malformedSource(1);
36 const claude = malformedSource(11, {
37 fingerprint: { ...codex.fingerprint, provider: "claude", resolvedHomePath: "/demo/claude" },
38 });
39​
40 expect(usageSourceMessages([codex, claude])).toEqual([
41 "12 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
42 ]);
43 });
44​
45 it.each([
46 { first: 1, second: 1, total: 2 },
47 { first: 1, second: 11, total: 12 },
48 ])(
49 "combines hosts reporting $first and $second skipped records once per source",
50 ({ first, second, total }) => {
51 const source = malformedSource(first);
52 const otherHost = malformedSource(second, {
53 fingerprint: { ...source.fingerprint, hostId: "other-demo-host" },
54 });
55​
56 expect(usageSourceMessages([source, source, otherHost, otherHost])).toEqual([
57 `${total} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
58 ]);
59 expect(usageSourceMessages([source, source])).toEqual([
60 `${first} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
61 ]);
62 expect(usageSourceMessages([otherHost, otherHost])).toEqual([
63 `${second} malformed or inconsistent usage record(s) were skipped; usage may be incomplete.`,
64 ]);
65 },
66 );
67​
68 it("keeps different machines with the same hostname and home path distinct", () => {
69 const source = malformedSource(1);
70 const otherMachine = malformedSource(11, {
71 fingerprint: { ...source.fingerprint, volumeId: "other-demo-volume" },
72 });
73​
74 expect(usageSourceMessages([source, otherMachine])).toEqual([
75 "12 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
76 ]);
77 });
78​
79 it("uses the largest count when connected environments report the same source", () => {
80 const source = malformedSource(1);
81​
82 expect(usageSourceMessages([source, malformedSource(11), source])).toEqual([
83 "11 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
84 ]);
85 });
86​
87 describe("shared Windows and WSL warning counts", () => {
88 const windows = malformedSource(3, {
89 fingerprint: {
90 hostId: "MainPC",
91 provider: "claude",
92 resolvedHomePath: "C:\\Users\\user\\.claude\\projects",
93 volumeId: "1280555009:3659174697951285",
94 physicalSourceId: "physical-claude-projects",
95 },
96 });
97 const wsl = malformedSource(3, {
98 fingerprint: {
99 ...windows.fingerprint,
100 resolvedHomePath: "/mnt/c/Users/user/.claude/projects",
101 volumeId: "112:3659174697951287",
102 },
103 });
104​
105 it("counts the same malformed records once across Windows and WSL", () => {
106 for (const sources of [
107 [windows, wsl],
108 [wsl, windows],
109 ]) {
110 expect(usageSourceMessages(sources)).toEqual([
111 "3 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
112 ]);
113 }
114 });
115​
116 it("keeps sources distinct without a shared alias or with different providers", () => {
117 const { physicalSourceId: _omitted, ...legacy } = wsl.fingerprint;
118 for (const fingerprint of [
119 legacy,
120 { ...wsl.fingerprint, physicalSourceId: "other-directory" },
121 { ...wsl.fingerprint, provider: "codex" as const },
122 ]) {
123 expect(usageSourceMessages([windows, { ...wsl, fingerprint }])).toEqual([
124 "6 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
125 ]);
126 }
127 });
128 });
129​
130 it("retains other diagnostics beside the combined warning", () => {
131 const source = malformedSource(1);
132 const readFailure = "2 transcript path(s) could not be read; usage may be incomplete.";
133 const cursor = malformedSource(0, {
134 fingerprint: { ...source.fingerprint, provider: "cursor" },
135 status: "ok",
136 message: "Cursor history is limited to the last 30 days.",
137 });
138​
139 expect(
140 usageSourceMessages([
141 { ...source, message: `${readFailure} ${source.message}` },
142 { ...source, malformedRecords: 0, message: readFailure },
143 cursor,
144 ]),
145 ).toEqual([
146 readFailure,
147 cursor.message,
148 "1 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
149 ]);
150 });
151​
152 it("includes cached diagnostics when a transcript directory is missing", () => {
153 const source = malformedSource(1);
154 const missing = malformedSource(2, {
155 fingerprint: { ...source.fingerprint, hostId: "other-demo-host" },
156 status: "missing",
157 scannedFiles: 0,
158 skippedFiles: 1,
159 distinctSessions: 0,
160 message:
161 "No transcript directory on this environment. 2 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
162 });
163​
164 expect(usageSourceMessages([source, missing, missing])).toEqual([
165 "No transcript directory on this environment.",
166 "3 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
167 ]);
168 });
169​
170 it("omits empty missing sources and healthy sources", () => {
171 expect(
172 usageSourceMessages([
173 malformedSource(0, { status: "missing" }),
174 malformedSource(1, { status: "ok" }),
175 ]),
176 ).toEqual([]);
177 expect(usageSourceMessages([])).toEqual([]);
178 });
179});
180​
181describe("hourly usage formatting", () => {
182 it("keeps requested zones separate when formatting repeated calls", () => {
183 const instant = "2026-08-11T12:37:00.000Z";
184 for (const zone of ["UTC", "America/New_York", "Asia/Kathmandu", "UTC"]) {
185 expect(formatHourShort(instant, zone)).toBe(
186 new Intl.DateTimeFormat("en-US", { timeZone: zone, hour: "numeric" }).format(
187 new Date(instant),
188 ),
189 );
190 }
191 expect(() => formatHourShort(instant, "Etc/Unknown")).toThrow(RangeError);
192 expect(formatHourShort("invalid", "UTC")).toBe("invalid");
193 });
194​
195 it("disambiguates repeated hours during a fall-back transition", () => {
196 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York")).toBe("1 AM EDT");
197 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
198 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York", false)).toBe(
199 "01:00 EDT",
200 );
201 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York", false)).toBe(
202 "01:00 EST",
203 );
204 });
205​
206 it("uses a 12-hour clock in locales that default to 24-hour", () => {
207 const instant = "2026-11-24T18:03:00.000Z";
208 for (const locale of ["fr", "de", "es"]) {
209 expect(formatHourShort(instant, "UTC", true, locale)).not.toContain("18");
210 }
211 expect(formatDateTimeShort(instant, "UTC", true, "fr")).not.toContain("18");
212 expect(formatHourShort(instant, "UTC", false, "es")).toBe("18:00");
213 });
214​
215 it("builds an exact minute-aligned 24-hour request", () => {
216 const window = makeWindow(1, new Date("2026-08-11T12:37:42.123Z"), "hour");
217​
218 expect(window.resolution).toBe("hour");
219 expect(window.sinceTime).toBe("2026-08-10T12:37:00.000Z");
220 expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
221 });
222​
223 it("degrades an unknown resolved zone to UTC instead of crashing", () => {
224 const resolved = new Intl.DateTimeFormat().resolvedOptions();
225 const resolvedOptions = vi
226 .spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions")
227 .mockReturnValue({ ...resolved, timeZone: "Etc/Unknown" });
228​
229 try {
230 const now = new Date("2026-08-11T12:37:42.123Z");
231​
232 expect(makeWindow(1, now, "hour").timeZone).toBe("UTC");
233 expect(makeWindow(30, now).timeZone).toBe("UTC");
234 } finally {
235 resolvedOptions.mockRestore();
236 }
237 });
238});
239​
240describe("percent formatting", () => {
241 it("keeps a share short of the whole below 100%", () => {
242 expect(formatPercent(0.9996)).toBe(">99.9%");
243 expect(formatPercent(0.999)).toBe("99.9%");
244 expect(formatPercent(1)).toBe("100.0%");
245 });
246});
247​
248describe("token formatting", () => {
249 it("moves to the next unit when rounding reaches it", () => {
250 expect(formatTokens(999_999)).toBe("1M");
251 expect(formatTokens(999_999_999)).toBe("1B");
252 expect(formatTokens(-999_999)).toBe("-1M");
253 });
254});
255​
256describe("localized usage source messages", () => {
257 it("uses singular and plural copy for skipped malformed records", () => {
258 expect(localizedUsageSourceMessages([malformedSource(1)])).toEqual([
259 "1 malformed or inconsistent usage record was skipped; usage may be incomplete.",
260 ]);
261 expect(localizedUsageSourceMessages([malformedSource(2)])).toEqual([
262 "2 malformed or inconsistent usage records were skipped; usage may be incomplete.",
263 ]);
264 });
265});