Use shared fake-codex helpers for TS tests and smoke runtime

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 16:04:17 +00:00
1 parent 9e3e806593
commit d01bd5e60a
2 files changed
+90 -87

No files matched your search

+2 -87
View File
@@ -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.");
+88
View File
@@ -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,
};
}