Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
test(mcp): cover run_prompt's Claude Code path and the tool groups
Jack Rudenko0c8300386b2a
diff --git a/packages/cli/src/mcp-blackbox/run-prompt.blackbox.test.ts b/packages/cli/src/mcp-blackbox/run-prompt.blackbox.test.tsnew file mode 100644index 00000000..02c616ed--- /dev/null+++ b/packages/cli/src/mcp-blackbox/run-prompt.blackbox.test.ts@@ -0,0 +1,444 @@+/**+ * BLACK-BOX: real claudish --mcp, public stdio protocol and a test-owned executable/endpoint.+ * Spec: dev-feature-mcp-run-prompt-claude-20261016-a7c3/requirements.md and+ * qa/sandbox/contract/architecture-contracts.md, run_prompt testing-strategy table.+ * No implementation source or module mocks. The fake's frames derive from a1/a3 measurements.+ */+import { afterEach, beforeEach, describe, expect, test } from "bun:test";+import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";+import { join } from "node:path";+import { useBlackboxEnv } from "../test-helpers/blackbox-env.js";+import {+ McpServer,+ type ToolResult,+ paneOrphans,+ serverEnv,+} from "../test-helpers/contract-mcp.js";+import { type TempLayout, makeTempLayout } from "../test-helpers/contract-records.js";++useBlackboxEnv();++const FAKE = join(import.meta.dir, "..", "test-helpers", "fake-print-claudish.ts");+const PROMPT = "- What is 4817 + 2906?";+const T_TEST = 35_000;+const FORBIDDEN_ENV = [+ "ANTHROPIC_API_KEY",+ "ANTHROPIC_AUTH_TOKEN",+ "ANTHROPIC_BASE_URL",+ "CLAUDECODE",+ "CLAUDE_CODE_CHILD_SESSION",+ "CLAUDE_CODE_SESSION_ID",+ "CLAUDISH_PANE_CHILD",+ "CLAUDISH_PANE_ENV",+ "CLAUDISH_PANE_CWD",+];++type Json = Record<string, unknown>;+interface PrintRecord {+ pid: number;+ invocationCount: number;+ argv: string[];+ cwd: string;+ stdin: string;+ env: Record<string, boolean>;+ envValues: Record<string, string>;+ cwdEntriesAtStart: string[];+ systemPromptFileContent: string | null;+ recordedAt: number;+}+interface ProviderRequest {+ body: Json;+ childRecordPresent: boolean;+ childWasAlive: boolean;+}++let layout: TempLayout;+let server: McpServer | undefined;+let recordPath: string;+let expectedEnv: Record<string, string>;+let endpoint: { stop(closeActiveConnections?: boolean): unknown } | undefined;+let providerRequests: ProviderRequest[];++function record(): PrintRecord {+ return JSON.parse(readFileSync(recordPath, "utf8")) as PrintRecord;+}++function alive(pid: number): boolean {+ try {+ process.kill(pid, 0);+ return true;+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code === "ESRCH") return false;+ throw error;+ }+}++async function until(check: () => boolean, timeoutMs: number, what: string): Promise<void> {+ const deadline = performance.now() + timeoutMs;+ while (!check()) {+ if (performance.now() >= deadline) throw new Error(`Timed out after ${timeoutMs} ms: ${what}`);+ await Bun.sleep(25);+ }+}++function argvFor(model: string, systemArgs = ["--system-prompt", ""]): string[] {+ return [+ "-p",+ "--model",+ model,+ "--output-format",+ "json",+ "--tools",+ "",+ "--strict-mcp-config",+ "--setting-sources",+ "",+ "--disable-slash-commands",+ "--no-session-persistence",+ ...systemArgs,+ ];+}++async function boot(extra: Record<string, string | undefined> = {}): Promise<McpServer> {+ expectedEnv = serverEnv(layout, {+ CLAUDISH_BIN: FAKE,+ FAKE_PRINT_RECORD: recordPath,+ CLAUDISH_MCP_TOOLS: "all",+ ...extra,+ });+ server = await McpServer.start({ env: expectedEnv, cwd: layout.cwd });+ return server;+}++function expectPromptError(result: ToolResult, model: string): void {+ expect(result.isError).toBe(true);+ expect(result.text.startsWith("Error: ")).toBe(true);+ expect(result.text).toContain("report_error");+ // The hint itself must name this model, not merely the preceding error message.+ expect(result.text.slice(result.text.indexOf("report_error"))).toContain(model);+}++function addLoopbackEndpoint(): void {+ const local = Bun.serve({+ hostname: "127.0.0.1",+ port: 0,+ async fetch(request) {+ const childRecordPresent = existsSync(recordPath);+ providerRequests.push({+ body: (await request.json()) as Json,+ childRecordPresent,+ childWasAlive: childRecordPresent && alive(record().pid),+ });+ // OpenAI SSE as specified by the custom-endpoint contract: one answer chunk + DONE.+ const chunk = {+ id: "rp-mock-answer",+ object: "chat.completion.chunk",+ model: "m",+ choices: [+ { index: 0, delta: { role: "assistant", content: "7723" }, finish_reason: "stop" },+ ],+ usage: { prompt_tokens: 2, completion_tokens: 4, total_tokens: 6 },+ };+ return new Response(`data: ${JSON.stringify(chunk)}\n\ndata: [DONE]\n\n`, {+ headers: { "Content-Type": "text/event-stream" },+ });+ },+ });+ endpoint = local;+ const configDir = join(layout.home, ".claudish");+ mkdirSync(configDir, { recursive: true });+ writeFileSync(+ join(configDir, "config.json"),+ JSON.stringify({+ customEndpoints: {+ "rp-mock": {+ kind: "simple",+ format: "openai",+ authScheme: "none",+ url: `http://127.0.0.1:${local.port}`,+ },+ },+ })+ );+}++function expectUnlimited(body: Json): void {+ for (const key of ["max_tokens", "max_completion_tokens", "max_output_tokens"]) {+ expect(Object.hasOwn(body, key)).toBe(false);+ }+}++beforeEach(() => {+ layout = makeTempLayout("bbrunprompt");+ chmodSync(FAKE, 0o755); // CLAUDISH_BIN is executed directly, not through a shell.+ recordPath = join(layout.root, "print-record.json");+ server = undefined;+ endpoint = undefined;+ providerRequests = [];+});++afterEach(async () => {+ // Cleanup is deliberately after assertions, so it cannot make the cancel test pass.+ try {+ if (existsSync(recordPath)) {+ const { pid } = record();+ if (alive(pid)) process.kill(pid, "SIGKILL");+ }+ if (server) {+ expect(+ server.notifications.filter((n) => n.method === "notifications/claude/channel")+ ).toEqual([]);+ }+ } finally {+ try {+ await server?.close();+ endpoint?.stop(true);+ const report = await paneOrphans(layout);+ expect(report).toEqual({ processes: [], files: [] });+ } finally {+ layout.cleanup();+ }+ }+});++describe("run_prompt Claude print contract", () => {+ test(+ "REQ-1 REQ-2 REQ-3 REQ-6: sends exact argv/stdin once and returns cached usage plus subscription auth",+ async () => {+ const mcp = await boot();+ const result = await mcp.callTool("run_prompt", { model: "claude-fake-ok", prompt: PROMPT });++ expect(result.isError).toBe(false);+ expect(record().invocationCount).toBe(1);+ expect(record().argv).toEqual(argvFor("claude-fake-ok"));+ expect(record().stdin).toBe(PROMPT);+ expect(record().systemPromptFileContent).toBeNull();+ expect(result.text).toBe(+ "7723\n\n---\nTokens: 582 input, 4 output\nAuth: Claude Code login (subscription)"+ );+ },+ T_TEST+ );++ test(+ "REQ-2 REQ-3: delivers a dash-leading system prompt through an exact --system-prompt-file argument",+ async () => {+ const mcp = await boot();+ const result = await mcp.callTool("run_prompt", {+ model: "claude-fake-ok",+ prompt: PROMPT,+ system_prompt: "- one word",+ });++ expect(result.isError).toBe(false);+ const seen = record();+ const file = seen.argv.at(-1)!;+ expect(file.length).toBeGreaterThan(0);+ expect(seen.argv).toEqual(argvFor("claude-fake-ok", ["--system-prompt-file", file]));+ expect(seen.systemPromptFileContent).toBe("- one word");+ expect(seen.stdin).toBe(PROMPT);+ },+ T_TEST+ );++ test(+ "REQ-5: strips the contracted inherited auth/session/pane variables and preserves other server environment values",+ async () => {+ // Exact inherited values from the contract's environment table row. In particular,+ // do not invent a pane snapshot format to bypass the public CLI startup path.+ const mcp = await boot({+ ANTHROPIC_API_KEY: "sk-test-not-real",+ ANTHROPIC_AUTH_TOKEN: "x",+ ANTHROPIC_BASE_URL: "http://127.0.0.1:1",+ CLAUDECODE: "1",+ CLAUDE_CODE_SESSION_ID: "s",+ CLAUDISH_PANE_CHILD: "1",+ FAKE_PRINT_KEEP: "unchanged sentinel",+ });+ const result = await mcp.callTool("run_prompt", { model: "claude-fake-ok", prompt: PROMPT });++ expect(result.isError).toBe(false);+ const seen = record();+ for (const key of FORBIDDEN_ENV) {+ expect(seen.env[key]).toBe(false);+ expect(Object.hasOwn(seen.envValues, key)).toBe(false);+ }+ expect(seen.env.PATH).toBe(true);+ expect(seen.env.HOME).toBe(true);+ for (const [key, value] of Object.entries(expectedEnv)) {+ if (!FORBIDDEN_ENV.includes(key)) expect(seen.envValues[key]).toBe(value);+ }+ },+ T_TEST+ );++ test(+ "REQ-4: starts in a distinct empty cwd and removes it after the call",+ async () => {+ const mcp = await boot();+ const result = await mcp.callTool("run_prompt", { model: "claude-fake-ok", prompt: PROMPT });++ expect(result.isError).toBe(false);+ const seen = record();+ expect(seen.cwd).not.toBe(layout.cwd);+ expect(seen.cwdEntriesAtStart).toEqual([]);+ expect(existsSync(seen.cwd)).toBe(false);+ },+ T_TEST+ );++ test(+ "REQ-8: returns the measured Claude error and HTTP 404 with a model-specific report_error hint",+ async () => {+ const mcp = await boot();+ const result = await mcp.callTool("run_prompt", { model: "claude-fake-404", prompt: PROMPT });++ expectPromptError(result, "claude-fake-404");+ expect(result.text).toContain("Error: Claude Code: There's an issue with the selected model");+ expect(result.text).toContain("(HTTP 404)");+ },+ T_TEST+ );++ test(+ "REQ-9: reports exit 3 and stderr when stdout is not a parseable result",+ async () => {+ const mcp = await boot();+ const result = await mcp.callTool("run_prompt", {+ model: "claude-fake-garbage",+ prompt: PROMPT,+ });++ expectPromptError(result, "claude-fake-garbage");+ expect(result.text).toContain("claudish -p exited 3");+ expect(result.text).toContain("boom");+ },+ T_TEST+ );++ test(+ "REQ-10: rejects an empty successful result and keeps the model-specific report_error hint",+ async () => {+ const mcp = await boot();+ const result = await mcp.callTool("run_prompt", {+ model: "claude-fake-empty",+ prompt: PROMPT,+ });++ expectPromptError(result, "claude-fake-empty");+ expect(result.text).toContain("Model returned empty response");+ },+ T_TEST+ );++ test(+ "REQ-7: accepts a single result object instead of a frame array",+ async () => {+ const mcp = await boot();+ const result = await mcp.callTool("run_prompt", {+ model: "claude-fake-object",+ prompt: PROMPT,+ });++ expect(result.isError).toBe(false);+ expect(result.text.startsWith("7723")).toBe(true);+ expect(record().invocationCount).toBe(1);+ },+ T_TEST+ );++ test(+ "REQ-11: reports cannot start claudish when CLAUDISH_BIN does not exist",+ async () => {+ const mcp = await boot({ CLAUDISH_BIN: "/nonexistent/claudish" });+ const result = await mcp.callTool("run_prompt", { model: "claude-fake-ok", prompt: PROMPT });++ expectPromptError(result, "claude-fake-ok");+ expect(result.text).toContain("cannot start claudish");+ expect(existsSync(recordPath)).toBe(false);+ },+ T_TEST+ );++ test(+ "REQ-14: notifications/cancelled removes the in-flight fake pid within 6 seconds",+ async () => {+ const mcp = await boot();+ // The request id belongs to this test, not to McpServer's private request counter.+ const requestId = 910001;+ const stdin = mcp.proc.stdin as { write(text: string): unknown; flush?(): unknown };+ stdin.write(+ `${JSON.stringify({+ jsonrpc: "2.0",+ id: requestId,+ method: "tools/call",+ params: { name: "run_prompt", arguments: { model: "claude-fake-hang", prompt: PROMPT } },+ })}\n`+ );+ stdin.flush?.();+ await until(() => existsSync(recordPath), 10_000, "hanging fake to record its pid");+ const { pid } = record();+ expect(alive(pid)).toBe(true);++ const cancelledAt = performance.now();+ mcp.notify("notifications/cancelled", { requestId, reason: "black-box cancellation test" });+ await until(() => !alive(pid), 6_000, `cancelled fake pid ${pid} to disappear`);++ expect(performance.now() - cancelledAt).toBeLessThanOrEqual(6_000);+ expect(alive(mcp.pid)).toBe(true);+ expect((await mcp.listTools()).map((tool) => tool.name)).toContain("run_prompt");+ },+ T_TEST+ );+});++describe("quick prompts through the non-Claude proxy", () => {+ test(+ "REQ-12: makes one unlimited loopback request and never starts the print child",+ async () => {+ addLoopbackEndpoint();+ const mcp = await boot();+ const result = await mcp.callTool("run_prompt", { model: "rp-mock@m", prompt: PROMPT });++ expect(result.isError).toBe(false);+ expect(result.text.startsWith("7723\n\n---\nTokens: ")).toBe(true);+ expect(result.text).not.toContain("Auth:");+ expect(existsSync(recordPath)).toBe(false);+ expect(providerRequests).toHaveLength(1);+ expect(providerRequests[0]!.childRecordPresent).toBe(false);+ expectUnlimited(providerRequests[0]!.body);+ expect(providerRequests[0]!.body.model).toBe("m");+ expect(JSON.stringify(providerRequests[0]!.body.messages)).toContain(PROMPT);+ },+ T_TEST+ );++ test(+ "REQ-1 REQ-12 REQ-13: compares Claude then non-Claude sequentially with one child, one unlimited request and two answers",+ async () => {+ addLoopbackEndpoint();+ const mcp = await boot();+ const result = await mcp.callTool("compare_models", {+ models: ["claude-fake-ok", "rp-mock@m"],+ prompt: PROMPT,+ });++ expect(result.isError).toBe(false);+ expect(record().invocationCount).toBe(1);+ expect(record().argv).toEqual(argvFor("claude-fake-ok"));+ expect(record().stdin).toBe(PROMPT);+ expect(providerRequests).toHaveLength(1);+ expect(providerRequests[0]!.childRecordPresent).toBe(true);+ expect(providerRequests[0]!.childWasAlive).toBe(false);+ expectUnlimited(providerRequests[0]!.body);+ const claudeSection = result.text.indexOf("claude-fake-ok");+ const proxySection = result.text.indexOf("rp-mock@m");+ expect(claudeSection).toBeGreaterThanOrEqual(0);+ expect(proxySection).toBeGreaterThan(claudeSection);+ expect(result.text.slice(claudeSection, proxySection)).toContain("7723");+ expect(result.text.slice(proxySection)).toContain("7723");+ expect(result.text.match(/7723/g)).toHaveLength(2);+ },+ T_TEST+ );+});diff --git a/packages/cli/src/mcp-blackbox/tool-surface.blackbox.test.ts b/packages/cli/src/mcp-blackbox/tool-surface.blackbox.test.tsindex 4d16a203..f374474c 100644--- a/packages/cli/src/mcp-blackbox/tool-surface.blackbox.test.ts+++ b/packages/cli/src/mcp-blackbox/tool-surface.blackbox.test.ts@@ -7,9 +7,11 @@import { afterEach, beforeEach, describe, expect, test } from "bun:test";import { existsSync } from "node:fs";import { join } from "node:path";+import { version as packageVersion } from "../../package.json";import { useBlackboxEnv } from "../test-helpers/blackbox-env.js";import {McpServer,+ SERVER_ENTRY,type ToolResult,paneOrphans,serverEnv,@@ -22,6 +24,43 @@ type Json = Record<string, unknown>;const keys = (v: unknown): string[] => Object.keys(v as Json).sort();const CAPABILITIES = ["list", "status", "cancel", "capture", "capture_since_seq", "capture_spans"];+// REQ-15: literal sorted sets from architecture-contracts.md, not from implementation.+const ALL_TOOLS = [+ "cancel_session",+ "capture_session",+ "compare_models",+ "create_session",+ "get_diagnostics",+ "get_output",+ "list_models",+ "list_sessions",+ "preflight",+ "report_error",+ "run_prompt",+ "search_models",+ "send_input",+ "team",+];+const SESSION_TOOLS = [+ "cancel_session",+ "capture_session",+ "create_session",+ "get_diagnostics",+ "get_output",+ "list_sessions",+ "send_input",+];+const TOOL_GROUPS: Array<{ value: string | undefined; names: string[] }> = [+ { value: undefined, names: ALL_TOOLS },+ { value: "all", names: ALL_TOOLS },+ { value: "low-level", names: ["list_models", "run_prompt", "search_models"] },+ { value: "agentic", names: ["preflight", "report_error", "team"] },+ { value: "session", names: SESSION_TOOLS },+ { value: "debug", names: ["compare_models"] },+ // Removed spelling is not a session alias: unrecognized values fall back to all.+ { value: "channel", names: ALL_TOOLS },+];+let layout: TempLayout;let server: McpServer;@@ -35,6 +74,10 @@ afterEach(async () => {const report = await paneOrphans(layout);layout.cleanup();expect(report).toEqual({ processes: [], files: [] });+ // REQ-19: none of these public calls may emit the removed push notification.+ expect(server.notifications.filter((n) => n.method === "notifications/claude/channel")).toEqual(+ []+ );});function contractError(r: ToolResult, code: string): void {@@ -46,6 +89,131 @@ function contractError(r: ToolResult, code: string): void {expect(typeof (body.error as Json).message).toBe("string");}+/** Capture the first initialize response on a fresh public stdio connection.+ * McpServer.start deliberately discards it; sending initialize twice would test+ * an unsupported MCP lifecycle rather than the contract's initialization.+ */+async function initializeOnce(env: Record<string, string>): Promise<Json> {+ const child = Bun.spawn([process.execPath, SERVER_ENTRY, "--mcp"], {+ env,+ cwd: layout.cwd,+ stdin: "pipe",+ stdout: "pipe",+ stderr: "pipe",+ });+ const stderr = new Response(child.stderr).text();+ const reader = child.stdout.getReader();+ const decoder = new TextDecoder();+ const timer = setTimeout(() => child.kill("SIGKILL"), 20_000);+ let buffer = "";+ try {+ child.stdin.write(+ `${JSON.stringify({+ jsonrpc: "2.0",+ id: 1,+ method: "initialize",+ params: {+ protocolVersion: "2025-06-18",+ capabilities: {},+ clientInfo: { name: "quick-prompt-blackbox", version: "0.0.0" },+ },+ })}\n`+ );+ child.stdin.flush();+ for (;;) {+ const chunk = await reader.read();+ if (chunk.done) throw new Error(`MCP initialize ended without a response: ${await stderr}`);+ buffer += decoder.decode(chunk.value, { stream: true });+ for (;;) {+ const newline = buffer.indexOf("\n");+ if (newline === -1) break;+ const line = buffer.slice(0, newline).trim();+ buffer = buffer.slice(newline + 1);+ if (!line.startsWith("{")) continue;+ const message = JSON.parse(line) as Json;+ if (message.id !== 1) continue;+ expect(message.error).toBeUndefined();+ child.stdin.write(+ `${JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" })}\n`+ );+ child.stdin.flush();+ return message.result as Json;+ }+ }+ } finally {+ reader.releaseLock();+ child.stdin.end();+ const ended = await Promise.race([+ child.exited.then(() => true),+ Bun.sleep(3_000).then(() => false),+ ]);+ if (!ended) child.kill("SIGKILL");+ await child.exited;+ clearTimeout(timer);+ await stderr;+ }+}++describe("quick-prompt public surface", () => {+ for (const { value, names } of TOOL_GROUPS) {+ test(`REQ-15 REQ-18: CLAUDISH_MCP_TOOLS=${value ?? "unset"} exposes the exact sorted set and polling-only initialization`, async () => {+ if (value !== undefined) {+ await server.close();+ server = await McpServer.start({+ env: serverEnv(layout, { CLAUDISH_MCP_TOOLS: value }),+ cwd: layout.cwd,+ });+ }+ const tools = await server.listTools();+ expect(tools.map((tool) => tool.name).sort()).toEqual(names);++ const result = await initializeOnce(serverEnv(layout, { CLAUDISH_MCP_TOOLS: value }));+ expect(result.serverInfo).toEqual({ name: "claudish", version: packageVersion });+ const capabilities = result.capabilities as Json;+ expect(Object.hasOwn(capabilities, "tools")).toBe(true);+ expect(capabilities.experimental).toBeUndefined();+ expect(Object.hasOwn(capabilities, "experimental")).toBe(false);+ expect(typeof result.instructions).toBe("string");+ const instructions = result.instructions as string;+ expect(instructions).toContain("list_sessions");+ expect(instructions).toContain("get_output");+ expect(instructions).not.toContain("<channel");+ expect(instructions.toLowerCase()).not.toContain("notification");+ }, 30_000);+ }++ test("REQ-16 REQ-17: quick-prompt schemas omit output limits and descriptions distinguish prompts from sessions/debug", async () => {+ const tools = await server.listTools();+ const prompt = tools.find((tool) => tool.name === "run_prompt");+ const compare = tools.find((tool) => tool.name === "compare_models");+ expect(prompt).toBeDefined();+ expect(compare).toBeDefined();+ expect(Object.keys(prompt?.inputSchema?.properties ?? {}).sort()).toEqual([+ "model",+ "prompt",+ "system_prompt",+ ]);+ expect(prompt?.inputSchema?.required).toEqual(["model", "prompt"]);+ expect(prompt?.inputSchema?.properties).not.toHaveProperty("max_tokens");+ expect(Object.keys(compare?.inputSchema?.properties ?? {}).sort()).toEqual([+ "models",+ "prompt",+ "system_prompt",+ ]);+ expect(compare?.inputSchema?.required).toEqual(["models", "prompt"]);+ expect(compare?.inputSchema?.properties).not.toHaveProperty("max_tokens");+ const promptDescription = (prompt as unknown as { description: string }).description;+ const compareDescription = (compare as unknown as { description: string }).description;+ expect(promptDescription.startsWith("One quick prompt, straight to one model's API")).toBe(+ true+ );+ expect(promptDescription).toContain("create_session");+ expect(promptDescription).toContain("team");+ expect(compareDescription.startsWith("DEBUG.")).toBe(true);+ }, 30_000);+});++// REQ-20 (TEST-21–TEST-26): preserve the existing pane-free session/team regressions.describe("tool list (FR11, §4)", () => {test("14 tools, capture_session among them, team modes are exactly the seven", async () => {const tools = await server.listTools();diff --git a/packages/cli/src/test-helpers/fake-print-claudish.ts b/packages/cli/src/test-helpers/fake-print-claudish.tsnew file mode 100755index 00000000..f08972b6--- /dev/null+++ b/packages/cli/src/test-helpers/fake-print-claudish.ts@@ -0,0 +1,112 @@+#!/usr/bin/env bun+/**+ * Public-process fake for the MCP print path; never loads claudish or credentials.+ * Frame projections copied from the real Claude Code 2.1.301 measurements:+ * ai-docs/sessions/dev-feature-mcp-run-prompt-claude-20261016-a7c3/measure/a1.stdout+ * ai-docs/sessions/dev-feature-mcp-run-prompt-claude-20261016-a7c3/measure/a3-badmodel.stdout+ * Only result text and usage counters are overridden as the architecture contract specifies.+ * Projecting frames here keeps the tests independent of gitignored session artifacts.+ */+import { existsSync, readFileSync, readdirSync, renameSync, writeFileSync } from "node:fs";++const argv = process.argv.slice(2);+const cwdEntriesAtStart = readdirSync(process.cwd());+const stdin = await Bun.stdin.text();+const model = argv[argv.indexOf("--model") + 1];+const systemFileIndex = argv.indexOf("--system-prompt-file");+const recordPath = process.env.FAKE_PRINT_RECORD;+if (!recordPath) throw new Error("FAKE_PRINT_RECORD must name a test-owned file");++const names = [+ "ANTHROPIC_API_KEY",+ "ANTHROPIC_AUTH_TOKEN",+ "ANTHROPIC_BASE_URL",+ "CLAUDECODE",+ "CLAUDE_CODE_CHILD_SESSION",+ "CLAUDE_CODE_SESSION_ID",+ "CLAUDISH_PANE_CHILD",+ "CLAUDISH_PANE_ENV",+ "CLAUDISH_PANE_CWD",+ "PATH",+ "HOME",+];+const previousCount = existsSync(recordPath)+ ? (JSON.parse(readFileSync(recordPath, "utf8")) as { invocationCount: number }).invocationCount+ : 0;+const pendingRecordPath = `${recordPath}.${process.pid}.tmp`;+writeFileSync(+ pendingRecordPath,+ JSON.stringify({+ pid: process.pid,+ invocationCount: previousCount + 1,+ argv,+ cwd: process.cwd(),+ stdin,+ env: Object.fromEntries(names.map((name) => [name, Object.hasOwn(process.env, name)])),+ envValues: { ...process.env },+ cwdEntriesAtStart,+ systemPromptFileContent:+ systemFileIndex === -1 ? null : readFileSync(argv[systemFileIndex + 1]!, "utf8"),+ recordedAt: Date.now(),+ })+);++// Publish atomically so a cancellation test never sees a half-written pid record.+renameSync(pendingRecordPath, recordPath);++// These discriminators/fields are projections of the measured init and result frames.+const init = { type: "system", subtype: "init", apiKeySource: "none" };+const success = {+ type: "result",+ subtype: "success",+ is_error: false,+ api_error_status: null,+ result: "7723",+ usage: {+ input_tokens: 2,+ cache_creation_input_tokens: 500,+ cache_read_input_tokens: 80,+ output_tokens: 4,+ },+};+const failure = {+ type: "result",+ subtype: "success",+ is_error: true,+ api_error_status: 404,+ result: "There's an issue with the selected model",+ usage: {+ input_tokens: 0,+ cache_creation_input_tokens: 0,+ cache_read_input_tokens: 0,+ output_tokens: 0,+ },+};++switch (model) {+ case "claude-fake-ok":+ console.log(JSON.stringify([init, success]));+ break;+ case "claude-fake-404":+ console.log(JSON.stringify([init, failure]));+ process.exit(1);+ break;+ case "claude-fake-garbage":+ console.log("not json");+ console.error("boom");+ process.exit(3);+ break;+ case "claude-fake-empty":+ console.log(JSON.stringify([init, { ...success, result: "" }]));+ break;+ case "claude-fake-object":+ console.log(JSON.stringify(success));+ break;+ case "claude-fake-hang":+ // No successful output or early exit: only cancellation/teardown can stop this child.+ await new Promise(() => setInterval(() => {}, 60_000));+ break;+ default:+ console.error(`Unexpected fake model: ${model}`);+ process.exit(2);+}