feat(M3): Async CLI Integration
This commit is contained in:
@@ -0,0 +1,278 @@
|
||||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import {
|
||||
reportError,
|
||||
reportCliError,
|
||||
handleSyncRun,
|
||||
handleAsyncRun,
|
||||
} from "../src/cli-helpers.js";
|
||||
import type { ExecResult, Job } from "../src/types.js";
|
||||
|
||||
function captureConsole() {
|
||||
const logs: string[] = [];
|
||||
const errors: string[] = [];
|
||||
const origLog = console.log;
|
||||
const origError = console.error;
|
||||
console.log = (...args: unknown[]) => logs.push(args.map(String).join(" "));
|
||||
console.error = (...args: unknown[]) => errors.push(args.map(String).join(" "));
|
||||
return {
|
||||
logs,
|
||||
errors,
|
||||
restore() {
|
||||
console.log = origLog;
|
||||
console.error = origError;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("reportError", () => {
|
||||
it("prints JSON error for Error instance when jsonMode=true", () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const code = reportError(new Error("boom"), true);
|
||||
assert.strictEqual(code, 1);
|
||||
assert.strictEqual(out.errors.length, 1);
|
||||
const parsed = JSON.parse(out.errors[0]);
|
||||
assert.strictEqual(parsed.error, "boom");
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("prints plain text error for Error instance when jsonMode=false", () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const code = reportError(new Error("boom"), false);
|
||||
assert.strictEqual(code, 1);
|
||||
assert.strictEqual(out.errors.length, 1);
|
||||
assert.strictEqual(out.errors[0], "boom");
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("prints JSON error for non-Error value when jsonMode=true", () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const code = reportError("plain string", true);
|
||||
assert.strictEqual(code, 1);
|
||||
const parsed = JSON.parse(out.errors[0]);
|
||||
assert.strictEqual(parsed.error, "plain string");
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("reportCliError", () => {
|
||||
it("prints JSON error with jsonMode=true", () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const code = reportCliError("missing arg", true);
|
||||
assert.strictEqual(code, 1);
|
||||
const parsed = JSON.parse(out.errors[0]);
|
||||
assert.strictEqual(parsed.error, "missing arg");
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("prints prefixed text error with jsonMode=false", () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const code = reportCliError("missing arg", false);
|
||||
assert.strictEqual(code, 1);
|
||||
assert.strictEqual(out.errors[0], "Error: missing arg");
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleSyncRun", () => {
|
||||
it("returns 0 and prints JSON result in jsonMode", async () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const result: ExecResult = {
|
||||
stdout: "out",
|
||||
stderr: "err",
|
||||
exitCode: 0,
|
||||
client: "codex",
|
||||
durationMs: 10,
|
||||
};
|
||||
const code = await handleSyncRun(
|
||||
async () => result,
|
||||
"codex",
|
||||
"hello",
|
||||
5000,
|
||||
false,
|
||||
{ jsonMode: true, stdoutWrite: () => {}, stderrWrite: () => {} }
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
assert.strictEqual(out.logs.length, 1);
|
||||
const parsed = JSON.parse(out.logs[0]);
|
||||
assert.strictEqual(parsed.stdout, "out");
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("returns 0 and writes stdout/stderr in text mode", async () => {
|
||||
const out = captureConsole();
|
||||
const stdout: string[] = [];
|
||||
const stderr: string[] = [];
|
||||
try {
|
||||
const result: ExecResult = {
|
||||
stdout: "out",
|
||||
stderr: "err",
|
||||
exitCode: 0,
|
||||
client: "codex",
|
||||
durationMs: 10,
|
||||
};
|
||||
const code = await handleSyncRun(
|
||||
async () => result,
|
||||
"codex",
|
||||
"hello",
|
||||
5000,
|
||||
false,
|
||||
{
|
||||
jsonMode: false,
|
||||
stdoutWrite: (c) => stdout.push(c),
|
||||
stderrWrite: (c) => stderr.push(c),
|
||||
}
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
assert.strictEqual(stdout.join(""), "out");
|
||||
assert.strictEqual(stderr.join(""), "err");
|
||||
assert.strictEqual(out.logs.length, 0);
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("passes timeoutMs and debug to executePrompt", async () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
let received: any;
|
||||
const code = await handleSyncRun(
|
||||
async (_c, _p, opts) => {
|
||||
received = opts;
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
client: "codex",
|
||||
durationMs: 1,
|
||||
};
|
||||
},
|
||||
"codex",
|
||||
"hello",
|
||||
12345,
|
||||
true,
|
||||
{ jsonMode: true, stdoutWrite: () => {}, stderrWrite: () => {} }
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
assert.strictEqual(received?.timeoutMs, 12345);
|
||||
assert.strictEqual(received?.debug, true);
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("returns 1 and prints error JSON when executePrompt throws", async () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const code = await handleSyncRun(
|
||||
async () => {
|
||||
throw new Error("fail");
|
||||
},
|
||||
"codex",
|
||||
"hello",
|
||||
undefined,
|
||||
false,
|
||||
{ jsonMode: true, stdoutWrite: () => {}, stderrWrite: () => {} }
|
||||
);
|
||||
assert.strictEqual(code, 1);
|
||||
const parsed = JSON.parse(out.errors[0]);
|
||||
assert.strictEqual(parsed.error, "fail");
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleAsyncRun", () => {
|
||||
it("returns 0 and prints JSON job in jsonMode", async () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const job: Job = {
|
||||
id: "j1",
|
||||
client: "codex",
|
||||
prompt: "hi",
|
||||
status: "running",
|
||||
startedAt: "2024-01-01T00:00:00Z",
|
||||
};
|
||||
const code = await handleAsyncRun(
|
||||
async () => job,
|
||||
"codex",
|
||||
"hi",
|
||||
undefined,
|
||||
false,
|
||||
{ jsonMode: true, stdoutWrite: () => {}, stderrWrite: () => {} }
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
const parsed = JSON.parse(out.logs[0]);
|
||||
assert.strictEqual(parsed.jobId, "j1");
|
||||
assert.strictEqual(parsed.status, "running");
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("returns 0 and prints text job info", async () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const job: Job = {
|
||||
id: "j1",
|
||||
client: "codex",
|
||||
prompt: "hi",
|
||||
status: "running",
|
||||
startedAt: "2024-01-01T00:00:00Z",
|
||||
};
|
||||
const code = await handleAsyncRun(
|
||||
async () => job,
|
||||
"codex",
|
||||
"hi",
|
||||
undefined,
|
||||
false,
|
||||
{ jsonMode: false, stdoutWrite: () => {}, stderrWrite: () => {} }
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
assert.ok(out.logs[0].includes("j1"));
|
||||
assert.ok(out.logs[0].includes("running"));
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("returns 1 and prints error JSON when startJob throws", async () => {
|
||||
const out = captureConsole();
|
||||
try {
|
||||
const code = await handleAsyncRun(
|
||||
async () => {
|
||||
throw new Error("fail");
|
||||
},
|
||||
"codex",
|
||||
"hi",
|
||||
undefined,
|
||||
false,
|
||||
{ jsonMode: true, stdoutWrite: () => {}, stderrWrite: () => {} }
|
||||
);
|
||||
assert.strictEqual(code, 1);
|
||||
const parsed = JSON.parse(out.errors[0]);
|
||||
assert.strictEqual(parsed.error, "fail");
|
||||
} finally {
|
||||
out.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,16 @@ import { describe, it } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { main } from "../src/cli.js";
|
||||
import { ClientNotFoundError } from "../src/types.js";
|
||||
import type { ClientInfo, ExecResult, ClientName } from "../src/types.js";
|
||||
import type { ClientInfo, ExecResult, ClientName, Job, JobStatus, DebugInfo } from "../src/types.js";
|
||||
|
||||
function mockJob(overrides: Partial<Job> & { id: string; status: JobStatus }): Job {
|
||||
return {
|
||||
client: "codex",
|
||||
prompt: "hello",
|
||||
startedAt: "2024-01-01T00:00:00Z",
|
||||
...overrides,
|
||||
} as Job;
|
||||
}
|
||||
|
||||
function captureOutput() {
|
||||
const logs: string[] = [];
|
||||
@@ -426,7 +435,8 @@ describe("main", () => {
|
||||
durationMs: 42,
|
||||
stderrLength: 0,
|
||||
stdoutLength: 6,
|
||||
} as any);
|
||||
noisySuccess: false,
|
||||
} satisfies DebugInfo);
|
||||
return {
|
||||
stdout: "output",
|
||||
stderr: "",
|
||||
@@ -516,7 +526,8 @@ describe("main", () => {
|
||||
durationMs: 42,
|
||||
stderrLength: 0,
|
||||
stdoutLength: 6,
|
||||
} as any);
|
||||
noisySuccess: false,
|
||||
} satisfies DebugInfo);
|
||||
return {
|
||||
stdout: "output",
|
||||
stderr: "",
|
||||
@@ -705,7 +716,8 @@ describe("main", () => {
|
||||
durationMs: 42,
|
||||
stderrLength: 0,
|
||||
stdoutLength: 6,
|
||||
} as any);
|
||||
noisySuccess: false,
|
||||
} satisfies DebugInfo);
|
||||
return { id: "job-def", client: "codex", prompt: "hello", status: "running", startedAt: new Date().toISOString() };
|
||||
},
|
||||
stderrWrite: (chunk) => stderrChunks.push(chunk),
|
||||
@@ -871,7 +883,8 @@ describe("main", () => {
|
||||
durationMs: 42,
|
||||
stderrLength: 0,
|
||||
stdoutLength: 6,
|
||||
} as any);
|
||||
noisySuccess: false,
|
||||
} satisfies DebugInfo);
|
||||
return { id: "job-ghi", client: "codex", prompt: "do something", status: "running", startedAt: new Date().toISOString() };
|
||||
},
|
||||
resolveClient: () => "codex",
|
||||
@@ -953,13 +966,7 @@ describe("main", () => {
|
||||
["node", "cli.ts", "status", "job-123"],
|
||||
{
|
||||
detectClients: () => mockClients,
|
||||
getJob: (jobId) => ({
|
||||
id: jobId,
|
||||
client: "codex",
|
||||
prompt: "hello",
|
||||
status: "running",
|
||||
startedAt: "2024-01-01T00:00:00Z",
|
||||
} as any),
|
||||
getJob: (jobId) => mockJob({ id: jobId, status: "running" }),
|
||||
}
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
@@ -979,15 +986,14 @@ describe("main", () => {
|
||||
["node", "cli.ts", "status", "job-456"],
|
||||
{
|
||||
detectClients: () => mockClients,
|
||||
getJob: (jobId) => ({
|
||||
getJob: (jobId) => mockJob({
|
||||
id: jobId,
|
||||
client: "claude",
|
||||
prompt: "write tests",
|
||||
status: "completed",
|
||||
startedAt: "2024-01-01T00:00:00Z",
|
||||
completedAt: "2024-01-01T00:01:00Z",
|
||||
result: { stdout: "ok", stderr: "", exitCode: 0, client: "claude", durationMs: 100 },
|
||||
} as any),
|
||||
}),
|
||||
}
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
@@ -1040,13 +1046,7 @@ describe("main", () => {
|
||||
["node", "cli.ts", "status", "job-123", "--text"],
|
||||
{
|
||||
detectClients: () => mockClients,
|
||||
getJob: (jobId) => ({
|
||||
id: jobId,
|
||||
client: "codex",
|
||||
prompt: "hello",
|
||||
status: "running",
|
||||
startedAt: "2024-01-01T00:00:00Z",
|
||||
} as any),
|
||||
getJob: (jobId) => mockJob({ id: jobId, status: "running" }),
|
||||
}
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
@@ -1169,13 +1169,7 @@ describe("main", () => {
|
||||
["node", "cli.ts", "cancel", "job-abc"],
|
||||
{
|
||||
detectClients: () => mockClients,
|
||||
getJob: (jobId) => ({
|
||||
id: jobId,
|
||||
client: "codex",
|
||||
prompt: "hello",
|
||||
status: "running",
|
||||
startedAt: "2024-01-01T00:00:00Z",
|
||||
} as any),
|
||||
getJob: (jobId) => mockJob({ id: jobId, status: "running" }),
|
||||
cancelJob: (jobId) => {
|
||||
cancelledJobId = jobId;
|
||||
},
|
||||
@@ -1198,14 +1192,7 @@ describe("main", () => {
|
||||
["node", "cli.ts", "cancel", "job-done"],
|
||||
{
|
||||
detectClients: () => mockClients,
|
||||
getJob: (jobId) => ({
|
||||
id: jobId,
|
||||
client: "codex",
|
||||
prompt: "hello",
|
||||
status: "completed",
|
||||
startedAt: "2024-01-01T00:00:00Z",
|
||||
completedAt: "2024-01-01T00:01:00Z",
|
||||
} as any),
|
||||
getJob: (jobId) => mockJob({ id: jobId, status: "completed", completedAt: "2024-01-01T00:01:00Z" }),
|
||||
}
|
||||
);
|
||||
assert.strictEqual(code, 1);
|
||||
@@ -1257,13 +1244,7 @@ describe("main", () => {
|
||||
["node", "cli.ts", "cancel", "job-abc", "--text"],
|
||||
{
|
||||
detectClients: () => mockClients,
|
||||
getJob: (jobId) => ({
|
||||
id: jobId,
|
||||
client: "codex",
|
||||
prompt: "hello",
|
||||
status: "running",
|
||||
startedAt: "2024-01-01T00:00:00Z",
|
||||
} as any),
|
||||
getJob: (jobId) => mockJob({ id: jobId, status: "running" }),
|
||||
cancelJob: () => {},
|
||||
}
|
||||
);
|
||||
@@ -1285,7 +1266,7 @@ describe("main", () => {
|
||||
listJobs: () => [
|
||||
{ id: "job-1", client: "codex", prompt: "p1", status: "running", startedAt: "2024-01-01T00:00:00Z" },
|
||||
{ id: "job-2", client: "claude", prompt: "p2", status: "completed", startedAt: "2024-01-01T00:01:00Z" },
|
||||
] as any[],
|
||||
],
|
||||
}
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
@@ -1310,7 +1291,7 @@ describe("main", () => {
|
||||
receivedFilter = options?.filter;
|
||||
return [
|
||||
{ id: "job-1", client: "codex", prompt: "p1", status: "running", startedAt: "2024-01-01T00:00:00Z" },
|
||||
] as any[];
|
||||
];
|
||||
},
|
||||
}
|
||||
);
|
||||
@@ -1333,7 +1314,7 @@ describe("main", () => {
|
||||
detectClients: () => mockClients,
|
||||
listJobs: () => [
|
||||
{ id: "job-1", client: "codex", prompt: "p1", status: "running", startedAt: "2024-01-01T00:00:00Z" },
|
||||
] as any[],
|
||||
],
|
||||
}
|
||||
);
|
||||
assert.strictEqual(code, 0);
|
||||
|
||||
Reference in New Issue
Block a user