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

usageLimits.test.ts

306 lines10.1 KB

1import {
2 EnvironmentId,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6 type UsageLimitSourceAccount,
7 UsageLimitSourceId,
8} from "@t3tools/contracts";
9import { describe, expect, it } from "vite-plus/test";
10​
11import {
12 collectLimitSources,
13 collectLimitsGroups,
14 elapsedShare,
15 formatResetsIn,
16 limitsNotice,
17 paceOf,
18 providersWithLimits,
19} from "./usageLimits.ts";
20​
21const now = Date.parse("2026-09-03T12:00:00.000Z");
22​
23const window = {
24 id: "five_hour",
25 kind: "session",
26 label: "Session",
27 usedPercent: 40,
28 windowDurationMins: 300,
29 resetsAt: "2026-09-03T14:00:00.000Z",
30} as const;
31​
32function provider(overrides: Partial<ServerProvider>): ServerProvider {
33 return {
34 instanceId: ProviderInstanceId.make("codex"),
35 driver: ProviderDriverKind.make("codex"),
36 enabled: true,
37 installed: true,
38 version: null,
39 status: "ready",
40 auth: { status: "authenticated" },
41 checkedAt: "2026-09-03T11:00:00.000Z",
42 models: [],
43 slashCommands: [],
44 skills: [],
45 ...overrides,
46 };
48​
49describe("pace", () => {
50 it("places the clock three fifths through a five-hour window with two hours left", () => {
51 expect(elapsedShare(window, now)).toBeCloseTo(0.6);
52 expect(paceOf(window, now)).toBe("under");
53 expect(paceOf({ ...window, usedPercent: 62 }, now)).toBe("on");
54 expect(paceOf({ ...window, usedPercent: 80 }, now)).toBe("ahead");
55 });
56​
57 it("has no pace without a reset or a duration", () => {
58 expect(paceOf({ ...window, resetsAt: undefined }, now)).toBeNull();
59 expect(paceOf({ ...window, windowDurationMins: undefined }, now)).toBeNull();
60 expect(formatResetsIn({ ...window, resetsAt: undefined }, now)).toBeNull();
61 });
62​
63 it("phrases the reset as a countdown", () => {
64 expect(formatResetsIn(window, now)).toBe("resets in 2h 0m");
65 expect(formatResetsIn({ ...window, resetsAt: "2026-09-06T15:30:00.000Z" }, now)).toBe(
66 "resets in 3d 3h",
67 );
68 expect(formatResetsIn({ ...window, resetsAt: "2026-09-03T11:00:00.000Z" }, now)).toBe(
69 "resets now",
70 );
71 });
72});
73​
74describe("limitsNotice", () => {
75 it("explains empty bars and passes provider messages through", () => {
76 const checkedAt = "2026-09-03T11:00:00.000Z";
77 expect(limitsNotice({ checkedAt, windows: [window] })).toBeNull();
78 expect(limitsNotice({ checkedAt, windows: [] })).toBe("No limits reported.");
79 expect(limitsNotice({ checkedAt, windows: [], unavailable: { reason: "unsupported" } })).toBe(
80 "This account has no subscription limits.",
81 );
82 expect(
83 limitsNotice({
84 checkedAt,
85 windows: [],
86 unavailable: { reason: "probeFailed", message: "Codex timed out." },
87 }),
88 ).toBe("Codex timed out.");
89 });
90});
91​
92describe("providersWithLimits", () => {
93 it("keeps only usable providers whose driver reports limits at all", () => {
94 const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] };
95 const codex = provider({ usageLimits: limits });
96 expect(
97 providersWithLimits([
98 codex,
99 provider({
100 instanceId: ProviderInstanceId.make("cursor"),
101 driver: ProviderDriverKind.make("cursor"),
102 }),
103 provider({
104 instanceId: ProviderInstanceId.make("off"),
105 enabled: false,
106 usageLimits: limits,
107 }),
108 provider({
109 instanceId: ProviderInstanceId.make("gone"),
110 installed: false,
111 usageLimits: limits,
112 }),
113 provider({
114 instanceId: ProviderInstanceId.make("shadow"),
115 availability: "unavailable",
116 usageLimits: limits,
117 }),
118 ]),
119 ).toEqual([codex]);
120 });
121});
122​
123describe("collectLimitsGroups", () => {
124 it("labels environments only when more than one reports limits", () => {
125 const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] };
126 const codex = provider({ usageLimits: limits });
127 const one = new Map([
128 ["env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [codex] } }],
129 [
130 "env-b",
131 { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [provider({})] } },
132 ],
133 ] as const);
134 expect(collectLimitsGroups(one as never).map((group) => group.environmentLabel)).toEqual([
135 null,
136 ]);
137​
138 const two = new Map([
139 ["env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [codex] } }],
140 ["env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [codex] } }],
141 ] as const);
142 expect(collectLimitsGroups(two as never).map((group) => group.environmentLabel)).toEqual([
143 "Laptop",
144 "Desktop",
145 ]);
146 });
147});
148​
149describe("collectLimitSources", () => {
150 const source = {
151 id: UsageLimitSourceId.make("cliproxy-hub"),
152 kind: "cliproxy" as const,
153 label: "hub",
154 checkedAt: "2026-09-03T11:00:00.000Z",
155 accounts: [],
156 };
157 const limits = { checkedAt: source.checkedAt, windows: [window] };
158 const account: UsageLimitSourceAccount = {
159 id: "codex-personal",
160 driver: ProviderDriverKind.make("codex"),
161 email: "person@example.com",
162 plan: "ChatGPT Pro Subscription",
163 usageLimits: limits,
164 };
165 const native = provider({
166 displayName: "Personal",
167 auth: { status: "authenticated", email: account.email },
168 usageLimits: { ...limits, resetCredits: { availableCount: 2 } },
169 });
170​
171 function presentations(
172 providers: readonly ServerProvider[],
173 accounts: readonly UsageLimitSourceAccount[] = [account],
174 ) {
175 return new Map([
176 [
177 EnvironmentId.make("env-a"),
178 {
179 entry: { target: { label: "Laptop" } },
180 serverConfig: { providers, usageLimitSources: [{ ...source, accounts }] },
181 },
182 ],
183 ]);
184 }
185​
186 it.each(["codex", "claudeAgent"])(
187 "prefers native %s limits by email without changing provider rows or source snapshots",
188 (kind) => {
189 const driver = ProviderDriverKind.make(kind);
190 const first = { ...native, driver };
191 const second = { ...first, instanceId: ProviderInstanceId.make("work") };
192 const accounts = [{ ...account, driver, email: " Person@Example.COM " }];
193 const input = presentations([first, second], accounts);
194​
195 expect(collectLimitSources(input)).toMatchObject([{ accounts: [], hiddenAccountCount: 1 }]);
196 expect(collectLimitsGroups(input)[0]?.providers).toEqual([first, second]);
197 expect(accounts).toHaveLength(1);
198 expect(first.usageLimits?.resetCredits?.availableCount).toBe(2);
199 },
200 );
201​
202 it("matches across environments even when the hub is visited before the native provider", () => {
203 const input = presentations([]);
204 input.set(EnvironmentId.make("env-b"), {
205 entry: { target: { label: "Desktop" } },
206 serverConfig: { providers: [native], usageLimitSources: [] },
207 });
208​
209 expect(collectLimitSources(input)).toMatchObject([
210 { accounts: [], hiddenAccountCount: 1, environmentId: "env-a" },
211 ]);
212 });
213​
214 it("keeps other providers, other emails, and unidentified accounts with the same plan", () => {
215 const accounts = [
216 account,
217 { ...account, id: "other-provider", driver: ProviderDriverKind.make("claudeAgent") },
218 { ...account, id: "other-email", email: "other@example.com" },
219 { ...account, id: "unknown-email", email: undefined },
220 ];
221​
222 expect(collectLimitSources(presentations([native], accounts))).toMatchObject([
223 { accounts: accounts.slice(1), hiddenAccountCount: 1 },
224 ]);
225 expect(
226 collectLimitSources(
227 presentations([{ ...native, auth: { status: "authenticated" } }], accounts),
228 )[0]?.accounts,
229 ).toEqual(accounts);
230 });
231​
232 it.each([
233 { enabled: false },
234 { installed: false },
235 { availability: "unavailable" },
236 { usageLimits: undefined },
237 { usageLimits: { ...limits, windows: [] } },
238 { usageLimits: { ...limits, unavailable: { reason: "probeFailed" } } },
239 { usageLimits: { ...limits, unavailable: { reason: "unsupported" } } },
240 ] satisfies Partial<ServerProvider>[])(
241 "retains hub limits when the native provider cannot show them: %j",
242 (overrides) => {
243 expect(collectLimitSources(presentations([{ ...native, ...overrides }]))).toMatchObject([
244 { accounts: [account], hiddenAccountCount: 0 },
245 ]);
246 },
247 );
248​
249 it("restores the hub account when the matching provider disappears", () => {
250 const input = presentations([native]);
251 expect(collectLimitSources(input)[0]?.accounts).toEqual([]);
252 input.delete(EnvironmentId.make("env-a"));
253 for (const [id, entry] of presentations([])) input.set(id, entry);
254​
255 expect(collectLimitSources(input)[0]?.accounts).toEqual([account]);
256 });
257​
258 it("keeps source errors and genuinely empty sources distinguishable from hidden accounts", () => {
259 const input = new Map([
260 [
261 EnvironmentId.make("env-a"),
262 {
263 entry: { target: { label: "Laptop" } },
264 serverConfig: {
265 providers: [native],
266 usageLimitSources: [{ ...source, error: "Hub unavailable" }],
267 },
268 },
269 ],
270 ]);
271 expect(collectLimitSources(input)).toMatchObject([
272 { accounts: [], hiddenAccountCount: 0, error: "Hub unavailable" },
273 ]);
274 });
275​
276 it("keys sources per environment and names the environment only when several have some", () => {
277 const one = new Map([
278 [
279 "env-a",
280 { entry: { target: { label: "Laptop" } }, serverConfig: { usageLimitSources: [source] } },
281 ],
282 [
283 "env-b",
284 { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [] } },
285 ],
286 ] as const);
287 expect(collectLimitSources(one as never).map((entry) => [entry.key, entry.label])).toEqual([
288 ["env-a:cliproxy-hub", "hub"],
289 ]);
290​
291 const two = new Map([
292 [
293 "env-a",
294 { entry: { target: { label: "Laptop" } }, serverConfig: { usageLimitSources: [source] } },
295 ],
296 [
297 "env-b",
298 { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [source] } },
299 ],
300 ] as const);
301 expect(collectLimitSources(two as never).map((entry) => entry.label)).toEqual([
302 "Laptop · hub",
303 "Desktop · hub",
304 ]);
305 });
306});