Files
pi-subagent/test/present.test.ts
T

114 lines
4.0 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
failedText,
formatStart,
formatUsageStats,
skippedText,
taskBody,
taskLabel,
} from "../src/present.ts";
import { emptyUsage, type JobSnapshot } from "../src/types.ts";
import type { StartPlan, WorkflowStartResult } from "../src/workflow-run.ts";
test("skipped text is one format and keeps the reason", () => {
assert.equal(
skippedText("Skipped because a required step did not succeed"),
"(skipped: a required step did not succeed)",
);
assert.equal(skippedText(undefined), "(skipped)");
});
test("the failed body takes the error message, then stderr, then output", () => {
assert.equal(failedText({ errorMessage: "boom", stderr: "stderr", output: "partial" }), "boom");
assert.equal(failedText({ stderr: "stderr", output: "partial" }), "stderr");
assert.equal(failedText({ output: "partial" }), "partial");
assert.equal(failedText({}), "(no output)");
});
test("one body rule answers for every task state", () => {
assert.equal(taskBody({ status: "queued", output: "", stderr: "" }), "(pending)");
assert.equal(taskBody({ status: "running", output: "working", stderr: "" }), "working");
assert.equal(taskBody({ status: "succeeded", output: "", stderr: "warning text" }), "warning text");
assert.equal(taskBody({ status: "succeeded", output: "", stderr: "" }), "(no output)");
assert.equal(
taskBody({ status: "failed", errorMessage: "boom", stderr: "stderr", output: "partial" }),
"boom",
);
assert.equal(
taskBody({ status: "skipped", errorMessage: "Skipped because a required step did not succeed" }),
"(skipped: a required step did not succeed)",
);
});
test("task labels speak each mode's vocabulary", () => {
assert.equal(taskLabel("single", {}, 0), "Task 1");
assert.equal(taskLabel("parallel", {}, 1), "Task 2");
assert.equal(taskLabel("chain", { step: 3 }, 0), "Step 3");
assert.equal(taskLabel("chain", {}, 1), "Step 2");
assert.equal(taskLabel("workflow", { stepId: "scan" }, 0), "Step scan");
assert.equal(taskLabel("workflow", { stepId: "audit", item: 2 }, 1), "Step audit item 2");
assert.equal(taskLabel("workflow", {}, 2), "Step 3");
});
test("usage text renders one format for snapshots and aggregates", () => {
assert.equal(
formatUsageStats(
{ input: 1000, output: 2000, cacheRead: 3000, cacheWrite: 4000, cost: 0.5, contextTokens: 5000, turns: 2 },
"scout",
),
"2 turns ↑1.0k ↓2.0k R3.0k W4.0k $0.5000 ctx:5.0k scout",
);
assert.equal(formatUsageStats({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0 }), "");
});
test("one start formatter serves both surfaces and their each visibility", () => {
const plan: StartPlan = {
name: "audit",
description: "Audit routes",
args: [],
steps: [
{ id: "scan", agent: "scout", task: "scan", needs: [] },
{ id: "audit", agent: "scout", task: "audit", needs: ["scan"], each: "scan" },
],
};
const job: JobSnapshot = {
id: "job_1",
mode: "workflow",
agentScope: "user",
projectAgentsDir: null,
projectAgentsDirs: [],
status: "running",
plannedTaskCount: 2,
results: [],
usage: emptyUsage(),
createdAt: 1,
};
const started: WorkflowStartResult = { id: "job_1", job, plan };
assert.equal(formatStart(started, { surface: "workflow" }), [
"Started workflow run job_1 (running).",
"Workflow: audit — Audit routes",
"Steps (2):",
"- scan (scout)",
"- audit (scout) needs scan",
'Use subagent_jobs { action: "get" | "wait", jobId: "job_1" } to retrieve it.',
].join("\n"));
assert.equal(formatStart(started, {
surface: "ultra",
each: true,
guidance: ["Size guideline: medium."],
footer: ["The run's journal is the only persisted copy of this plan."],
}), [
"Started ultra run job_1 (running).",
"Inline plan: audit — Audit routes",
"Steps (2):",
"- scan (scout)",
"- audit (scout) needs scan each {steps.scan.output}",
"Size guideline: medium.",
'Use subagent_jobs { action: "get" | "wait", jobId: "job_1" } to retrieve it.',
"The run's journal is the only persisted copy of this plan.",
].join("\n"));
});