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
4 changes: 2 additions & 2 deletions src/codex/auth-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ function codexAccountPersistenceConflict(
: undefined;
}

function isThirtyDayOnlyPlan(plan: string | null | undefined): boolean {
const normalized = plan?.trim().toLowerCase();
function isThirtyDayOnlyPlan(plan: unknown): boolean {
const normalized = typeof plan === "string" ? plan.trim().toLowerCase() : undefined;
return normalized === "go" || normalized === "free";
}

Expand Down
4 changes: 2 additions & 2 deletions src/codex/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ function deleteScopedHealth(accountId: string, scope: CodexQuotaScope): void {
export function computeCodexUsageScore(quota: {
weeklyPercent?: number;
monthlyPercent?: number;
} | null, plan?: string | null): number {
} | null, plan?: unknown): number {
if (!quota) return CODEX_UNKNOWN_USAGE_SCORE;
const normalizedPlan = plan?.trim().toLowerCase();
const normalizedPlan = typeof plan === "string" ? plan.trim().toLowerCase() : undefined;
if (normalizedPlan === "go" || normalizedPlan === "free") {
return typeof quota.monthlyPercent === "number" && Number.isFinite(quota.monthlyPercent)
? quota.monthlyPercent
Expand Down
25 changes: 25 additions & 0 deletions tests/codex-auth-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,31 @@ describe("codex-auth API", () => {
}
});

test("GET /api/codex-auth/accounts tolerates a non-string persisted plan", async () => {
const config = makeConfig({
codexAccounts: [
{ id: "pool-invalid-plan", email: "invalid@example.test", plan: { tier: "go" }, isMain: false },
] as unknown as OcxConfig["codexAccounts"],
});
saveCodexAccountCredential("pool-invalid-plan", {
accessToken: "access-invalid-plan",
refreshToken: "refresh-invalid-plan",
expiresAt: Date.now() + 5 * 60_000,
chatgptAccountId: "acct-invalid-plan",
});
updateAccountQuota("pool-invalid-plan", 91, 111, 33, 333);

const req = new Request("http://localhost/api/codex-auth/accounts", { method: "GET" });
const resp = await handleCodexAuthAPI(req, new URL(req.url), config);
const data = await resp!.json() as { accounts: Array<{ id: string; quota?: Record<string, unknown> }> };

expect(resp?.status).toBe(200);
expect(data.accounts.find(a => a.id === "pool-invalid-plan")?.quota).toMatchObject({
weeklyPercent: 91,
monthlyPercent: 33,
});
});

test("GET /api/codex-auth/accounts maps go tertiary quota response to 30d display quota", async () => {
const config = makeConfig({
codexAccounts: [{ id: "pool-go-primary", email: "go-primary@example.test", plan: "go", isMain: false }],
Expand Down
5 changes: 5 additions & 0 deletions tests/codex-routing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ describe("codex routing", () => {
expect(computeCodexUsageScore({ weeklyPercent: 1 }, "go")).toBe(CODEX_UNKNOWN_USAGE_SCORE);
});

test("usage score tolerates a non-string persisted plan", () => {
expect(computeCodexUsageScore({ weeklyPercent: 27, monthlyPercent: 12 }, { tier: "go" })).toBe(27);
expect(computeCodexUsageScore({ weeklyPercent: 27, monthlyPercent: 12 }, 1)).toBe(27);
});

test("usage score treats unknown quota conservatively", () => {
expect(computeCodexUsageScore(null)).toBe(CODEX_UNKNOWN_USAGE_SCORE);
expect(computeCodexUsageScore({})).toBe(CODEX_UNKNOWN_USAGE_SCORE);
Expand Down
Loading