Files
pi/packages/coding-agent/test/subagent-renderer.test.ts
2026-09-22 19:15:35 +08:00

92 lines
2.6 KiB
TypeScript

import type { Text } from "@earendil-works/pi-tui";
import { beforeAll, describe, expect, it } from "vitest";
import { subagentRenderers } from "../src/core/tools/renderers/subagent.ts";
import type { SubagentToolDetails } from "../src/core/tools/subagent.ts";
import { initTheme, theme } from "../src/modes/interactive/theme/theme.ts";
import { stripAnsi } from "../src/utils/ansi.ts";
function render(details: SubagentToolDetails, isPartial: boolean): string {
const component = subagentRenderers.renderResult?.(
{ content: [{ type: "text", text: details.text || "(no output)" }], details },
{ expanded: false, isPartial },
theme,
{ lastComponent: undefined } as never,
) as Text;
return stripAnsi(component.render(200).join("\n"));
}
describe("subagent renderer", () => {
beforeAll(() => {
initTheme("dark");
});
it("shows the current tool and counts while running", () => {
const output = render(
{
status: "running",
agentType: "explore",
description: "search files",
text: "searching",
toolUseCount: 2,
turnCount: 1,
currentTool: "grep",
},
true,
);
expect(output).toContain("running explore turns=1 tools=2 grep");
expect(output).toContain("searching");
});
it("shows usage and truncation markers after completion", () => {
const output = render(
{
status: "completed",
agentType: "general",
description: "edit files",
text: "done",
toolUseCount: 3,
turnCount: 4,
truncated: true,
usage: {
input: 1,
output: 2,
cacheRead: 3,
cacheWrite: 4,
totalTokens: 100,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
},
false,
);
expect(output).toContain("completed general turns=4 tools=3 tokens=100 truncated");
});
it("shows the short description in the call header", () => {
const component = subagentRenderers.renderCall?.(
{ description: "find auth flow", task: "very long task text", agent: "explore" },
theme,
{ lastComponent: undefined } as never,
) as Text;
const output = stripAnsi(component.render(200).join("\n"));
expect(output).toContain("subagent explore find auth flow");
expect(output).not.toContain("very long task text");
});
it("caps the collapsed preview and points at expand", () => {
const output = render(
{
status: "completed",
agentType: "general",
description: "long output",
text: Array.from({ length: 20 }, (_, index) => `line-${index}`).join("\n"),
toolUseCount: 0,
turnCount: 1,
},
false,
);
expect(output).toContain("line-11");
expect(output).not.toContain("line-12");
expect(output).toContain("8 more lines");
});
});