import { EnvironmentId, ProviderDriverKind, ProviderInstanceId, UsageLimitSourceId, type ServerProvider, } from "@t3tools/contracts"; import { describe, expect, it, vi } from "vite-plus/test"; import { buildSubscriptionUsageSnapshot, createWidgetRefresher, WIDGET_REFRESH_INTERVAL, } from "./subscriptionUsageSnapshot"; const checkedAt = "2026-09-05T12:00:00.000Z"; const now = Date.parse(checkedAt); const window = { id: "session", kind: "session", label: "5 hours", usedPercent: 40, resetsAt: "2026-09-05T12:10:00.000Z", } as const; const limits = { checkedAt, windows: [window] }; const deepLink = "t3code-dev://settings/usage?tab=limits"; function provider(overrides: Partial = {}): ServerProvider { return { instanceId: ProviderInstanceId.make("codex"), driver: ProviderDriverKind.make("codex"), enabled: true, installed: true, version: null, status: "ready", auth: { status: "authenticated", email: "private@example.com" }, checkedAt, models: [], slashCommands: [], skills: [], usageLimits: limits, ...overrides, }; } function presentations(providers: readonly ServerProvider[] = [provider()]) { return new Map([ [ EnvironmentId.make("env"), { entry: { target: { label: "Remote" } }, serverConfig: { providers } }, ], ]); } describe("subscription widget snapshots", () => { it("uses provider data and its observation time without exposing account emails", () => { const snapshot = buildSubscriptionUsageSnapshot( presentations([provider({ displayName: "private@example.com" })]), deepLink, ); expect(snapshot.checkedAt).toBe(now); expect(snapshot.providers[0]).toMatchObject({ name: "Codex", windows: [{ remaining: 60 }], expiresAt: now + 10 * 60_000, }); expect(snapshot.url).toBe(deepLink); expect(JSON.stringify(snapshot)).not.toContain("private@example.com"); }); it("splits a pooled window by account without exposing emails", () => { const claude = (id: string, overrides: Partial) => provider({ instanceId: ProviderInstanceId.make(id), driver: ProviderDriverKind.make("claudeAgent"), auth: { status: "authenticated", email: `${id}@example.com` }, ...overrides, }); const snapshot = buildSubscriptionUsageSnapshot( presentations([ claude("smol", { displayName: "smol", accentColor: "#4c6ef5", usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 29, windowDurationMins: 300 }], }, }), claude("relay", { displayName: "relay@example.com", usageLimits: { checkedAt, windows: [{ ...window, id: "seven_day", kind: "weekly", label: "Weekly" }], }, }), ]), deepLink, ); const session = snapshot.providers[0]?.windows.find((w) => w.kind === "session"); expect(session?.accounts).toStrictEqual([ { name: "smol", color: "#4c6ef5", remaining: 71, refill: 29, resetsAt: Date.parse(window.resetsAt), reset: expect.any(String), durationMs: 300 * 60_000, }, { name: "RE" }, ]); expect(JSON.stringify(snapshot)).not.toContain("example.com"); }); it("uses upstream deduplication for a native account also present in a proxy hub", () => { const input = new Map([ [ EnvironmentId.make("env"), { entry: { target: { label: "Remote" } }, serverConfig: { providers: [provider()], usageLimitSources: [ { id: UsageLimitSourceId.make("hub"), kind: "cliproxy" as const, label: "Hub", checkedAt, accounts: [ { id: "account", driver: ProviderDriverKind.make("codex"), email: " PRIVATE@example.com ", usageLimits: limits, }, ], }, ], }, }, ], ]); expect(buildSubscriptionUsageSnapshot(input, deepLink).providers[0]?.detail).toBe( "Subscription remaining", ); input.get(EnvironmentId.make("env"))!.serverConfig.providers = []; const snapshot = buildSubscriptionUsageSnapshot(input, deepLink); expect(snapshot.providers[0]?.name).toBe("Codex"); expect(JSON.stringify(snapshot)).not.toContain("example.com"); }); it("bounds OS storage and puts the most constrained windows first", () => { const windows = Array.from({ length: 20 }, (_, index) => ({ ...window, id: `${index}`, usedPercent: index * 5, })); const snapshot = buildSubscriptionUsageSnapshot( presentations([provider({ usageLimits: { checkedAt, windows } })]), deepLink, ); expect(snapshot.providers[0]?.windows).toHaveLength(6); expect(snapshot.providers[0]?.totalWindows).toBe(20); expect(snapshot.providers[0]?.windows[0]?.remaining).toBe(5); }); it("uses the freshest copy of an account across environments before pooling", () => { const input = presentations(); input.set(EnvironmentId.make("other"), { entry: { target: { label: "Other" } }, serverConfig: { providers: [ provider({ usageLimits: { checkedAt: new Date(now + 60_000).toISOString(), windows: [{ ...window, usedPercent: 80 }], }, }), ], }, }); const snapshot = buildSubscriptionUsageSnapshot(input, deepLink); expect(snapshot.providers[0]?.detail).toBe("Subscription remaining"); expect(snapshot.providers[0]?.windows[0]?.remaining).toBe(20); }); }); describe("subscription widget expiry", () => { const maxAgeDeadline = now + 15 * 60_000; function single(resetsAt: string) { return buildSubscriptionUsageSnapshot( presentations([provider({ usageLimits: { checkedAt, windows: [{ ...window, resetsAt }] } })]), deepLink, ).providers[0]; } it.each(["2026-09-05T11:50:00.000Z", checkedAt])( "keeps usage whose reset at %s already happened when observed", (resetsAt) => { expect(single(resetsAt)).toMatchObject({ detail: "Subscription remaining", expiresAt: maxAgeDeadline, windows: [{ remaining: 60, reset: expect.stringMatching(/^Reset /) }], }); }, ); it.each([ { resetAccount: 1, expiresAt: maxAgeDeadline, label: /^Reset / }, { resetAccount: 0, expiresAt: now + 2 * 60_000, label: /^Next reset / }, ])( "compares a pooled reset with its own account's observation (account $resetAccount)", ({ resetAccount, expiresAt, label }) => { // Account 0 was observed at 12:00, account 1 at 12:05; the pooled reset is at 12:02. const snapshot = buildSubscriptionUsageSnapshot( presentations( [checkedAt, "2026-09-05T12:05:00.000Z"].map((accountCheckedAt, index) => provider({ instanceId: ProviderInstanceId.make(`codex-${index}`), auth: { status: "authenticated", email: `account-${index}@example.com` }, usageLimits: { checkedAt: accountCheckedAt, windows: [ { ...window, resetsAt: index === resetAccount ? "2026-09-05T12:02:00.000Z" : "2026-09-05T13:00:00.000Z", }, ], }, }), ), ), deepLink, ); expect(snapshot.providers[0]).toMatchObject({ detail: "2 accounts ยท pooled", expiresAt, windows: [{ remaining: 60, reset: expect.stringMatching(label) }], }); }, ); }); describe("widget refresh probes", () => { it("does not overlap a slow probe even after the refresh interval", async () => { let finish!: () => void; const probe = vi.fn( () => new Promise((resolve) => { finish = resolve; }), ); const refresh = createWidgetRefresher(probe); const first = refresh(["one", "one"], now); await refresh(["one"], now + WIDGET_REFRESH_INTERVAL); expect(probe).toHaveBeenCalledTimes(1); finish(); await first; }); });