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

usageLimits.test.ts

1,469 lines49.2 KB

1import {
2 EnvironmentId,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6 UsageLimitSourceId,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9​
10import {
11 type LimitAccount,
12 isUsageLimitsCommand,
13 collectProviderUsageLimits,
14 sameUsageLimitCommandCoverage,
15 withUsageLimitsCommands,
16 collectLimitAccounts,
17 collectExternalUsageLinks,
18 collectLimitNotices,
19 collectLimitPools,
20 displayLimitWindows,
21 elapsedShare,
22 formatResetsIn,
23 limitsNotice,
24 paceOf,
25 providersWithLimits,
26 remainingPercent,
27 usesChatGptSharing,
28} from "./usageLimits.ts";
29​
30const now = Date.parse("2026-09-03T12:00:00.000Z");
31​
32const window = {
33 id: "five_hour",
34 kind: "session",
35 label: "Session",
36 usedPercent: 40,
37 windowDurationMins: 300,
38 resetsAt: "2026-09-03T14:00:00.000Z",
39} as const;
40​
41function provider(overrides: Partial<ServerProvider>): ServerProvider {
42 return {
43 instanceId: ProviderInstanceId.make("codex"),
44 driver: ProviderDriverKind.make("codex"),
45 enabled: true,
46 installed: true,
47 version: null,
48 status: "ready",
49 auth: { status: "authenticated" },
50 checkedAt: "2026-09-03T11:00:00.000Z",
51 models: [],
52 slashCommands: [],
53 skills: [],
54 ...overrides,
55 };
57​
58describe("pace", () => {
59 it.each([
60 ["2026-09-03T12:01:00.000Z", "resets in 1m"],
61 ["2026-09-03T12:00:59.999Z", "Resets momentarily"],
62 ["2026-09-03T12:00:00.001Z", "Resets momentarily"],
63 ["2026-09-03T12:00:00.000Z", "resets now"],
64 ["2026-09-03T11:59:59.999Z", "resets now"],
65 ])("formats the reset boundary at %s", (resetsAt, label) => {
66 expect(formatResetsIn({ ...window, resetsAt }, now)).toBe(label);
67 });
68​
69 it("ignores invalid reset timestamps", () => {
70 expect(formatResetsIn({ ...window, resetsAt: "invalid" }, now)).toBeNull();
71 });
72​
73 it("places the clock three fifths through a five-hour window with two hours left", () => {
74 expect(elapsedShare(window, now)).toBeCloseTo(0.6);
75 expect(paceOf(window, now)).toBe("under");
76 expect(paceOf({ ...window, usedPercent: 62 }, now)).toBe("on");
77 expect(paceOf({ ...window, usedPercent: 80 }, now)).toBe("ahead");
78 });
79​
80 it("has no pace without a reset or a duration", () => {
81 expect(paceOf({ ...window, resetsAt: undefined }, now)).toBeNull();
82 expect(paceOf({ ...window, windowDurationMins: undefined }, now)).toBeNull();
83 expect(formatResetsIn({ ...window, resetsAt: undefined }, now)).toBeNull();
84 });
85​
86 it("phrases the reset as a countdown", () => {
87 expect(formatResetsIn(window, now)).toBe("resets in 2h 0m");
88 expect(formatResetsIn({ ...window, resetsAt: "2026-09-06T15:30:00.000Z" }, now)).toBe(
89 "resets in 3d 3h",
90 );
91 expect(formatResetsIn({ ...window, resetsAt: "2026-09-03T11:00:00.000Z" }, now)).toBe(
92 "resets now",
93 );
94 });
95});
96​
97describe("limitsNotice", () => {
98 it("explains empty bars and passes provider messages through", () => {
99 const checkedAt = "2026-09-03T11:00:00.000Z";
100 expect(limitsNotice({ checkedAt, windows: [window] })).toBeNull();
101 expect(limitsNotice({ checkedAt, windows: [] })).toBe("No limits reported.");
102 expect(limitsNotice({ checkedAt, windows: [], unavailable: { reason: "unsupported" } })).toBe(
103 "This account has no subscription limits.",
104 );
105 expect(
106 limitsNotice({
107 checkedAt,
108 windows: [],
109 unavailable: { reason: "probeFailed", message: "Codex timed out." },
110 }),
111 ).toBe("Codex timed out.");
112 });
113});
114​
115describe("providersWithLimits", () => {
116 it("keeps only usable providers whose driver reports limits at all", () => {
117 const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] };
118 const codex = provider({ usageLimits: limits });
119 expect(
120 providersWithLimits([
121 codex,
122 provider({
123 instanceId: ProviderInstanceId.make("cursor"),
124 driver: ProviderDriverKind.make("cursor"),
125 }),
126 provider({
127 instanceId: ProviderInstanceId.make("off"),
128 enabled: false,
129 usageLimits: limits,
130 }),
131 provider({
132 instanceId: ProviderInstanceId.make("gone"),
133 installed: false,
134 usageLimits: limits,
135 }),
136 provider({
137 instanceId: ProviderInstanceId.make("shadow"),
138 availability: "unavailable",
139 usageLimits: limits,
140 }),
141 ]),
142 ).toEqual([codex]);
143 });
144});
145​
146describe("pools", () => {
147 const checkedAt = "2026-09-03T11:00:00.000Z";
148 const weekly = {
149 id: "seven_day",
150 kind: "weekly",
151 label: "Weekly",
152 windowDurationMins: 7 * 24 * 60,
153 resetsAt: "2026-09-06T12:00:00.000Z",
154 } as const;
155 const claude = ProviderDriverKind.make("claudeAgent");
156 const source = {
157 id: UsageLimitSourceId.make("hub"),
158 kind: "cliproxy" as const,
159 label: "hub",
160 checkedAt,
161 };
162 const laptop = { entry: { target: { label: "Laptop" } } };
163​
164 it("merges one account reported natively on two environments and by a hub into one entry", () => {
165 const native = provider({
166 driver: claude,
167 instanceId: ProviderInstanceId.make("claude"),
168 auth: { status: "authenticated", email: "Same@example.com" },
169 usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 40 }] },
170 });
171 const input = new Map([
172 [EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native] } }],
173 [
174 EnvironmentId.make("env-b"),
175 {
176 entry: { target: { label: "Desktop" } },
177 serverConfig: {
178 providers: [
179 {
180 ...native,
181 usageLimits: {
182 checkedAt: "2026-09-03T11:30:00.000Z",
183 windows: [{ ...window, usedPercent: 55 }],
184 },
185 },
186 ],
187 usageLimitSources: [
188 {
189 ...source,
190 accounts: [
191 {
192 id: "claude-same@example.com.json",
193 driver: claude,
194 email: "same@example.com",
195 plan: "Claude Subscription",
196 usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 10 }] },
197 },
198 ],
199 },
200 ],
201 },
202 },
203 ],
204 ]);
205 const accounts = collectLimitAccounts(input);
206 expect(accounts).toHaveLength(1);
207 expect(accounts[0]).toMatchObject({
208 key: "env-a:claude",
209 sourceLabel: null,
210 // Desktop's read is fresher, so its credits and its redeem are the ones on show.
211 redeem: { environmentId: "env-b", input: { instanceId: "claude" } },
212 environments: [
213 { environmentId: "env-a", label: "Laptop" },
214 { environmentId: "env-b", label: "Desktop" },
215 ],
216 });
217 // The fresher native snapshot wins; the hub row is pre-filtered by email.
218 expect(accounts[0]?.limits.windows[0]?.usedPercent).toBe(55);
219 });
220​
221 it("merges OpenCode Go limits from machines with the same API key", () => {
222 const go = provider({
223 driver: ProviderDriverKind.make("opencode"),
224 instanceId: ProviderInstanceId.make("opencode"),
225 auth: { status: "authenticated" },
226 usageLimits: {
227 checkedAt,
228 credentialFingerprint: "shared-go-key",
229 windows: [{ ...window, id: "go_rolling", usedPercent: 3 }],
230 },
231 });
232 const input = new Map([
233 [EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [go] } }],
234 [
235 EnvironmentId.make("env-b"),
236 {
237 entry: { target: { label: "Desktop" } },
238 serverConfig: {
239 providers: [
240 {
241 ...go,
242 usageLimits: {
243 ...go.usageLimits!,
244 checkedAt: "2026-09-03T11:30:00.000Z",
245 windows: [{ ...window, id: "go_rolling", usedPercent: 4 }],
246 },
247 },
248 ],
249 },
250 },
251 ],
252 ]);
253 const accounts = collectLimitAccounts(input);
254 expect(accounts).toHaveLength(1);
255 expect(accounts[0]?.environments).toEqual([
256 { environmentId: "env-a", label: "Laptop" },
257 { environmentId: "env-b", label: "Desktop" },
258 ]);
259 expect(collectLimitPools(accounts, now)[0]?.windows[0]?.members).toHaveLength(1);
260 expect(accounts[0]?.limits.windows[0]?.usedPercent).toBe(4);
261​
262 const differentKey = {
263 ...go,
264 usageLimits: { ...go.usageLimits!, credentialFingerprint: "other-go-key" },
265 };
266 input.set(EnvironmentId.make("env-b"), {
267 entry: { target: { label: "Desktop" } },
268 serverConfig: { providers: [differentKey] },
269 });
270 expect(collectLimitAccounts(input)).toHaveLength(2);
271​
272 input.set(EnvironmentId.make("env-a"), {
273 ...laptop,
274 serverConfig: {
275 providers: [{ ...go, auth: { status: "authenticated", email: "same@example.com" } }],
276 },
277 });
278 input.set(EnvironmentId.make("env-b"), {
279 entry: { target: { label: "Desktop" } },
280 serverConfig: {
281 providers: [
282 { ...differentKey, auth: { status: "authenticated", email: "SAME@example.com" } },
283 ],
284 },
285 });
286 expect(collectLimitAccounts(input)).toHaveLength(1);
287 });
288​
289 it("takes windows from a fresher hub read but credits and redeem from the native instance", () => {
290 const native = provider({
291 driver: claude,
292 instanceId: ProviderInstanceId.make("claude"),
293 auth: { status: "authenticated", email: "same@example.com" },
294 usageLimits: {
295 checkedAt,
296 windows: [{ ...window, usedPercent: 40 }],
297 resetCredits: { availableCount: 2 },
298 },
299 });
300 const input = new Map([
301 [
302 EnvironmentId.make("env-a"),
303 {
304 ...laptop,
305 serverConfig: {
306 providers: [native],
307 usageLimitSources: [
308 {
309 ...source,
310 accounts: [
311 {
312 id: "claude-same@example.com.json",
313 driver: claude,
314 email: "same@example.com",
315 usageLimits: {
316 checkedAt: "2026-09-03T11:30:00.000Z",
317 windows: [{ ...window, usedPercent: 55 }],
318 },
319 },
320 ],
321 },
322 ],
323 },
324 },
325 ],
326 ]);
327 const [account] = collectLimitAccounts(input);
328 expect(account?.limits.windows[0]?.usedPercent).toBe(55);
329 expect(account?.limits.resetCredits?.availableCount).toBe(2);
330 expect(account?.redeem).toEqual({ environmentId: "env-a", input: { instanceId: "claude" } });
331 expect(account?.environments).toEqual([{ environmentId: "env-a", label: "Laptop" }]);
332 });
333​
334 it("redeems through the hub when it holds a credit, even with a fresher native read", () => {
335 const native = provider({
336 driver: claude,
337 instanceId: ProviderInstanceId.make("claude"),
338 auth: { status: "authenticated", email: "same@example.com" },
339 usageLimits: {
340 checkedAt: "2026-09-03T11:30:00.000Z",
341 windows: [{ ...window, usedPercent: 40 }],
342 resetCredits: { availableCount: 3, nextCreditId: "native-credit" },
343 },
344 });
345 const input = new Map([
346 [
347 EnvironmentId.make("env-a"),
348 {
349 ...laptop,
350 serverConfig: {
351 providers: [native],
352 usageLimitSources: [
353 {
354 ...source,
355 accounts: [
356 {
357 id: "claude-same@example.com.json",
358 driver: claude,
359 email: "same@example.com",
360 usageLimits: {
361 checkedAt,
362 windows: [{ ...window, usedPercent: 55 }],
363 resetCredits: { availableCount: 2, nextCreditId: "hub-credit" },
364 },
365 },
366 ],
367 },
368 ],
369 },
370 },
371 ],
372 ]);
373 const [account] = collectLimitAccounts(input);
374 // Only the hub path clears the routing cooldown it holds for this account.
375 expect(account?.redeem).toEqual({
376 environmentId: "env-a",
377 input: { sourceId: "hub", accountId: "claude-same@example.com.json", creditId: "hub-credit" },
378 });
379 // The fresher native balance is still the one shown.
380 expect(account?.limits.resetCredits?.availableCount).toBe(3);
381 });
382​
383 it("redeems on the environment whose snapshot supplied the credits on show", () => {
384 const stale = provider({
385 auth: { status: "authenticated", email: "same@example.com" },
386 usageLimits: {
387 checkedAt,
388 windows: [window],
389 resetCredits: { availableCount: 0 },
390 },
391 });
392 const fresh = {
393 ...stale,
394 usageLimits: {
395 checkedAt: "2026-09-03T11:30:00.000Z",
396 windows: [window],
397 resetCredits: { availableCount: 2 },
398 },
399 };
400 const input = new Map([
401 [EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [stale] } }],
402 [
403 EnvironmentId.make("env-b"),
404 { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [fresh] } },
405 ],
406 ]);
407 const [account] = collectLimitAccounts(input);
408 expect(account?.limits.resetCredits?.availableCount).toBe(2);
409 expect(account?.redeem).toEqual({ environmentId: "env-b", input: { instanceId: "codex" } });
410 });
411​
412 it("uses the freshest hub credit and its environment even when the account is also native", () => {
413 const native = provider({
414 auth: { status: "authenticated", email: "same@example.com" },
415 usageLimits: { checkedAt, windows: [window], resetCredits: { availableCount: 1 } },
416 });
417 const hubAccount = {
418 id: "codex-same.json",
419 driver: native.driver,
420 email: "same@example.com",
421 usageLimits: {
422 checkedAt: "2026-09-03T11:30:00.000Z",
423 windows: [window],
424 resetCredits: { availableCount: 2, nextCreditId: "credit-2" },
425 },
426 };
427 const input = new Map([
428 [EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native] } }],
429 [
430 EnvironmentId.make("env-b"),
431 {
432 ...laptop,
433 serverConfig: {
434 providers: [],
435 usageLimitSources: [{ ...source, accounts: [hubAccount] }],
436 },
437 },
438 ],
439 ]);
440 const [account] = collectLimitAccounts(input);
441 expect(account?.limits.resetCredits?.availableCount).toBe(2);
442 expect(account?.redeem).toEqual({
443 environmentId: "env-b",
444 input: { sourceId: "hub", accountId: "codex-same.json", creditId: "credit-2" },
445 });
446 hubAccount.usageLimits.resetCredits.availableCount = 0;
447 expect(collectLimitAccounts(input)[0]?.limits.resetCredits?.availableCount).toBe(0);
448 });
449​
450 it("keeps distinct hub accounts redeemable through their own source", () => {
451 const hubAccounts = ["first", "second"].map((id) => ({
452 id,
453 driver: ProviderDriverKind.make("codex"),
454 email: `${id}@example.com`,
455 usageLimits: {
456 checkedAt,
457 windows: [window],
458 resetCredits: { availableCount: 2, nextCreditId: `${id}-credit` },
459 },
460 }));
461 const input = new Map([
462 [
463 EnvironmentId.make("env-a"),
464 {
465 ...laptop,
466 serverConfig: {
467 providers: [],
468 usageLimitSources: [{ ...source, accounts: hubAccounts }],
469 },
470 },
471 ],
472 ]);
473 expect(collectLimitAccounts(input).map((account) => account.redeem)).toEqual(
474 hubAccounts.map((account) => ({
475 environmentId: "env-a",
476 input: { sourceId: "hub", accountId: account.id, creditId: `${account.id}-credit` },
477 })),
478 );
479 });
480​
481 it("does not give old credits the timestamp of a newer window-only read", () => {
482 const snapshots = [
483 { checkedAt, windows: [window], resetCredits: { availableCount: 2 } },
484 { checkedAt: "2026-09-03T12:00:00.000Z", windows: [window] },
485 {
486 checkedAt: "2026-09-03T11:30:00.000Z",
487 windows: [window],
488 resetCredits: { availableCount: 1 },
489 },
490 ];
491 const input = new Map(
492 snapshots.map((usageLimits, i) => [
493 EnvironmentId.make(`env-${i}`),
494 {
495 ...laptop,
496 serverConfig: {
497 providers: [
498 provider({
499 auth: { status: "authenticated", email: "same@example.com" },
500 usageLimits,
501 }),
502 ],
503 },
504 },
505 ]),
506 );
507 const [account] = collectLimitAccounts(input);
508 expect(account?.limits.checkedAt).toBe("2026-09-03T12:00:00.000Z");
509 expect(account?.limits.resetCredits?.availableCount).toBe(1);
510 expect(account?.redeem?.environmentId).toBe("env-2");
511 });
512​
513 it("keeps one email in two workspaces apart while merging each workspace's duplicates", () => {
514 const login = (instanceId: string, workspaceId: string, usedPercent: number) =>
515 provider({
516 instanceId: ProviderInstanceId.make(instanceId),
517 auth: { status: "authenticated", email: "same@example.com", workspaceId },
518 usageLimits: { checkedAt, windows: [{ ...weekly, usedPercent }] },
519 });
520 const hubAccount = (id: string, workspaceId?: string) => ({
521 id,
522 driver: ProviderDriverKind.make("codex"),
523 email: "same@example.com",
524 ...(workspaceId ? { workspaceId } : {}),
525 usageLimits: { checkedAt, windows: [{ ...weekly, usedPercent: 50 }] },
526 });
527 const input = new Map([
528 [
529 EnvironmentId.make("env-a"),
530 {
531 ...laptop,
532 serverConfig: {
533 providers: [login("business", "ws-business", 8), login("personal", "ws-plus", 26)],
534 usageLimitSources: [
535 // A hub that names the workspace joins it; one that does not
536 // cannot say which of the two it read, so it keeps its own row.
537 { ...source, accounts: [hubAccount("plus.json", "ws-plus"), hubAccount("x.json")] },
538 ],
539 },
540 },
541 ],
542 [
543 EnvironmentId.make("env-b"),
544 {
545 entry: { target: { label: "Desktop" } },
546 serverConfig: { providers: [login("personal", "ws-plus", 26)] },
547 },
548 ],
549 ]);
550 const accounts = collectLimitAccounts(input);
551 expect(accounts.map((account) => account.key)).toEqual([
552 "env-a:business",
553 "env-a:personal",
554 "hub:x.json",
555 ]);
556 expect(accounts[0]?.limits.windows[0]?.usedPercent).toBe(8);
557 // One hub file name in two workspaces stays two accounts, even without an
558 // email to tell them apart.
559 const withoutEmail = (id: string, workspaceId: string) => {
560 const { email: _email, ...account } = hubAccount(id, workspaceId);
561 return account;
562 };
563 const sameFile = new Map([
564 [
565 EnvironmentId.make("env-a"),
566 {
567 ...laptop,
568 serverConfig: {
569 usageLimitSources: [{ ...source, accounts: [withoutEmail("a.json", "ws-1")] }],
570 },
571 },
572 ],
573 [
574 EnvironmentId.make("env-b"),
575 {
576 ...laptop,
577 serverConfig: {
578 usageLimitSources: [{ ...source, accounts: [withoutEmail("a.json", "ws-2")] }],
579 },
580 },
581 ],
582 ]);
583 expect(collectLimitAccounts(sameFile).map((account) => account.key)).toEqual([
584 "hub:a.json:ws-1",
585 "hub:a.json:ws-2",
586 ]);
587 expect(accounts[1]?.environments.map((environment) => environment.label)).toEqual([
588 "Laptop",
589 "Desktop",
590 ]);
591 });
592​
593 it("joins a report without a workspace to the only workspace signed in with its email", () => {
594 const native = provider({
595 driver: claude,
596 auth: { status: "authenticated", email: "same@example.com", workspaceId: "org-a" },
597 usageLimits: { checkedAt, windows: [window] },
598 });
599 const hubAccount = {
600 id: "claude.json",
601 driver: claude,
602 email: "same@example.com",
603 usageLimits: { checkedAt, windows: [window] },
604 };
605 const withOther = (other: ServerProvider) =>
606 new Map([
607 [
608 EnvironmentId.make("env-a"),
609 {
610 ...laptop,
611 serverConfig: {
612 providers: [native, other],
613 usageLimitSources: [{ ...source, accounts: [hubAccount] }],
614 },
615 },
616 ],
617 ]);
618 const unrelated = provider({ instanceId: ProviderInstanceId.make("codex-2") });
619 expect(collectLimitAccounts(withOther(unrelated)).map((account) => account.key)).toEqual([
620 "env-a:codex",
621 ]);
622 // A second org whose limits could not be read still makes the hub ambiguous.
623 const unreadable = provider({
624 ...native,
625 instanceId: ProviderInstanceId.make("team"),
626 auth: { ...native.auth, workspaceId: "org-b" },
627 usageLimits: { checkedAt, windows: [], unavailable: { reason: "probeFailed" } },
628 });
629 expect(collectLimitAccounts(withOther(unreadable)).map((account) => account.key)).toEqual([
630 "env-a:codex",
631 "hub:claude.json",
632 ]);
633 });
634​
635 it("names an environment once however many of its instances share the account", () => {
636 const shared = provider({
637 auth: { status: "authenticated", email: "same@example.com" },
638 usageLimits: { checkedAt, windows: [window] },
639 });
640 const input = new Map([
641 [
642 EnvironmentId.make("env-a"),
643 {
644 ...laptop,
645 serverConfig: {
646 providers: [shared, { ...shared, instanceId: ProviderInstanceId.make("work") }],
647 },
648 },
649 ],
650 ]);
651 expect(collectLimitAccounts(input)[0]?.environments).toEqual([
652 { environmentId: "env-a", label: "Laptop" },
653 ]);
654 });
655​
656 it("keys a hub account without an email by hub, so two environments on one hub share it", () => {
657 const seat = {
658 id: "claude-team-seat.json",
659 driver: claude,
660 usageLimits: { checkedAt, windows: [window] },
661 };
662 const hub = { ...source, accounts: [seat] };
663 const input = new Map([
664 [EnvironmentId.make("env-a"), { ...laptop, serverConfig: { usageLimitSources: [hub] } }],
665 [
666 EnvironmentId.make("env-b"),
667 { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [hub] } },
668 ],
669 ]);
670 const accounts = collectLimitAccounts(input);
671 expect(accounts.map((account) => account.key)).toEqual(["hub:claude-team-seat.json"]);
672 expect(accounts[0]?.displayName).toBe("claude-team-seat");
673 });
674​
675 it("pools Codex windows by kind, whichever slot reported them", () => {
676 const codexAccount = (id: string, slots: readonly string[]) => ({
677 id,
678 driver: ProviderDriverKind.make("codex"),
679 usageLimits: {
680 checkedAt,
681 windows: slots.map((slot) => ({ ...weekly, id: slot, usedPercent: 50 })),
682 },
683 });
684 const pooled = (accounts: ReturnType<typeof codexAccount>[]) =>
685 collectLimitPools(
686 collectLimitAccounts(
687 new Map([
688 [
689 EnvironmentId.make("env-a"),
690 { ...laptop, serverConfig: { usageLimitSources: [{ ...source, accounts }] } },
691 ],
692 ]),
693 ),
694 now,
695 )[0]?.windows ?? [];
696 // A weekly-only plan reports weekly as `primary`. The pool keeps one id
697 // whichever account sorts first.
698 for (const order of [
699 [codexAccount("business", ["primary"]), codexAccount("plus", ["secondary"])],
700 [codexAccount("plus", ["secondary"]), codexAccount("business", ["primary"])],
701 ]) {
702 const windows = pooled(order);
703 expect(windows.map((window) => window.id)).toEqual(["weekly"]);
704 expect(windows[0]?.members).toHaveLength(2);
705 }
706 // A second window of the same kind on one account stays apart, with its own id.
707 expect(
708 pooled([codexAccount("a", ["secondary"]), codexAccount("b", ["primary", "secondary"])]).map(
709 (window) => window.id,
710 ),
711 ).toEqual(["weekly", "weekly:secondary"]);
712 });
713​
714 it("pools windows by id across accounts and orders resets by when they land", () => {
715 const input = new Map([
716 [
717 EnvironmentId.make("env-a"),
718 {
719 ...laptop,
720 serverConfig: {
721 providers: [],
722 usageLimitSources: [
723 {
724 ...source,
725 accounts: [
726 {
727 id: "a",
728 driver: claude,
729 usageLimits: {
730 checkedAt,
731 windows: [
732 { ...window, usedPercent: 80, resetsAt: "2026-09-03T13:00:00.000Z" },
733 { ...weekly, usedPercent: 20 },
734 ],
735 },
736 },
737 {
738 id: "b",
739 driver: claude,
740 usageLimits: {
741 checkedAt,
742 windows: [{ ...window, usedPercent: 40 }],
743 },
744 },
745 {
746 id: "c",
747 driver: ProviderDriverKind.make("codex"),
748 usageLimits: { checkedAt, windows: [{ ...weekly, usedPercent: 50 }] },
749 },
750 {
751 id: "unsupported",
752 driver: claude,
753 usageLimits: {
754 checkedAt,
755 windows: [],
756 unavailable: { reason: "unsupported" as const },
757 },
758 },
759 ],
760 },
761 ],
762 },
763 },
764 ],
765 ]);
766 const pools = collectLimitPools(collectLimitAccounts(input), now);
767 expect(pools.map((pool) => [pool.driver, pool.accounts.length])).toEqual([
768 ["claudeAgent", 2],
769 ["codex", 1],
770 ]);
771 const [session, week] = pools[0]!.windows;
772 // A member with no reset has no clock, so it does not vote on pace.
773 const untimed = collectLimitPools(
774 collectLimitAccounts(input).map((account) =>
775 account.key === "hub:b"
776 ? {
777 ...account,
778 limits: {
779 ...account.limits,
780 windows: account.limits.windows.map((w) => ({ ...w, resetsAt: undefined })),
781 },
782 }
783 : account,
784 ),
785 now,
786 );
787 // Only a votes: 80% used, 80% elapsed.
788 expect(untimed[0]?.windows[0]?.pace).toBe("on");
789 // a is 80% through its window and b 60%: the pool is 70% elapsed, 60% used.
790 expect(session).toMatchObject({
791 id: "five_hour",
792 remainingPercent: 40,
793 usedPercent: 60,
794 pace: "under",
795 });
796 expect(
797 session?.resets.map((reset) => [reset.member.account.key, reset.restoresPercent]),
798 ).toEqual([
799 ["hub:a", 40],
800 ["hub:b", 20],
801 ]);
802 expect(week).toMatchObject({ id: "seven_day", remainingPercent: 80, members: [{}] });
803 // Codex reports `primary` for both its five-hour and (on Go) monthly window.
804 const mixed = collectLimitPools(
805 [
806 ...collectLimitAccounts(input),
807 {
808 key: "go",
809 driver: claude,
810 displayName: "Go",
811 email: undefined,
812 plan: undefined,
813 accentColor: undefined,
814 environments: [],
815 sourceLabel: null,
816 redeem: null,
817 limits: {
818 checkedAt,
819 windows: [
820 {
821 id: "five_hour",
822 kind: "monthly",
823 label: "Monthly",
824 usedPercent: 82,
825 windowDurationMins: 30 * 24 * 60,
826 resetsAt: "2026-09-14T12:00:00.000Z",
827 },
828 ],
829 },
830 },
831 ],
832 now,
833 );
834 expect(mixed[0]?.windows.map((window) => [window.kind, window.members.length])).toEqual([
835 ["session", 2],
836 ["weekly", 1],
837 ["monthly", 1],
838 ]);
839 // Session resets determine the account order for every row.
840 expect(session?.members.map((member) => member.account.key)).toEqual(["hub:a", "hub:b"]);
841 expect(pools[0]?.accounts.map((account) => account.key)).toEqual(["hub:a", "hub:b"]);
842 });
843});
844​
845describe("pooled account columns", () => {
846 const weekly = {
847 ...window,
848 id: "seven_day",
849 kind: "weekly",
850 label: "Weekly",
851 windowDurationMins: 7 * 24 * 60,
852 } as const;
853 const account = (key: string, windows: LimitAccount["limits"]["windows"]): LimitAccount => ({
854 key,
855 driver: ProviderDriverKind.make("claudeAgent"),
856 displayName: key,
857 email: undefined,
858 plan: undefined,
859 accentColor: undefined,
860 environments: [],
861 sourceLabel: "Hub",
862 redeem: null,
863 limits: { checkedAt: "2026-09-03T11:00:00.000Z", windows },
864 });
865 const keys = (pool: ReturnType<typeof collectLimitPools>[number]) =>
866 pool.windows.map((row) =>
867 row.columns.map((member) => (member.window ? member.account.key : null)),
868 );
869​
870 it("keeps session columns across rows with opposite reset and usage orders", () => {
871 const accounts = [
872 account("a", [
873 { ...weekly, usedPercent: 80, resetsAt: "2026-09-05T12:00:00.000Z" },
874 { ...window, usedPercent: 10, resetsAt: "2026-09-03T15:00:00.000Z" },
875 ]),
876 account("b", [
877 { ...weekly, usedPercent: 20, resetsAt: "2026-09-06T12:00:00.000Z" },
878 { ...window, usedPercent: 90, resetsAt: "2026-09-03T13:00:00.000Z" },
879 ]),
880 ];
881 const [pool] = collectLimitPools(accounts, now);
882 expect(pool!.accounts.map((account) => account.key)).toEqual(["b", "a"]);
883 expect(keys(pool!)).toEqual([
884 ["b", "a"],
885 ["b", "a"],
886 ]);
887 expect(pool!.windows[1]!.resets.map((reset) => reset.member.account.key)).toEqual(["a", "b"]);
888 expect(pool!.windows[1]!.remainingPercent).toBe(50);
889 expect(keys(collectLimitPools(accounts.toReversed(), now)[0]!)).toEqual(keys(pool!));
890 });
891​
892 it("preserves gaps without counting missing windows toward pooled quota", () => {
893 const [pool] = collectLimitPools(
894 [
895 account("a", [window]),
896 account("b", [
897 { ...window, resetsAt: "2026-09-03T15:00:00.000Z" },
898 { ...weekly, usedPercent: 80 },
899 ]),
900 account("c", [weekly]),
901 ],
902 now,
903 );
904 expect(keys(pool!)).toEqual([
905 ["a", "b", null],
906 [null, "b", "c"],
907 ]);
908 expect(pool!.windows[1]!.members.map((member) => member.account.key)).toEqual(["b", "c"]);
909 expect(pool!.windows[1]!.remainingPercent).toBe(40);
910 expect(pool!.windows[1]!.resets.map((reset) => reset.restoresPercent)).toEqual([40, 20]);
911 });
912​
913 it("falls back to weekly resets when no account reports a session", () => {
914 const [pool] = collectLimitPools(
915 [
916 account("a", [{ ...weekly, resetsAt: "2026-09-06T12:00:00.000Z" }]),
917 account("b", [{ ...weekly, resetsAt: "2026-09-05T12:00:00.000Z" }]),
918 ],
919 now,
920 );
921 expect(keys(pool!)).toEqual([["b", "a"]]);
922 });
923​
924 it("sorts unknown resets last and breaks ties consistently", () => {
925 const accounts = [
926 account("z", [{ ...window, resetsAt: undefined }]),
927 account("b", [window]),
928 account("a", [window]),
929 account("y", [{ ...window, resetsAt: "invalid" }]),
930 ];
931 expect(keys(collectLimitPools(accounts, now)[0]!)).toEqual([["a", "b", "y", "z"]]);
932 expect(keys(collectLimitPools(accounts.toReversed(), now)[0]!)).toEqual([["a", "b", "y", "z"]]);
933 });
934});
935​
936describe("Cursor limit presentation", () => {
937 const cursorAccount: LimitAccount = {
938 key: "cursor",
939 driver: ProviderDriverKind.make("cursor"),
940 displayName: "Cursor",
941 email: undefined,
942 plan: undefined,
943 accentColor: undefined,
944 environments: [],
945 sourceLabel: "Cursor",
946 redeem: null,
947 limits: {
948 checkedAt: "2026-09-03T11:00:00.000Z",
949 windows: [
950 { id: "apiPercentUsed", kind: "monthly", label: "Other Models", usedPercent: 49 },
951 { id: "autoPercentUsed", kind: "monthly", label: "Cursor Models", usedPercent: 9 },
952 { id: "totalPercentUsed", kind: "monthly", label: "Overall", usedPercent: 15 },
953 ],
954 },
955 };
956​
957 it("hides the combined percentage and orders the two pools", () => {
958 const [pool] = collectLimitPools([cursorAccount], now);
959 const display = displayLimitWindows(pool!);
960 expect(display.map((window) => window.id)).toEqual(["autoPercentUsed", "apiPercentUsed"]);
961 });
962​
963 it("keeps the combined percentage as a card if either allowance is missing", () => {
964 const [pool] = collectLimitPools(
965 [
966 {
967 ...cursorAccount,
968 limits: {
969 ...cursorAccount.limits,
970 windows: cursorAccount.limits.windows.filter(
971 (window) => window.id !== "apiPercentUsed",
972 ),
973 },
974 },
975 ],
976 now,
977 );
978 const display = displayLimitWindows(pool!);
979 expect(display.map((window) => window.id)).toEqual(["totalPercentUsed", "autoPercentUsed"]);
980 });
981});
982​
983describe("collectLimitNotices", () => {
984 const checkedAt = "2026-09-03T11:00:00.000Z";
985 const claude = ProviderDriverKind.make("claudeAgent");
986 const laptop = { entry: { target: { label: "Laptop" } } };
987 const hub = {
988 id: UsageLimitSourceId.make("hub"),
989 kind: "cliproxy" as const,
990 label: "hub",
991 checkedAt,
992 accounts: [],
993 };
994​
995 it("names failures and silence, skips unsupported accounts, and labels environments only when several", () => {
996 const failed = provider({
997 instanceId: ProviderInstanceId.make("claude"),
998 driver: claude,
999 displayName: "Claude Max",
1000 usageLimits: { checkedAt, windows: [], unavailable: { reason: "probeFailed" } },
1001 });
1002 const apiKey = provider({
1003 instanceId: ProviderInstanceId.make("api"),
1004 driver: claude,
1005 usageLimits: { checkedAt, windows: [], unavailable: { reason: "unsupported" } },
1006 });
1007 const silent = provider({ usageLimits: { checkedAt, windows: [] } });
1008 const one = new Map([
1010 EnvironmentId.make("env-a"),
1012 ...laptop,
1013 serverConfig: {
1014 providers: [failed, apiKey, silent],
1015 usageLimitSources: [
1016 hub,
1017 { ...hub, id: UsageLimitSourceId.make("down"), label: "down", error: "ECONNREFUSED" },
1018 ],
1019 },
1020 },
1021 ],
1022 ]);
1023 expect(collectLimitNotices(one)).toEqual([
1024 "Claude Max: Could not read limits.",
1025 "codex: No limits reported.",
1026 "hub: No accounts reported.",
1027 "down: ECONNREFUSED",
1028 ]);
1029​
1030 one.set(EnvironmentId.make("env-b"), {
1031 entry: { target: { label: "Desktop" } },
1032 serverConfig: { providers: [], usageLimitSources: [] },
1033 });
1034 expect(collectLimitNotices(one)[0]).toBe("Laptop · Claude Max: Could not read limits.");
1035 });
1036});
1037​
1038describe("/usage-limits", () => {
1039 const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] };
1040 const selected = provider({
1041 usageLimits: limits,
1042 auth: { status: "authenticated", email: "same@example.com" },
1043 });
1044 const sources = [
1046 id: UsageLimitSourceId.make("hub"),
1047 kind: "cliproxy" as const,
1048 label: "Accounts",
1049 checkedAt: limits.checkedAt,
1050 accounts: [
1052 id: "duplicate",
1053 driver: selected.driver,
1054 email: "SAME@example.com",
1055 usageLimits: limits,
1056 },
1057 { id: "oss", driver: selected.driver, plan: "Codex OSS", usageLimits: limits },
1058 { id: "other-provider", driver: ProviderDriverKind.make("claude"), usageLimits: limits },
1059 ],
1060 },
1061 ];
1062​
1063 it("uses hub credit balances and redemption targets in the composer, including native duplicates", () => {
1064 const hubs = sources.map((source) => ({
1065 ...source,
1066 accounts: source.accounts.map((account) => ({
1067 ...account,
1068 usageLimits: {
1069 ...account.usageLimits,
1070 resetCredits: { availableCount: 2, nextCreditId: `${account.id}-credit` },
1071 },
1072 })),
1073 }));
1074 const report = collectProviderUsageLimits(selected.instanceId, [selected], hubs, now);
1075 expect(report?.accounts[0]?.limits.resetCredits?.availableCount).toBe(2);
1076 expect(report?.accounts[0]?.resetCreditInput).toEqual({
1077 sourceId: "hub",
1078 accountId: "duplicate",
1079 creditId: "duplicate-credit",
1080 });
1081 expect(report?.accounts.find((account) => account.id === "hub:oss")?.resetCreditInput).toEqual({
1082 sourceId: "hub",
1083 accountId: "oss",
1084 creditId: "oss-credit",
1085 });
1086 });
1087​
1088 it("gives a hub credit to no native row when its email is signed in to two workspaces", () => {
1089 const workspaces = ["ws-a", "ws-b"].map((workspaceId) =>
1090 provider({
1091 instanceId: ProviderInstanceId.make(workspaceId),
1092 usageLimits: limits,
1093 auth: { status: "authenticated", email: "same@example.com", workspaceId },
1094 }),
1095 );
1096 const hub = [
1098 ...sources[0]!,
1099 accounts: [
1101 id: "duplicate",
1102 driver: selected.driver,
1103 email: "same@example.com",
1104 usageLimits: { ...limits, resetCredits: { availableCount: 2, nextCreditId: "c" } },
1105 },
1106 ],
1107 },
1108 ];
1109 const report = collectProviderUsageLimits(workspaces[0]!.instanceId, workspaces, hub, now);
1110 expect(report?.accounts.map((account) => account.resetCreditInput)).toEqual([
1111 { instanceId: "ws-a" },
1112 { instanceId: "ws-b" },
1113 { sourceId: "hub", accountId: "duplicate", creditId: "c" },
1114 ]);
1115 });
1116​
1117 it("redeems a native duplicate through the hub even when the native snapshot is fresher", () => {
1118 const fresher = provider({
1119 usageLimits: {
1120 checkedAt: "2026-09-03T11:30:00.000Z",
1121 windows: [window],
1122 resetCredits: { availableCount: 3, nextCreditId: "native-credit" },
1123 },
1124 auth: { status: "authenticated", email: "same@example.com" },
1125 });
1126 const stale = [
1128 id: UsageLimitSourceId.make("hub"),
1129 kind: "cliproxy" as const,
1130 label: "Accounts",
1131 checkedAt: limits.checkedAt,
1132 accounts: [
1134 id: "duplicate",
1135 driver: fresher.driver,
1136 email: "SAME@example.com",
1137 usageLimits: {
1138 ...limits,
1139 resetCredits: { availableCount: 2, nextCreditId: "hub-credit" },
1140 },
1141 },
1142 ],
1143 },
1144 ];
1145 const report = collectProviderUsageLimits(fresher.instanceId, [fresher], stale, now);
1146 // Only redeeming through the hub clears the routing cooldown it holds for
1147 // this account, so the hub wins the path even with a staler balance.
1148 expect(report?.accounts[0]?.resetCreditInput).toEqual({
1149 sourceId: "hub",
1150 accountId: "duplicate",
1151 creditId: "hub-credit",
1152 });
1153 // The fresher native balance is still the one shown.
1154 expect(report?.accounts[0]?.limits.resetCredits?.availableCount).toBe(3);
1155 });
1156​
1157 it("keeps accounts and custom instances separate, filtering by driver", () => {
1158 const report = collectProviderUsageLimits(
1159 selected.instanceId,
1161 selected,
1162 provider({
1163 instanceId: ProviderInstanceId.make("codex-work"),
1164 displayName: "Work",
1165 usageLimits: { ...limits, resetCredits: { availableCount: 2 } },
1166 }),
1167 provider({
1168 driver: ProviderDriverKind.make("claude"),
1169 instanceId: ProviderInstanceId.make("claude"),
1170 usageLimits: limits,
1171 }),
1172 ],
1173 sources,
1174 now,
1175 );
1176 expect(report?.createdAt).toBe("2026-09-03T12:00:00.000Z");
1177 expect(report?.accounts.map((account) => account.id)).toEqual([
1178 "codex",
1179 "codex-work",
1180 "hub:oss",
1181 ]);
1182 expect(report?.accounts[0]).toMatchObject({
1183 instanceId: selected.instanceId,
1184 email: selected.auth.email,
1185 });
1186 expect(report?.accounts[1]).toMatchObject({
1187 displayName: "Work",
1188 limits: { resetCredits: { availableCount: 2 } },
1189 });
1190 expect(report?.accounts[2]).toMatchObject({
1191 label: "Accounts · oss",
1192 sourceLabel: "CLI Proxy",
1193 plan: "Codex OSS",
1194 });
1195 expect(report?.notices).toEqual([]);
1196 });
1197​
1198 it("supports a source-only provider and keeps duplicates when the native probe failed", () => {
1199 expect(
1200 collectProviderUsageLimits(selected.instanceId, [provider({})], sources, now)?.accounts.map(
1201 (account) => account.id,
1202 ),
1203 ).toEqual(["hub:duplicate", "hub:oss"]);
1204 const failed = provider({ usageLimits: { ...limits, unavailable: { reason: "probeFailed" } } });
1205 expect(
1206 collectProviderUsageLimits(selected.instanceId, [failed], sources, now)?.accounts.map(
1207 (account) => account.id,
1208 ),
1209 ).toEqual(["codex", "hub:duplicate", "hub:oss"]);
1210 expect(collectProviderUsageLimits(selected.instanceId, [provider({})], [], now)).toBeNull();
1211 expect(
1212 collectProviderUsageLimits(
1213 selected.instanceId,
1214 [provider({ enabled: false, usageLimits: limits })],
1215 [],
1216 now,
1217 ),
1218 ).toBeNull();
1219 });
1220​
1221 it("surfaces source errors only for sources that carry the selected driver", () => {
1222 const failing = { ...sources[0]!, error: "token expired" };
1223 expect(
1224 collectProviderUsageLimits(selected.instanceId, [selected], [failing], now)?.notices,
1225 ).toEqual(["Accounts: token expired"]);
1226 const claudeOnly = { ...failing, accounts: failing.accounts.slice(2) };
1227 expect(
1228 collectProviderUsageLimits(selected.instanceId, [selected], [claudeOnly], now)?.notices,
1229 ).toEqual([]);
1230 // A read failure clears the accounts, so the error must not depend on a match.
1231 const unreadable = { ...failing, accounts: [] };
1232 expect(
1233 collectProviderUsageLimits(selected.instanceId, [selected], [unreadable], now)?.notices,
1234 ).toEqual(["Accounts: token expired"]);
1235 // A source-only provider still gets the report, carrying only the error.
1236 const sourceOnly = collectProviderUsageLimits(
1237 selected.instanceId,
1238 [provider({})],
1239 [unreadable],
1240 now,
1241 );
1242 expect(sourceOnly?.accounts).toEqual([]);
1243 expect(sourceOnly?.notices).toEqual(["Accounts: token expired"]);
1244 });
1245​
1246 it("advertises global and workspace commands only for providers present in Limits", () => {
1247 const withWorkspace = provider({
1248 workspaceSnapshots: [
1249 { cwd: "/tmp/project", checkedAt: limits.checkedAt, slashCommands: [], skills: [] },
1250 ],
1251 });
1252 const [supported] = withUsageLimitsCommands([withWorkspace], sources);
1253 expect(supported?.slashCommands.map((command) => command.name)).toEqual(["usage-limits"]);
1254 expect(
1255 supported?.workspaceSnapshots?.[0]?.slashCommands.map((command) => command.name),
1256 ).toEqual(["usage-limits"]);
1257 expect(withUsageLimitsCommands([withWorkspace], [])[0]?.slashCommands).toEqual([]);
1258 // A provider's own command of the same name is left alone without coverage.
1259 const ownCommand = provider({
1260 slashCommands: [{ name: "usage-limits", description: "Provider's own" }],
1261 });
1262 expect(withUsageLimitsCommands([ownCommand], [])[0]?.slashCommands).toEqual([
1263 { name: "usage-limits", description: "Provider's own" },
1264 ]);
1265 const unreadable = { ...sources[0]!, accounts: [], error: "token expired" };
1266 expect(
1267 withUsageLimitsCommands([withWorkspace], [unreadable])[0]?.slashCommands.map(
1268 (command) => command.name,
1269 ),
1270 ).toEqual(["usage-limits"]);
1271 expect(
1272 withUsageLimitsCommands([selected], [])[0]?.slashCommands.map((command) => command.name),
1273 ).toEqual(["usage-limits"]);
1274 });
1275});
1276​
1277describe("sameUsageLimitCommandCoverage", () => {
1278 const codexAccount = {
1279 id: "a",
1280 driver: ProviderDriverKind.make("codex"),
1281 usageLimits: { checkedAt: "2026-09-03T11:00:00.000Z", windows: [] },
1282 };
1283 const base = {
1284 id: UsageLimitSourceId.make("hub"),
1285 kind: "cliproxy" as const,
1286 label: "Accounts",
1287 checkedAt: "2026-09-03T11:00:00.000Z",
1288 };
1289 it("ignores quota movement but not the drivers offered the command", () => {
1290 const withCodex = [{ ...base, accounts: [codexAccount] }];
1291 const withCodexLater = [
1293 ...base,
1294 accounts: [
1296 ...codexAccount,
1297 usageLimits: { ...codexAccount.usageLimits, checkedAt: "2026-09-03T12:00:00.000Z" },
1298 },
1299 ],
1300 },
1301 ];
1302 expect(sameUsageLimitCommandCoverage(withCodex, withCodexLater)).toBe(true);
1303 expect(sameUsageLimitCommandCoverage(withCodex, [{ ...base, accounts: [] }])).toBe(false);
1304 });
1305 it("treats a failed read as a change in coverage, in both directions", () => {
1306 const empty = [{ ...base, accounts: [] }];
1307 const failed = [{ ...base, accounts: [], error: "token expired" }];
1308 expect(sameUsageLimitCommandCoverage(empty, failed)).toBe(false);
1309 expect(sameUsageLimitCommandCoverage(failed, empty)).toBe(false);
1310 expect(
1311 sameUsageLimitCommandCoverage(failed, [{ ...base, accounts: [], error: "still down" }]),
1312 ).toBe(true);
1313 });
1314});
1315​
1316describe("remainingPercent", () => {
1317 it("inverts and clamps the reported usage", () => {
1318 expect(remainingPercent(window)).toBe(60);
1319 expect(remainingPercent({ ...window, usedPercent: 0 })).toBe(100);
1320 expect(remainingPercent({ ...window, usedPercent: 100 })).toBe(0);
1321 expect(remainingPercent({ ...window, usedPercent: 33.4 })).toBe(67);
1322 });
1323});
1324​
1325describe("isUsageLimitsCommand", () => {
1326 it("recognizes only the standalone local action", () => {
1327 expect(isUsageLimitsCommand(" /USAGE-LIMITS\n")).toBe(true);
1328 expect(isUsageLimitsCommand("/usage-limits explain")).toBe(false);
1329 expect(isUsageLimitsCommand("Explain /usage-limits")).toBe(false);
1330 expect(isUsageLimitsCommand("/usage")).toBe(false);
1331 });
1332});
1333​
1334describe("external usage settings", () => {
1335 it("deduplicates destinations across accounts and environments without inventing quota pools", () => {
1336 const managed = provider({
1337 usageLimits: {
1338 checkedAt: "2026-09-03T11:00:00.000Z",
1339 windows: [],
1340 unavailable: { reason: "unsupported", message: "Track usage in ChatGPT." },
1341 externalUsage: { label: "ChatGPT usage", url: "https://chatgpt.com/#settings/Usage" },
1342 },
1343 });
1344 const presentations = new Map([
1346 EnvironmentId.make("a"),
1348 entry: { target: { label: "A" } },
1349 serverConfig: {
1350 providers: [managed, { ...managed, instanceId: ProviderInstanceId.make("personal") }],
1351 },
1352 },
1353 ],
1355 EnvironmentId.make("b"),
1356 { entry: { target: { label: "B" } }, serverConfig: { providers: [managed] } },
1357 ],
1358 ]);
1359 expect(collectExternalUsageLinks(presentations)).toEqual([
1361 ...managed.usageLimits!.externalUsage,
1362 message: "Track usage in ChatGPT.",
1363 accounts: [`${managed.instanceId} on A`, "personal on A", `${managed.instanceId} on B`],
1364 },
1365 ]);
1366 expect(collectLimitAccounts(presentations)).toEqual([]);
1367 expect(collectLimitNotices(presentations)).toEqual([]);
1368 });
1369 it("omits disabled, uninstalled and signed-out providers", () => {
1370 const managed = provider({
1371 usageLimits: {
1372 checkedAt: "2026-09-03T11:00:00.000Z",
1373 windows: [],
1374 externalUsage: { label: "ChatGPT usage", url: "https://chatgpt.com/#settings/Usage" },
1375 },
1376 });
1377 const presentations = new Map([
1379 EnvironmentId.make("a"),
1381 entry: { target: { label: "A" } },
1382 serverConfig: {
1383 providers: [
1384 { ...managed, enabled: false },
1385 { ...managed, installed: false },
1386 { ...managed, auth: { status: "unauthenticated" as const } },
1387 provider({}),
1388 ],
1389 },
1390 },
1391 ],
1392 ]);
1393 expect(collectExternalUsageLinks(presentations)).toEqual([]);
1394 });
1395});
1396​
1397describe("ChatGPT sharing presentation", () => {
1398 it("requires verified sharing metadata rather than the Codex driver or login type", () => {
1399 const codex = provider({ auth: { status: "authenticated", type: "chatgpt" } });
1400 expect(usesChatGptSharing(codex)).toBe(false);
1401 expect(
1402 usesChatGptSharing({ ...codex, auth: { ...codex.auth, subscriptionSharing: true } }),
1403 ).toBe(true);
1404 expect(
1405 usesChatGptSharing({
1406 ...codex,
1407 auth: { status: "unauthenticated", subscriptionSharing: true },
1408 }),
1409 ).toBe(false);
1410 });
1411});
1412​
1413describe("Antigravity limit presentation", () => {
1414 it("keeps the shared Claude/GPT bucket separate from Gemini and pairs their windows", () => {
1415 const antigravity = provider({
1416 instanceId: ProviderInstanceId.make("antigravity"),
1417 driver: ProviderDriverKind.make("antigravity"),
1418 usageLimits: {
1419 checkedAt: "2026-10-09T12:00:00.000Z",
1420 windows: [
1421 { ...window, id: "3p-5h", label: "Claude & GPT (shared) · 5-hour", usedPercent: 90 },
1422 { ...window, id: "gemini-5h", label: "Gemini · 5-hour", usedPercent: 20 },
1424 ...window,
1425 id: "3p-weekly",
1426 kind: "weekly",
1427 label: "Claude & GPT (shared) · Weekly",
1428 usedPercent: 100,
1429 },
1431 ...window,
1432 id: "gemini-weekly",
1433 kind: "weekly",
1434 label: "Gemini · Weekly",
1435 usedPercent: 10,
1436 },
1437 ],
1438 },
1439 });
1440 const accounts = collectLimitAccounts(
1441 new Map([
1443 EnvironmentId.make("a"),
1445 entry: { target: { label: "Local" } },
1446 serverConfig: {
1447 providers: [
1448 provider({
1449 usageLimits: { checkedAt: "2026-10-09T12:00:00.000Z", windows: [window] },
1450 }),
1451 antigravity,
1452 ],
1453 },
1454 },
1455 ],
1456 ]),
1457 );
1458 const pools = collectLimitPools(accounts, now);
1459 expect(pools.map((pool) => pool.driver)).toEqual(["codex", "antigravity"]);
1460 const windows = displayLimitWindows(pools[1]!);
1461 expect(windows.map((window) => window.id)).toEqual([
1462 "gemini-5h",
1463 "gemini-weekly",
1464 "3p-5h",
1465 "3p-weekly",
1466 ]);
1467 expect(windows.map((window) => window.remainingPercent)).toEqual([80, 90, 10, 0]);
1468 });
1469});