Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/providers/quota-wire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions tests/command-code-quota.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading