|
| 1 | +/** |
| 2 | + * Regression guard for B-006 / D-121. |
| 3 | + * |
| 4 | + * The Claude Agent SDK resolves its own executable via `import.meta.url`, |
| 5 | + * which is undefined inside the bundled CJS build and crashes with |
| 6 | + * `fileURLToPath(undefined)`. Every direct `sdk.query()` call site must |
| 7 | + * therefore either: |
| 8 | + * a) build its options through `buildAgentQueryOptions()` (which sets |
| 9 | + * `pathToClaudeCodeExecutable` via `findClaudePath()`), or |
| 10 | + * b) import `findClaudePath` and set `pathToClaudeCodeExecutable` |
| 11 | + * explicitly on its own options object. |
| 12 | + * |
| 13 | + * This test greps every `src/agents/**.ts` file for `sdk.query(` and fails |
| 14 | + * if none of the two helpers are imported. It is static, so it does not |
| 15 | + * require the SDK, the `claude` binary, or any network access. |
| 16 | + */ |
| 17 | + |
| 18 | +import { readFileSync, readdirSync, statSync } from "node:fs"; |
| 19 | +import { join } from "node:path"; |
| 20 | +import { test } from "node:test"; |
| 21 | +import assert from "node:assert"; |
| 22 | + |
| 23 | +const AGENTS_DIR = new URL("../src/agents/", import.meta.url).pathname; |
| 24 | + |
| 25 | +function walk(dir: string, out: string[] = []): string[] { |
| 26 | + for (const entry of readdirSync(dir)) { |
| 27 | + const full = join(dir, entry); |
| 28 | + const st = statSync(full); |
| 29 | + if (st.isDirectory()) walk(full, out); |
| 30 | + else if (entry.endsWith(".ts")) out.push(full); |
| 31 | + } |
| 32 | + return out; |
| 33 | +} |
| 34 | + |
| 35 | +test("every src/agents file calling sdk.query imports buildAgentQueryOptions or findClaudePath", () => { |
| 36 | + const files = walk(AGENTS_DIR); |
| 37 | + const offenders: string[] = []; |
| 38 | + |
| 39 | + for (const file of files) { |
| 40 | + const src = readFileSync(file, "utf-8"); |
| 41 | + if (!src.includes("sdk.query(")) continue; |
| 42 | + |
| 43 | + const hasBuilder = /import\s+[^;]*\bbuildAgentQueryOptions\b[^;]*from\s+["'][^"']*agent-options/.test(src); |
| 44 | + const hasFinder = /import\s+[^;]*\bfindClaudePath\b[^;]*from\s+["'][^"']*agent-options/.test(src); |
| 45 | + |
| 46 | + if (!hasBuilder && !hasFinder) { |
| 47 | + offenders.push(file.replace(AGENTS_DIR, "")); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + assert.deepStrictEqual( |
| 52 | + offenders, |
| 53 | + [], |
| 54 | + `The following files call sdk.query() but import neither buildAgentQueryOptions ` + |
| 55 | + `nor findClaudePath from utils/agent-options. Without pathToClaudeCodeExecutable ` + |
| 56 | + `the bundled CJS build will crash with fileURLToPath(undefined) (B-006 / D-121):\n` + |
| 57 | + ` ${offenders.join("\n ")}`, |
| 58 | + ); |
| 59 | +}); |
| 60 | + |
| 61 | +test("session-auditor.ts manual queryOpts include pathToClaudeCodeExecutable", () => { |
| 62 | + const src = readFileSync(new URL("../src/agents/session-auditor.ts", import.meta.url), "utf-8"); |
| 63 | + const queryOptsBlocks = src.split("const queryOpts = {").slice(1); |
| 64 | + assert.ok(queryOptsBlocks.length >= 2, "expected at least 2 queryOpts blocks in session-auditor.ts"); |
| 65 | + |
| 66 | + for (const block of queryOptsBlocks) { |
| 67 | + const untilClose = block.slice(0, block.indexOf("};")); |
| 68 | + assert.ok( |
| 69 | + untilClose.includes("pathToClaudeCodeExecutable"), |
| 70 | + `session-auditor.ts queryOpts block missing pathToClaudeCodeExecutable:\n${untilClose.slice(0, 300)}`, |
| 71 | + ); |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +test("memory-extractor.ts manual queryOpts include pathToClaudeCodeExecutable", () => { |
| 76 | + const src = readFileSync(new URL("../src/agents/memory-extractor.ts", import.meta.url), "utf-8"); |
| 77 | + assert.ok( |
| 78 | + src.includes("pathToClaudeCodeExecutable"), |
| 79 | + "memory-extractor.ts must set pathToClaudeCodeExecutable on its queryOpts", |
| 80 | + ); |
| 81 | +}); |
0 commit comments