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

usageLimits.test.ts

1,395 lines46.4 KB

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