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