@@ -1,4 +1,11 @@
11 import { afterEach, describe, expect, test } from "bun:test";
2+import {
3+ cel,
4+ MockTerminal,
5+ measureContentHeight,
6+ Text,
7+ VStack,
8+} from "@cel-tui/core";
29 import type { Node } from "@cel-tui/types";
310 import {
411 fauxAssistantMessage,
@@ -10,17 +17,17 @@ import { DEFAULT_THEME } from "../theme.ts";
1017 import {
1118 buildConversationLogNodes,
1219 type PendingToolResult,
13- previewToolRenderLines,
1420 renderAssistantMessage,
1521 renderToolResult,
1622 resetConversationRenderCache,
17- type ToolRenderLine,
1823 } from "./conversation.ts";
1924
25+const PREVIEW_WIDTH = 32;
2026 const RENDER_OPTS = {
2127 showReasoning: false,
2228 verbose: false,
2329 theme: DEFAULT_THEME,
30+ previewWidth: PREVIEW_WIDTH,
2431 };
2532
2633 function collectText(node: Node | null): string[] {
@@ -36,28 +43,98 @@ function collectText(node: Node | null): string[] {
3643 return node.children.flatMap((child) => collectText(child));
3744 }
3845
46+function measureRenderedHeight(node: Node | null, width: number): number {
47+ if (!node) {
48+ return 0;
49+ }
50+ return measureContentHeight(VStack({}, [node]), { width });
51+}
52+
53+async function waitForCelRender(): Promise<void> {
54+ await new Promise((resolve) => setTimeout(resolve, 10));
55+}
56+
57+async function renderBufferRows(
58+ node: Node | null,
59+ cols = PREVIEW_WIDTH,
60+ rows = 24,
61+): Promise<Array<{ text: string; fgColors: Array<string | null> }>> {
62+ if (!node) {
63+ return [];
64+ }
65+
66+ const terminal = new MockTerminal(cols, rows);
67+ cel.init(terminal);
68+ cel.viewport(() => VStack({ width: cols, height: rows }, [node]));
69+ await waitForCelRender();
70+
71+ const buffer = cel._getBuffer();
72+ if (!buffer) {
73+ throw new Error("Expected cel-tui to produce a render buffer");
74+ }
75+
76+ const snapshot: Array<{ text: string; fgColors: Array<string | null> }> = [];
77+ for (let y = 0; y < rows; y++) {
78+ let text = "";
79+ const fgColors: Array<string | null> = [];
80+ for (let x = 0; x < cols; x++) {
81+ const cell = buffer.get(x, y);
82+ text += cell.char;
83+ fgColors.push(cell.fgColor);
84+ }
85+ snapshot.push({ text, fgColors });
86+ }
87+
88+ cel.stop();
89+ return snapshot;
90+}
91+
92+async function renderVisibleText(
93+ node: Node | null,
94+ cols = PREVIEW_WIDTH,
95+ rows = 24,
96+): Promise<string[]> {
97+ const snapshot = await renderBufferRows(node, cols, rows);
98+ const lines: string[] = [];
99+
100+ for (const row of snapshot) {
101+ const normalized = row.text.trim().replace(/^│\s*/, "");
102+ if (normalized !== "") {
103+ lines.push(normalized);
104+ }
105+ }
106+
107+ return lines;
108+}
109+
39110 afterEach(() => {
40111 resetConversationRenderCache();
112+ cel.stop();
41113 });
42114
43115 describe("ui/conversation", () => {
44116 test("renderAssistantMessage preserves visible paragraph order in streamed markdown", () => {
117+ // Arrange
45118 const message = fauxAssistantMessage("First paragraph\n\nSecond paragraph");
46119
120+ // Act
47121 const text = collectText(renderAssistantMessage(message, RENDER_OPTS));
48122 const firstParagraphIndex = text.indexOf("First paragraph");
49123 const secondParagraphIndex = text.indexOf("Second paragraph");
50124
125+ // Assert
51126 expect(firstParagraphIndex).toBeGreaterThanOrEqual(0);
52127 expect(secondParagraphIndex).toBeGreaterThan(firstParagraphIndex);
53128 });
54129
55- test("renderAssistantMessage shows thinking blocks when reasoning is enabled", () => {
130+ test("renderAssistantMessage with reasoning enabled shows thinking blocks", () => {
131+ // Arrange
56132 const message = fauxAssistantMessage([
57133 fauxThinking("I should inspect the tests first."),
58134 fauxText("Done."),
59135 ]);
60136
137+ // Act
61138 const text = collectText(
62139 renderAssistantMessage(message, {
63140 ...RENDER_OPTS,
@@ -65,29 +142,70 @@ describe("ui/conversation", () => {
65142 }),
66143 );
67144
145+ // Assert
68146 expect(text).toContain("I should inspect the tests first.");
69147 expect(text).toContain("Done.");
70148 });
71149
72- test("renderAssistantMessage shows a thinking line-count placeholder when reasoning is hidden", () => {
150+ test("renderAssistantMessage with reasoning hidden shows a thinking line-count placeholder", () => {
151+ // Arrange
73152 const message = fauxAssistantMessage([
74153 fauxThinking("line one\nline two\nline three"),
75154 fauxText("Done."),
76155 ]);
77156
78- const node = renderAssistantMessage(message, {
79- ...RENDER_OPTS,
80- showReasoning: false,
81- });
82- const text = collectText(node);
157+ // Act
158+ const text = collectText(
159+ renderAssistantMessage(message, {
160+ ...RENDER_OPTS,
161+ showReasoning: false,
162+ }),
163+ );
83164
165+ // Assert
84166 expect(text).toContain("Thinking... 3 lines.");
85167 expect(text).toContain("Done.");
86168 expect(text).not.toContain("line one");
87169 expect(text).not.toContain("line two");
88170 });
89171
90- test("buildConversationLogNodes keeps tool call args and streamed shell output append-only", () => {
172+ test("renderAssistantMessage with mixed top-level blocks keeps a single blank line between sections", () => {
173+ // Arrange
174+ const message = fauxAssistantMessage([
175+ fauxThinking("Plan first."),
176+ fauxText("Done."),
177+ fauxToolCall("shell", { command: "echo hi" }, { id: "tool-1" }),
178+ ]);
179+
180+ // Act
181+ const height = measureRenderedHeight(
182+ renderAssistantMessage(message, {
183+ ...RENDER_OPTS,
184+ showReasoning: true,
185+ }),
186+ PREVIEW_WIDTH,
187+ );
188+
189+ // Assert
190+ expect(height).toBe(6);
191+ });
192+
193+ test("renderAssistantMessage keeps markdown paragraph spacing inside a single text block", () => {
194+ // Arrange
195+ const message = fauxAssistantMessage("First paragraph\n\nSecond paragraph");
196+
197+ // Act
198+ const height = measureRenderedHeight(
199+ renderAssistantMessage(message, RENDER_OPTS),
200+ PREVIEW_WIDTH,
201+ );
202+
203+ // Assert
204+ expect(height).toBe(3);
205+ });
206+
207+ test("buildConversationLogNodes with a pending shell result keeps the streamed call and result append-only", () => {
208+ // Arrange
91209 const pendingToolResults: PendingToolResult[] = [
92210 {
93211 toolCallId: "tool-1",
@@ -97,6 +215,7 @@ describe("ui/conversation", () => {
97215 },
98216 ];
99217
218+ // Act
100219 const nodes = buildConversationLogNodes(
101220 {
102221 messages: [
@@ -114,6 +233,8 @@ describe("ui/conversation", () => {
114233 content: [],
115234 pendingToolResults,
116235 },
236+ 0,
237+ PREVIEW_WIDTH,
117238 );
118239 const text = collectText({
119240 type: "vstack",
@@ -121,13 +242,17 @@ describe("ui/conversation", () => {
121242 children: nodes,
122243 });
123244
245+ // Assert
124246 expect(text).toContain("Working...");
125- expect(text.filter((line) => line === "$ echo hi")).toHaveLength(1);
247+ expect(text.filter((line) => line === "[shell ->]")).toHaveLength(1);
248+ expect(text).toContain("echo hi");
249+ expect(text).toContain("[shell <-]");
126250 expect(text).toContain("partial output");
127251 expect(text).not.toContain("Exit code: 0");
128252 });
129253
130- test("buildConversationLogNodes keeps hidden tool call args available when rendering a sliced window", () => {
254+ test("buildConversationLogNodes with a sliced window keeps hidden tool-call args available for compact edit results", () => {
255+ // Arrange
131256 const nodes = buildConversationLogNodes(
132257 {
133258 messages: [
@@ -146,7 +271,7 @@ describe("ui/conversation", () => {
146271 role: "toolResult" as const,
147272 toolCallId: "tool-1",
148273 toolName: "edit",
149- content: [{ type: "text" as const, text: "Updated src/app.ts" }],
274+ content: [{ type: "text" as const, text: "Edited src/app.ts" }],
150275 isError: false,
151276 timestamp: Date.now(),
152277 },
@@ -161,6 +286,7 @@ describe("ui/conversation", () => {
161286 pendingToolResults: [],
162287 },
163288 1,
289+ PREVIEW_WIDTH,
164290 );
165291 const text = collectText({
166292 type: "vstack",
@@ -168,12 +294,15 @@ describe("ui/conversation", () => {
168294 children: nodes,
169295 });
170296
171- expect(text).toContain("@@ -1,1 +1,1 @@");
172- expect(text).toContain("-before");
173- expect(text).toContain("+after");
297+ // Assert
298+ expect(text).toContain("[edit <-]");
299+ expect(text).toContain("~ src/app.ts");
300+ expect(text).not.toContain("before");
301+ expect(text).not.toContain("after");
174302 });
175303
176- test("buildConversationLogNodes reuses cached committed nodes when the log is unchanged", () => {
304+ test("buildConversationLogNodes with unchanged state reuses cached committed nodes", () => {
305+ // Arrange
177306 const state = {
178307 messages: [fauxAssistantMessage("Committed response")],
179308 showReasoning: false,
@@ -186,13 +315,21 @@ describe("ui/conversation", () => {
186315 pendingToolResults: [],
187316 };
188317
189- const first = buildConversationLogNodes(state, streaming);
190- const second = buildConversationLogNodes(state, streaming);
318+ // Act
319+ const first = buildConversationLogNodes(state, streaming, 0, PREVIEW_WIDTH);
320+ const second = buildConversationLogNodes(
321+ state,
322+ streaming,
323+ 0,
324+ PREVIEW_WIDTH,
325+ );
191326
327+ // Assert
192328 expect(second).toBe(first);
193329 });
194330
195- test("buildConversationLogNodes reuses the committed prefix when only the streaming tail changes", () => {
331+ test("buildConversationLogNodes with only a new streaming tail reuses the committed prefix", () => {
332+ // Arrange
196333 const state = {
197334 messages: [fauxAssistantMessage("Committed response")],
198335 showReasoning: false,
@@ -200,28 +337,41 @@ describe("ui/conversation", () => {
200337 theme: DEFAULT_THEME,
201338 };
202339
203- const committed = buildConversationLogNodes(state, {
204- isStreaming: false,
205- content: [],
206- pendingToolResults: [],
207- });
208- const withStreamingTail = buildConversationLogNodes(state, {
209- isStreaming: true,
210- content: [fauxText("Streaming tail")],
211- pendingToolResults: [],
212- });
340+ // Act
341+ const committed = buildConversationLogNodes(
342+ state,
343+ {
344+ isStreaming: false,
345+ content: [],
346+ pendingToolResults: [],
347+ },
348+ 0,
349+ PREVIEW_WIDTH,
350+ );
351+ const withStreamingTail = buildConversationLogNodes(
352+ state,
353+ {
354+ isStreaming: true,
355+ content: [fauxText("Streaming tail")],
356+ pendingToolResults: [],
357+ },
358+ 0,
359+ PREVIEW_WIDTH,
360+ );
213361 const text = collectText({
214362 type: "vstack",
215363 props: {},
216364 children: withStreamingTail,
217365 });
218366
367+ // Assert
219368 expect(withStreamingTail[0]).toBe(committed[0]);
220369 expect(text).toContain("Committed response");
221370 expect(text).toContain("Streaming tail");
222371 });
223372
224- test("buildConversationLogNodes rebuilds cached tool nodes when verbose mode changes", () => {
373+ test("buildConversationLogNodes when verbose mode changes rebuilds cached tool nodes", async () => {
374+ // Arrange
225375 const output = Array.from({ length: 25 }, (_, i) => `line ${i + 1}`).join(
226376 "\n",
227377 );
@@ -244,16 +394,22 @@ describe("ui/conversation", () => {
244394 theme: DEFAULT_THEME,
245395 };
246396
247- const previewNodes = buildConversationLogNodes(state, {
248- isStreaming: false,
249- content: [],
250- pendingToolResults: [],
251- });
252- const previewText = collectText({
253- type: "vstack",
254- props: {},
255- children: previewNodes,
256- });
397+ // Act
398+ const previewNodes = buildConversationLogNodes(
399+ state,
400+ {
401+ isStreaming: false,
402+ content: [],
403+ pendingToolResults: [],
404+ },
405+ 0,
406+ PREVIEW_WIDTH,
407+ );
408+ const previewText = await renderVisibleText(
409+ VStack({}, previewNodes),
410+ PREVIEW_WIDTH,
411+ 24,
412+ );
257413
258414 const verboseNodes = buildConversationLogNodes(
259415 { ...state, verbose: true },
@@ -262,13 +418,16 @@ describe("ui/conversation", () => {
262418 content: [],
263419 pendingToolResults: [],
264420 },
421+ 0,
422+ PREVIEW_WIDTH,
423+ );
424+ const verboseText = await renderVisibleText(
425+ VStack({}, verboseNodes),
426+ PREVIEW_WIDTH,
427+ 40,
265428 );
266- const verboseText = collectText({
267- type: "vstack",
268- props: {},
269- children: verboseNodes,
270- });
271429
430+ // Assert
272431 expect(previewNodes).not.toBe(verboseNodes);
273432 expect(previewText).toContain("line 18");
274433 expect(previewText).toContain("line 25");
@@ -276,314 +435,458 @@ describe("ui/conversation", () => {
276435 expect(previewText).not.toContain("line 17");
277436 expect(verboseText).toContain("line 17");
278437 expect(verboseText).toContain("line 25");
438+ expect(verboseText).not.toContain("And 17 lines more");
279439 });
280440
281- test("renderAssistantMessage shows in-progress thinking when reasoning is enabled", () => {
282- const node = renderAssistantMessage(
283- {
284- content: [fauxThinking("Reasoning in progress")],
285- },
286- {
441+ test("buildConversationLogNodes when preview width changes rebuilds cached tool nodes", () => {
442+ // Arrange
443+ const state = {
444+ messages: [
445+ fauxAssistantMessage([
446+ fauxToolCall(
447+ "shell",
448+ {
449+ command:
450+ "printf 'this is a very long wrapped line that depends on width'",
451+ },
452+ { id: "tool-1" },
453+ ),
454+ ]),
455+ ],
456+ showReasoning: false,
457+ verbose: false,
458+ theme: DEFAULT_THEME,
459+ };
460+ const streaming = {
461+ isStreaming: false,
462+ content: [],
463+ pendingToolResults: [],
464+ };
465+
466+ // Act
467+ const wide = buildConversationLogNodes(state, streaming, 0, 40);
468+ const narrow = buildConversationLogNodes(state, streaming, 0, 20);
469+
470+ // Assert
471+ expect(narrow).not.toBe(wide);
472+ });
473+
474+ test("renderAssistantMessage with in-progress reasoning visible shows thinking text", () => {
475+ // Arrange
476+ const assistant = {
477+ content: [fauxThinking("Reasoning in progress")],
478+ };
479+
480+ // Act
481+ const text = collectText(
482+ renderAssistantMessage(assistant, {
287483 ...RENDER_OPTS,
288484 showReasoning: true,
289- },
485+ }),
290486 );
291- const text = collectText(node);
292487
488+ // Assert
293489 expect(text).toContain("Reasoning in progress");
294490 });
295491
296- test("renderAssistantMessage shows a 1-line thinking placeholder for in-progress content", () => {
297- const node = renderAssistantMessage(
298- {
299- content: [fauxThinking("some thinking")],
300- },
301- {
492+ test("renderAssistantMessage with in-progress reasoning hidden shows a one-line placeholder", () => {
493+ // Arrange
494+ const assistant = {
495+ content: [fauxThinking("some thinking")],
496+ };
497+
498+ // Act
499+ const text = collectText(
500+ renderAssistantMessage(assistant, {
302501 ...RENDER_OPTS,
303502 showReasoning: false,
304- },
503+ }),
305504 );
306- const text = collectText(node);
307505
506+ // Assert
308507 expect(text).toContain("Thinking... 1 line.");
309508 });
310509
311- test("renderAssistantMessage shows streamed shell arguments semantically", () => {
312- const node = renderAssistantMessage(
313- {
314- content: [
315- fauxToolCall("shell", { command: "echo hi" }, { id: "tool-1" }),
316- ],
317- },
318- RENDER_OPTS,
319- );
320- const text = collectText(node);
510+ test("renderAssistantMessage for a shell tool call shows the command under the new header pill", () => {
511+ // Arrange
512+ const assistant = {
513+ content: [
514+ fauxToolCall("shell", { command: "echo hi" }, { id: "tool-1" }),
515+ ],
516+ };
321517
322- expect(text).toContain("$ echo hi");
518+ // Act
519+ const text = collectText(renderAssistantMessage(assistant, RENDER_OPTS));
520+
521+ // Assert
522+ expect(text).toContain("[shell ->]");
523+ expect(text).toContain("echo hi");
323524 expect(text).not.toContain('"command": "echo hi"');
324- expect(text).not.toContain("Preparing...");
325525 });
326526
327- test("renderAssistantMessage shows the last 8 lines of multi-line shell commands when verbose mode is off", () => {
328- const command = Array.from({ length: 10 }, (_, i) => `echo ${i + 1}`).join(
329- "\n",
330- );
Cut short: this commit's diff is over 1000 lines.