|
| 1 | +import { EventBus } from "@devagent/runtime"; |
| 2 | +import React, { useEffect, useMemo } from "react"; |
| 3 | +import { afterEach, describe, expect, it } from "vitest"; |
| 4 | + |
| 5 | +import { TranscriptView } from "./App.js"; |
| 6 | +import { |
| 7 | + cleanupRenderedInstances, |
| 8 | + renderForTest, |
| 9 | + settle, |
| 10 | + stripAnsi, |
| 11 | + waitForRenders, |
| 12 | +} from "./App.test-utils.js"; |
| 13 | +import { SingleShotApp } from "./SingleShotApp.js"; |
| 14 | +import { useAgentLog } from "./useAgentLog.js"; |
| 15 | +import { makeTurnSummaryPart } from "../transcript-presenter.js"; |
| 16 | + |
| 17 | +afterEach(cleanupRenderedInstances); |
| 18 | + |
| 19 | +function emitTool(bus: EventBus, name: string, callId: string, durationMs: number): void { |
| 20 | + bus.emit("tool:before", { |
| 21 | + name, |
| 22 | + params: { pattern: "." }, |
| 23 | + callId, |
| 24 | + }); |
| 25 | + emitToolAfter(bus, name, callId, durationMs); |
| 26 | +} |
| 27 | + |
| 28 | +function emitToolAfter(bus: EventBus, name: string, callId: string, durationMs: number): void { |
| 29 | + bus.emit("tool:after", { |
| 30 | + name, |
| 31 | + callId, |
| 32 | + durationMs, |
| 33 | + result: { |
| 34 | + success: true, |
| 35 | + output: "1 file(s) found", |
| 36 | + error: null, |
| 37 | + artifacts: [], |
| 38 | + }, |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +function VisualHarness( |
| 43 | + { mode, onSpinnerMessage }: { |
| 44 | + readonly mode: "grouped" | "sequential"; |
| 45 | + readonly onSpinnerMessage?: (message: string | undefined) => void; |
| 46 | + }, |
| 47 | +): React.ReactElement { |
| 48 | + const bus = useMemo(() => new EventBus(), []); |
| 49 | + const { transcriptNodes, spinnerMessage, startTurn, completeTurn, nextId } = useAgentLog({ bus, model: "test-model" }); |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + onSpinnerMessage?.(spinnerMessage); |
| 53 | + }, [onSpinnerMessage, spinnerMessage]); |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + startTurn(nextId("turn"), mode === "grouped" ? "Check LSP" : "Find twice", Date.now()); |
| 57 | + if (mode === "grouped") { |
| 58 | + bus.emit("tool:before", { name: "lsp", params: { operation: "diagnostics", path: "src/tmp.ts" }, callId: "call-1" }); |
| 59 | + bus.emit("tool:before", { name: "lsp", params: { operation: "symbols", path: "src/tmp.ts" }, callId: "call-2" }); |
| 60 | + emitToolAfter(bus, "lsp", "call-1", 1); |
| 61 | + emitToolAfter(bus, "lsp", "call-2", 9); |
| 62 | + } else { |
| 63 | + emitTool(bus, "find_files", "call-1", 50); |
| 64 | + emitTool(bus, "find_files", "call-2", 52); |
| 65 | + } |
| 66 | + completeTurn(nextId("summary"), makeTurnSummaryPart({ iterations: 1, toolCalls: 2, cost: 0, elapsedMs: 120 })); |
| 67 | + }, [bus, completeTurn, mode, nextId, startTurn]); |
| 68 | + |
| 69 | + return React.createElement(TranscriptView, { showWelcome: false, transcriptNodes, model: "test-model" }); |
| 70 | +} |
| 71 | + |
| 72 | +describe("TUI visual regressions", () => { |
| 73 | + it("flushes grouped tool rows and clears the active tool label", async () => { |
| 74 | + const spinnerMessages: Array<string | undefined> = []; |
| 75 | + const view = renderForTest(React.createElement(VisualHarness, { |
| 76 | + mode: "grouped", |
| 77 | + onSpinnerMessage: (message) => spinnerMessages.push(message), |
| 78 | + })); |
| 79 | + |
| 80 | + await settle(); |
| 81 | + |
| 82 | + const plain = stripAnsi(view.stdout.readAll()); |
| 83 | + expect(plain).toContain("✓ lsp ×2"); |
| 84 | + expect(spinnerMessages.at(-1)).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("does not add grouped summaries for sequential same-name tools", async () => { |
| 88 | + const view = renderForTest(React.createElement(VisualHarness, { mode: "sequential" })); |
| 89 | + |
| 90 | + await settle(); |
| 91 | + |
| 92 | + const plain = stripAnsi(view.stdout.readAll()); |
| 93 | + expect(plain).toContain("✓ find_files (50ms)"); |
| 94 | + expect(plain).toContain("✓ find_files (52ms)"); |
| 95 | + expect(plain).not.toContain("✓ find_files ×2"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("finishes single-shot turns after tool-event rerenders", async () => { |
| 99 | + const bus = new EventBus(); |
| 100 | + let finalOutput: string | null = null; |
| 101 | + const view = renderForTest(React.createElement(SingleShotApp, { |
| 102 | + bus, |
| 103 | + query: "single shot tools", |
| 104 | + model: "test-model", |
| 105 | + onFinalOutput: (text) => { finalOutput = text; }, |
| 106 | + onQuery: () => new Promise((resolve) => { |
| 107 | + emitTool(bus, "lsp", "call-lsp-1", 5); |
| 108 | + setTimeout(() => resolve({ |
| 109 | + iterations: 1, |
| 110 | + toolCalls: 1, |
| 111 | + lastText: "LSP finished.", |
| 112 | + status: "success", |
| 113 | + }), 0); |
| 114 | + }), |
| 115 | + }), { columns: 180 }); |
| 116 | + |
| 117 | + await waitForRenders(); |
| 118 | + |
| 119 | + const output = stripAnsi(view.stdout.readAll()); |
| 120 | + expect(output).toContain("╭─ completed single shot tools"); |
| 121 | + expect(output).toContain("LSP finished."); |
| 122 | + expect(finalOutput).toBe("LSP finished."); |
| 123 | + }); |
| 124 | +}); |
0 commit comments