@@ -0,0 +1,498 @@
1+import { afterEach, describe, expect, test } from "bun:test";
2+import type { TurnEvent } from "../llm-api/types.ts";
3+import { renderError, renderUserMessage } from "./output.ts";
4+import { Spinner } from "./spinner.ts";
5+import { renderTurn } from "./stream-render.ts";
6+import {
7+ captureStdout,
8+ getCapturedStdout,
9+ restoreStdout,
10+ simulateTerminal,
11+ stripAnsi,
12+} from "./test-helpers.ts";
13+import { renderToolCall, renderToolResult } from "./tool-render.ts";
14+
15+afterEach(() => {
16+ restoreStdout();
17+});
18+
19+function eventsFrom(events: TurnEvent[]): AsyncIterable<TurnEvent> {
20+ return (async function* () {
21+ for (const event of events) {
22+ yield event;
23+ }
24+ })();
25+}
26+
27+function done(): TurnEvent {
28+ return {
29+ type: "turn-complete",
30+ inputTokens: 10,
31+ outputTokens: 20,
32+ contextTokens: 100,
33+ messages: [],
34+ };
35+}
36+
37+function shellResult(opts: {
38+ stdout?: string;
39+ stderr?: string;
40+ exitCode?: number;
41+ success?: boolean;
42+}) {
43+ return {
44+ stdout: opts.stdout ?? "",
45+ stderr: opts.stderr ?? "",
46+ exitCode: opts.exitCode ?? 0,
47+ success: opts.success ?? true,
48+ timedOut: false,
49+ };
50+}
51+
52+// ─── Visual hierarchy documentation ───────────────────────────────────────────
53+//
54+// The output hierarchy from the user's perspective should be:
55+//
56+// [0-indent] › user message (green ›)
57+// [0-indent] · reasoning (dim · header)
58+// [2-space] reasoning body text (dim italic)
59+// [0-indent] ◆ assistant reply (cyan ◆)
60+// [2-space] $ shell command (dim $, tool call)
61+// [2-space] ⇢ subagent prompt (cyan ⇢, tool call)
62+// [4-space] done exit 0 · ... (tool result badge)
63+// [4-space] stderr (N lines) (tool result preview label)
64+// [4-space] │ preview line (tool result preview content)
65+// [0-indent] · info message (dim ·)
66+// [0-indent] ✖ error message (red ✖)
67+// [2-space] hint text (dim hint)
68+// [0-indent] · context pruned ... (dim ·)
69+//
70+// Between blocks (reasoning→text, text→tool, tool→reasoning, etc.):
71+// A single blank line separates visually distinct blocks.
72+
73+describe("UI audit: output hierarchy", () => {
74+ describe("user message rendering", () => {
75+ test("single-line message has › prefix at column 0", () => {
76+ captureStdout();
77+ renderUserMessage("hello world");
78+ expect(stripAnsi(getCapturedStdout())).toBe("› hello world\n");
79+ });
80+
81+ test("multi-line message indents continuations with 2 spaces", () => {
82+ captureStdout();
83+ renderUserMessage("line 1\nline 2\nline 3");
84+ expect(stripAnsi(getCapturedStdout())).toBe(
85+ "› line 1\n line 2\n line 3\n",
86+ );
87+ });
88+ });
89+
90+ describe("tool call rendering", () => {
91+ test("shell call is indented 2 spaces with $ glyph", () => {
92+ captureStdout();
93+ renderToolCall("shell", { command: "echo hi" });
94+ expect(stripAnsi(getCapturedStdout())).toBe(" $ echo hi\n");
95+ });
96+
97+ test("subagent call is indented 2 spaces with ⇢ glyph", () => {
98+ captureStdout();
99+ renderToolCall("subagent", {
100+ prompt: "Review code",
101+ agentName: "reviewer",
102+ });
103+ const plain = stripAnsi(getCapturedStdout());
104+ expect(plain).toStartWith(" ⇢");
105+ expect(plain).toContain("[@reviewer]");
106+ expect(plain).toContain("Review code");
107+ });
108+
109+ test("readSkill call is indented 2 spaces with ← glyph", () => {
110+ captureStdout();
111+ renderToolCall("readSkill", { name: "deploy" });
112+ const plain = stripAnsi(getCapturedStdout());
113+ expect(plain).toStartWith(" ←");
114+ expect(plain).toContain("read skill");
115+ expect(plain).toContain("deploy");
116+ });
117+
118+ test("MCP tool call is indented 2 spaces with ⚙ glyph", () => {
119+ captureStdout();
120+ renderToolCall("mcp_myserver_do_thing", { arg: "val" });
121+ const plain = stripAnsi(getCapturedStdout());
122+ expect(plain).toStartWith(" ⚙");
123+ });
124+ });
125+
126+ describe("tool result rendering", () => {
127+ test("successful shell result badge is indented 4 spaces", () => {
128+ captureStdout();
129+ renderToolResult("shell", shellResult({ stdout: "ok" }), false);
130+ const plain = stripAnsi(getCapturedStdout());
131+ expect(plain).toMatch(/^ {4}done/m);
132+ });
133+
134+ test("failed shell result badge is indented 4 spaces", () => {
135+ captureStdout();
136+ renderToolResult(
137+ "shell",
138+ shellResult({ stderr: "fail", exitCode: 1, success: false }),
139+ false,
140+ );
141+ const plain = stripAnsi(getCapturedStdout());
142+ expect(plain).toMatch(/^ {4}error/m);
143+ });
144+
145+ test("shell stderr preview lines are indented 4 spaces with │", () => {
146+ captureStdout();
147+ renderToolResult(
148+ "shell",
149+ shellResult({ stderr: "bad\nstuff", exitCode: 1, success: false }),
150+ false,
151+ );
152+ const plain = stripAnsi(getCapturedStdout());
153+ expect(plain).toContain(" │ bad");
154+ expect(plain).toContain(" │ stuff");
155+ });
156+
157+ test("shell stdout preview lines are indented 4 spaces with │", () => {
158+ captureStdout();
159+ renderToolResult("shell", shellResult({ stdout: "line1\nline2" }), false);
160+ const plain = stripAnsi(getCapturedStdout());
161+ expect(plain).toContain(" │ line1");
162+ expect(plain).toContain(" │ line2");
163+ });
164+
165+ test("tool error badge is indented 4 spaces", () => {
166+ captureStdout();
167+ renderToolResult("shell", "something broke", true);
168+ const plain = stripAnsi(getCapturedStdout());
169+ expect(plain).toMatch(/^ {4}✖/m);
170+ });
171+
172+ test("subagent result is indented 4 spaces", () => {
173+ captureStdout();
174+ renderToolResult(
175+ "subagent",
176+ { inputTokens: 100, outputTokens: 50, agentName: "reviewer" },
177+ false,
178+ );
179+ const plain = stripAnsi(getCapturedStdout());
180+ expect(plain).toMatch(/^ {4}⇢/m);
181+ });
182+ });
183+
184+ describe("error rendering", () => {
185+ test("error headline is at column 0 with ✖ glyph", () => {
186+ captureStdout();
187+ renderError("something went wrong");
188+ const plain = stripAnsi(getCapturedStdout());
189+ expect(plain).toMatch(/^✖ something went wrong/m);
190+ });
191+ });
192+});
193+
194+describe("UI audit: full turn scenarios", () => {
195+ test("simple text reply: reasoning → reply with blank line separator", async () => {
196+ captureStdout();
197+
198+ await renderTurn(
199+ eventsFrom([
200+ { type: "reasoning-delta", delta: "Let me think" },
201+ { type: "text-delta", delta: "Here is the answer." },
202+ done(),
203+ ]),
204+ new Spinner(),
205+ );
206+
207+ const plain = simulateTerminal(getCapturedStdout());
208+ expect(plain).toBe(
209+ "· reasoning\n Let me think\n\n◆ Here is the answer.\n",
210+ );
211+ });
212+
213+ test("tool use: tool-call → tool-result → reply", async () => {
214+ captureStdout();
215+
216+ await renderTurn(
217+ eventsFrom([
218+ {
219+ type: "tool-call-start",
220+ toolName: "shell",
221+ toolCallId: "t1",
222+ args: { command: "ls" },
223+ },
224+ {
225+ type: "tool-result",
226+ toolName: "shell",
227+ toolCallId: "t1",
228+ isError: false,
229+ result: shellResult({ stdout: "file.txt" }),
230+ },
231+ { type: "text-delta", delta: "Found file.txt" },
232+ done(),
233+ ]),
234+ new Spinner(),
235+ );
236+
237+ const plain = simulateTerminal(getCapturedStdout());
238+ // Tool call at 2-space indent
239+ expect(plain).toContain(" $ ls");
240+ // Tool result at 4-space indent
241+ expect(plain).toMatch(/^ {4}done/m);
242+ // Reply at 0-indent with blank line before
243+ expect(plain).toContain("\n\n◆ Found file.txt");
244+ });
245+
246+ test("multiple tools: each tool-call/result pair is visually grouped", async () => {
247+ captureStdout();
248+
249+ await renderTurn(
250+ eventsFrom([
251+ {
252+ type: "tool-call-start",
253+ toolName: "shell",
254+ toolCallId: "t1",
255+ args: { command: "cat a.txt" },
256+ },
257+ {
258+ type: "tool-result",
259+ toolName: "shell",
260+ toolCallId: "t1",
261+ isError: false,
262+ result: shellResult({ stdout: "hello" }),
263+ },
264+ {
265+ type: "tool-call-start",
266+ toolName: "shell",
267+ toolCallId: "t2",
268+ args: { command: "cat b.txt" },
269+ },
270+ {
271+ type: "tool-result",
272+ toolName: "shell",
273+ toolCallId: "t2",
274+ isError: false,
275+ result: shellResult({ stdout: "world" }),
276+ },
277+ done(),
278+ ]),
279+ new Spinner(),
280+ );
281+
282+ const plain = simulateTerminal(getCapturedStdout());
283+ // Both tool calls present
284+ expect(plain).toContain(" $ cat a.txt");
285+ expect(plain).toContain(" $ cat b.txt");
286+ // Blank line between first result and second call
287+ const lines = plain.split("\n");
288+ const firstResultIdx = lines.findIndex((l) =>
289+ l.match(/^ {4}done.*out: hello/),
290+ );
291+ const secondCallIdx = lines.findIndex((l) => l.includes("$ cat b.txt"));
292+ expect(firstResultIdx).toBeGreaterThan(-1);
293+ expect(secondCallIdx).toBeGreaterThan(firstResultIdx);
294+ // There should be a blank line between the first result and second tool call
295+ const between = lines.slice(firstResultIdx + 1, secondCallIdx);
296+ expect(between.some((l) => l.trim() === "")).toBe(true);
297+ });
298+
299+ test("reasoning → tool → reasoning → reply: two reasoning blocks", async () => {
300+ captureStdout();
301+
302+ await renderTurn(
303+ eventsFrom([
304+ { type: "reasoning-delta", delta: "plan A" },
305+ {
306+ type: "tool-call-start",
307+ toolName: "shell",
308+ toolCallId: "t1",
309+ args: { command: "echo test" },
310+ },
311+ {
312+ type: "tool-result",
313+ toolName: "shell",
314+ toolCallId: "t1",
315+ isError: false,
316+ result: shellResult({ stdout: "test" }),
317+ },
318+ { type: "reasoning-delta", delta: "plan B" },
319+ { type: "text-delta", delta: "done" },
320+ done(),
321+ ]),
322+ new Spinner(),
323+ );
324+
325+ const plain = simulateTerminal(getCapturedStdout());
326+ // Two separate reasoning blocks
327+ const reasoningCount = (plain.match(/· reasoning/g) ?? []).length;
328+ expect(reasoningCount).toBe(2);
329+ expect(plain).toContain("plan A");
330+ expect(plain).toContain("plan B");
331+ });
332+
333+ test("context-pruned event renders at 0-indent with · glyph", async () => {
334+ captureStdout();
335+
336+ await renderTurn(
337+ eventsFrom([
338+ {
339+ type: "context-pruned",
340+ mode: "balanced",
341+ beforeMessageCount: 100,
342+ afterMessageCount: 80,
343+ removedMessageCount: 20,
344+ beforeTotalBytes: 30000,
345+ afterTotalBytes: 20000,
346+ removedBytes: 10000,
347+ },
348+ { type: "text-delta", delta: "continuing" },
349+ done(),
350+ ]),
351+ new Spinner(),
352+ );
353+
354+ const plain = simulateTerminal(getCapturedStdout());
355+ expect(plain).toContain("· context pruned");
356+ expect(plain).toContain("balanced");
357+ expect(plain).toContain("–20 messages");
358+ });
359+
360+ test("text reply with no reasoning: just ◆ prefix", async () => {
361+ captureStdout();
362+
363+ await renderTurn(
364+ eventsFrom([{ type: "text-delta", delta: "Hello!" }, done()]),
365+ new Spinner(),
366+ );
367+
368+ const plain = simulateTerminal(getCapturedStdout());
369+ expect(plain).toBe("◆ Hello!\n");
370+ });
371+
372+ test("tool error shows ✖ at 4-space indent", async () => {
373+ captureStdout();
374+
375+ await renderTurn(
376+ eventsFrom([
377+ {
378+ type: "tool-call-start",
379+ toolName: "shell",
380+ toolCallId: "t1",
381+ args: { command: "bad-cmd" },
Cut short: this commit's diff is over 1000 lines.