Files
pi-subagent/test/dashboard.test.ts

202 lines
6.0 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { SubagentDashboard } from "../src/dashboard.ts";
import { visibleWidth } from "@earendil-works/pi-tui";
import { emptyUsage, type JobSnapshot, type TaskSnapshot } from "../src/types.ts";
const theme = {
fg: (_color: any, text: string) => text,
bold: (text: string) => text,
};
const keybindings = {
matches(data: string, keybinding: string) {
return data === keybinding;
},
};
function task(overrides: Partial<TaskSnapshot> = {}): TaskSnapshot {
return {
agent: "scout",
agentSource: "user",
task: "inspect the code",
cwd: "/tmp",
status: "running",
exitCode: null,
output: "working",
outputTruncated: false,
outputBytesOmitted: 0,
stderr: "",
stderrTruncated: false,
usage: emptyUsage(),
activity: [{ kind: "toolCall", summary: "$ npm test", timestamp: 100 }],
startedAt: 1_000,
...overrides,
};
}
function job(overrides: Partial<JobSnapshot> = {}): JobSnapshot {
return {
id: "job_0001",
mode: "parallel",
agentScope: "user",
projectAgentsDir: null,
projectAgentsDirs: [],
status: "running",
plannedTaskCount: 1,
results: [task()],
usage: emptyUsage(),
createdAt: 1_000,
startedAt: 1_000,
...overrides,
};
}
function dashboard(jobs: JobSnapshot[], overrides: Record<string, unknown> = {}) {
return new SubagentDashboard({
getJobs: () => jobs,
cancelJob: (id: string) => {
throw new Error(`Unexpected cancellation: ${id}`);
},
theme,
keybindings,
requestRender: () => {},
done: () => {},
now: () => 12_000,
...overrides,
});
}
test("dashboard leads with active work and shows the selected task's latest activity", () => {
const view = dashboard([
job({
id: "job_0002",
status: "failed",
results: [task({ status: "failed", output: "boom", activity: [] })],
createdAt: 2_000,
}),
job(),
]);
const text = view.render(120).join("\n");
assert.match(text, /Subagents/);
assert.match(text, /1 active/);
assert.ok(text.indexOf("job_0001") < text.indexOf("job_0002"));
assert.match(text, /\$ npm test/);
assert.match(text, /working/);
});
test("cancellation requires confirmation and terminal jobs cannot be cancelled", () => {
let jobs = [job()];
const cancelled: string[] = [];
let closed = 0;
const view = dashboard(jobs, {
getJobs: () => jobs,
cancelJob: (id: string) => {
cancelled.push(id);
jobs = [{ ...jobs[0], status: "cancelling" }];
return jobs[0];
},
done: () => { closed++; },
});
view.handleInput("c");
assert.match(view.render(100).join("\n"), /Cancel job_0001\?/);
assert.deepEqual(cancelled, []);
view.handleInput("n");
assert.match(view.render(100).join("\n"), /not requested/);
view.handleInput("c");
view.handleInput("y");
assert.deepEqual(cancelled, ["job_0001"]);
assert.match(view.render(100).join("\n"), /Cancellation requested/);
assert.match(view.render(100).join("\n"), /cancelling/);
const terminal = dashboard([job({ status: "succeeded" })], {
cancelJob: (id: string) => { throw new Error(`Unexpected ${id}`); },
});
terminal.handleInput("c");
assert.match(terminal.render(100).join("\n"), /already terminal/);
terminal.handleInput("tui.select.cancel");
view.handleInput("tui.select.cancel");
assert.equal(closed, 1);
});
test("navigation, expansion, filtering, and refresh preserve a coherent selection", () => {
let jobs = [
job({ results: [task(), task({ agent: "reviewer", output: "second output", activity: [] })] , plannedTaskCount: 2 }),
job({ id: "job_0002", status: "failed", createdAt: 2_000, results: [task({ status: "failed", output: "boom", activity: [] })] }),
];
let renders = 0;
const view = dashboard(jobs, { getJobs: () => jobs, requestRender: () => { renders++; } });
view.handleInput("\x1b[C");
assert.match(view.render(100).join("\n"), /second output/);
view.handleInput("tui.select.down");
assert.match(view.render(100).join("\n"), /> job_0002/);
assert.match(view.render(100).join("\n"), /boom/);
view.handleInput("tui.select.confirm");
assert.match(view.render(100).join("\n"), /Enter less/);
jobs = [job({ id: "job_0003", createdAt: 3_000 }), ...jobs];
view.refresh();
assert.match(view.render(100).join("\n"), /> job_0002/);
assert.ok(renders > 0);
view.handleInput("f");
assert.doesNotMatch(view.render(100).join("\n"), /job_0002/);
jobs = jobs.map((item) => isActiveForTest(item) ? { ...item, status: "succeeded", finishedAt: 12_000 } : item);
view.refresh();
assert.match(view.render(100).join("\n"), /No active subagent jobs/);
view.handleInput("f");
assert.match(view.render(100).join("\n"), /job_0002/);
});
test("dashboard fits narrow widths and strips terminal controls from child output", () => {
const view = dashboard([job({
results: [task({
task: "审查 the implementation",
output: "\u001b[31mwide 中文 output\u001b[0m\nsecond line",
activity: [{ kind: "text", summary: "\u001b[31m reading 中文日本語 \u001b[0m", timestamp: 100 }],
})],
})]);
for (const width of [20, 32, 80]) {
const lines = view.render(width);
for (const line of lines) {
assert.ok(visibleWidth(line) <= width, `width ${width}: ${line}`);
assert.equal(line.includes("\u001b[31m"), false, JSON.stringify(line));
}
}
});
function isActiveForTest(item: JobSnapshot): boolean {
return item.status === "queued" || item.status === "running" || item.status === "cancelling";
}
test("empty and restored job states remain inspectable", () => {
const empty = dashboard([]);
assert.match(empty.render(60).join("\n"), /No subagent jobs/);
empty.handleInput("f");
assert.match(empty.render(60).join("\n"), /No subagent jobs/);
const restored = dashboard([
job({
id: "job_restored",
mode: "workflow",
status: "interrupted",
results: [task({
stepId: "scan",
item: 2,
status: "interrupted",
output: "partial result",
errorMessage: "Interrupted by session restart",
})],
}),
]);
const text = restored.render(100).join("\n");
assert.match(text, /Step scan item 2/);
assert.match(text, /Interrupted by session restart/);
assert.match(text, /partial result/);
});