import { EnvironmentId, ProviderDriverKind, ProviderInstanceId, type ServerProvider, type UsageLimitSourceAccount, UsageLimitSourceId,} from "@t3tools/contracts";import { describe, expect, it } from "vite-plus/test"; import { type LimitAccount, isUsageLimitsCommand, collectProviderUsageLimits, sameUsageLimitCommandCoverage, withUsageLimitsCommands, collectLimitAccounts, collectLimitNotices, collectLimitPools, collectLimitSources, collectLimitsGroups, elapsedShare, formatResetsIn, limitsNotice, paceOf, providersWithLimits, remainingPercent,} from "./usageLimits.ts"; const now = Date.parse("2026-09-03T12:00:00.000Z"); const window = { id: "five_hour", kind: "session", label: "Session", usedPercent: 40, windowDurationMins: 300, resetsAt: "2026-09-03T14:00:00.000Z",} as const; function provider(overrides: Partial<ServerProvider>): ServerProvider { return { instanceId: ProviderInstanceId.make("codex"), driver: ProviderDriverKind.make("codex"), enabled: true, installed: true, version: null, status: "ready", auth: { status: "authenticated" }, checkedAt: "2026-09-03T11:00:00.000Z", models: [], slashCommands: [], skills: [], ...overrides, };} describe("pace", () => { it("places the clock three fifths through a five-hour window with two hours left", () => { expect(elapsedShare(window, now)).toBeCloseTo(0.6); expect(paceOf(window, now)).toBe("under"); expect(paceOf({ ...window, usedPercent: 62 }, now)).toBe("on"); expect(paceOf({ ...window, usedPercent: 80 }, now)).toBe("ahead"); }); it("has no pace without a reset or a duration", () => { expect(paceOf({ ...window, resetsAt: undefined }, now)).toBeNull(); expect(paceOf({ ...window, windowDurationMins: undefined }, now)).toBeNull(); expect(formatResetsIn({ ...window, resetsAt: undefined }, now)).toBeNull(); }); it("phrases the reset as a countdown", () => { expect(formatResetsIn(window, now)).toBe("resets in 2h 0m"); expect(formatResetsIn({ ...window, resetsAt: "2026-09-06T15:30:00.000Z" }, now)).toBe( "resets in 3d 3h", ); expect(formatResetsIn({ ...window, resetsAt: "2026-09-03T11:00:00.000Z" }, now)).toBe( "resets now", ); });}); describe("limitsNotice", () => { it("explains empty bars and passes provider messages through", () => { const checkedAt = "2026-09-03T11:00:00.000Z"; expect(limitsNotice({ checkedAt, windows: [window] })).toBeNull(); expect(limitsNotice({ checkedAt, windows: [] })).toBe("No limits reported."); expect(limitsNotice({ checkedAt, windows: [], unavailable: { reason: "unsupported" } })).toBe( "This account has no subscription limits.", ); expect( limitsNotice({ checkedAt, windows: [], unavailable: { reason: "probeFailed", message: "Codex timed out." }, }), ).toBe("Codex timed out."); });}); describe("providersWithLimits", () => { it("keeps only usable providers whose driver reports limits at all", () => { const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] }; const codex = provider({ usageLimits: limits }); expect( providersWithLimits([ codex, provider({ instanceId: ProviderInstanceId.make("cursor"), driver: ProviderDriverKind.make("cursor"), }), provider({ instanceId: ProviderInstanceId.make("off"), enabled: false, usageLimits: limits, }), provider({ instanceId: ProviderInstanceId.make("gone"), installed: false, usageLimits: limits, }), provider({ instanceId: ProviderInstanceId.make("shadow"), availability: "unavailable", usageLimits: limits, }), ]), ).toEqual([codex]); });}); describe("collectLimitsGroups", () => { it("labels environments only when more than one reports limits", () => { const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] }; const codex = provider({ usageLimits: limits }); const one = new Map([ ["env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [codex] } }], [ "env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [provider({})] } }, ], ] as const); expect(collectLimitsGroups(one as never).map((group) => group.environmentLabel)).toEqual([ null, ]); const two = new Map([ ["env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [codex] } }], ["env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [codex] } }], ] as const); expect(collectLimitsGroups(two as never).map((group) => group.environmentLabel)).toEqual([ "Laptop", "Desktop", ]); });}); describe("collectLimitSources", () => { const source = { id: UsageLimitSourceId.make("cliproxy-hub"), kind: "cliproxy" as const, label: "hub", checkedAt: "2026-09-03T11:00:00.000Z", accounts: [], }; const limits = { checkedAt: source.checkedAt, windows: [window] }; const account: UsageLimitSourceAccount = { id: "codex-personal", driver: ProviderDriverKind.make("codex"), email: "person@example.com", plan: "ChatGPT Pro Subscription", usageLimits: limits, }; const native = provider({ displayName: "Personal", auth: { status: "authenticated", email: account.email }, usageLimits: { ...limits, resetCredits: { availableCount: 2 } }, }); function presentations( providers: readonly ServerProvider[], accounts: readonly UsageLimitSourceAccount[] = [account], ) { return new Map([ [ EnvironmentId.make("env-a"), { entry: { target: { label: "Laptop" } }, serverConfig: { providers, usageLimitSources: [{ ...source, accounts }] }, }, ], ]); } it.each(["codex", "claudeAgent"])( "prefers native %s limits by email without changing provider rows or source snapshots", (kind) => { const driver = ProviderDriverKind.make(kind); const first = { ...native, driver }; const second = { ...first, instanceId: ProviderInstanceId.make("work") }; const accounts = [{ ...account, driver, email: " Person@Example.COM " }]; const input = presentations([first, second], accounts); expect(collectLimitSources(input)).toMatchObject([{ accounts: [], hiddenAccountCount: 1 }]); expect(collectLimitsGroups(input)[0]?.providers).toEqual([first, second]); expect(accounts).toHaveLength(1); expect(first.usageLimits?.resetCredits?.availableCount).toBe(2); }, ); it("matches across environments even when the hub is visited before the native provider", () => { const input = presentations([]); input.set(EnvironmentId.make("env-b"), { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [native], usageLimitSources: [] }, }); expect(collectLimitSources(input)).toMatchObject([ { accounts: [], hiddenAccountCount: 1, environmentId: "env-a" }, ]); }); it("keeps other providers, other emails, and unidentified accounts with the same plan", () => { const accounts = [ account, { ...account, id: "other-provider", driver: ProviderDriverKind.make("claudeAgent") }, { ...account, id: "other-email", email: "other@example.com" }, { ...account, id: "unknown-email", email: undefined }, ]; expect(collectLimitSources(presentations([native], accounts))).toMatchObject([ { accounts: accounts.slice(1), hiddenAccountCount: 1 }, ]); expect( collectLimitSources( presentations([{ ...native, auth: { status: "authenticated" } }], accounts), )[0]?.accounts, ).toEqual(accounts); }); it.each([ { enabled: false }, { installed: false }, { availability: "unavailable" }, { usageLimits: undefined }, { usageLimits: { ...limits, windows: [] } }, { usageLimits: { ...limits, unavailable: { reason: "probeFailed" } } }, { usageLimits: { ...limits, unavailable: { reason: "unsupported" } } }, ] satisfies Partial<ServerProvider>[])( "retains hub limits when the native provider cannot show them: %j", (overrides) => { expect(collectLimitSources(presentations([{ ...native, ...overrides }]))).toMatchObject([ { accounts: [account], hiddenAccountCount: 0 }, ]); }, ); it("restores the hub account when the matching provider disappears", () => { const input = presentations([native]); expect(collectLimitSources(input)[0]?.accounts).toEqual([]); input.delete(EnvironmentId.make("env-a")); for (const [id, entry] of presentations([])) input.set(id, entry); expect(collectLimitSources(input)[0]?.accounts).toEqual([account]); }); it("keeps source errors and genuinely empty sources distinguishable from hidden accounts", () => { const input = new Map([ [ EnvironmentId.make("env-a"), { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [native], usageLimitSources: [{ ...source, error: "Hub unavailable" }], }, }, ], ]); expect(collectLimitSources(input)).toMatchObject([ { accounts: [], hiddenAccountCount: 0, error: "Hub unavailable" }, ]); }); it("keys sources per environment and names the environment only when several have some", () => { const one = new Map([ [ "env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { usageLimitSources: [source] } }, ], [ "env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [] } }, ], ] as const); expect(collectLimitSources(one as never).map((entry) => [entry.key, entry.label])).toEqual([ ["env-a:cliproxy-hub", "hub"], ]); const two = new Map([ [ "env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { usageLimitSources: [source] } }, ], [ "env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [source] } }, ], ] as const); expect(collectLimitSources(two as never).map((entry) => entry.label)).toEqual([ "Laptop · hub", "Desktop · hub", ]); });}); describe("pools", () => { const checkedAt = "2026-09-03T11:00:00.000Z"; const weekly = { id: "seven_day", kind: "weekly", label: "Weekly", windowDurationMins: 7 * 24 * 60, resetsAt: "2026-09-06T12:00:00.000Z", } as const; const claude = ProviderDriverKind.make("claudeAgent"); const source = { id: UsageLimitSourceId.make("hub"), kind: "cliproxy" as const, label: "hub", checkedAt, }; const laptop = { entry: { target: { label: "Laptop" } } }; it("merges one account reported natively on two environments and by a hub into one entry", () => { const native = provider({ driver: claude, instanceId: ProviderInstanceId.make("claude"), auth: { status: "authenticated", email: "Same@example.com" }, usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 40 }] }, }); const input = new Map([ [EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native] } }], [ EnvironmentId.make("env-b"), { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [ { ...native, usageLimits: { checkedAt: "2026-09-03T11:30:00.000Z", windows: [{ ...window, usedPercent: 55 }], }, }, ], usageLimitSources: [ { ...source, accounts: [ { id: "claude-same@example.com.json", driver: claude, email: "same@example.com", plan: "Claude Subscription", usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 10 }] }, }, ], }, ], }, }, ], ]); const accounts = collectLimitAccounts(input); expect(accounts).toHaveLength(1); expect(accounts[0]).toMatchObject({ key: "env-a:claude", sourceLabel: null, // Desktop's read is fresher, so its credits and its redeem are the ones on show. redeem: { environmentId: "env-b", input: { instanceId: "claude" } }, environments: [ { environmentId: "env-a", label: "Laptop" }, { environmentId: "env-b", label: "Desktop" }, ], }); // The fresher native snapshot wins; the hub row is pre-filtered by email. expect(accounts[0]?.limits.windows[0]?.usedPercent).toBe(55); }); it("takes windows from a fresher hub read but credits and redeem from the native instance", () => { const native = provider({ driver: claude, instanceId: ProviderInstanceId.make("claude"), auth: { status: "authenticated", email: "same@example.com" }, usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 40 }], resetCredits: { availableCount: 2 }, }, }); const input = new Map([ [ EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native], usageLimitSources: [ { ...source, accounts: [ { id: "claude-same@example.com.json", driver: claude, email: "same@example.com", usageLimits: { checkedAt: "2026-09-03T11:30:00.000Z", windows: [{ ...window, usedPercent: 55 }], }, }, ], }, ], }, }, ], ]); const [account] = collectLimitAccounts(input); expect(account?.limits.windows[0]?.usedPercent).toBe(55); expect(account?.limits.resetCredits?.availableCount).toBe(2); expect(account?.redeem).toEqual({ environmentId: "env-a", input: { instanceId: "claude" } }); expect(account?.environments).toEqual([{ environmentId: "env-a", label: "Laptop" }]); }); it("redeems through the hub when it holds a credit, even with a fresher native read", () => { const native = provider({ driver: claude, instanceId: ProviderInstanceId.make("claude"), auth: { status: "authenticated", email: "same@example.com" }, usageLimits: { checkedAt: "2026-09-03T11:30:00.000Z", windows: [{ ...window, usedPercent: 40 }], resetCredits: { availableCount: 3, nextCreditId: "native-credit" }, }, }); const input = new Map([ [ EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native], usageLimitSources: [ { ...source, accounts: [ { id: "claude-same@example.com.json", driver: claude, email: "same@example.com", usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 55 }], resetCredits: { availableCount: 2, nextCreditId: "hub-credit" }, }, }, ], }, ], }, }, ], ]); const [account] = collectLimitAccounts(input); // Only the hub path clears the routing cooldown it holds for this account. expect(account?.redeem).toEqual({ environmentId: "env-a", input: { sourceId: "hub", accountId: "claude-same@example.com.json", creditId: "hub-credit" }, }); // The fresher native balance is still the one shown. expect(account?.limits.resetCredits?.availableCount).toBe(3); }); it("redeems on the environment whose snapshot supplied the credits on show", () => { const stale = provider({ auth: { status: "authenticated", email: "same@example.com" }, usageLimits: { checkedAt, windows: [window], resetCredits: { availableCount: 0 }, }, }); const fresh = { ...stale, usageLimits: { checkedAt: "2026-09-03T11:30:00.000Z", windows: [window], resetCredits: { availableCount: 2 },Lines 1–500 of 1198. Load more
import { EnvironmentId, ProviderDriverKind, ProviderInstanceId, type ServerProvider, type UsageLimitSourceAccount, UsageLimitSourceId, } from "@t3tools/contracts"; import { describe, expect, it } from "vite-plus/test"; import { type LimitAccount, isUsageLimitsCommand, collectProviderUsageLimits, sameUsageLimitCommandCoverage, withUsageLimitsCommands, collectLimitAccounts, collectLimitNotices, collectLimitPools, collectLimitSources, collectLimitsGroups, elapsedShare, formatResetsIn, limitsNotice, paceOf, providersWithLimits, remainingPercent, } from "./usageLimits.ts"; const now = Date.parse("2026-09-03T12:00:00.000Z"); const window = { id: "five_hour", kind: "session", label: "Session", usedPercent: 40, windowDurationMins: 300, resetsAt: "2026-09-03T14:00:00.000Z", } as const; function provider(overrides: Partial<ServerProvider>): ServerProvider { return { instanceId: ProviderInstanceId.make("codex"), driver: ProviderDriverKind.make("codex"), enabled: true, installed: true, version: null, status: "ready", auth: { status: "authenticated" }, checkedAt: "2026-09-03T11:00:00.000Z", models: [], slashCommands: [], skills: [], ...overrides, }; } describe("pace", () => { it("places the clock three fifths through a five-hour window with two hours left", () => { expect(elapsedShare(window, now)).toBeCloseTo(0.6); expect(paceOf(window, now)).toBe("under"); expect(paceOf({ ...window, usedPercent: 62 }, now)).toBe("on"); expect(paceOf({ ...window, usedPercent: 80 }, now)).toBe("ahead"); }); it("has no pace without a reset or a duration", () => { expect(paceOf({ ...window, resetsAt: undefined }, now)).toBeNull(); expect(paceOf({ ...window, windowDurationMins: undefined }, now)).toBeNull(); expect(formatResetsIn({ ...window, resetsAt: undefined }, now)).toBeNull(); }); it("phrases the reset as a countdown", () => { expect(formatResetsIn(window, now)).toBe("resets in 2h 0m"); expect(formatResetsIn({ ...window, resetsAt: "2026-09-06T15:30:00.000Z" }, now)).toBe( "resets in 3d 3h", ); expect(formatResetsIn({ ...window, resetsAt: "2026-09-03T11:00:00.000Z" }, now)).toBe( "resets now", ); }); }); describe("limitsNotice", () => { it("explains empty bars and passes provider messages through", () => { const checkedAt = "2026-09-03T11:00:00.000Z"; expect(limitsNotice({ checkedAt, windows: [window] })).toBeNull(); expect(limitsNotice({ checkedAt, windows: [] })).toBe("No limits reported."); expect(limitsNotice({ checkedAt, windows: [], unavailable: { reason: "unsupported" } })).toBe( "This account has no subscription limits.", ); expect( limitsNotice({ checkedAt, windows: [], unavailable: { reason: "probeFailed", message: "Codex timed out." }, }), ).toBe("Codex timed out."); }); }); describe("providersWithLimits", () => { it("keeps only usable providers whose driver reports limits at all", () => { const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] }; const codex = provider({ usageLimits: limits }); expect( providersWithLimits([ codex, provider({ instanceId: ProviderInstanceId.make("cursor"), driver: ProviderDriverKind.make("cursor"), }), provider({ instanceId: ProviderInstanceId.make("off"), enabled: false, usageLimits: limits, }), provider({ instanceId: ProviderInstanceId.make("gone"), installed: false, usageLimits: limits, }), provider({ instanceId: ProviderInstanceId.make("shadow"), availability: "unavailable", usageLimits: limits, }), ]), ).toEqual([codex]); }); }); describe("collectLimitsGroups", () => { it("labels environments only when more than one reports limits", () => { const limits = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [window] }; const codex = provider({ usageLimits: limits }); const one = new Map([ ["env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [codex] } }], [ "env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [provider({})] } }, ], ] as const); expect(collectLimitsGroups(one as never).map((group) => group.environmentLabel)).toEqual([ null, ]); const two = new Map([ ["env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [codex] } }], ["env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [codex] } }], ] as const); expect(collectLimitsGroups(two as never).map((group) => group.environmentLabel)).toEqual([ "Laptop", "Desktop", ]); }); }); describe("collectLimitSources", () => { const source = { id: UsageLimitSourceId.make("cliproxy-hub"), kind: "cliproxy" as const, label: "hub", checkedAt: "2026-09-03T11:00:00.000Z", accounts: [], }; const limits = { checkedAt: source.checkedAt, windows: [window] }; const account: UsageLimitSourceAccount = { id: "codex-personal", driver: ProviderDriverKind.make("codex"), email: "person@example.com", plan: "ChatGPT Pro Subscription", usageLimits: limits, }; const native = provider({ displayName: "Personal", auth: { status: "authenticated", email: account.email }, usageLimits: { ...limits, resetCredits: { availableCount: 2 } }, }); function presentations( providers: readonly ServerProvider[], accounts: readonly UsageLimitSourceAccount[] = [account], ) { return new Map([ [ EnvironmentId.make("env-a"), { entry: { target: { label: "Laptop" } }, serverConfig: { providers, usageLimitSources: [{ ...source, accounts }] }, }, ], ]); } it.each(["codex", "claudeAgent"])( "prefers native %s limits by email without changing provider rows or source snapshots", (kind) => { const driver = ProviderDriverKind.make(kind); const first = { ...native, driver }; const second = { ...first, instanceId: ProviderInstanceId.make("work") }; const accounts = [{ ...account, driver, email: " Person@Example.COM " }]; const input = presentations([first, second], accounts); expect(collectLimitSources(input)).toMatchObject([{ accounts: [], hiddenAccountCount: 1 }]); expect(collectLimitsGroups(input)[0]?.providers).toEqual([first, second]); expect(accounts).toHaveLength(1); expect(first.usageLimits?.resetCredits?.availableCount).toBe(2); }, ); it("matches across environments even when the hub is visited before the native provider", () => { const input = presentations([]); input.set(EnvironmentId.make("env-b"), { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [native], usageLimitSources: [] }, }); expect(collectLimitSources(input)).toMatchObject([ { accounts: [], hiddenAccountCount: 1, environmentId: "env-a" }, ]); }); it("keeps other providers, other emails, and unidentified accounts with the same plan", () => { const accounts = [ account, { ...account, id: "other-provider", driver: ProviderDriverKind.make("claudeAgent") }, { ...account, id: "other-email", email: "other@example.com" }, { ...account, id: "unknown-email", email: undefined }, ]; expect(collectLimitSources(presentations([native], accounts))).toMatchObject([ { accounts: accounts.slice(1), hiddenAccountCount: 1 }, ]); expect( collectLimitSources( presentations([{ ...native, auth: { status: "authenticated" } }], accounts), )[0]?.accounts, ).toEqual(accounts); }); it.each([ { enabled: false }, { installed: false }, { availability: "unavailable" }, { usageLimits: undefined }, { usageLimits: { ...limits, windows: [] } }, { usageLimits: { ...limits, unavailable: { reason: "probeFailed" } } }, { usageLimits: { ...limits, unavailable: { reason: "unsupported" } } }, ] satisfies Partial<ServerProvider>[])( "retains hub limits when the native provider cannot show them: %j", (overrides) => { expect(collectLimitSources(presentations([{ ...native, ...overrides }]))).toMatchObject([ { accounts: [account], hiddenAccountCount: 0 }, ]); }, ); it("restores the hub account when the matching provider disappears", () => { const input = presentations([native]); expect(collectLimitSources(input)[0]?.accounts).toEqual([]); input.delete(EnvironmentId.make("env-a")); for (const [id, entry] of presentations([])) input.set(id, entry); expect(collectLimitSources(input)[0]?.accounts).toEqual([account]); }); it("keeps source errors and genuinely empty sources distinguishable from hidden accounts", () => { const input = new Map([ [ EnvironmentId.make("env-a"), { entry: { target: { label: "Laptop" } }, serverConfig: { providers: [native], usageLimitSources: [{ ...source, error: "Hub unavailable" }], }, }, ], ]); expect(collectLimitSources(input)).toMatchObject([ { accounts: [], hiddenAccountCount: 0, error: "Hub unavailable" }, ]); }); it("keys sources per environment and names the environment only when several have some", () => { const one = new Map([ [ "env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { usageLimitSources: [source] } }, ], [ "env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [] } }, ], ] as const); expect(collectLimitSources(one as never).map((entry) => [entry.key, entry.label])).toEqual([ ["env-a:cliproxy-hub", "hub"], ]); const two = new Map([ [ "env-a", { entry: { target: { label: "Laptop" } }, serverConfig: { usageLimitSources: [source] } }, ], [ "env-b", { entry: { target: { label: "Desktop" } }, serverConfig: { usageLimitSources: [source] } }, ], ] as const); expect(collectLimitSources(two as never).map((entry) => entry.label)).toEqual([ "Laptop · hub", "Desktop · hub", ]); }); }); describe("pools", () => { const checkedAt = "2026-09-03T11:00:00.000Z"; const weekly = { id: "seven_day", kind: "weekly", label: "Weekly", windowDurationMins: 7 * 24 * 60, resetsAt: "2026-09-06T12:00:00.000Z", } as const; const claude = ProviderDriverKind.make("claudeAgent"); const source = { id: UsageLimitSourceId.make("hub"), kind: "cliproxy" as const, label: "hub", checkedAt, }; const laptop = { entry: { target: { label: "Laptop" } } }; it("merges one account reported natively on two environments and by a hub into one entry", () => { const native = provider({ driver: claude, instanceId: ProviderInstanceId.make("claude"), auth: { status: "authenticated", email: "Same@example.com" }, usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 40 }] }, }); const input = new Map([ [EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native] } }], [ EnvironmentId.make("env-b"), { entry: { target: { label: "Desktop" } }, serverConfig: { providers: [ { ...native, usageLimits: { checkedAt: "2026-09-03T11:30:00.000Z", windows: [{ ...window, usedPercent: 55 }], }, }, ], usageLimitSources: [ { ...source, accounts: [ { id: "claude-same@example.com.json", driver: claude, email: "same@example.com", plan: "Claude Subscription", usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 10 }] }, }, ], }, ], }, }, ], ]); const accounts = collectLimitAccounts(input); expect(accounts).toHaveLength(1); expect(accounts[0]).toMatchObject({ key: "env-a:claude", sourceLabel: null, // Desktop's read is fresher, so its credits and its redeem are the ones on show. redeem: { environmentId: "env-b", input: { instanceId: "claude" } }, environments: [ { environmentId: "env-a", label: "Laptop" }, { environmentId: "env-b", label: "Desktop" }, ], }); // The fresher native snapshot wins; the hub row is pre-filtered by email. expect(accounts[0]?.limits.windows[0]?.usedPercent).toBe(55); }); it("takes windows from a fresher hub read but credits and redeem from the native instance", () => { const native = provider({ driver: claude, instanceId: ProviderInstanceId.make("claude"), auth: { status: "authenticated", email: "same@example.com" }, usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 40 }], resetCredits: { availableCount: 2 }, }, }); const input = new Map([ [ EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native], usageLimitSources: [ { ...source, accounts: [ { id: "claude-same@example.com.json", driver: claude, email: "same@example.com", usageLimits: { checkedAt: "2026-09-03T11:30:00.000Z", windows: [{ ...window, usedPercent: 55 }], }, }, ], }, ], }, }, ], ]); const [account] = collectLimitAccounts(input); expect(account?.limits.windows[0]?.usedPercent).toBe(55); expect(account?.limits.resetCredits?.availableCount).toBe(2); expect(account?.redeem).toEqual({ environmentId: "env-a", input: { instanceId: "claude" } }); expect(account?.environments).toEqual([{ environmentId: "env-a", label: "Laptop" }]); }); it("redeems through the hub when it holds a credit, even with a fresher native read", () => { const native = provider({ driver: claude, instanceId: ProviderInstanceId.make("claude"), auth: { status: "authenticated", email: "same@example.com" }, usageLimits: { checkedAt: "2026-09-03T11:30:00.000Z", windows: [{ ...window, usedPercent: 40 }], resetCredits: { availableCount: 3, nextCreditId: "native-credit" }, }, }); const input = new Map([ [ EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native], usageLimitSources: [ { ...source, accounts: [ { id: "claude-same@example.com.json", driver: claude, email: "same@example.com", usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 55 }], resetCredits: { availableCount: 2, nextCreditId: "hub-credit" }, }, }, ], }, ], }, }, ], ]); const [account] = collectLimitAccounts(input); // Only the hub path clears the routing cooldown it holds for this account. expect(account?.redeem).toEqual({ environmentId: "env-a", input: { sourceId: "hub", accountId: "claude-same@example.com.json", creditId: "hub-credit" }, }); // The fresher native balance is still the one shown. expect(account?.limits.resetCredits?.availableCount).toBe(3); }); it("redeems on the environment whose snapshot supplied the credits on show", () => { const stale = provider({ auth: { status: "authenticated", email: "same@example.com" }, usageLimits: { checkedAt, windows: [window], resetCredits: { availableCount: 0 }, }, }); const fresh = { ...stale, usageLimits: { checkedAt: "2026-09-03T11:30:00.000Z", windows: [window], resetCredits: { availableCount: 2 },