72 lines
4.8 KiB
TypeScript
72 lines
4.8 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import path from "node:path";
|
|
import os from "node:os";
|
|
import { startManagerServer } from "../../mgr/server.js";
|
|
import { MemoryAgentRunStore } from "../../mgr/store.js";
|
|
import { ManagerClient } from "../../mgr/client.js";
|
|
import { runOnce } from "../../runner/run-once.js";
|
|
import type { FailureKind, JsonRecord, TerminalStatus } from "../../common/types.js";
|
|
import { assertNoSecretLeak, createRunWithCommand, type SelfTestCase, type SelfTestContext } from "../harness.js";
|
|
|
|
const selfTest: SelfTestCase = async (context) => {
|
|
const server = await startManagerServer({ port: 0, host: "127.0.0.1", sourceCommit: "self-test", store: new MemoryAgentRunStore() });
|
|
try {
|
|
const client = new ManagerClient(server.baseUrl);
|
|
const happy = await createRunWithCommand(client, context, "hello", "selftest-turn", 15_000);
|
|
const result = await runOnce({ managerUrl: server.baseUrl, runId: happy.runId, codexCommand: context.fakeCodexCommand, codexArgs: context.fakeCodexArgs, codexHome: context.codexHome, env: { CODEX_HOME: context.codexHome } });
|
|
assert.equal(result.terminalStatus, "completed");
|
|
assert.equal(typeof (result.runner as { id?: unknown }).id, "string");
|
|
const events = await client.get(`/api/v1/runs/${happy.runId}/events?afterSeq=0&limit=100`) as { items?: Array<{ type: string; payload: unknown }> };
|
|
assert.ok(events.items?.some((event) => event.type === "assistant_message"));
|
|
assert.ok(events.items?.some((event) => event.type === "backend_status" && JSON.stringify(event.payload).includes("run-claimed")));
|
|
assertNoSecretLeak(events);
|
|
const finalRun = await client.get(`/api/v1/runs/${happy.runId}`) as { terminalStatus?: string };
|
|
assert.equal(finalRun.terminalStatus, "completed");
|
|
|
|
await runFailureCase({ client, managerUrl: server.baseUrl, context, mode: "missing-turn-result", expectedStatus: "failed", expectedFailureKind: "backend-response-invalid" });
|
|
await runFailureCase({ client, managerUrl: server.baseUrl, context, mode: "invalid-json", expectedStatus: "failed", expectedFailureKind: "backend-json-parse-error" });
|
|
await runFailureCase({ client, managerUrl: server.baseUrl, context, mode: "missing-terminal", expectedStatus: "failed", expectedFailureKind: "backend-timeout", timeoutMs: 500 });
|
|
await runSpawnFailureCase({ client, managerUrl: server.baseUrl, context });
|
|
|
|
return { name: "codex-stdio", tests: ["runner-lease-heartbeat", "codex-stdio-fake-turn", "codex-stdio-missing-turn-result", "codex-stdio-invalid-json", "codex-stdio-timeout", "codex-stdio-spawn-failure"] };
|
|
} finally {
|
|
await new Promise<void>((resolve) => server.server.close(() => resolve()));
|
|
}
|
|
};
|
|
|
|
async function runFailureCase(options: { client: ManagerClient; managerUrl: string; context: SelfTestContext; mode: string; expectedStatus: TerminalStatus; expectedFailureKind: FailureKind; timeoutMs?: number }): Promise<void> {
|
|
const item = await createRunWithCommand(options.client, options.context, `failure ${options.mode}`, `selftest-${options.mode}`, options.timeoutMs ?? 3_000);
|
|
const result = await runOnce({
|
|
managerUrl: options.managerUrl,
|
|
runId: item.runId,
|
|
codexCommand: options.context.fakeCodexCommand,
|
|
codexArgs: options.context.fakeCodexArgs,
|
|
codexHome: options.context.codexHome,
|
|
env: { CODEX_HOME: options.context.codexHome, AGENTRUN_FAKE_CODEX_MODE: options.mode },
|
|
}) as JsonRecord;
|
|
assert.equal(result.terminalStatus, options.expectedStatus, options.mode);
|
|
assert.equal(result.failureKind, options.expectedFailureKind, options.mode);
|
|
const events = await options.client.get(`/api/v1/runs/${item.runId}/events?afterSeq=0&limit=100`) as { items?: Array<{ type: string; payload: unknown }> };
|
|
assert.ok(events.items?.some((event) => event.type === "error"), options.mode);
|
|
assertNoSecretLeak(events);
|
|
}
|
|
|
|
async function runSpawnFailureCase(options: { client: ManagerClient; managerUrl: string; context: SelfTestContext }): Promise<void> {
|
|
const item = await createRunWithCommand(options.client, options.context, "failure spawn", "selftest-spawn-failure", 3_000);
|
|
const result = await runOnce({
|
|
managerUrl: options.managerUrl,
|
|
runId: item.runId,
|
|
codexCommand: path.join(os.tmpdir(), `agentrun-missing-codex-${process.pid}`),
|
|
codexArgs: [],
|
|
codexHome: options.context.codexHome,
|
|
env: { CODEX_HOME: options.context.codexHome },
|
|
}) as JsonRecord;
|
|
assert.equal(result.terminalStatus, "failed", "spawn failure");
|
|
assert.equal(result.failureKind, "backend-spawn-failed", "spawn failure");
|
|
const events = await options.client.get(`/api/v1/runs/${item.runId}/events?afterSeq=0&limit=100`) as { items?: Array<{ type: string; payload: unknown }> };
|
|
assert.ok(events.items?.some((event) => event.type === "error"), "spawn failure");
|
|
assertNoSecretLeak(events);
|
|
}
|
|
|
|
export default selfTest;
|