Files
pi-subagent/test/job-support.test.ts
T

101 lines
3.2 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { formatJob, formatJobList } from "../src/job-format.ts";
import { extractJobSnapshots, persistedJob, SUBAGENT_JOB_ENTRY } from "../src/persistence.ts";
import { emptyUsage, type JobSnapshot } from "../src/types.ts";
const job: JobSnapshot = {
id: "job_0001",
mode: "single",
agentScope: "user",
projectAgentsDir: null,
projectAgentsDirs: [],
status: "failed",
plannedTaskCount: 1,
usage: emptyUsage(),
createdAt: 10,
startedAt: 20,
finishedAt: 30,
results: [{
agent: "scout",
agentSource: "user",
task: "inspect",
cwd: "/tmp",
status: "failed",
exitCode: 1,
output: "partial",
outputTruncated: false,
outputBytesOmitted: 0,
stderr: "bad stderr",
stderrTruncated: false,
usage: emptyUsage(),
errorMessage: "boom",
}],
};
test("job formatting exposes terminal status, diagnostics, and bounded output", () => {
const text = formatJob(job);
assert.match(text, /job_0001/);
assert.match(text, /failed/);
assert.match(text, /boom/);
assert.match(formatJobList([job]), /1 failed/);
assert.match(formatJob({ ...job, status: "running", finishedAt: undefined, timedOut: true }), /timed out/i);
});
test("persistence wraps snapshots and ignores foreign or malformed entries", () => {
const entry = { type: "custom", customType: SUBAGENT_JOB_ENTRY, data: persistedJob("session-a", job) };
const restored = extractJobSnapshots([
entry,
{ type: "custom", customType: SUBAGENT_JOB_ENTRY, data: { version: 1, job: { id: "broken" } } },
{ type: "custom", customType: "other", data: persistedJob("session-a", job) },
{ type: "message" },
]);
assert.deepEqual(restored, [job]);
});
test("persistence validates activity summaries and accepts legacy snapshots without them", () => {
const withActivity: JobSnapshot = {
...job,
results: [{
...job.results[0],
activity: [
{ kind: "toolCall", summary: "read src/index.ts", timestamp: 20 },
{ kind: "toolResult", summary: "read: contents", timestamp: 21, isError: false },
],
}],
};
const entry = { type: "custom", customType: SUBAGENT_JOB_ENTRY, data: persistedJob("session-a", withActivity) };
assert.deepEqual(extractJobSnapshots([entry]), [withActivity]);
const broken = structuredClone(entry);
broken.data.job.results[0].activity![0].timestamp = "yesterday" as unknown as number;
assert.deepEqual(extractJobSnapshots([broken]), []);
});
test("workflow formatting and persistence keep step ids and skipped steps", () => {
const workflowJob: JobSnapshot = {
...job,
mode: "workflow",
status: "failed",
plannedTaskCount: 2,
results: [
{ ...job.results[0], stepId: "scan", task: "scan", errorMessage: "scan exploded" },
{
...job.results[0],
stepId: "audit",
task: "audit {steps.scan.output}",
status: "skipped",
exitCode: null,
errorMessage: "a required step did not succeed",
},
],
};
const text = formatJob(workflowJob);
assert.match(text, /### Step scan/);
assert.match(text, /### Step audit: scout — skipped/);
const entry = { type: "custom", customType: SUBAGENT_JOB_ENTRY, data: persistedJob("session-a", workflowJob) };
assert.deepEqual(extractJobSnapshots([entry]), [workflowJob]);
});