| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- import { stripVTControlCharacters } from "node:util";
- import { describe, expect, it, vi } from "vitest";
- import { TuiBackgroundWorkPanel } from "../../src/tui/background-work/panel.js";
- import type { TuiBackgroundTask } from "../../src/runtime/port.js";
- import type { TuiAgentTeamSnapshot } from "../../src/tui/agent-team/model.js";
- import { TuiAltScreen } from "../../src/tui/engine/tui-alt-screen.js";
- import { VirtualTerminal } from "../pi-084-upstream/virtual-terminal.js";
- import { visibleWidth } from "../../src/tui/rendering/text.js";
- function teamSnapshot(): TuiAgentTeamSnapshot {
- return {
- rootSessionId: "root",
- capturedAtMs: 10_000,
- summary: {
- total: 1,
- running: 1,
- waiting: 0,
- queued: 0,
- done: 0,
- failed: 0,
- stopped: 0,
- },
- members: [
- {
- sessionId: "reviewer",
- parentSessionId: "root",
- agentName: "reviewer",
- task: "Review the implementation",
- status: "running",
- phase: "tool",
- activity: "Reading task-panel.ts",
- toolCount: 2,
- turnId: "turn-reviewer",
- startedAtMs: 1_000,
- updatedAtMs: 9_000,
- },
- ],
- };
- }
- function backgroundTasks(): readonly TuiBackgroundTask[] {
- return [
- {
- taskId: "bg-running",
- kind: "bash",
- status: "running",
- ownerSessionId: "root",
- description: "pnpm typecheck",
- createdAtMs: 2_000,
- updatedAtMs: 9_000,
- startedAtMs: 3_000,
- },
- {
- taskId: "bg-failed",
- kind: "bash",
- status: "failed",
- ownerSessionId: "root",
- description: "build docs",
- createdAtMs: 1_000,
- updatedAtMs: 8_000,
- startedAtMs: 2_000,
- endedAtMs: 8_000,
- lastError: "exit code 1\n\u001B]8;;https://example.com\u0007unsafe",
- },
- {
- taskId: "bg-delivered",
- kind: "bash",
- status: "succeeded",
- ownerSessionId: "root",
- description: "completed validation",
- createdAtMs: 500,
- updatedAtMs: 7_000,
- startedAtMs: 600,
- endedAtMs: 6_000,
- deliveredAtMs: 7_000,
- },
- ];
- }
- describe("TuiBackgroundWorkPanel", () => {
- it("renders one unified list for Agent Team and Runtime background tasks", () => {
- const agentTeam = vi.fn(teamSnapshot);
- const panel = new TuiBackgroundWorkPanel({
- agentTeam,
- backgroundTasks,
- activeSessionId: () => "root",
- onOpenAgent: vi.fn(),
- onCancel: vi.fn(),
- requestRender: vi.fn(),
- });
- const rendered = stripVTControlCharacters(panel.render(100).join("\n"));
- expect(rendered).toContain("Tasks");
- expect(rendered).toContain("4 total · 2 active · 1 failed");
- expect(rendered).toContain("[Agent] reviewer");
- expect(rendered).toContain("Reading task-panel.ts");
- expect(rendered).toContain("[Bash] pnpm typecheck");
- expect(rendered).toContain("[Bash] build docs");
- expect(rendered).toContain("[Bash] completed validation");
- expect(rendered).toContain("Enter open transcript");
- expect(agentTeam).toHaveBeenCalledOnce();
- });
- it("opens Agent transcripts and inspects Runtime task details without mutating them", () => {
- const onOpenAgent = vi.fn();
- const requestRender = vi.fn();
- const panel = new TuiBackgroundWorkPanel({
- agentTeam: teamSnapshot,
- backgroundTasks,
- activeSessionId: () => "root",
- onOpenAgent,
- onCancel: vi.fn(),
- requestRender,
- });
- panel.handleInput("\r");
- expect(onOpenAgent).toHaveBeenCalledWith("reviewer");
- panel.handleInput("\u001B[B");
- panel.handleInput("\r");
- let rendered = stripVTControlCharacters(panel.render(80).join("\n"));
- expect(rendered).toContain("Background task · Running");
- expect(rendered).toContain("pnpm typecheck");
- expect(rendered).toContain("bg-running");
- panel.handleInput("\u001B");
- rendered = stripVTControlCharacters(panel.render(80).join("\n"));
- expect(rendered).toContain("[Agent] reviewer");
- expect(rendered).toContain("[Bash] pnpm typecheck");
- expect(requestRender).toHaveBeenCalled();
- panel.handleInput("\u001B[B");
- panel.handleInput("\r");
- rendered = stripVTControlCharacters(panel.render(80).join("\n"));
- expect(rendered).toContain("Background task · Failed");
- expect(rendered).toContain("exit code 1");
- expect(rendered).toContain("unsafe");
- expect(rendered).not.toContain("https://example.com");
- });
- it("keeps the full multiline command, task ID and failure reason readable at narrow widths", () => {
- const command = `printf "开始"\n${"x".repeat(140)}\n\n echo "command-end"`;
- const task = {
- ...backgroundTasks()[1]!,
- taskId: `bg-${"a".repeat(50)}`,
- description: "short summary...",
- command: `${command}\u001B]52;c;Y2xpcGJvYXJk\u0007`,
- lastError: `exit code 1\n${"e".repeat(130)}\nPermission denied at error-end`,
- };
- const panel = new TuiBackgroundWorkPanel({
- agentTeam: teamSnapshot,
- backgroundTasks: () => [task],
- activeSessionId: () => "root",
- onOpenAgent: vi.fn(),
- onCancel: vi.fn(),
- requestRender: vi.fn(),
- });
- panel.handleInput("\u001B[B");
- expect(stripVTControlCharacters(panel.render(60).join("\n"))).toContain(
- "short summary...",
- );
- panel.handleInput("\r");
- for (const width of [16, 24, 40, 80, 120]) {
- const lines = panel.render(width);
- expect(lines.every((line) => visibleWidth(line) <= width)).toBe(true);
- const body = lines.map((line) =>
- stripVTControlCharacters(line)
- .replace(/^│ | │$/gu, "")
- .trimEnd(),
- );
- const text = body.join("\n");
- expect(text).not.toContain("short summary...");
- expect(text).not.toContain("Y2xpcGJvYXJk");
- expect(text).not.toContain("…");
- expect(text.replace(/\s/gu, "")).toContain(
- task.lastError.replace(/\s/gu, ""),
- );
- expect(text.replace(/\s/gu, "")).toContain(command.replace(/\s/gu, ""));
- expect(text.replace(/\s/gu, "")).toContain(task.taskId);
- }
- const wide = stripVTControlCharacters(panel.render(120).join("\n"));
- expect(wide).toContain(' echo "command-end"');
- expect(wide.indexOf("Error")).toBeLessThan(wide.indexOf("Command"));
- panel.handleInput("\u001B");
- expect(stripVTControlCharacters(panel.render(60).join("\n"))).toContain(
- "short summary...",
- );
- });
- it("pages through long details and returns to the selected task after resize", async () => {
- const terminal = new VirtualTerminal(60, 16);
- const tui = new TuiAltScreen(terminal);
- const task = {
- ...backgroundTasks()[1]!,
- command: `${Array.from({ length: 50 }, (_, i) => `echo line-${i}`).join("\n")}\necho command-end`,
- };
- const panel = new TuiBackgroundWorkPanel({
- agentTeam: teamSnapshot,
- backgroundTasks: () => [task],
- activeSessionId: () => "root",
- onOpenAgent: vi.fn(),
- onCancel: vi.fn(),
- requestRender: () => tui.requestRender(),
- });
- tui.setLayoutRoot(panel.layoutRoot);
- tui.setFocus(panel);
- tui.start();
- try {
- await terminal.waitForRender();
- terminal.sendInput("\u001B[B");
- terminal.sendInput("\r");
- await terminal.waitForRender();
- expect(terminal.getViewport().join("\n")).toContain("exit code 1");
- for (let i = 0; i < 15; i++) {
- terminal.sendInput("\u001B[6~");
- await terminal.waitForRender();
- }
- expect(terminal.getViewport().join("\n")).toContain("command-end");
- terminal.resize(40, 12);
- await terminal.waitForRender();
- for (let i = 0; i < 15; i++) {
- terminal.sendInput("\u001B[6~");
- await terminal.waitForRender();
- }
- expect(terminal.getViewport().join("\n")).toContain("command-end");
- terminal.sendInput("\u001B[5~");
- await terminal.waitForRender();
- expect(terminal.getViewport().join("\n")).not.toContain("command-end");
- terminal.sendInput("\u001B");
- await terminal.waitForRender();
- expect(terminal.getViewport().join("\n")).toContain("[Bash] build docs");
- terminal.sendInput("\r");
- await terminal.waitForRender();
- expect(terminal.getViewport().join("\n")).toContain("exit code 1");
- } finally {
- tui.stop();
- }
- });
- it("labels legacy tasks without a full command as descriptions", () => {
- const panel = new TuiBackgroundWorkPanel({
- agentTeam: teamSnapshot,
- backgroundTasks,
- activeSessionId: () => "root",
- onOpenAgent: vi.fn(),
- onCancel: vi.fn(),
- requestRender: vi.fn(),
- });
- panel.handleInput("\u001B[B");
- panel.handleInput("\r");
- const text = stripVTControlCharacters(panel.render(80).join("\n"));
- expect(text).toContain("Description");
- expect(text).toContain("pnpm typecheck");
- expect(text).not.toContain("Command");
- });
- it("closes from the list, keeps detail escape local, and respects narrow widths", () => {
- const onCancel = vi.fn();
- const panel = new TuiBackgroundWorkPanel({
- agentTeam: teamSnapshot,
- backgroundTasks,
- activeSessionId: () => "root",
- onOpenAgent: vi.fn(),
- onCancel,
- requestRender: vi.fn(),
- });
- for (const width of [1, 8, 24, 40]) {
- expect(
- panel.render(width).every((line) => visibleWidth(line) <= width),
- ).toBe(true);
- }
- panel.handleInput("\u001B[B");
- panel.handleInput("\r");
- panel.handleInput("\u001B");
- expect(onCancel).not.toHaveBeenCalled();
- panel.handleInput("\u001B");
- expect(onCancel).toHaveBeenCalledOnce();
- });
- it("keeps a selected task visible and stable when Agent rows are inserted ahead of it", () => {
- let snapshot = teamSnapshot();
- const panel = new TuiBackgroundWorkPanel({
- agentTeam: () => snapshot,
- backgroundTasks,
- activeSessionId: () => "root",
- onOpenAgent: vi.fn(),
- onCancel: vi.fn(),
- requestRender: vi.fn(),
- });
- const viewport = (
- panel as unknown as {
- bodyViewport: {
- readonly scrollTop: number;
- updateLayout(
- contentHeight: number,
- viewportHeight: number,
- requestRender: () => void,
- ): void;
- };
- }
- ).bodyViewport;
- panel.render(80);
- panel.handleInput("\u001B[B");
- panel.handleInput("\u001B[B");
- viewport.updateLayout(3, 1, vi.fn());
- expect(viewport.scrollTop).toBe(2);
- snapshot = {
- ...snapshot,
- summary: { ...snapshot.summary, total: 4, running: 4 },
- members: [
- ...snapshot.members,
- ...["planner", "builder", "verifier"].map((sessionId) => ({
- ...snapshot.members[0]!,
- sessionId,
- agentName: sessionId,
- })),
- ],
- };
- panel.render(80);
- viewport.updateLayout(6, 1, vi.fn());
- expect(viewport.scrollTop).toBe(5);
- panel.handleInput("\r");
- expect(stripVTControlCharacters(panel.render(80).join("\n"))).toContain(
- "Background task · Failed",
- );
- });
- });
|