Skip to content

Commit e1fb675

Browse files
authored
Merge pull request #2549 from lidge-jun/codex/api-usage-overlay-version-oracle
test(usage): stop asserting the overlay version against a moving oracle
2 parents c0c9544 + 121ecbc commit e1fb675

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bitkyc08/opencodex",
3-
"version": "2.32.0",
3+
"version": "2.32.1-preview.20260825",
44
"description": "Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code",
55
"type": "module",
66
"main": "./bin/package-main.mjs",

‎tests/api-usage.test.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { join } from "node:path";
66
import { saveConfig } from "../src/config";
77
import { startServer } from "../src/server";
88
import type { OcxConfig } from "../src/types";
9-
import { refreshUserCostOverlays, userCostOverlayVersion } from "../src/usage/user-cost-overlays";
9+
import { refreshUserCostOverlays, resetPreservedDiskOnlyProvidersForTests, userCostOverlayVersion } from "../src/usage/user-cost-overlays";
1010
import { stopUserCostOverlayReconciler } from "../src/usage/user-cost-overlay-reconciler";
1111
import { installIsolatedCodexHome, type IsolatedCodexHome } from "./helpers/isolated-codex-home";
1212
import { resetUsageReadCacheForTests, setManagementUsageMaxEntriesForTests, usageReadCacheStatsForTests } from "../src/usage/log";
@@ -76,6 +76,16 @@ beforeEach(() => {
7676
testDir = mkdtempSync(join(tmpdir(), "ocx-api-usage-"));
7777
process.env.OPENCODEX_HOME = testDir;
7878
resetUsageReadCacheForTests();
79+
// The overlay registry is MODULE-level state that outlives a test file, and
80+
// this file asserts on `userCostOverlayVersion()` moving. A preserved
81+
// disk-only provider left behind by an earlier test — or by an earlier file in
82+
// the same process — makes a refresh byte-identical, so the version does not
83+
// bump and the mid-read assertion reads one version behind.
84+
//
85+
// Every other overlay suite already resets this; this file did not, which is
86+
// why it passed in CI's dedicated single-file job and failed locally in any
87+
// run that shared a process with overlay state.
88+
resetPreservedDiskOnlyProvidersForTests();
7989
saveConfig(baseConfig());
8090
});
8191

@@ -84,6 +94,8 @@ afterEach(() => {
8494
// wedged shutdown on Linux CI must not leave the 5s poll timer keeping the
8595
// isolate worker alive for later shard files (e.g. cli-restore-back).
8696
stopUserCostOverlayReconciler();
97+
// Leave no overlay state for the next file, for the same reason.
98+
resetPreservedDiskOnlyProvidersForTests();
8799
if (previousHome === undefined) delete process.env.OPENCODEX_HOME;
88100
else process.env.OPENCODEX_HOME = previousHome;
89101
isolatedCodexHome?.restore();
@@ -417,9 +429,19 @@ describe("GET /api/usage", () => {
417429
spy.mockRestore();
418430
// Once the overlay is settled, the next request recomputes and caches
419431
// under the new version.
432+
//
433+
// Capture the version the settled request will price under BEFORE issuing
434+
// it. The live counter is not a stable oracle here: the server's own
435+
// overlay reconciler refreshes the registry on its poll, so re-reading it
436+
// after the response can observe a later version than the one the summary
437+
// was computed with. The contract under test is "the cache is stamped with
438+
// the version its summary was priced under", not "the counter never moves
439+
// again" — asserting the latter made this test fail on any machine where a
440+
// poll landed inside the request.
441+
const settledVersion = userCostOverlayVersion();
420442
const settled = await fetch(new URL("/api/usage?range=30d", server.url)).then(res => res.json());
421443
expect(settled.summary.requests).toBe(raced.summary.requests);
422-
expect(getUsageSummaryCacheEntry("30d:all")?.overlayVersion).toBe(userCostOverlayVersion());
444+
expect(getUsageSummaryCacheEntry("30d:all")?.overlayVersion).toBeGreaterThanOrEqual(settledVersion);
423445
} finally {
424446
spy.mockRestore();
425447
// Clear the module-level overlay and summary cache even when an

0 commit comments

Comments
 (0)