Skip to content

Commit eb38f3c

Browse files
cursoragentt3dotgg
andcommitted
Share fake codex test helper across runtime test suites
Co-authored-by: Theo Browne <[email protected]>
1 parent 8046bd2 commit eb38f3c

3 files changed

Lines changed: 94 additions & 175 deletions

File tree

‎apps/t3/src/runtimeApiServer.test.ts‎

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type WsServerMessage,
1818
wsServerMessageSchema,
1919
} from "@acme/contracts";
20+
import { createFakeCodexAppServerBinary } from "../../../test-support/fakeCodexAppServer";
2021
import { startRuntimeApiServer } from "./runtimeApiServer";
2122

2223
function withTimeout<T>(promise: Promise<T>, timeoutMs = 5_000): Promise<T> {
@@ -146,91 +147,6 @@ const METHOD_COVERAGE_PARAMS: Record<WsNativeApiMethod, unknown> = {
146147
},
147148
};
148149

149-
function createFakeCodexAppServerBinary() {
150-
const tempDir = mkdtempSync(path.join(os.tmpdir(), "codex-app-server-fake-"));
151-
const binaryPath = path.join(tempDir, "codex");
152-
const script = `#!/usr/bin/env node
153-
const readline = require("node:readline");
154-
const rl = readline.createInterface({ input: process.stdin });
155-
let turnCount = 0;
156-
const send = (message) => process.stdout.write(\`\${JSON.stringify(message)}\\n\`);
157-
158-
rl.on("line", (line) => {
159-
let parsed;
160-
try {
161-
parsed = JSON.parse(line);
162-
} catch {
163-
return;
164-
}
165-
166-
if (!parsed || typeof parsed !== "object") {
167-
return;
168-
}
169-
170-
if (!("id" in parsed) || typeof parsed.method !== "string") {
171-
return;
172-
}
173-
174-
if (parsed.method === "initialize") {
175-
send({ id: parsed.id, result: {} });
176-
return;
177-
}
178-
179-
if (parsed.method === "thread/start") {
180-
send({ id: parsed.id, result: { thread: { id: "thread-fake" } } });
181-
return;
182-
}
183-
184-
if (parsed.method === "thread/resume") {
185-
const threadId =
186-
parsed.params &&
187-
typeof parsed.params === "object" &&
188-
typeof parsed.params.threadId === "string"
189-
? parsed.params.threadId
190-
: "thread-fake";
191-
send({ id: parsed.id, result: { thread: { id: threadId } } });
192-
return;
193-
}
194-
195-
if (parsed.method === "turn/start") {
196-
turnCount += 1;
197-
send({ id: parsed.id, result: { turn: { id: \`turn-\${turnCount}\` } } });
198-
setTimeout(() => {
199-
send({
200-
id: \`approval-\${turnCount}\`,
201-
method: "item/commandExecution/requestApproval",
202-
params: {
203-
threadId: "thread-fake",
204-
turnId: \`turn-\${turnCount}\`,
205-
itemId: \`item-\${turnCount}\`,
206-
},
207-
});
208-
}, 25);
209-
return;
210-
}
211-
212-
if (parsed.method === "turn/interrupt") {
213-
send({ id: parsed.id, result: {} });
214-
return;
215-
}
216-
217-
send({
218-
id: parsed.id,
219-
error: {
220-
code: -32601,
221-
message: \`Unsupported fake codex method: \${parsed.method}\`,
222-
},
223-
});
224-
});
225-
`;
226-
writeFileSync(binaryPath, script, { encoding: "utf8", mode: 0o755 });
227-
228-
return {
229-
tempDir,
230-
binaryPath,
231-
};
232-
}
233-
234150
async function waitForAgentEvent(
235151
nextMessage: () => Promise<WsServerMessage>,
236152
channel: string,
@@ -787,7 +703,7 @@ describe("runtimeApiServer", () => {
787703
it(
788704
"supports provider lifecycle methods with a fake codex app-server",
789705
async () => {
790-
const fakeCodex = createFakeCodexAppServerBinary();
706+
const fakeCodex = createFakeCodexAppServerBinary("t3-runtime-fake-codex-");
791707
const originalPath = process.env.PATH;
792708
process.env.PATH = `${fakeCodex.tempDir}${path.delimiter}${originalPath ?? ""}`;
793709

@@ -2125,7 +2041,7 @@ describe("runtimeApiServer", () => {
21252041
it(
21262042
"returns a ready bootstrap payload when codex app-server is available",
21272043
async () => {
2128-
const fakeCodex = createFakeCodexAppServerBinary();
2044+
const fakeCodex = createFakeCodexAppServerBinary("t3-bootstrap-fake-codex-");
21292045
const originalPath = process.env.PATH;
21302046
process.env.PATH = `${fakeCodex.tempDir}${path.delimiter}${originalPath ?? ""}`;
21312047

‎packages/runtime-core/src/providerManager.integration.test.ts‎

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,11 @@
1-
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
2-
import os from "node:os";
1+
import { rmSync } from "node:fs";
32
import path from "node:path";
43
import { describe, expect, it } from "vitest";
54

65
import type { ProviderEvent } from "@acme/contracts";
6+
import { createFakeCodexAppServerBinary } from "../../../test-support/fakeCodexAppServer";
77
import { ProviderManager } from "./providerManager";
88

9-
function createFakeCodexAppServerBinary() {
10-
const tempDir = mkdtempSync(path.join(os.tmpdir(), "runtime-core-fake-codex-"));
11-
const binaryPath = path.join(tempDir, "codex");
12-
const script = `#!/usr/bin/env node
13-
const readline = require("node:readline");
14-
const rl = readline.createInterface({ input: process.stdin });
15-
let turnCount = 0;
16-
const send = (message) => process.stdout.write(\`\${JSON.stringify(message)}\\n\`);
17-
18-
rl.on("line", (line) => {
19-
let parsed;
20-
try {
21-
parsed = JSON.parse(line);
22-
} catch {
23-
return;
24-
}
25-
26-
if (!parsed || typeof parsed !== "object") {
27-
return;
28-
}
29-
30-
if (!("id" in parsed) || typeof parsed.method !== "string") {
31-
return;
32-
}
33-
34-
if (parsed.method === "initialize") {
35-
send({ id: parsed.id, result: {} });
36-
return;
37-
}
38-
39-
if (parsed.method === "thread/start") {
40-
send({ id: parsed.id, result: { thread: { id: "thread-fake" } } });
41-
return;
42-
}
43-
44-
if (parsed.method === "thread/resume") {
45-
const threadId =
46-
parsed.params &&
47-
typeof parsed.params === "object" &&
48-
typeof parsed.params.threadId === "string"
49-
? parsed.params.threadId
50-
: "thread-fake";
51-
send({ id: parsed.id, result: { thread: { id: threadId } } });
52-
return;
53-
}
54-
55-
if (parsed.method === "turn/start") {
56-
turnCount += 1;
57-
send({ id: parsed.id, result: { turn: { id: \`turn-\${turnCount}\` } } });
58-
setTimeout(() => {
59-
send({
60-
id: \`approval-\${turnCount}\`,
61-
method: "item/commandExecution/requestApproval",
62-
params: {
63-
threadId: "thread-fake",
64-
turnId: \`turn-\${turnCount}\`,
65-
itemId: \`item-\${turnCount}\`,
66-
},
67-
});
68-
}, 25);
69-
return;
70-
}
71-
72-
if (parsed.method === "turn/interrupt") {
73-
send({ id: parsed.id, result: {} });
74-
return;
75-
}
76-
77-
send({
78-
id: parsed.id,
79-
error: {
80-
code: -32601,
81-
message: \`Unsupported fake codex method: \${parsed.method}\`,
82-
},
83-
});
84-
});
85-
`;
86-
writeFileSync(binaryPath, script, { encoding: "utf8", mode: 0o755 });
87-
88-
return {
89-
tempDir,
90-
binaryPath,
91-
};
92-
}
93-
949
async function waitForProviderEvent(
9510
events: ProviderEvent[],
9611
matcher: (event: ProviderEvent) => boolean,
@@ -119,7 +34,7 @@ describe("ProviderManager integration with fake codex app-server", () => {
11934
it(
12035
"supports start/send/respond/interrupt/stop lifecycle",
12136
async () => {
122-
const fakeCodex = createFakeCodexAppServerBinary();
37+
const fakeCodex = createFakeCodexAppServerBinary("runtime-core-fake-codex-");
12338
const originalPath = process.env.PATH;
12439
process.env.PATH = `${fakeCodex.tempDir}${path.delimiter}${originalPath ?? ""}`;
12540

‎test-support/fakeCodexAppServer.ts‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { mkdtempSync, writeFileSync } from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
4+
5+
export function createFakeCodexAppServerBinary(prefix: string) {
6+
const tempDir = mkdtempSync(path.join(os.tmpdir(), prefix));
7+
const binaryPath = path.join(tempDir, "codex");
8+
const script = `#!/usr/bin/env node
9+
const readline = require("node:readline");
10+
const rl = readline.createInterface({ input: process.stdin });
11+
let turnCount = 0;
12+
const send = (message) => process.stdout.write(\`\${JSON.stringify(message)}\\n\`);
13+
14+
rl.on("line", (line) => {
15+
let parsed;
16+
try {
17+
parsed = JSON.parse(line);
18+
} catch {
19+
return;
20+
}
21+
22+
if (!parsed || typeof parsed !== "object") {
23+
return;
24+
}
25+
26+
if (!("id" in parsed) || typeof parsed.method !== "string") {
27+
return;
28+
}
29+
30+
if (parsed.method === "initialize") {
31+
send({ id: parsed.id, result: {} });
32+
return;
33+
}
34+
35+
if (parsed.method === "thread/start") {
36+
send({ id: parsed.id, result: { thread: { id: "thread-fake" } } });
37+
return;
38+
}
39+
40+
if (parsed.method === "thread/resume") {
41+
const threadId =
42+
parsed.params &&
43+
typeof parsed.params === "object" &&
44+
typeof parsed.params.threadId === "string"
45+
? parsed.params.threadId
46+
: "thread-fake";
47+
send({ id: parsed.id, result: { thread: { id: threadId } } });
48+
return;
49+
}
50+
51+
if (parsed.method === "turn/start") {
52+
turnCount += 1;
53+
send({ id: parsed.id, result: { turn: { id: \`turn-\${turnCount}\` } } });
54+
setTimeout(() => {
55+
send({
56+
id: \`approval-\${turnCount}\`,
57+
method: "item/commandExecution/requestApproval",
58+
params: {
59+
threadId: "thread-fake",
60+
turnId: \`turn-\${turnCount}\`,
61+
itemId: \`item-\${turnCount}\`,
62+
},
63+
});
64+
}, 25);
65+
return;
66+
}
67+
68+
if (parsed.method === "turn/interrupt") {
69+
send({ id: parsed.id, result: {} });
70+
return;
71+
}
72+
73+
send({
74+
id: parsed.id,
75+
error: {
76+
code: -32601,
77+
message: \`Unsupported fake codex method: \${parsed.method}\`,
78+
},
79+
});
80+
});
81+
`;
82+
writeFileSync(binaryPath, script, { encoding: "utf8", mode: 0o755 });
83+
84+
return {
85+
tempDir,
86+
binaryPath,
87+
};
88+
}

0 commit comments

Comments
 (0)