244 lines
7.7 KiB
TypeScript
244 lines
7.7 KiB
TypeScript
import { setKeybindings, visibleWidth } from "@earendil-works/pi-tui";
|
|
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { KeybindingsManager } from "../src/core/keybindings.ts";
|
|
import { SubagentRunTracker } from "../src/core/subagent/tracker.ts";
|
|
import { SubagentPanelComponent } from "../src/modes/interactive/components/subagent-panel.ts";
|
|
import { initTheme } from "../src/modes/interactive/theme/theme.ts";
|
|
import { stripAnsi } from "../src/utils/ansi.ts";
|
|
|
|
function seedRun(
|
|
tracker: SubagentRunTracker,
|
|
id: string,
|
|
options: {
|
|
agentType: "general" | "explore";
|
|
description: string;
|
|
task: string;
|
|
status: "running" | "completed" | "error" | "aborted";
|
|
text: string;
|
|
toolCalls?: string[];
|
|
currentTool?: string;
|
|
},
|
|
): void {
|
|
tracker.begin(id, {
|
|
agentType: options.agentType,
|
|
description: options.description,
|
|
task: options.task,
|
|
});
|
|
tracker.update(id, {
|
|
status: options.status,
|
|
agentType: options.agentType,
|
|
description: options.description,
|
|
text: options.text,
|
|
toolUseCount: options.toolCalls?.length ?? 0,
|
|
turnCount: 1,
|
|
...(options.toolCalls ? { toolCalls: options.toolCalls } : {}),
|
|
...(options.currentTool ? { currentTool: options.currentTool } : {}),
|
|
});
|
|
}
|
|
|
|
function createPanel(keybindings: KeybindingsManager): {
|
|
tracker: SubagentRunTracker;
|
|
panel: SubagentPanelComponent;
|
|
onClose: ReturnType<typeof vi.fn>;
|
|
onOpenTranscript: ReturnType<typeof vi.fn>;
|
|
} {
|
|
const tracker = new SubagentRunTracker();
|
|
const onClose = vi.fn();
|
|
const onOpenTranscript = vi.fn();
|
|
const panel = new SubagentPanelComponent({ keybindings, onClose, onOpenTranscript });
|
|
panel.setTracker(tracker);
|
|
return { tracker, panel, onClose, onOpenTranscript };
|
|
}
|
|
|
|
describe("subagent panel", () => {
|
|
beforeAll(() => {
|
|
initTheme("dark");
|
|
});
|
|
|
|
beforeEach(() => {
|
|
setKeybindings(new KeybindingsManager());
|
|
});
|
|
|
|
it("renders nothing without runs and refuses to expand", () => {
|
|
const { panel } = createPanel(new KeybindingsManager());
|
|
expect(panel.render(80)).toEqual([]);
|
|
expect(panel.expand()).toBe(false);
|
|
expect(panel.isExpanded()).toBe(false);
|
|
});
|
|
|
|
it("lists every run in the status line with a toggle hint", () => {
|
|
const { tracker, panel } = createPanel(new KeybindingsManager());
|
|
seedRun(tracker, "call-1", {
|
|
agentType: "explore",
|
|
description: "find files",
|
|
task: "find files",
|
|
status: "completed",
|
|
text: "done",
|
|
});
|
|
seedRun(tracker, "call-2", {
|
|
agentType: "general",
|
|
description: "write tests",
|
|
task: "write tests",
|
|
status: "running",
|
|
text: "working",
|
|
});
|
|
|
|
const lines = panel.render(200).map((line) => stripAnsi(line));
|
|
expect(lines).toHaveLength(1);
|
|
expect(lines[0]).toContain("subagents");
|
|
expect(lines[0]).toContain("1 ✓ explore: find files");
|
|
expect(lines[0]).toContain("2 ● general: write tests");
|
|
expect(lines[0]).toContain("alt+s to inspect");
|
|
});
|
|
|
|
it("shows the configured toggle key in the status line", () => {
|
|
setKeybindings(new KeybindingsManager({ "app.subagents.toggle": "ctrl+u" }));
|
|
const { tracker, panel } = createPanel(new KeybindingsManager({ "app.subagents.toggle": "ctrl+u" }));
|
|
seedRun(tracker, "call-1", {
|
|
agentType: "explore",
|
|
description: "find files",
|
|
task: "find files",
|
|
status: "completed",
|
|
text: "done",
|
|
});
|
|
expect(stripAnsi(panel.render(200).join("\n"))).toContain("ctrl+u to inspect");
|
|
});
|
|
|
|
it("expands onto the newest running run and switches runs with left and right", () => {
|
|
const { tracker, panel } = createPanel(new KeybindingsManager());
|
|
seedRun(tracker, "call-1", {
|
|
agentType: "explore",
|
|
description: "find files",
|
|
task: "task one",
|
|
status: "completed",
|
|
text: "first answer",
|
|
});
|
|
seedRun(tracker, "call-2", {
|
|
agentType: "general",
|
|
description: "write tests",
|
|
task: "task two",
|
|
status: "running",
|
|
text: "second answer",
|
|
toolCalls: ["read"],
|
|
currentTool: "read",
|
|
});
|
|
|
|
expect(panel.expand()).toBe(true);
|
|
expect(panel.isExpanded()).toBe(true);
|
|
const expanded = panel.render(200).map((line) => stripAnsi(line));
|
|
expect(expanded).toHaveLength(8);
|
|
expect(expanded[0]).toContain("[2/2]");
|
|
expect(expanded[0]).toContain("▸ 2 ● general: write tests");
|
|
expect(expanded.join("\n")).toContain("task two");
|
|
expect(expanded.join("\n")).toContain("now read");
|
|
|
|
panel.handleInput("\x1b[D");
|
|
const previous = panel.render(200).map((line) => stripAnsi(line));
|
|
expect(previous[0]).toContain("[1/2]");
|
|
expect(previous[0]).toContain("▸ 1 ✓ explore: find files");
|
|
expect(previous.join("\n")).toContain("task one");
|
|
expect(previous.join("\n")).toContain("first answer");
|
|
expect(previous.join("\n")).not.toContain("second answer");
|
|
|
|
panel.handleInput("\x1b[C");
|
|
expect(stripAnsi(panel.render(200)[0]!)).toContain("[2/2]");
|
|
});
|
|
|
|
it("scrolls run details with up and down", () => {
|
|
const { tracker, panel } = createPanel(new KeybindingsManager());
|
|
const output = Array.from({ length: 12 }, (_, index) => `line ${index}`).join("\n");
|
|
seedRun(tracker, "call-1", {
|
|
agentType: "general",
|
|
description: "long run",
|
|
task: "long task",
|
|
status: "running",
|
|
text: output,
|
|
toolCalls: ["read", "grep"],
|
|
});
|
|
|
|
panel.expand();
|
|
const first = panel.render(200).map((line) => stripAnsi(line));
|
|
expect(first[1]).toContain("turns 1 · tools 2");
|
|
expect(first.join("\n")).toContain("calls: read → grep");
|
|
|
|
panel.handleInput("\x1b[B");
|
|
const scrolled = panel.render(200).map((line) => stripAnsi(line));
|
|
expect(scrolled[1]).toContain("task: long task");
|
|
|
|
panel.handleInput("\x1b[A");
|
|
expect(stripAnsi(panel.render(200)[1]!)).toContain("turns 1 · tools 2");
|
|
});
|
|
|
|
it("opens the selected run's transcript on enter", () => {
|
|
const { tracker, panel, onOpenTranscript } = createPanel(new KeybindingsManager());
|
|
seedRun(tracker, "call-1", {
|
|
agentType: "explore",
|
|
description: "find files",
|
|
task: "find files",
|
|
status: "completed",
|
|
text: "done",
|
|
});
|
|
seedRun(tracker, "call-2", {
|
|
agentType: "general",
|
|
description: "write tests",
|
|
task: "write tests",
|
|
status: "running",
|
|
text: "working",
|
|
});
|
|
|
|
panel.expand();
|
|
panel.handleInput("\r");
|
|
expect(onOpenTranscript).toHaveBeenCalledWith("call-2");
|
|
|
|
panel.handleInput("\x1b[D");
|
|
panel.handleInput("\r");
|
|
expect(onOpenTranscript).toHaveBeenLastCalledWith("call-1");
|
|
});
|
|
|
|
it("keeps every rendered line within the terminal width", () => {
|
|
// Regression: the compact status line ignored the "subagents " prefix width and overflowed
|
|
// (crash: Rendered line 7185 exceeds terminal width (217 > 209)).
|
|
for (const width of [209, 120, 80, 40, 30, 27, 26, 20]) {
|
|
const { tracker, panel } = createPanel(new KeybindingsManager());
|
|
for (let i = 1; i <= 4; i++) {
|
|
seedRun(tracker, `call-${i}`, {
|
|
agentType: i % 2 === 0 ? "explore" : "general",
|
|
description: `Inventory ${i} gateway core English UI strings for the platform adapter`,
|
|
task: `task ${i}`,
|
|
status: i % 2 === 0 ? "completed" : "running",
|
|
text: `output ${i}`,
|
|
});
|
|
}
|
|
|
|
for (const line of panel.render(width)) {
|
|
expect(visibleWidth(line), `width=${width}`).toBeLessThanOrEqual(width);
|
|
}
|
|
|
|
panel.expand();
|
|
for (const line of panel.render(width)) {
|
|
expect(visibleWidth(line), `width=${width} expanded`).toBeLessThanOrEqual(width);
|
|
}
|
|
}
|
|
});
|
|
|
|
it("closes on escape and on the toggle key", () => {
|
|
const keybindings = new KeybindingsManager({ "app.subagents.toggle": "ctrl+u" });
|
|
const { tracker, panel, onClose } = createPanel(keybindings);
|
|
seedRun(tracker, "call-1", {
|
|
agentType: "explore",
|
|
description: "find files",
|
|
task: "find files",
|
|
status: "completed",
|
|
text: "done",
|
|
});
|
|
|
|
panel.expand();
|
|
panel.handleInput("\x1b");
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
|
|
panel.expand();
|
|
panel.handleInput("\x15");
|
|
expect(onClose).toHaveBeenCalledTimes(2);
|
|
});
|
|
});
|