Skip to content
Draft
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
9 changes: 8 additions & 1 deletion gui/src/provider-workspace/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ const finite = (value: unknown): number | undefined => (
typeof value === "number" && Number.isFinite(value) ? value : undefined
);

const dateTimestamp = (value: unknown): number | undefined => {
const timestamp = finite(value);
if (timestamp === undefined) return undefined;
const milliseconds = timestamp > 10_000_000_000 ? timestamp : timestamp * 1000;
return Number.isFinite(new Date(milliseconds).getTime()) ? timestamp : undefined;
};

function quotaFromUnknown(quota: unknown, fallbackUpdatedAt?: number): AccountQuota | null {
if (!quota || typeof quota !== "object" || Array.isArray(quota)) return null;
const q = quota as Record<string, unknown>;
Expand All @@ -61,7 +68,7 @@ function quotaFromUnknown(quota: unknown, fallbackUpdatedAt?: number): AccountQu
const creditsLimit = finite(creditsRaw?.limit);
const creditsRemaining = finite(creditsRaw?.remaining);
const creditsPercent = finite(creditsRaw?.percent);
const creditsExpiresAt = finite(creditsRaw?.expiresAt);
const creditsExpiresAt = dateTimestamp(creditsRaw?.expiresAt);
const creditsUsd = creditsUsed !== undefined
&& creditsLimit !== undefined
&& creditsRemaining !== undefined
Expand Down
6 changes: 6 additions & 0 deletions gui/tests/provider-capacity-credits.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ test("credits with an expiry render the localized billing-period end date", () =
expect(markup).toContain("Credits balance");
expect(markup).toContain("Billing period ends 26 Aug 2026");
});

test("credits with an out-of-range expiry render without the billing-period line", () => {
const markup = renderCredits(1e20);
expect(markup).toContain("Credits balance");
expect(markup).not.toContain("Billing period ends");
});
16 changes: 16 additions & 0 deletions gui/tests/provider-capacity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ test("provider quota reports reject malformed required credits and drop malforme
creditsUsd: { used: 12.5, limit: 50, remaining: 37.5, percent: 25 },
updatedAt: 123,
});

expect(accountQuotaFromReport({
updatedAt: 123,
quota: {
creditsUsd: {
used: 12.5,
limit: 50,
remaining: 37.5,
percent: 25,
expiresAt: 1e20,
},
},
})).toEqual({
creditsUsd: { used: 12.5, limit: 50, remaining: 37.5, percent: 25 },
updatedAt: 123,
});
});

test("capacity metadata preserves estimate, raw current quota, recovery percent, and incomplete coverage", () => {
Expand Down
3 changes: 2 additions & 1 deletion src/providers/quota.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ function normalizeResetAt(value: unknown): number | undefined {
/** Unix 0 / negative values are sentinels, not reset clocks (Command Code fiveHour.resetAt: 0). */
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;
return Number.isFinite(new Date(milliseconds).getTime()) ? milliseconds : undefined;
}

function toFiniteNumber(value: unknown): number | undefined {
Expand Down
5 changes: 2 additions & 3 deletions tests/command-code-quota.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,13 @@ describe("Command Code provider quota", () => {
expect(result.reports).toEqual([]);
});

test("a fully exhausted account still reports a zero-remaining credit window", async () => {
test("a fully exhausted account drops an out-of-range subscription expiry", 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: "2026-09-01T00:00:00.000Z" } }
? { data: { currentPeriodStart: "2026-08-01T00:00:00.000Z", currentPeriodEnd: 1e20 } }
: url.includes("/alpha/usage/summary")
? { totalCost: 12 }
: {
Expand All @@ -358,7 +358,6 @@ describe("Command Code provider quota", () => {
limit: 12,
remaining: 0,
percent: 100,
expiresAt: Date.parse("2026-09-01T00:00:00.000Z"),
},
updatedAt: expect.any(Number),
});
Expand Down
Loading