From d01bd5e60ad56cdbcc8e78c8661d1f72bb73e7a2 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 16:04:17 +0000 Subject: [PATCH] Use shared fake-codex helpers for TS tests and smoke runtime Co-authored-by: Theo Browne --- apps/t3/scripts/smoke-test.mjs | 89 +---------------------------- test-support/fakeCodexAppServer.mjs | 88 ++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 87 deletions(-) create mode 100644 test-support/fakeCodexAppServer.mjs diff --git a/apps/t3/scripts/smoke-test.mjs b/apps/t3/scripts/smoke-test.mjs index e7c092ff53..9b16580381 100644 --- a/apps/t3/scripts/smoke-test.mjs +++ b/apps/t3/scripts/smoke-test.mjs @@ -1,9 +1,9 @@ import { spawn } from "node:child_process"; import fs from "node:fs"; import { createServer } from "node:net"; -import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; +import { createFakeCodexAppServerBinary } from "../../../test-support/fakeCodexAppServer.mjs"; const WS_CLOSE_CODES = { replacedByNewClient: 4000, @@ -221,91 +221,6 @@ function getFreePort() { }); } -function createFakeCodexAppServerBinary() { - const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "t3-smoke-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}\`, - }, - }); -}); -`; - fs.writeFileSync(binaryPath, script, { encoding: "utf8", mode: 0o755 }); - - return { - tempDir, - binaryPath, - }; -} - function waitForProcessExit(processRef) { return new Promise((resolve) => { processRef.once("exit", (code) => resolve(code)); @@ -570,7 +485,7 @@ async function main() { const [backendPort, webPort] = await Promise.all([getFreePort(), getFreePort()]); const scriptDir = path.dirname(fileURLToPath(import.meta.url)); const appRoot = path.resolve(scriptDir, ".."); - const fakeCodex = createFakeCodexAppServerBinary(); + const fakeCodex = createFakeCodexAppServerBinary("t3-smoke-fake-codex-"); const distCli = path.join(appRoot, "dist", "cli.js"); if (!fs.existsSync(distCli)) { throw new Error("Missing dist/cli.js. Run `bun run --cwd apps/t3 build` first."); diff --git a/test-support/fakeCodexAppServer.mjs b/test-support/fakeCodexAppServer.mjs new file mode 100644 index 0000000000..cce0cc7c34 --- /dev/null +++ b/test-support/fakeCodexAppServer.mjs @@ -0,0 +1,88 @@ +import { mkdtempSync, writeFileSync } from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +export function createFakeCodexAppServerBinary(prefix) { + 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, + }; +}