diff --git a/src/providers/quota-wire.ts b/src/providers/quota-wire.ts index 0abcb69efe..31a0c81457 100644 --- a/src/providers/quota-wire.ts +++ b/src/providers/quota-wire.ts @@ -30,7 +30,12 @@ export const QUOTA_JSON_READ_FAILURE = Symbol("quota-json-read-failure"); /** Unix 0 / negative values are sentinels, not reset clocks (Command Code fiveHour.resetAt: 0). */ export function epochMillis(value: number): number | undefined { if (!Number.isFinite(value) || value <= 0) return undefined; - return value > 10_000_000_000 ? value : value * 1000; + const milliseconds = value > 10_000_000_000 ? value : value * 1000; + // A finite number is not necessarily a representable date. ECMAScript caps time values at + // ±8.64e15 ms, and `Intl.DateTimeFormat.format()` throws a RangeError past that instead of + // rendering something wrong. A provider that reports a bogus expiry must not become a + // rendering fault in every consumer that formats it. + return Number.isFinite(new Date(milliseconds).getTime()) ? milliseconds : undefined; } export function normalizeResetAt(value: unknown): number | undefined { diff --git a/tests/command-code-quota.test.ts b/tests/command-code-quota.test.ts index 3e8da6d0d3..8ba447baaf 100644 --- a/tests/command-code-quota.test.ts +++ b/tests/command-code-quota.test.ts @@ -364,6 +364,38 @@ describe("Command Code provider quota", () => { }); }); + // An out-of-range period end is not merely a wrong date: every consumer that formats it + // through Intl throws a RangeError. Drop the field and keep the usable credit figures. + test("an out-of-range subscription period end is dropped, not carried into the report", async () => { + globalThis.fetch = (async (input: RequestInfo | URL) => { + const url = String(input); + const body = url.includes("/alpha/whoami") + ? {} + : url.includes("/alpha/billing/subscriptions") + ? { data: { currentPeriodStart: "2026-08-01T00:00:00.000Z", currentPeriodEnd: 1e20 } } + : url.includes("/alpha/usage/summary") + ? { totalCost: 12 } + : { + credits: { monthlyCredits: 0, purchasedCredits: 0, freeCredits: 0 }, + windowLimits: { fiveHour: { cap: 100, used: 40 } }, + }; + return new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } }); + }) as typeof fetch; + + const result = await fetchProviderQuotaReports(commandCodeConfig(), true); + + expect(result.reports[0]?.quota).toEqual({ + fiveHourPercent: 40, + creditsUsd: { + used: 12, + limit: 12, + remaining: 0, + percent: 100, + }, + updatedAt: expect.any(Number), + }); + }); + test("a mixed balance with roll-over purchased credits carries no subscription expiry", async () => { globalThis.fetch = (async (input: RequestInfo | URL) => { const url = String(input);