|
| 1 | +import {afterEach, describe, expect, it, vi} from "vitest"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as os from "node:os"; |
| 4 | +import * as path from "node:path"; |
| 5 | +import {ResponseError, type MessageConnection} from "vscode-jsonrpc/node"; |
| 6 | +import {explainCodexError} from "../CodexErrorExplanations"; |
| 7 | +import {CodexAppServerClient} from "../CodexAppServerClient"; |
| 8 | +import {createMockConnections} from "./CodexACPAgent/test-utils"; |
| 9 | +import {createCodexMockTestFixture} from "./acp-test-utils"; |
| 10 | + |
| 11 | +// The texts Codex 0.159.1 answers with when its working directory is gone (IJAI-2923). |
| 12 | +const configReadFailure = () => new ResponseError( |
| 13 | + -32603, "failed to resolve feature override precedence: No such file or directory (os error 2)", {extra: 1}); |
| 14 | +const threadStartFailure = () => new ResponseError( |
| 15 | + -32600, "failed to load configuration: No such file or directory (os error 2)"); |
| 16 | + |
| 17 | +const context = {codexHome: "/home/codex"}; |
| 18 | +const originalDirectory = process.cwd(); |
| 19 | +const directories: string[] = []; |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + process.chdir(originalDirectory); |
| 23 | + directories.splice(0).forEach(directory => fs.rmSync(directory, {recursive: true, force: true})); |
| 24 | +}); |
| 25 | + |
| 26 | +/** Loads the module as in an agent started in a new directory, then runs `change` on that directory. */ |
| 27 | +async function explainerStartedIn(change: (directory: string) => void = () => {}) { |
| 28 | + const directory = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "codex-cwd-"))); |
| 29 | + directories.push(directory); |
| 30 | + process.chdir(directory); |
| 31 | + vi.resetModules(); |
| 32 | + const {explainCodexError} = await import("../CodexErrorExplanations"); |
| 33 | + change(directory); |
| 34 | + return (err: Error) => { |
| 35 | + const result = explainCodexError(err, context) as ResponseError<unknown>; |
| 36 | + return { |
| 37 | + code: result.code, |
| 38 | + message: result.message.replace(directory, "<working directory>"), |
| 39 | + data: result.data ?? null, |
| 40 | + }; |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +describe("CodexErrorExplanations", () => { |
| 45 | + it.skipIf(process.platform === "win32")("explains config load failures", async () => { |
| 46 | + // Each explanation runs while the process is still in the directory of its agent. |
| 47 | + const deleted = await explainerStartedIn(fs.rmdirSync); |
| 48 | + const withDeletedDirectory = { |
| 49 | + configReadWithDeletedDirectory: deleted(configReadFailure()), |
| 50 | + threadStartWithDeletedDirectory: deleted(threadStartFailure()), |
| 51 | + otherConfigReadFailureWithDeletedDirectory: deleted(new ResponseError( |
| 52 | + -32603, "failed to resolve feature override precedence: invalid TOML in config.toml")), |
| 53 | + }; |
| 54 | + const recreated = await explainerStartedIn(directory => { |
| 55 | + fs.rmdirSync(directory); |
| 56 | + fs.mkdirSync(directory); |
| 57 | + }); |
| 58 | + const withRecreatedDirectory = {threadStartWithRecreatedDirectory: recreated(threadStartFailure())}; |
| 59 | + const present = await explainerStartedIn(); |
| 60 | + const results = { |
| 61 | + ...withDeletedDirectory, |
| 62 | + ...withRecreatedDirectory, |
| 63 | + configReadWithPresentDirectory: present(configReadFailure()), |
| 64 | + threadStartWithPresentDirectory: present(threadStartFailure()), |
| 65 | + reloadFailure: present(new Error("failed to reload config: filesystem path `/tmp` must be absolute")), |
| 66 | + reloadFailureThatNeedsLogout: present(new ResponseError(-32603, "failed to reload config: " |
| 67 | + + "Your refresh token was already used. Please log out and sign in again.")), |
| 68 | + }; |
| 69 | + await expect(`${JSON.stringify(results, null, 2)}\n`) |
| 70 | + .toMatchFileSnapshot("data/codex-error-explanations/config-load-failures.json"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("keeps other errors unchanged", () => { |
| 74 | + const unrelated = new ResponseError(-32600, "thread not found: thread-1"); |
| 75 | + expect(explainCodexError(unrelated, context)).toBe(unrelated); |
| 76 | + }); |
| 77 | + |
| 78 | + it("explains the errors of app-server requests", async () => { |
| 79 | + const mocks = createMockConnections(); |
| 80 | + mocks.mockCodexConnection.sendRequest.mockRejectedValue(configReadFailure()); |
| 81 | + const client = new CodexAppServerClient(mocks.mockCodexConnection as unknown as MessageConnection); |
| 82 | + |
| 83 | + await expect(client.configRead({includeLayers: false})).rejects.toMatchObject({ |
| 84 | + code: -32603, |
| 85 | + message: expect.stringMatching(/^failed to load configuration: No such file or directory \(os error 2\)\n\nCheck global /), |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it.skipIf(process.platform === "win32")("does not point to the config files when the working directory was deleted", async () => { |
| 90 | + const explain = await explainerStartedIn(fs.rmdirSync); |
| 91 | + const fixture = createCodexMockTestFixture(); |
| 92 | + vi.spyOn(fixture.getCodexAcpClient(), "readAuthRequirement").mockResolvedValue({required: false, account: null}); |
| 93 | + const {code, message} = explain(threadStartFailure()); |
| 94 | + vi.spyOn(fixture.getCodexAppServerClient(), "threadStart").mockRejectedValue(new ResponseError(code, message)); |
| 95 | + |
| 96 | + const error = await fixture.getCodexAcpAgent().newSession({cwd: "", mcpServers: []}).catch(e => e); |
| 97 | + expect(error.message).toMatch(/was deleted or replaced after the agent started/); |
| 98 | + expect(error.message).not.toContain("config.toml"); |
| 99 | + }); |
| 100 | +}); |
0 commit comments