|
| 1 | +import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"; |
| 2 | + |
| 3 | +// Reused by both the TUI and exec boot paths (src/tui/runner.ts, |
| 4 | +// src/exec/runner.ts) to refresh the pinned Codex instructions before first |
| 5 | +// Codex inference. This exercises the shared refresh/fallback logic directly, |
| 6 | +// with disk I/O faked so tests never touch the real ~/.corbits cache. |
| 7 | + |
| 8 | +let fakeDisk = new Map<string, string>(); |
| 9 | + |
| 10 | +mock.module("node:fs", () => ({ |
| 11 | + readFileSync: (path: string) => { |
| 12 | + const contents = fakeDisk.get(path); |
| 13 | + if (contents === undefined) { |
| 14 | + const err = new Error("ENOENT") as NodeJS.ErrnoException; |
| 15 | + err.code = "ENOENT"; |
| 16 | + throw err; |
| 17 | + } |
| 18 | + return contents; |
| 19 | + }, |
| 20 | + writeFileSync: (path: string, contents: string) => { |
| 21 | + fakeDisk.set(path, contents); |
| 22 | + }, |
| 23 | + mkdirSync: () => undefined, |
| 24 | +})); |
| 25 | + |
| 26 | +const { refreshCodexInstructions, codexInstructions, codexInstructionsHash } = await import( |
| 27 | + "./instructions.js" |
| 28 | +); |
| 29 | +const { GPT_5_CODEX_PROMPT } = await import("./prompts/gpt-5-codex.js"); |
| 30 | + |
| 31 | +const VALID_PROMPT = `You are Codex${"x".repeat(1200)}`; |
| 32 | + |
| 33 | +function mockFetchSequence(tag: string, promptResponse: () => Response): typeof fetch { |
| 34 | + return (async (input: RequestInfo | URL) => { |
| 35 | + const url = String(input); |
| 36 | + if (url.includes("releases/latest")) { |
| 37 | + return new Response(JSON.stringify({ tag_name: tag }), { status: 200 }); |
| 38 | + } |
| 39 | + return promptResponse(); |
| 40 | + }) as typeof fetch; |
| 41 | +} |
| 42 | + |
| 43 | +describe("refreshCodexInstructions", () => { |
| 44 | + const originalFetch = global.fetch; |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + fakeDisk = new Map(); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + global.fetch = originalFetch; |
| 52 | + }); |
| 53 | + |
| 54 | + test("bundled copy is used before any refresh", () => { |
| 55 | + expect(codexInstructions()).toBe(GPT_5_CODEX_PROMPT); |
| 56 | + }); |
| 57 | + |
| 58 | + test("updates in-memory instructions on a successful fetch", async () => { |
| 59 | + global.fetch = mockFetchSequence("rust-v1.2.3", () => new Response(VALID_PROMPT, { status: 200 })); |
| 60 | + |
| 61 | + await refreshCodexInstructions(); |
| 62 | + expect(codexInstructions()).toBe(VALID_PROMPT); |
| 63 | + }); |
| 64 | + |
| 65 | + test("falls back without throwing the run when the release lookup network call fails", async () => { |
| 66 | + const before = codexInstructions(); |
| 67 | + global.fetch = (() => Promise.reject(new Error("network down"))) as unknown as typeof fetch; |
| 68 | + |
| 69 | + // The function itself rejects; callers (TUI/exec boot) catch this and |
| 70 | + // keep running on cache/bundled instructions — see src/exec/runner.ts and |
| 71 | + // src/tui/runner.ts refresh call sites. |
| 72 | + await expect(refreshCodexInstructions()).rejects.toThrow("network down"); |
| 73 | + expect(codexInstructions()).toBe(before); |
| 74 | + }); |
| 75 | + |
| 76 | + test("falls back without throwing when the prompt fetch returns a non-200", async () => { |
| 77 | + const before = codexInstructions(); |
| 78 | + global.fetch = mockFetchSequence("rust-v1.2.3", () => new Response("not found", { status: 404 })); |
| 79 | + |
| 80 | + await expect(refreshCodexInstructions()).rejects.toThrow(/HTTP 404/); |
| 81 | + expect(codexInstructions()).toBe(before); |
| 82 | + }); |
| 83 | + |
| 84 | + test("rejects a 200 response whose body is not a valid Codex prompt (CDN error page)", async () => { |
| 85 | + const before = codexInstructions(); |
| 86 | + global.fetch = mockFetchSequence("rust-v1.2.3", () => new Response("<html>oops</html>", { status: 200 })); |
| 87 | + |
| 88 | + await expect(refreshCodexInstructions()).rejects.toThrow(/unexpected body/); |
| 89 | + expect(codexInstructions()).toBe(before); |
| 90 | + }); |
| 91 | + |
| 92 | + test("rejects within the timeout when a fetch never resolves (hung connection)", async () => { |
| 93 | + const before = codexInstructions(); |
| 94 | + global.fetch = ((_input: RequestInfo | URL, init?: RequestInit) => { |
| 95 | + return new Promise((_resolve, reject) => { |
| 96 | + const signal = init?.signal; |
| 97 | + if (signal) { |
| 98 | + signal.addEventListener("abort", () => reject(signal.reason as Error)); |
| 99 | + } |
| 100 | + }); |
| 101 | + }) as unknown as typeof fetch; |
| 102 | + |
| 103 | + const started = Date.now(); |
| 104 | + await expect(refreshCodexInstructions()).rejects.toBeTruthy(); |
| 105 | + expect(Date.now() - started).toBeLessThan(15_000); |
| 106 | + expect(codexInstructions()).toBe(before); |
| 107 | + }, 20_000); |
| 108 | + |
| 109 | + test("codexInstructionsHash reflects the currently resolved instructions text", async () => { |
| 110 | + const hashBefore = codexInstructionsHash(); |
| 111 | + expect(hashBefore).toMatch(/^[0-9a-f]{12}$/); |
| 112 | + |
| 113 | + const otherPrompt = `You are Codex${"y".repeat(1200)}`; |
| 114 | + global.fetch = mockFetchSequence("rust-v1.2.4", () => new Response(otherPrompt, { status: 200 })); |
| 115 | + await refreshCodexInstructions(); |
| 116 | + |
| 117 | + expect(codexInstructionsHash()).not.toBe(hashBefore); |
| 118 | + }); |
| 119 | +}); |
0 commit comments