|
1 | | -import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
| 1 | +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"; |
2 | 2 | import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { join } from "node:path"; |
| 5 | +import * as authApi from "../src/codex/auth-api"; |
5 | 6 | import { clearAccountNeedsReauth, markAccountNeedsReauth } from "../src/codex/account-runtime-state"; |
6 | 7 | import { clearMainAccountInfoCache } from "../src/codex/main-account-cache"; |
7 | 8 | import { clearAccountQuota, updateAccountQuota } from "../src/codex/quota"; |
@@ -544,6 +545,35 @@ describe("fetchProviderQuotaReports", () => { |
544 | 545 | expect(calls).toEqual(new Map([["chatgpt-main-account", 1], ["added-chatgpt-id", 1]])); |
545 | 546 | }); |
546 | 547 |
|
| 548 | + test("one non-forced Pool refresh shares its probe snapshot before the commit recheck", async () => { |
| 549 | + saveCodexAccountCredential("added", { |
| 550 | + accessToken: "added-access", refreshToken: "added-refresh", |
| 551 | + expiresAt: Date.now() + 3600_000, chatgptAccountId: "added-chatgpt-id", |
| 552 | + }); |
| 553 | + const config = testConfig(); |
| 554 | + config.providers = { openai: config.providers.openai }; |
| 555 | + config.codexAccounts = [{ id: "added", email: "[email protected]", plan: "prolite", isMain: false }]; |
| 556 | + globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => { |
| 557 | + const accountId = (init?.headers as Record<string, string> | undefined)?.["ChatGPT-Account-Id"]; |
| 558 | + return new Response(JSON.stringify({ |
| 559 | + plan_type: accountId === "added-chatgpt-id" ? "prolite" : "plus", |
| 560 | + rate_limit: { secondary_window: { used_percent: 25, reset_at: 1_999_000_000 } }, |
| 561 | + }), { status: 200, headers: { "content-type": "application/json" } }); |
| 562 | + }) as typeof fetch; |
| 563 | + |
| 564 | + const snapshotSpy = spyOn(authApi, "listCodexAuthAccountsSnapshot"); |
| 565 | + try { |
| 566 | + await fetchProviderQuotaReports(config); |
| 567 | + |
| 568 | + // The cache-key and provider phases share call 1; call 2 is the intentional |
| 569 | + // post-probe commit-key recheck. Before the fix there were 3 calls. |
| 570 | + expect(snapshotSpy).toHaveBeenCalledTimes(2); |
| 571 | + expect(snapshotSpy.mock.calls.map(call => call[1] ?? false)).toEqual([false, false]); |
| 572 | + } finally { |
| 573 | + snapshotSpy.mockRestore(); |
| 574 | + } |
| 575 | + }); |
| 576 | + |
547 | 577 | test("all-excluded pool still returns a coverage-only OpenAI report", async () => { |
548 | 578 | rmSync(join(codexHome, "auth.json"), { force: true }); |
549 | 579 | clearMainAccountInfoCache(); |
|
0 commit comments