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

usageFormat.test.ts

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