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