Skip to content

Commit 43d0eff

Browse files
committed
test(codex): cover real catalog sync write signals
1 parent da79601 commit 43d0eff

1 file changed

Lines changed: 122 additions & 1 deletion

File tree

‎tests/codex-refresh.test.ts‎

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { describe, expect, test } from "bun:test";
1+
import { afterEach, describe, expect, test } from "bun:test";
2+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { invalidateCodexModelsCache, syncCatalogModels } from "../src/codex/catalog";
26
import { refreshCodexModelCatalog } from "../src/codex/refresh";
37
import type { OcxConfig } from "../src/types";
48

@@ -8,6 +12,51 @@ const config = {
812
providers: {},
913
} as OcxConfig;
1014

15+
const tempHomes: string[] = [];
16+
17+
function installTempHomes(): { codexHome: string; opencodexHome: string; restore(): void } {
18+
const previousCodexHome = process.env.CODEX_HOME;
19+
const previousOpenCodexHome = process.env.OPENCODEX_HOME;
20+
const codexHome = mkdtempSync(join(tmpdir(), "ocx-refresh-codex-"));
21+
const opencodexHome = mkdtempSync(join(tmpdir(), "ocx-refresh-ocx-"));
22+
tempHomes.push(codexHome, opencodexHome);
23+
process.env.CODEX_HOME = codexHome;
24+
process.env.OPENCODEX_HOME = opencodexHome;
25+
26+
return {
27+
codexHome,
28+
opencodexHome,
29+
restore() {
30+
if (previousCodexHome === undefined) delete process.env.CODEX_HOME;
31+
else process.env.CODEX_HOME = previousCodexHome;
32+
if (previousOpenCodexHome === undefined) delete process.env.OPENCODEX_HOME;
33+
else process.env.OPENCODEX_HOME = previousOpenCodexHome;
34+
rmSync(codexHome, { recursive: true, force: true });
35+
rmSync(opencodexHome, { recursive: true, force: true });
36+
},
37+
};
38+
}
39+
40+
function nativeCatalogFixture(slug = "gpt-5.5"): string {
41+
return JSON.stringify({
42+
models: [{
43+
slug,
44+
display_name: slug,
45+
description: "native",
46+
priority: 9,
47+
visibility: "list",
48+
base_instructions: "You are Codex, a coding agent based on GPT-5.",
49+
supported_reasoning_levels: [{ effort: "medium", description: "m" }],
50+
}],
51+
}, null, 2) + "\n";
52+
}
53+
54+
afterEach(() => {
55+
for (const path of tempHomes.splice(0)) {
56+
rmSync(path, { recursive: true, force: true });
57+
}
58+
});
59+
1160
describe("Codex catalog refresh", () => {
1261
test("writes an expired Codex models cache whenever the materialized catalog exists", async () => {
1362
let invalidated = 0;
@@ -93,4 +142,76 @@ describe("Codex catalog refresh", () => {
93142
expect(result.catalogWritten).toBe(false);
94143
expect(result.cacheSynced).toBe(false);
95144
});
145+
146+
test("reports catalogWritten true after syncCatalogModels rewrites a real catalog file", async () => {
147+
const home = installTempHomes();
148+
try {
149+
const catalogPath = join(home.codexHome, "nested", "catalog.json");
150+
mkdirSync(join(home.codexHome, "nested"), { recursive: true });
151+
writeFileSync(join(home.codexHome, "config.toml"), 'model_catalog_json = "nested/catalog.json"\n', "utf8");
152+
writeFileSync(catalogPath, nativeCatalogFixture("gpt-5.6-sol"), "utf8");
153+
const before = readFileSync(catalogPath, "utf8");
154+
155+
const result = await syncCatalogModels(config);
156+
const after = readFileSync(catalogPath, "utf8");
157+
const rewritten = JSON.parse(after);
158+
159+
expect(result.path).toBe(join(realpathSync.native(home.codexHome), "nested", "catalog.json"));
160+
expect(result.catalogWritten).toBe(true);
161+
expect(after).not.toBe(before);
162+
expect(rewritten.models[0].slug).toBe("gpt-5.6-sol");
163+
expect(rewritten.models[0].display_name).toBe("GPT-5.6-Sol");
164+
expect(rewritten.models[0].context_window).toBeGreaterThan(0);
165+
} finally {
166+
home.restore();
167+
}
168+
});
169+
170+
test("invalidateCodexModelsCache reports real cache write success and failure cases", () => {
171+
const success = installTempHomes();
172+
try {
173+
writeFileSync(join(success.codexHome, "config.toml"), 'model_catalog_json = "opencodex-catalog.json"\n', "utf8");
174+
writeFileSync(join(success.codexHome, "opencodex-catalog.json"), nativeCatalogFixture("gpt-5.6-sol"), "utf8");
175+
176+
expect(invalidateCodexModelsCache()).toBe(true);
177+
const cache = JSON.parse(readFileSync(join(success.codexHome, "models_cache.json"), "utf8"));
178+
expect(cache.fetched_at).toBe("2000-01-01T00:00:00Z");
179+
expect(cache.client_version).toBe("0.0.0");
180+
expect(cache.models[0].slug).toBe("gpt-5.6-sol");
181+
} finally {
182+
success.restore();
183+
}
184+
185+
const missingCatalog = installTempHomes();
186+
try {
187+
writeFileSync(join(missingCatalog.codexHome, "config.toml"), 'model_catalog_json = "missing-catalog.json"\n', "utf8");
188+
189+
expect(invalidateCodexModelsCache()).toBe(false);
190+
expect(existsSync(join(missingCatalog.codexHome, "models_cache.json"))).toBe(false);
191+
} finally {
192+
missingCatalog.restore();
193+
}
194+
195+
const malformedCatalog = installTempHomes();
196+
try {
197+
writeFileSync(join(malformedCatalog.codexHome, "config.toml"), 'model_catalog_json = "opencodex-catalog.json"\n', "utf8");
198+
writeFileSync(join(malformedCatalog.codexHome, "opencodex-catalog.json"), "{not-json", "utf8");
199+
200+
expect(invalidateCodexModelsCache()).toBe(false);
201+
expect(existsSync(join(malformedCatalog.codexHome, "models_cache.json"))).toBe(false);
202+
} finally {
203+
malformedCatalog.restore();
204+
}
205+
206+
const unwritableCache = installTempHomes();
207+
try {
208+
writeFileSync(join(unwritableCache.codexHome, "config.toml"), 'model_catalog_json = "opencodex-catalog.json"\n', "utf8");
209+
writeFileSync(join(unwritableCache.codexHome, "opencodex-catalog.json"), nativeCatalogFixture(), "utf8");
210+
mkdirSync(join(unwritableCache.codexHome, "models_cache.json"));
211+
212+
expect(invalidateCodexModelsCache()).toBe(false);
213+
} finally {
214+
unwritableCache.restore();
215+
}
216+
});
96217
});

0 commit comments

Comments
 (0)