188 lines
9.1 KiB
TypeScript
188 lines
9.1 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test";
|
|
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { prepareAgentSession, readAgentSessionRecord } from "@bbbug/shared/agent_session";
|
|
|
|
const WRAPPER = path.resolve(import.meta.dir, "../../bin/claude-codex.ts");
|
|
const dirs: string[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
function makeFixture(): { root: string; fakeCodex: string; argsLog: string; bridgeDir: string; workspace: string } {
|
|
const root = mkdtempSync(path.join(tmpdir(), "streamline-codex-test-"));
|
|
dirs.push(root);
|
|
const fakeCodex = path.join(root, "fake-codex.sh");
|
|
const argsLog = path.join(root, "codex-args.log");
|
|
const bridgeDir = path.join(root, "bridge");
|
|
const workspace = path.join(root, "security-case");
|
|
mkdirSync(bridgeDir, { recursive: true });
|
|
mkdirSync(workspace, { recursive: true });
|
|
const script = [
|
|
"#!/bin/sh",
|
|
"set -eu",
|
|
"printf '%s\\n' \"$@\" > \"$CODEX_ARGS_LOG\"",
|
|
"last_message=''",
|
|
"while [ \"$#\" -gt 0 ]; do",
|
|
" if [ \"$1\" = \"--output-last-message\" ]; then last_message=\"$2\"; shift 2; else shift; fi",
|
|
"done",
|
|
"if [ -n \"$last_message\" ]; then printf '%s' \"${CODEX_FINAL_MESSAGE:-STATUS: EVIDENCE_READY}\" > \"$last_message\"; fi",
|
|
"printf '%s\\n' '{\"type\":\"item.completed\",\"item\":{\"id\":\"warning-1\",\"type\":\"error\",\"message\":\"deprecated config key\"}}'",
|
|
"printf '%s\\n' '{\"type\":\"thread.started\",\"thread_id\":\"codex-thread-1\"}'",
|
|
"printf '%s\\n' '{\"type\":\"turn.started\"}'",
|
|
"printf '%s\\n' '{\"type\":\"item.started\",\"item\":{\"id\":\"cmd-1\",\"type\":\"command_execution\",\"command\":\"echo hi\"}}'",
|
|
"printf '%s\\n' '{\"type\":\"item.completed\",\"item\":{\"id\":\"cmd-1\",\"type\":\"command_execution\",\"command\":\"echo hi\",\"aggregated_output\":\"hi\",\"exit_code\":0}}'",
|
|
"printf '{\"type\":\"item.completed\",\"item\":{\"id\":\"msg-1\",\"type\":\"agent_message\",\"text\":\"%s\"}}\\n' \"${CODEX_AGENT_MESSAGE:-STATUS: EVIDENCE_READY}\"",
|
|
"printf '%s\\n' '{\"type\":\"turn.completed\",\"usage\":{\"input_tokens\":4,\"cached_input_tokens\":2,\"cache_write_input_tokens\":1,\"output_tokens\":3,\"reasoning_output_tokens\":2}}'",
|
|
].join("\n");
|
|
writeFileSync(fakeCodex, script, "utf8");
|
|
chmodSync(fakeCodex, 0o755);
|
|
return { root, fakeCodex, argsLog, bridgeDir, workspace };
|
|
}
|
|
|
|
async function runWrapper(
|
|
fixture: ReturnType<typeof makeFixture>,
|
|
args: string[],
|
|
env: Record<string, string> = {},
|
|
): Promise<{ code: number; stdout: string; stderr: string; events: Array<Record<string, unknown>> }> {
|
|
const proc = Bun.spawn({
|
|
cmd: [process.execPath, WRAPPER, ...args],
|
|
cwd: fixture.root,
|
|
stdout: "pipe",
|
|
stderr: "pipe",
|
|
env: {
|
|
...process.env,
|
|
CODEX_BIN: fixture.fakeCodex,
|
|
CODEX_ARGS_LOG: fixture.argsLog,
|
|
CODEX_BRIDGE_DIR: fixture.bridgeDir,
|
|
STREAMLINE_CODEX_SANDBOX: "workspace-write",
|
|
...env,
|
|
},
|
|
});
|
|
const [stdout, stderr, code] = await Promise.all([
|
|
new Response(proc.stdout).text(),
|
|
new Response(proc.stderr).text(),
|
|
proc.exited,
|
|
]);
|
|
const events = stdout.split("\n").filter(Boolean).map((line) => JSON.parse(line) as Record<string, unknown>);
|
|
return { code, stdout, stderr, events };
|
|
}
|
|
|
|
describe("Codex stream compatibility wrapper", () => {
|
|
test("reports the default model from Codex config when no override is passed", async () => {
|
|
const fixture = makeFixture();
|
|
const codexHome = path.join(fixture.root, "codex-home");
|
|
mkdirSync(codexHome, { recursive: true });
|
|
writeFileSync(path.join(codexHome, "config.toml"), 'model = "gpt-5.3-codex-spark"\n', "utf8");
|
|
const result = await runWrapper(fixture, [
|
|
"-p", "collect evidence", "--output-format", "stream-json", "--verbose",
|
|
"--add-dir", fixture.workspace,
|
|
], { CODEX_HOME: codexHome });
|
|
expect(result.events.some((event) => (
|
|
event.type === "system"
|
|
&& event.subtype === "init"
|
|
&& event.model === "gpt-5.3-codex-spark"
|
|
))).toBe(true);
|
|
});
|
|
|
|
test("normalises Codex JSONL into the existing Claude stream contract", async () => {
|
|
const fixture = makeFixture();
|
|
const result = await runWrapper(fixture, [
|
|
"-p", "collect evidence", "--output-format", "stream-json", "--verbose",
|
|
"--model", "gpt-5.6-sol",
|
|
"--dangerously-skip-permissions", "--add-dir", fixture.workspace,
|
|
"--remote-control", "streamline-security-abcdef12",
|
|
]);
|
|
|
|
expect(result.code).toBe(0);
|
|
expect(result.stderr).toBe("");
|
|
expect(result.events.some((event) => event.type === "system" && event.subtype === "init" && event.model === "gpt-5.6-sol")).toBe(true);
|
|
expect(result.events.some((event) => event.type === "assistant" && JSON.stringify(event).includes("tool_use"))).toBe(true);
|
|
expect(result.events.some((event) => event.type === "user" && JSON.stringify(event).includes("tool_result"))).toBe(true);
|
|
expect(result.events.some((event) => JSON.stringify(event).includes("CodexError"))).toBe(false);
|
|
expect(result.events.some((event) => event.type === "assistant" && JSON.stringify(event).includes("STATUS: EVIDENCE_READY"))).toBe(true);
|
|
const terminal = result.events.at(-1)!;
|
|
expect(terminal.type).toBe("result");
|
|
expect(terminal.is_error).toBe(false);
|
|
expect((terminal.usage as Record<string, unknown>).cache_read_input_tokens).toBe(2);
|
|
expect((terminal.usage as Record<string, unknown>).thinking_tokens).toBe(2);
|
|
expect(terminal.usage).not.toHaveProperty("reasoning_tokens");
|
|
|
|
const args = readFileSync(fixture.argsLog, "utf8").split("\n").filter(Boolean);
|
|
expect(args).toContain("exec");
|
|
expect(args).toContain("--json");
|
|
expect(args).toContain("--sandbox");
|
|
expect(args).toContain("workspace-write");
|
|
expect(args).toContain("--skip-git-repo-check");
|
|
expect(args).toContain("--ephemeral");
|
|
expect(args).toContain("--add-dir");
|
|
expect(args).toContain(fixture.workspace);
|
|
});
|
|
|
|
test("does not synthesize a missing security STATUS from EvidenceReport.json", async () => {
|
|
const fixture = makeFixture();
|
|
writeFileSync(path.join(fixture.workspace, "EvidenceReport.json"), JSON.stringify({ overall: "REPRODUCED" }), "utf8");
|
|
const result = await runWrapper(fixture, [
|
|
"-p", "collect evidence", "--output-format", "stream-json", "--verbose",
|
|
"--add-dir", fixture.workspace, "--remote-control", "streamline-security-abcdef12",
|
|
], { CODEX_AGENT_MESSAGE: "finished", CODEX_FINAL_MESSAGE: "finished" });
|
|
|
|
expect(result.code).toBe(0);
|
|
expect(result.events.some((event) => JSON.stringify(event).includes("STATUS: EVIDENCE_READY"))).toBe(false);
|
|
expect(result.events.at(-1)!.type).toBe("result");
|
|
expect(result.events.at(-1)!.is_error).toBe(false);
|
|
expect(result.events.at(-1)!.result).toBe("finished");
|
|
});
|
|
|
|
test("maps Asking's stable session UUID to Codex exec resume", async () => {
|
|
const fixture = makeFixture();
|
|
const session = "outer-session-1";
|
|
await runWrapper(fixture, ["-p", "first", "--session-id", session, "--output-format", "stream-json", "--verbose", "--add-dir", fixture.workspace]);
|
|
expect(readFileSync(path.join(fixture.bridgeDir, `${session}.thread`), "utf8").trim()).toBe("codex-thread-1");
|
|
|
|
const resumed = await runWrapper(fixture, ["-p", "second", "--resume", session, "--output-format", "stream-json", "--verbose", "--add-dir", fixture.workspace]);
|
|
expect(resumed.code).toBe(0);
|
|
const args = readFileSync(fixture.argsLog, "utf8").split("\n").filter(Boolean);
|
|
expect(args.slice(0, 2)).toEqual(["exec", "resume"]);
|
|
expect(args).toContain("codex-thread-1");
|
|
expect(args).not.toContain("--ephemeral");
|
|
});
|
|
|
|
test("persists a run session in the task workspace and resumes it after restart", async () => {
|
|
const fixture = makeFixture();
|
|
const first = prepareAgentSession({
|
|
workspaceDir: fixture.workspace,
|
|
taskKind: "security",
|
|
taskId: "security-1",
|
|
provider: "codex",
|
|
});
|
|
await runWrapper(fixture, [
|
|
"-p", "implement", ...first.args,
|
|
"--output-format", "stream-json", "--verbose", "--add-dir", fixture.workspace,
|
|
"--remote-control", "streamline-security-abcdef12",
|
|
], first.env);
|
|
|
|
expect(readAgentSessionRecord(first.recordPath)?.agentSessionId).toBe("codex-thread-1");
|
|
let args = readFileSync(fixture.argsLog, "utf8").split("\n").filter(Boolean);
|
|
expect(args).not.toContain("--ephemeral");
|
|
|
|
const afterRestart = prepareAgentSession({
|
|
workspaceDir: fixture.workspace,
|
|
taskKind: "security",
|
|
taskId: "security-1",
|
|
provider: "codex",
|
|
});
|
|
expect(afterRestart.resume).toBe(true);
|
|
await runWrapper(fixture, [
|
|
"-p", "continue", ...afterRestart.args,
|
|
"--output-format", "stream-json", "--verbose", "--add-dir", fixture.workspace,
|
|
"--remote-control", "streamline-security-abcdef12",
|
|
], afterRestart.env);
|
|
args = readFileSync(fixture.argsLog, "utf8").split("\n").filter(Boolean);
|
|
expect(args.slice(0, 2)).toEqual(["exec", "resume"]);
|
|
expect(args).toContain("codex-thread-1");
|
|
});
|
|
});
|