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

usageFormat.test.ts

213 lines7.8 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 it("retains other diagnostics beside the combined warning", () => {
87 const source = malformedSource(1);
88 const readFailure = "2 transcript path(s) could not be read; usage may be incomplete.";
89 const cursor = malformedSource(0, {
90 fingerprint: { ...source.fingerprint, provider: "cursor" },
91 status: "ok",
92 message: "Cursor history is limited to the last 30 days.",
93 });
94​
95 expect(
96 usageSourceMessages([
97 { ...source, message: `${readFailure} ${source.message}` },
98 { ...source, malformedRecords: 0, message: readFailure },
99 cursor,
100 ]),
101 ).toEqual([
102 readFailure,
103 cursor.message,
104 "1 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
105 ]);
106 });
107​
108 it("includes cached diagnostics when a transcript directory is missing", () => {
109 const source = malformedSource(1);
110 const missing = malformedSource(2, {
111 fingerprint: { ...source.fingerprint, hostId: "other-demo-host" },
112 status: "missing",
113 scannedFiles: 0,
114 skippedFiles: 1,
115 distinctSessions: 0,
116 message:
117 "No transcript directory on this environment. 2 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
118 });
119​
120 expect(usageSourceMessages([source, missing, missing])).toEqual([
121 "No transcript directory on this environment.",
122 "3 malformed or inconsistent usage record(s) were skipped; usage may be incomplete.",
123 ]);
124 });
125​
126 it("omits empty missing sources and healthy sources", () => {
127 expect(
128 usageSourceMessages([
129 malformedSource(0, { status: "missing" }),
130 malformedSource(1, { status: "ok" }),
131 ]),
132 ).toEqual([]);
133 expect(usageSourceMessages([])).toEqual([]);
134 });
135});
136​
137describe("hourly usage formatting", () => {
138 it("keeps requested zones separate when formatting repeated calls", () => {
139 const instant = "2026-08-11T12:37:00.000Z";
140 for (const zone of ["UTC", "America/New_York", "Asia/Kathmandu", "UTC"]) {
141 expect(formatHourShort(instant, zone)).toBe(
142 new Intl.DateTimeFormat("en-US", { timeZone: zone, hour: "numeric" }).format(
143 new Date(instant),
144 ),
145 );
146 }
147 expect(() => formatHourShort(instant, "Etc/Unknown")).toThrow(RangeError);
148 expect(formatHourShort("invalid", "UTC")).toBe("invalid");
149 });
150​
151 it("disambiguates repeated hours during a fall-back transition", () => {
152 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York")).toBe("1 AM EDT");
153 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York")).toBe("1 AM EST");
154 expect(formatHourShort("2026-11-01T05:37:00.000Z", "America/New_York", false)).toBe(
155 "01:00 EDT",
156 );
157 expect(formatHourShort("2026-11-01T06:37:00.000Z", "America/New_York", false)).toBe(
158 "01:00 EST",
159 );
160 });
161​
162 it("uses a 12-hour clock in locales that default to 24-hour", () => {
163 const instant = "2026-11-24T18:03:00.000Z";
164 for (const locale of ["fr", "de", "es"]) {
165 expect(formatHourShort(instant, "UTC", true, locale)).not.toContain("18");
166 }
167 expect(formatDateTimeShort(instant, "UTC", true, "fr")).not.toContain("18");
168 expect(formatHourShort(instant, "UTC", false, "es")).toBe("18:00");
169 });
170​
171 it("builds an exact minute-aligned 24-hour request", () => {
172 const window = makeWindow(1, new Date("2026-08-11T12:37:42.123Z"), "hour");
173​
174 expect(window.resolution).toBe("hour");
175 expect(window.sinceTime).toBe("2026-08-10T12:37:00.000Z");
176 expect(window.untilTime).toBe("2026-08-11T12:37:00.000Z");
177 });
178​
179 it("degrades an unknown resolved zone to UTC instead of crashing", () => {
180 const resolved = new Intl.DateTimeFormat().resolvedOptions();
181 const resolvedOptions = vi
182 .spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions")
183 .mockReturnValue({ ...resolved, timeZone: "Etc/Unknown" });
184​
185 try {
186 const now = new Date("2026-08-11T12:37:42.123Z");
187​
188 expect(makeWindow(1, now, "hour").timeZone).toBe("UTC");
189 expect(makeWindow(30, now).timeZone).toBe("UTC");
190 } finally {
191 resolvedOptions.mockRestore();
192 }
193 });
194});
195​
196describe("token formatting", () => {
197 it("moves to the next unit when rounding reaches it", () => {
198 expect(formatTokens(999_999)).toBe("1M");
199 expect(formatTokens(999_999_999)).toBe("1B");
200 expect(formatTokens(-999_999)).toBe("-1M");
201 });
202});
203​
204describe("localized usage source messages", () => {
205 it("uses singular and plural copy for skipped malformed records", () => {
206 expect(localizedUsageSourceMessages([malformedSource(1)])).toEqual([
207 "1 malformed or inconsistent usage record was skipped; usage may be incomplete.",
208 ]);
209 expect(localizedUsageSourceMessages([malformedSource(2)])).toEqual([
210 "2 malformed or inconsistent usage records were skipped; usage may be incomplete.",
211 ]);
212 });
213});