From eb38f3c4755316f1e24adbba0c51cbdd6ea04636 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 15:49:57 +0000 Subject: [PATCH] Share fake codex test helper across runtime test suites Co-authored-by: Theo Browne --- apps/t3/src/runtimeApiServer.test.ts | 90 +----------------- .../src/providerManager.integration.test.ts | 91 +------------------ test-support/fakeCodexAppServer.ts | 88 ++++++++++++++++++ 3 files changed, 94 insertions(+), 175 deletions(-) create mode 100644 test-support/fakeCodexAppServer.ts diff --git a/apps/t3/src/runtimeApiServer.test.ts b/apps/t3/src/runtimeApiServer.test.ts index 6d2fa2cb9b..a89ab68d9d 100644 --- a/apps/t3/src/runtimeApiServer.test.ts +++ b/apps/t3/src/runtimeApiServer.test.ts @@ -17,6 +17,7 @@ import { type WsServerMessage, wsServerMessageSchema, } from "@acme/contracts"; +import { createFakeCodexAppServerBinary } from "../../../test-support/fakeCodexAppServer"; import { startRuntimeApiServer } from "./runtimeApiServer"; function withTimeout(promise: Promise, timeoutMs = 5_000): Promise { @@ -146,91 +147,6 @@ const METHOD_COVERAGE_PARAMS: Record = { }, }; -function createFakeCodexAppServerBinary() { - const tempDir = mkdtempSync(path.join(os.tmpdir(), "codex-app-server-fake-")); - const binaryPath = path.join(tempDir, "codex"); - const script = `#!/usr/bin/env node -const readline = require("node:readline"); -const rl = readline.createInterface({ input: process.stdin }); -let turnCount = 0; -const send = (message) => process.stdout.write(\`\${JSON.stringify(message)}\\n\`); - -rl.on("line", (line) => { - let parsed; - try { - parsed = JSON.parse(line); - } catch { - return; - } - - if (!parsed || typeof parsed !== "object") { - return; - } - - if (!("id" in parsed) || typeof parsed.method !== "string") { - return; - } - - if (parsed.method === "initialize") { - send({ id: parsed.id, result: {} }); - return; - } - - if (parsed.method === "thread/start") { - send({ id: parsed.id, result: { thread: { id: "thread-fake" } } }); - return; - } - - if (parsed.method === "thread/resume") { - const threadId = - parsed.params && - typeof parsed.params === "object" && - typeof parsed.params.threadId === "string" - ? parsed.params.threadId - : "thread-fake"; - send({ id: parsed.id, result: { thread: { id: threadId } } }); - return; - } - - if (parsed.method === "turn/start") { - turnCount += 1; - send({ id: parsed.id, result: { turn: { id: \`turn-\${turnCount}\` } } }); - setTimeout(() => { - send({ - id: \`approval-\${turnCount}\`, - method: "item/commandExecution/requestApproval", - params: { - threadId: "thread-fake", - turnId: \`turn-\${turnCount}\`, - itemId: \`item-\${turnCount}\`, - }, - }); - }, 25); - return; - } - - if (parsed.method === "turn/interrupt") { - send({ id: parsed.id, result: {} }); - return; - } - - send({ - id: parsed.id, - error: { - code: -32601, - message: \`Unsupported fake codex method: \${parsed.method}\`, - }, - }); -}); -`; - writeFileSync(binaryPath, script, { encoding: "utf8", mode: 0o755 }); - - return { - tempDir, - binaryPath, - }; -} - async function waitForAgentEvent( nextMessage: () => Promise, channel: string, @@ -787,7 +703,7 @@ describe("runtimeApiServer", () => { it( "supports provider lifecycle methods with a fake codex app-server", async () => { - const fakeCodex = createFakeCodexAppServerBinary(); + const fakeCodex = createFakeCodexAppServerBinary("t3-runtime-fake-codex-"); const originalPath = process.env.PATH; process.env.PATH = `${fakeCodex.tempDir}${path.delimiter}${originalPath ?? ""}`; @@ -2125,7 +2041,7 @@ describe("runtimeApiServer", () => { it( "returns a ready bootstrap payload when codex app-server is available", async () => { - const fakeCodex = createFakeCodexAppServerBinary(); + const fakeCodex = createFakeCodexAppServerBinary("t3-bootstrap-fake-codex-"); const originalPath = process.env.PATH; process.env.PATH = `${fakeCodex.tempDir}${path.delimiter}${originalPath ?? ""}`; diff --git a/packages/runtime-core/src/providerManager.integration.test.ts b/packages/runtime-core/src/providerManager.integration.test.ts index 6f4465bcbd..721a929c45 100644 --- a/packages/runtime-core/src/providerManager.integration.test.ts +++ b/packages/runtime-core/src/providerManager.integration.test.ts @@ -1,96 +1,11 @@ -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; -import os from "node:os"; +import { rmSync } from "node:fs"; import path from "node:path"; import { describe, expect, it } from "vitest"; import type { ProviderEvent } from "@acme/contracts"; +import { createFakeCodexAppServerBinary } from "../../../test-support/fakeCodexAppServer"; import { ProviderManager } from "./providerManager"; -function createFakeCodexAppServerBinary() { - const tempDir = mkdtempSync(path.join(os.tmpdir(), "runtime-core-fake-codex-")); - const binaryPath = path.join(tempDir, "codex"); - const script = `#!/usr/bin/env node -const readline = require("node:readline"); -const rl = readline.createInterface({ input: process.stdin }); -let turnCount = 0; -const send = (message) => process.stdout.write(\`\${JSON.stringify(message)}\\n\`); - -rl.on("line", (line) => { - let parsed; - try { - parsed = JSON.parse(line); - } catch { - return; - } - - if (!parsed || typeof parsed !== "object") { - return; - } - - if (!("id" in parsed) || typeof parsed.method !== "string") { - return; - } - - if (parsed.method === "initialize") { - send({ id: parsed.id, result: {} }); - return; - } - - if (parsed.method === "thread/start") { - send({ id: parsed.id, result: { thread: { id: "thread-fake" } } }); - return; - } - - if (parsed.method === "thread/resume") { - const threadId = - parsed.params && - typeof parsed.params === "object" && - typeof parsed.params.threadId === "string" - ? parsed.params.threadId - : "thread-fake"; - send({ id: parsed.id, result: { thread: { id: threadId } } }); - return; - } - - if (parsed.method === "turn/start") { - turnCount += 1; - send({ id: parsed.id, result: { turn: { id: \`turn-\${turnCount}\` } } }); - setTimeout(() => { - send({ - id: \`approval-\${turnCount}\`, - method: "item/commandExecution/requestApproval", - params: { - threadId: "thread-fake", - turnId: \`turn-\${turnCount}\`, - itemId: \`item-\${turnCount}\`, - }, - }); - }, 25); - return; - } - - if (parsed.method === "turn/interrupt") { - send({ id: parsed.id, result: {} }); - return; - } - - send({ - id: parsed.id, - error: { - code: -32601, - message: \`Unsupported fake codex method: \${parsed.method}\`, - }, - }); -}); -`; - writeFileSync(binaryPath, script, { encoding: "utf8", mode: 0o755 }); - - return { - tempDir, - binaryPath, - }; -} - async function waitForProviderEvent( events: ProviderEvent[], matcher: (event: ProviderEvent) => boolean, @@ -119,7 +34,7 @@ describe("ProviderManager integration with fake codex app-server", () => { it( "supports start/send/respond/interrupt/stop lifecycle", async () => { - const fakeCodex = createFakeCodexAppServerBinary(); + const fakeCodex = createFakeCodexAppServerBinary("runtime-core-fake-codex-"); const originalPath = process.env.PATH; process.env.PATH = `${fakeCodex.tempDir}${path.delimiter}${originalPath ?? ""}`; diff --git a/test-support/fakeCodexAppServer.ts b/test-support/fakeCodexAppServer.ts new file mode 100644 index 0000000000..dce7282539 --- /dev/null +++ b/test-support/fakeCodexAppServer.ts @@ -0,0 +1,88 @@ +import { mkdtempSync, writeFileSync } from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +export function createFakeCodexAppServerBinary(prefix: string) { + const tempDir = mkdtempSync(path.join(os.tmpdir(), prefix)); + const binaryPath = path.join(tempDir, "codex"); + const script = `#!/usr/bin/env node +const readline = require("node:readline"); +const rl = readline.createInterface({ input: process.stdin }); +let turnCount = 0; +const send = (message) => process.stdout.write(\`\${JSON.stringify(message)}\\n\`); + +rl.on("line", (line) => { + let parsed; + try { + parsed = JSON.parse(line); + } catch { + return; + } + + if (!parsed || typeof parsed !== "object") { + return; + } + + if (!("id" in parsed) || typeof parsed.method !== "string") { + return; + } + + if (parsed.method === "initialize") { + send({ id: parsed.id, result: {} }); + return; + } + + if (parsed.method === "thread/start") { + send({ id: parsed.id, result: { thread: { id: "thread-fake" } } }); + return; + } + + if (parsed.method === "thread/resume") { + const threadId = + parsed.params && + typeof parsed.params === "object" && + typeof parsed.params.threadId === "string" + ? parsed.params.threadId + : "thread-fake"; + send({ id: parsed.id, result: { thread: { id: threadId } } }); + return; + } + + if (parsed.method === "turn/start") { + turnCount += 1; + send({ id: parsed.id, result: { turn: { id: \`turn-\${turnCount}\` } } }); + setTimeout(() => { + send({ + id: \`approval-\${turnCount}\`, + method: "item/commandExecution/requestApproval", + params: { + threadId: "thread-fake", + turnId: \`turn-\${turnCount}\`, + itemId: \`item-\${turnCount}\`, + }, + }); + }, 25); + return; + } + + if (parsed.method === "turn/interrupt") { + send({ id: parsed.id, result: {} }); + return; + } + + send({ + id: parsed.id, + error: { + code: -32601, + message: \`Unsupported fake codex method: \${parsed.method}\`, + }, + }); +}); +`; + writeFileSync(binaryPath, script, { encoding: "utf8", mode: 0o755 }); + + return { + tempDir, + binaryPath, + }; +}