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 usageLimitSources: [{ ...source, accounts: [withoutEmail("a.json", "ws-1")] }],
562 },
563 },
564 ],
565 [
566 EnvironmentId.make("env-b"),
567 {
568 ...laptop,
569 serverConfig: {
570 usageLimitSources: [{ ...source, accounts: [withoutEmail("a.json", "ws-2")] }],
571 },
572 },
573 ],
574 ]);
575 expect(collectLimitAccounts(sameFile).map((account) => account.key)).toEqual([
576 "hub:a.json:ws-1",
577 "hub:a.json:ws-2",
578 ]);
579 expect(accounts[1]?.environments.map((environment) => environment.label)).toEqual([
580 "Laptop",
581 "Desktop",
582 ]);
583 });
584
585 it("joins a report without a workspace to the only workspace signed in with its email", () => {
586 const native = provider({
587 driver: claude,
588 auth: { status: "authenticated", email: "same@example.com", workspaceId: "org-a" },
589 usageLimits: { checkedAt, windows: [window] },
590 });
591 const hubAccount = {
592 id: "claude.json",
593 driver: claude,
594 email: "same@example.com",
595 usageLimits: { checkedAt, windows: [window] },
596 };
597 const withOther = (other: ServerProvider) =>
598 new Map([
599 [
600 EnvironmentId.make("env-a"),
601 {
602 ...laptop,
603 serverConfig: {
604 providers: [native, other],
605 usageLimitSources: [{ ...source, accounts: [hubAccount] }],
606 },
607 },
608 ],
609 ]);
610 const unrelated = provider({ instanceId: ProviderInstanceId.make("codex-2") });
611 expect(collectLimitAccounts(withOther(unrelated)).map((account) => account.key)).toEqual([
612 "env-a:codex",
613 ]);
614 const unreadable = provider({
615 ...native,
616 instanceId: ProviderInstanceId.make("team"),
617 auth: { ...native.auth, workspaceId: "org-b" },
618 usageLimits: { checkedAt, windows: [], unavailable: { reason: "probeFailed" } },
619 });
620 expect(collectLimitAccounts(withOther(unreadable)).map((account) => account.key)).toEqual([
621 "env-a:codex",
622 "hub:claude.json",
623 ]);
624 });
625
626 it("names an environment once however many of its instances share the account", () => {
627 const shared = provider({
628 auth: { status: "authenticated", email: "same@example.com" },
629 usageLimits: { checkedAt, windows: [window] },
630 });
631 const input = new Map([
632 [
633 EnvironmentId.make("env-a"),
634 {
635 ...laptop,
636 serverConfig: {
637 providers: [shared, { ...shared, instanceId: ProviderInstanceId.make("work") }],
638 },
639 },
640 ],
641 ]);
642 expect(collectLimitAccounts(input)[0]?.environments).toEqual([
643 { environmentId: "env-a", label: "Laptop" },
644 ]);
645 });
646
647 it("keys a hub account without an email by hub, so two environments on one hub share it", () => {
648 const seat = {
649 id: "claude-team-seat.json",
650 driver: claude,
651 usageLimits: { checkedAt, windows: [window] },
652 };
653 const hub = { ...source, accounts: [seat] };
654 const input = new Map([
655 [EnvironmentId.make("env-a"), { ...laptop, serverConfig: { usageLimitSources: [hub] } }],
656 [
657 EnvironmentId.make("env-b"),
658 { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [hub] } },
659 ],
660 ]);
661 const accounts = collectLimitAccounts(input);
662 expect(accounts.map((account) => account.key)).toEqual(["hub:claude-team-seat.json"]);
663 expect(accounts[0]?.displayName).toBe("claude-team-seat");
664 });
665
666 it("pools Codex windows by kind, whichever slot reported them", () => {
667 const codexAccount = (id: string, slots: readonly string[]) => ({
668 id,
669 driver: ProviderDriverKind.make("codex"),
670 usageLimits: {
671 checkedAt,
672 windows: slots.map((slot) => ({ ...weekly, id: slot, usedPercent: 50 })),
673 },
674 });
675 const pooled = (accounts: ReturnType<typeof codexAccount>[]) =>
676 collectLimitPools(
677 collectLimitAccounts(
678 new Map([
679 [
680 EnvironmentId.make("env-a"),
681 { ...laptop, serverConfig: { usageLimitSources: [{ ...source, accounts }] } },
682 ],
683 ]),
684 ),
685 now,
686 )[0]?.windows ?? [];
687 for (const order of [
688 [codexAccount("business", ["primary"]), codexAccount("plus", ["secondary"])],
689 [codexAccount("plus", ["secondary"]), codexAccount("business", ["primary"])],
690 ]) {
691 const windows = pooled(order);
692 expect(windows.map((window) => window.id)).toEqual(["weekly"]);
693 expect(windows[0]?.members).toHaveLength(2);
694 }
695 expect(
696 pooled([codexAccount("a", ["secondary"]), codexAccount("b", ["primary", "secondary"])]).map(
697 (window) => window.id,
698 ),
699 ).toEqual(["weekly", "weekly:secondary"]);
700 });
701
702 it("pools windows by id across accounts and orders resets by when they land", () => {
703 const input = new Map([
704 [
705 EnvironmentId.make("env-a"),
706 {
707 ...laptop,
708 serverConfig: {
709 providers: [],
710 usageLimitSources: [
711 {
712 ...source,
713 accounts: [
714 {
715 id: "a",
716 driver: claude,
717 usageLimits: {
718 checkedAt,
719 windows: [
720 { ...window, usedPercent: 80, resetsAt: "2026-09-03T13:00:00.000Z" },
721 { ...weekly, usedPercent: 20 },
722 ],
723 },
724 },
725 {
726 id: "b",
727 driver: claude,
728 usageLimits: {
729 checkedAt,
730 windows: [{ ...window, usedPercent: 40 }],
731 },
732 },
733 {
734 id: "c",
735 driver: ProviderDriverKind.make("codex"),
736 usageLimits: { checkedAt, windows: [{ ...weekly, usedPercent: 50 }] },
737 },
738 {
739 id: "unsupported",
740 driver: claude,
741 usageLimits: {
742 checkedAt,
743 windows: [],
744 unavailable: { reason: "unsupported" as const },
745 },
746 },
747 ],
748 },
749 ],
750 },
751 },
752 ],
753 ]);
754 const pools = collectLimitPools(collectLimitAccounts(input), now);
755 expect(pools.map((pool) => [pool.driver, pool.accounts.length])).toEqual([
756 ["claudeAgent", 2],
757 ["codex", 1],
758 ]);
759 const [session, week] = pools[0]!.windows;
760 const untimed = collectLimitPools(
761 collectLimitAccounts(input).map((account) =>
762 account.key === "hub:b"
763 ? {
764 ...account,
765 limits: {
766 ...account.limits,
767 windows: account.limits.windows.map((w) => ({ ...w, resetsAt: undefined })),
768 },
769 }
770 : account,
771 ),
772 now,
773 );
774 expect(untimed[0]?.windows[0]?.pace).toBe("on");
775 expect(session).toMatchObject({
776 id: "five_hour",
777 remainingPercent: 40,
778 usedPercent: 60,
779 pace: "under",
780 });
781 expect(
782 session?.resets.map((reset) => [reset.member.account.key, reset.restoresPercent]),
783 ).toEqual([
784 ["hub:a", 40],
785 ["hub:b", 20],
786 ]);
787 expect(week).toMatchObject({ id: "seven_day", remainingPercent: 80, members: [{}] });
788 // Codex reports `primary` for both its five-hour and (on Go) monthly window.
789 const mixed = collectLimitPools(
790 [
791 ...collectLimitAccounts(input),
792 {
793 key: "go",
794 driver: claude,
795 displayName: "Go",
796 email: undefined,
797 plan: undefined,
798 accentColor: undefined,
799 environments: [],
800 sourceLabel: null,
801 redeem: null,
802 limits: {
803 checkedAt,
804 windows: [
805 {
806 id: "five_hour",
807 kind: "monthly",
808 label: "Monthly",
809 usedPercent: 82,
810 windowDurationMins: 30 * 24 * 60,
811 resetsAt: "2026-09-14T12:00:00.000Z",
812 },
813 ],
814 },
815 },
816 ],
817 now,
818 );
819 expect(mixed[0]?.windows.map((window) => [window.kind, window.members.length])).toEqual([
820 ["session", 2],
821 ["weekly", 1],
822 ["monthly", 1],
823 ]);
824 expect(session?.members.map((member) => member.account.key)).toEqual(["hub:a", "hub:b"]);
825 expect(pools[0]?.accounts.map((account) => account.key)).toEqual(["hub:a", "hub:b"]);
826 });
827});
828
829describe("pooled account columns", () => {
830 const weekly = {
831 ...window,
832 id: "seven_day",
833 kind: "weekly",
834 label: "Weekly",
835 windowDurationMins: 7 * 24 * 60,
836 } as const;
837 const account = (key: string, windows: LimitAccount["limits"]["windows"]): LimitAccount => ({
838 key,
839 driver: ProviderDriverKind.make("claudeAgent"),
840 displayName: key,
841 email: undefined,
842 plan: undefined,
843 accentColor: undefined,
844 environments: [],
845 sourceLabel: "Hub",
846 redeem: null,
847 limits: { checkedAt: "2026-09-03T11:00:00.000Z", windows },
848 });
849 const keys = (pool: ReturnType<typeof collectLimitPools>[number]) =>
850 pool.windows.map((row) =>
851 row.columns.map((member) => (member.window ? member.account.key : null)),
852 );
853
854 it("keeps session columns across rows with opposite reset and usage orders", () => {
855 const accounts = [
856 account("a", [
857 { ...weekly, usedPercent: 80, resetsAt: "2026-09-05T12:00:00.000Z" },
858 { ...window, usedPercent: 10, resetsAt: "2026-09-03T15:00:00.000Z" },
859 ]),
860 account("b", [
861 { ...weekly, usedPercent: 20, resetsAt: "2026-09-06T12:00:00.000Z" },
862 { ...window, usedPercent: 90, resetsAt: "2026-09-03T13:00:00.000Z" },
863 ]),
864 ];
865 const [pool] = collectLimitPools(accounts, now);
866 expect(pool!.accounts.map((account) => account.key)).toEqual(["b", "a"]);
867 expect(keys(pool!)).toEqual([
868 ["b", "a"],
869 ["b", "a"],
870 ]);
871 expect(pool!.windows[1]!.resets.map((reset) => reset.member.account.key)).toEqual(["a", "b"]);
872 expect(pool!.windows[1]!.remainingPercent).toBe(50);
873 expect(keys(collectLimitPools(accounts.toReversed(), now)[0]!)).toEqual(keys(pool!));
874 });
875
876 it("preserves gaps without counting missing windows toward pooled quota", () => {
877 const [pool] = collectLimitPools(
878 [
879 account("a", [window]),
880 account("b", [
881 { ...window, resetsAt: "2026-09-03T15:00:00.000Z" },
882 { ...weekly, usedPercent: 80 },
883 ]),
884 account("c", [weekly]),
885 ],
886 now,
887 );
888 expect(keys(pool!)).toEqual([
889 ["a", "b", null],
890 [null, "b", "c"],
891 ]);
892 expect(pool!.windows[1]!.members.map((member) => member.account.key)).toEqual(["b", "c"]);
893 expect(pool!.windows[1]!.remainingPercent).toBe(40);
894 expect(pool!.windows[1]!.resets.map((reset) => reset.restoresPercent)).toEqual([40, 20]);
895 });
896
897 it("falls back to weekly resets when no account reports a session", () => {
898 const [pool] = collectLimitPools(
899 [
900 account("a", [{ ...weekly, resetsAt: "2026-09-06T12:00:00.000Z" }]),
901 account("b", [{ ...weekly, resetsAt: "2026-09-05T12:00:00.000Z" }]),
902 ],
903 now,
904 );
905 expect(keys(pool!)).toEqual([["b", "a"]]);
906 });
907
908 it("sorts unknown resets last and breaks ties consistently", () => {
909 const accounts = [
910 account("z", [{ ...window, resetsAt: undefined }]),
911 account("b", [window]),
912 account("a", [window]),
913 account("y", [{ ...window, resetsAt: "invalid" }]),
914 ];
915 expect(keys(collectLimitPools(accounts, now)[0]!)).toEqual([["a", "b", "y", "z"]]);
916 expect(keys(collectLimitPools(accounts.toReversed(), now)[0]!)).toEqual([["a", "b", "y", "z"]]);
917 });
918});
919
920describe("Cursor limit presentation", () => {
921 const cursorAccount: LimitAccount = {
922 key: "cursor",
923 driver: ProviderDriverKind.make("cursor"),
924 displayName: "Cursor",
925 email: undefined,
926 plan: undefined,
927 accentColor: undefined,
928 environments: [],
929 sourceLabel: "Cursor",
930 redeem: null,
931 limits: {
932 checkedAt: "2026-09-03T11:00:00.000Z",
933 windows: [
934 { id: "apiPercentUsed", kind: "monthly", label: "Other Models", usedPercent: 49 },
935 { id: "autoPercentUsed", kind: "monthly", label: "Cursor Models", usedPercent: 9 },
936 { id: "totalPercentUsed", kind: "monthly", label: "Overall", usedPercent: 15 },
937 ],
938 },
939 };
940
941 it("hides the combined percentage and orders the two pools", () => {
942 const [pool] = collectLimitPools([cursorAccount], now);
943 const display = displayLimitWindows(pool!);
944 expect(display.map((window) => window.id)).toEqual(["autoPercentUsed", "apiPercentUsed"]);
945 });
946
947 it("keeps the combined percentage as a card if either allowance is missing", () => {
948 const [pool] = collectLimitPools(
949 [
950 {
951 ...cursorAccount,
952 limits: {
953 ...cursorAccount.limits,
954 windows: cursorAccount.limits.windows.filter(
955 (window) => window.id !== "apiPercentUsed",
956 ),
957 },
958 },
959 ],
960 now,
961 );
962 const display = displayLimitWindows(pool!);
963 expect(display.map((window) => window.id)).toEqual(["totalPercentUsed", "autoPercentUsed"]);
964 });
965});
966
967describe("collectLimitNotices", () => {
968 const checkedAt = "2026-09-03T11:00:00.000Z";
969 const claude = ProviderDriverKind.make("claudeAgent");
970 const laptop = { entry: { target: { label: "Laptop" } } };
971 const hub = {
972 id: UsageLimitSourceId.make("hub"),
973 kind: "cliproxy" as const,
974 label: "hub",
975 checkedAt,
976 accounts: [],
977 };
978
979 it("names failures and silence, skips unsupported accounts, and labels environments only when several", () => {
980 const failed = provider({
981 instanceId: ProviderInstanceId.make("claude"),
982 driver: claude,
983 displayName: "Claude Max",
984 usageLimits: { checkedAt, windows: [], unavailable: { reason: "probeFailed" } },
985 });
986 const apiKey = provider({
987 instanceId: ProviderInstanceId.make("api"),
988 driver: claude,
989 usageLimits: { checkedAt, windows: [], unavailable: { reason: "unsupported" } },
990 });
991 const silent = provider({ usageLimits: { checkedAt, windows: [] } });
992 const one = new Map([
993 [
994 EnvironmentId.make("env-a"),
995 {
996 ...laptop,
997 serverConfig: {
998 providers: [failed, apiKey, silent],
999 usageLimitSources: [
1000 hub,
1001 { ...hub, id: UsageLimitSourceId.make("down"), label: "down", error: "ECONNREFUSED" },
1002 ],
1003 },
1004 },
1005 ],
1006 ]);
1007 expect(collectLimitNotices(one)).toEqual([
1008 "Claude Max: Could not read limits.",
1009 "codex: No limits reported.",
1010 "hub: No accounts reported.",
1011 "down: ECONNREFUSED",
1012 ]);
1013
1014 one.set(EnvironmentId.make("env-b"), {
1015 entry: { target: { label: "Desktop" } },
1016 serverConfig: { providers: [], usageLimitSources: [] },
1017 });
1018 expect(collectLimitNotices(one)[0]).toBe("Laptop · Claude Max: Could not read limits.");
1019 });
1020});
1021
1022describe("/usage-limits", () => {
1023 const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] };
1024 const selected = provider({
1025 usageLimits: limits,
1026 auth: { status: "authenticated", email: "same@example.com" },
1027 });
1028 const sources = [
1029 {
1030 id: UsageLimitSourceId.make("hub"),
1031 kind: "cliproxy" as const,
1032 label: "Accounts",
1033 checkedAt: limits.checkedAt,
1034 accounts: [
1035 {
1036 id: "duplicate",
1037 driver: selected.driver,
1038 email: "SAME@example.com",
1039 usageLimits: limits,
1040 },
1041 { id: "oss", driver: selected.driver, plan: "Codex OSS", usageLimits: limits },
1042 { id: "other-provider", driver: ProviderDriverKind.make("claude"), usageLimits: limits },
1043 ],
1044 },
1045 ];
1046
1047 it("uses hub credit balances and redemption targets in the composer, including native duplicates", () => {
1048 const hubs = sources.map((source) => ({
1049 ...source,
1050 accounts: source.accounts.map((account) => ({
1051 ...account,
1052 usageLimits: {
1053 ...account.usageLimits,
1054 resetCredits: { availableCount: 2, nextCreditId: `${account.id}-credit` },
1055 },
1056 })),
1057 }));
1058 const report = collectProviderUsageLimits(selected.instanceId, [selected], hubs, now);
1059 expect(report?.accounts[0]?.limits.resetCredits?.availableCount).toBe(2);
1060 expect(report?.accounts[0]?.resetCreditInput).toEqual({
1061 sourceId: "hub",
1062 accountId: "duplicate",
1063 creditId: "duplicate-credit",
1064 });
1065 expect(report?.accounts.find((account) => account.id === "hub:oss")?.resetCreditInput).toEqual({
1066 sourceId: "hub",
1067 accountId: "oss",
1068 creditId: "oss-credit",
1069 });
1070 });
1071
1072 it("gives a hub credit to no native row when its email is signed in to two workspaces", () => {
1073 const workspaces = ["ws-a", "ws-b"].map((workspaceId) =>
1074 provider({
1075 instanceId: ProviderInstanceId.make(workspaceId),
1076 usageLimits: limits,
1077 auth: { status: "authenticated", email: "same@example.com", workspaceId },
1078 }),
1079 );
1080 const hub = [
1081 {
1082 ...sources[0]!,
1083 accounts: [
1084 {
1085 id: "duplicate",
1086 driver: selected.driver,
1087 email: "same@example.com",
1088 usageLimits: { ...limits, resetCredits: { availableCount: 2, nextCreditId: "c" } },
1089 },
1090 ],
1091 },
1092 ];
1093 const report = collectProviderUsageLimits(workspaces[0]!.instanceId, workspaces, hub, now);
1094 expect(report?.accounts.map((account) => account.resetCreditInput)).toEqual([
1095 { instanceId: "ws-a" },
1096 { instanceId: "ws-b" },
1097 { sourceId: "hub", accountId: "duplicate", creditId: "c" },
1098 ]);
1099 });
1100
1101 it("redeems a native duplicate through the hub even when the native snapshot is fresher", () => {
1102 const fresher = provider({
1103 usageLimits: {
1104 checkedAt: "2026-09-03T11:30:00.000Z",
1105 windows: [window],
1106 resetCredits: { availableCount: 3, nextCreditId: "native-credit" },
1107 },
1108 auth: { status: "authenticated", email: "same@example.com" },
1109 });
1110 const stale = [
1111 {
1112 id: UsageLimitSourceId.make("hub"),
1113 kind: "cliproxy" as const,
1114 label: "Accounts",
1115 checkedAt: limits.checkedAt,
1116 accounts: [
1117 {
1118 id: "duplicate",
1119 driver: fresher.driver,
1120 email: "SAME@example.com",
1121 usageLimits: {
1122 ...limits,
1123 resetCredits: { availableCount: 2, nextCreditId: "hub-credit" },
1124 },
1125 },
1126 ],
1127 },
1128 ];
1129 const report = collectProviderUsageLimits(fresher.instanceId, [fresher], stale, now);
1130 expect(report?.accounts[0]?.resetCreditInput).toEqual({
1131 sourceId: "hub",
1132 accountId: "duplicate",
1133 creditId: "hub-credit",
1134 });
1135 expect(report?.accounts[0]?.limits.resetCredits?.availableCount).toBe(3);
1136 });
1137
1138 it("keeps accounts and custom instances separate, filtering by driver", () => {
1139 const report = collectProviderUsageLimits(
1140 selected.instanceId,
1141 [
1142 selected,
1143 provider({
1144 instanceId: ProviderInstanceId.make("codex-work"),
1145 displayName: "Work",
1146 usageLimits: { ...limits, resetCredits: { availableCount: 2 } },
1147 }),
1148 provider({
1149 driver: ProviderDriverKind.make("claude"),
1150 instanceId: ProviderInstanceId.make("claude"),
1151 usageLimits: limits,
1152 }),
1153 ],
1154 sources,
1155 now,
1156 );
1157 expect(report?.createdAt).toBe("2026-09-03T12:00:00.000Z");
1158 expect(report?.accounts.map((account) => account.id)).toEqual([
1159 "codex",
1160 "codex-work",
1161 "hub:oss",
1162 ]);
1163 expect(report?.accounts[0]).toMatchObject({
1164 instanceId: selected.instanceId,
1165 email: selected.auth.email,
1166 });
1167 expect(report?.accounts[1]).toMatchObject({
1168 displayName: "Work",
1169 limits: { resetCredits: { availableCount: 2 } },
1170 });
1171 expect(report?.accounts[2]).toMatchObject({
1172 label: "Accounts · oss",
1173 sourceLabel: "CLI Proxy",
1174 plan: "Codex OSS",
1175 });
1176 expect(report?.notices).toEqual([]);
1177 });
1178
1179 it("supports a source-only provider and keeps duplicates when the native probe failed", () => {
1180 expect(
1181 collectProviderUsageLimits(selected.instanceId, [provider({})], sources, now)?.accounts.map(
1182 (account) => account.id,
1183 ),
1184 ).toEqual(["hub:duplicate", "hub:oss"]);
1185 const failed = provider({ usageLimits: { ...limits, unavailable: { reason: "probeFailed" } } });
1186 expect(
1187 collectProviderUsageLimits(selected.instanceId, [failed], sources, now)?.accounts.map(
1188 (account) => account.id,
1189 ),
1190 ).toEqual(["codex", "hub:duplicate", "hub:oss"]);
1191 expect(collectProviderUsageLimits(selected.instanceId, [provider({})], [], now)).toBeNull();
1192 expect(
1193 collectProviderUsageLimits(
1194 selected.instanceId,
1195 [provider({ enabled: false, usageLimits: limits })],
1196 [],
1197 now,
1198 ),
1199 ).toBeNull();
1200 });
1201
1202 it("surfaces source errors only for sources that carry the selected driver", () => {
1203 const failing = { ...sources[0]!, error: "token expired" };
1204 expect(
1205 collectProviderUsageLimits(selected.instanceId, [selected], [failing], now)?.notices,
1206 ).toEqual(["Accounts: token expired"]);
1207 const claudeOnly = { ...failing, accounts: failing.accounts.slice(2) };
1208 expect(
1209 collectProviderUsageLimits(selected.instanceId, [selected], [claudeOnly], now)?.notices,
1210 ).toEqual([]);
1211 const unreadable = { ...failing, accounts: [] };
1212 expect(
1213 collectProviderUsageLimits(selected.instanceId, [selected], [unreadable], now)?.notices,
1214 ).toEqual(["Accounts: token expired"]);
1215 const sourceOnly = collectProviderUsageLimits(
1216 selected.instanceId,
1217 [provider({})],
1218 [unreadable],
1219 now,
1220 );
1221 expect(sourceOnly?.accounts).toEqual([]);
1222 expect(sourceOnly?.notices).toEqual(["Accounts: token expired"]);
1223 });
1224
1225 it("advertises global and workspace commands only for providers present in Limits", () => {
1226 const withWorkspace = provider({
1227 workspaceSnapshots: [
1228 { cwd: "/tmp/project", checkedAt: limits.checkedAt, slashCommands: [], skills: [] },
1229 ],
1230 });
1231 const [supported] = withUsageLimitsCommands([withWorkspace], sources);
1232 expect(supported?.slashCommands.map((command) => command.name)).toEqual(["usage-limits"]);
1233 expect(
1234 supported?.workspaceSnapshots?.[0]?.slashCommands.map((command) => command.name),
1235 ).toEqual(["usage-limits"]);
1236 expect(withUsageLimitsCommands([withWorkspace], [])[0]?.slashCommands).toEqual([]);
1237 const ownCommand = provider({
1238 slashCommands: [{ name: "usage-limits", description: "Provider's own" }],
1239 });
1240 expect(withUsageLimitsCommands([ownCommand], [])[0]?.slashCommands).toEqual([
1241 { name: "usage-limits", description: "Provider's own" },
1242 ]);
1243 const unreadable = { ...sources[0]!, accounts: [], error: "token expired" };
1244 expect(
1245 withUsageLimitsCommands([withWorkspace], [unreadable])[0]?.slashCommands.map(
1246 (command) => command.name,
1247 ),
1248 ).toEqual(["usage-limits"]);
1249 expect(
1250 withUsageLimitsCommands([selected], [])[0]?.slashCommands.map((command) => command.name),
1251 ).toEqual(["usage-limits"]);
1252 });
1253});
1254
1255describe("sameUsageLimitCommandCoverage", () => {
1256 const codexAccount = {
1257 id: "a",
1258 driver: ProviderDriverKind.make("codex"),
1259 usageLimits: { checkedAt: "2026-09-03T11:00:00.000Z", windows: [] },
1260 };
1261 const base = {
1262 id: UsageLimitSourceId.make("hub"),
1263 kind: "cliproxy" as const,
1264 label: "Accounts",
1265 checkedAt: "2026-09-03T11:00:00.000Z",
1266 };
1267 it("ignores quota movement but not the drivers offered the command", () => {
1268 const withCodex = [{ ...base, accounts: [codexAccount] }];
1269 const withCodexLater = [
1270 {
1271 ...base,
1272 accounts: [
1273 {
1274 ...codexAccount,
1275 usageLimits: { ...codexAccount.usageLimits, checkedAt: "2026-09-03T12:00:00.000Z" },
1276 },
1277 ],
1278 },
1279 ];
1280 expect(sameUsageLimitCommandCoverage(withCodex, withCodexLater)).toBe(true);
1281 expect(sameUsageLimitCommandCoverage(withCodex, [{ ...base, accounts: [] }])).toBe(false);
1282 });
1283 it("treats a failed read as a change in coverage, in both directions", () => {
1284 const empty = [{ ...base, accounts: [] }];
1285 const failed = [{ ...base, accounts: [], error: "token expired" }];
1286 expect(sameUsageLimitCommandCoverage(empty, failed)).toBe(false);
1287 expect(sameUsageLimitCommandCoverage(failed, empty)).toBe(false);
1288 expect(
1289 sameUsageLimitCommandCoverage(failed, [{ ...base, accounts: [], error: "still down" }]),
1290 ).toBe(true);
1291 });
1292});
1293
1294describe("remainingPercent", () => {
1295 it("inverts and clamps the reported usage", () => {
1296 expect(remainingPercent(window)).toBe(60);
1297 expect(remainingPercent({ ...window, usedPercent: 0 })).toBe(100);
1298 expect(remainingPercent({ ...window, usedPercent: 100 })).toBe(0);
1299 expect(remainingPercent({ ...window, usedPercent: 33.4 })).toBe(67);
1300 });
1301});
1302
1303describe("isUsageLimitsCommand", () => {
1304 it("recognizes only the standalone local action", () => {
1305 expect(isUsageLimitsCommand(" /USAGE-LIMITS\n")).toBe(true);
1306 expect(isUsageLimitsCommand("/usage-limits explain")).toBe(false);
1307 expect(isUsageLimitsCommand("Explain /usage-limits")).toBe(false);
1308 expect(isUsageLimitsCommand("/usage")).toBe(false);
1309 });
1310});
1311
1312describe("external usage settings", () => {
1313 it("deduplicates destinations across accounts and environments without inventing quota pools", () => {
1314 const managed = provider({
1315 usageLimits: {
1316 checkedAt: "2026-09-03T11:00:00.000Z",
1317 windows: [],
1318 unavailable: { reason: "unsupported", message: "Track usage in ChatGPT." },
1319 externalUsage: { label: "ChatGPT usage", url: "https://chatgpt.com/#settings/Usage" },
1320 },
1321 });
1322 const presentations = new Map([
1323 [
1324 EnvironmentId.make("a"),
1325 {
1326 entry: { target: { label: "A" } },
1327 serverConfig: {
1328 providers: [managed, { ...managed, instanceId: ProviderInstanceId.make("personal") }],
1329 },
1330 },
1331 ],
1332 [
1333 EnvironmentId.make("b"),
1334 { entry: { target: { label: "B" } }, serverConfig: { providers: [managed] } },
1335 ],
1336 ]);
1337 expect(collectExternalUsageLinks(presentations)).toEqual([
1338 {
1339 ...managed.usageLimits!.externalUsage,
1340 message: "Track usage in ChatGPT.",
1341 accounts: [`${managed.instanceId} on A`, "personal on A", `${managed.instanceId} on B`],
1342 },
1343 ]);
1344 expect(collectLimitAccounts(presentations)).toEqual([]);
1345 expect(collectLimitNotices(presentations)).toEqual([]);
1346 });
1347 it("omits disabled, uninstalled and signed-out providers", () => {
1348 const managed = provider({
1349 usageLimits: {
1350 checkedAt: "2026-09-03T11:00:00.000Z",
1351 windows: [],
1352 externalUsage: { label: "ChatGPT usage", url: "https://chatgpt.com/#settings/Usage" },
1353 },
1354 });
1355 const presentations = new Map([
1356 [
1357 EnvironmentId.make("a"),
1358 {
1359 entry: { target: { label: "A" } },
1360 serverConfig: {
1361 providers: [
1362 { ...managed, enabled: false },
1363 { ...managed, installed: false },
1364 { ...managed, auth: { status: "unauthenticated" as const } },
1365 provider({}),
1366 ],
1367 },
1368 },
1369 ],
1370 ]);
1371 expect(collectExternalUsageLinks(presentations)).toEqual([]);
1372 });
1373});
1374
1375describe("ChatGPT sharing presentation", () => {
1376 it("requires verified sharing metadata rather than the Codex driver or login type", () => {
1377 const codex = provider({ auth: { status: "authenticated", type: "chatgpt" } });
1378 expect(usesChatGptSharing(codex)).toBe(false);
1379 expect(
1380 usesChatGptSharing({ ...codex, auth: { ...codex.auth, subscriptionSharing: true } }),
1381 ).toBe(true);
1382 expect(
1383 usesChatGptSharing({
1384 ...codex,
1385 auth: { status: "unauthenticated", subscriptionSharing: true },
1386 }),
1387 ).toBe(false);
1388 });
1389});
1390
1391describe("Antigravity limit presentation", () => {
1392 it("keeps the shared Claude/GPT bucket separate from Gemini and pairs their windows", () => {
1393 const antigravity = provider({
1394 instanceId: ProviderInstanceId.make("antigravity"),
1395 driver: ProviderDriverKind.make("antigravity"),
1396 usageLimits: {
1397 checkedAt: "2026-10-09T12:00:00.000Z",
1398 windows: [
1399 { ...window, id: "3p-5h", label: "Claude & GPT (shared) · 5-hour", usedPercent: 90 },
1400 { ...window, id: "gemini-5h", label: "Gemini · 5-hour", usedPercent: 20 },
1401 {
1402 ...window,
1403 id: "3p-weekly",
1404 kind: "weekly",
1405 label: "Claude & GPT (shared) · Weekly",
1406 usedPercent: 100,
1407 },
1408 {
1409 ...window,
1410 id: "gemini-weekly",
1411 kind: "weekly",
1412 label: "Gemini · Weekly",
1413 usedPercent: 10,
1414 },
1415 ],
1416 },
1417 });
1418 const accounts = collectLimitAccounts(
1419 new Map([
1420 [
1421 EnvironmentId.make("a"),
1422 {
1423 entry: { target: { label: "Local" } },
1424 serverConfig: {
1425 providers: [
1426 provider({
1427 usageLimits: { checkedAt: "2026-10-09T12:00:00.000Z", windows: [window] },
1428 }),
1429 antigravity,
1430 ],
1431 },
1432 },
1433 ],
1434 ]),
1435 );
1436 const pools = collectLimitPools(accounts, now);
1437 expect(pools.map((pool) => pool.driver)).toEqual(["codex", "antigravity"]);
1438 const windows = displayLimitWindows(pools[1]!);
1439 expect(windows.map((window) => window.id)).toEqual([
1440 "gemini-5h",
1441 "gemini-weekly",
1442 "3p-5h",
1443 "3p-weekly",
1444 ]);
1445 expect(windows.map((window) => window.remainingPercent)).toEqual([80, 90, 10, 0]);
1446 });
1447});