@@ -1,14 +1,16 @@
11 import { afterEach, describe, expect, test } from "bun:test";
2-import type { TurnEvent } from "../llm-api/types.ts";
32 import { renderError, renderUserMessage } from "./output.ts";
43 import { Spinner } from "./spinner.ts";
54 import { renderTurn } from "./stream-render.ts";
65 import {
76 captureStdout,
7+ eventsFrom,
88 getCapturedStdout,
99 restoreStdout,
10+ shellResult,
1011 simulateTerminal,
1112 stripAnsi,
13+ turnDone,
1214 } from "./test-helpers.ts";
1315 import { renderToolCall, renderToolResult } from "./tool-render.ts";
1416
@@ -16,39 +18,6 @@ afterEach(() => {
1618 restoreStdout();
1719 });
1820
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-
5221 // ─── Visual hierarchy documentation ───────────────────────────────────────────
5322 //
5423 // The output hierarchy from the user's perspective should be:
@@ -199,7 +168,7 @@ describe("UI audit: full turn scenarios", () => {
199168 eventsFrom([
200169 { type: "reasoning-delta", delta: "Let me think" },
201170 { type: "text-delta", delta: "Here is the answer." },
202- done(),
171+ turnDone(),
203172 ]),
204173 new Spinner(),
205174 );
@@ -229,21 +198,18 @@ describe("UI audit: full turn scenarios", () => {
229198 result: shellResult({ stdout: "file.txt" }),
230199 },
231200 { type: "text-delta", delta: "Found file.txt" },
232- done(),
201+ turnDone(),
233202 ]),
234203 new Spinner(),
235204 );
236205
237206 const plain = simulateTerminal(getCapturedStdout());
238- // Tool call at 2-space indent
239207 expect(plain).toContain(" $ ls");
240- // Tool result at 4-space indent
241208 expect(plain).toMatch(/^ {4}done/m);
242- // Reply at 0-indent with blank line before
243209 expect(plain).toContain("\n\n◆ Found file.txt");
244210 });
245211
246- test("multiple tools: each tool-call/result pair is visually grouped", async () => {
212+ test("multiple sequential tools: blank line between result and next call", async () => {
247213 captureStdout();
248214
249215 await renderTurn(
@@ -274,16 +240,12 @@ describe("UI audit: full turn scenarios", () => {
274240 isError: false,
275241 result: shellResult({ stdout: "world" }),
276242 },
277- done(),
243+ turnDone(),
278244 ]),
279245 new Spinner(),
280246 );
281247
282248 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
287249 const lines = plain.split("\n");
288250 const firstResultIdx = lines.findIndex((l) =>
289251 l.match(/^ {4}done.*out: hello/),
@@ -291,7 +253,6 @@ describe("UI audit: full turn scenarios", () => {
291253 const secondCallIdx = lines.findIndex((l) => l.includes("$ cat b.txt"));
292254 expect(firstResultIdx).toBeGreaterThan(-1);
293255 expect(secondCallIdx).toBeGreaterThan(firstResultIdx);
294- // There should be a blank line between the first result and second tool call
295256 const between = lines.slice(firstResultIdx + 1, secondCallIdx);
296257 expect(between.some((l) => l.trim() === "")).toBe(true);
297258 });
@@ -317,13 +278,12 @@ describe("UI audit: full turn scenarios", () => {
317278 },
318279 { type: "reasoning-delta", delta: "plan B" },
319280 { type: "text-delta", delta: "done" },
320- done(),
281+ turnDone(),
321282 ]),
322283 new Spinner(),
323284 );
324285
325286 const plain = simulateTerminal(getCapturedStdout());
326- // Two separate reasoning blocks
327287 const reasoningCount = (plain.match(/· reasoning/g) ?? []).length;
328288 expect(reasoningCount).toBe(2);
329289 expect(plain).toContain("plan A");
@@ -346,7 +306,7 @@ describe("UI audit: full turn scenarios", () => {
346306 removedBytes: 10000,
347307 },
348308 { type: "text-delta", delta: "continuing" },
349- done(),
309+ turnDone(),
350310 ]),
351311 new Spinner(),
352312 );
@@ -361,7 +321,7 @@ describe("UI audit: full turn scenarios", () => {
361321 captureStdout();
362322
363323 await renderTurn(
364- eventsFrom([{ type: "text-delta", delta: "Hello!" }, done()]),
324+ eventsFrom([{ type: "text-delta", delta: "Hello!" }, turnDone()]),
365325 new Spinner(),
366326 );
367327
@@ -387,7 +347,7 @@ describe("UI audit: full turn scenarios", () => {
387347 isError: true,
388348 result: "command not found",
389349 },
390- done(),
350+ turnDone(),
391351 ]),
392352 new Spinner(),
393353 );
@@ -400,14 +360,298 @@ describe("UI audit: full turn scenarios", () => {
400360 test("empty turn (no text, no tools) still outputs a newline", async () => {
401361 captureStdout();
402362
403- await renderTurn(eventsFrom([done()]), new Spinner());
363+ await renderTurn(eventsFrom([turnDone()]), new Spinner());
404364
405365 const raw = getCapturedStdout();
406366 expect(raw).toContain("\n");
407367 });
408368 });
409369
410-describe("UI audit: spinner does not leak into output", () => {
370+describe("UI audit: parallel tool calls", () => {
371+ test("two parallel shell calls render both calls before any results", async () => {
372+ captureStdout();
373+
374+ await renderTurn(
375+ eventsFrom([
376+ {
377+ type: "tool-call-start",
378+ toolName: "shell",
379+ toolCallId: "p1",
380+ args: { command: "echo alpha" },
381+ },
382+ {
383+ type: "tool-call-start",
384+ toolName: "shell",
385+ toolCallId: "p2",
386+ args: { command: "echo bravo" },
387+ },
388+ {
389+ type: "tool-result",
390+ toolName: "shell",
391+ toolCallId: "p1",
392+ isError: false,
393+ result: shellResult({ stdout: "alpha" }),
394+ },
395+ {
396+ type: "tool-result",
397+ toolName: "shell",
398+ toolCallId: "p2",
399+ isError: false,
400+ result: shellResult({ stdout: "bravo" }),
401+ },
402+ { type: "text-delta", delta: "Both done." },
403+ turnDone(),
404+ ]),
405+ new Spinner(),
406+ );
407+
408+ const plain = simulateTerminal(getCapturedStdout());
409+ const lines = plain.split("\n");
410+
411+ // Both tool calls appear before the first result
412+ const call1 = lines.findIndex((l) => l.includes("$ echo alpha"));
413+ const call2 = lines.findIndex((l) => l.includes("$ echo bravo"));
414+ const result1 = lines.findIndex((l) => l.match(/^ {4}done.*alpha/));
415+ const result2 = lines.findIndex((l) => l.match(/^ {4}done.*bravo/));
416+
417+ expect(call1).toBeGreaterThan(-1);
418+ expect(call2).toBeGreaterThan(-1);
419+ expect(result1).toBeGreaterThan(-1);
420+ expect(result2).toBeGreaterThan(-1);
421+
422+ // Calls come before results
423+ expect(call1).toBeLessThan(result1);
424+ expect(call2).toBeLessThan(result2);
425+
426+ // Both calls appear before first result
427+ expect(call2).toBeLessThan(result1);
428+
429+ // Reply follows
430+ expect(plain).toContain("◆ Both done.");
431+ });
432+
433+ test("three parallel mixed tools: shell + subagent + readSkill", async () => {
434+ captureStdout();
435+
436+ await renderTurn(
437+ eventsFrom([
438+ {
439+ type: "tool-call-start",
440+ toolName: "shell",
441+ toolCallId: "m1",
442+ args: { command: "ls src" },
443+ },
444+ {
445+ type: "tool-call-start",
446+ toolName: "subagent",
447+ toolCallId: "m2",
448+ args: { prompt: "Analyze code" },
449+ },
450+ {
451+ type: "tool-call-start",
452+ toolName: "readSkill",
453+ toolCallId: "m3",
454+ args: { name: "deploy" },
455+ },
456+ {
457+ type: "tool-result",
458+ toolName: "shell",
459+ toolCallId: "m1",
460+ isError: false,
461+ result: shellResult({ stdout: "index.ts" }),
462+ },
463+ {
464+ type: "tool-result",
465+ toolName: "subagent",
466+ toolCallId: "m2",
467+ isError: false,
468+ result: { inputTokens: 50, outputTokens: 30 },
469+ },
470+ {
471+ type: "tool-result",
472+ toolName: "readSkill",
473+ toolCallId: "m3",
474+ isError: false,
475+ result: {
476+ skill: {
477+ name: "deploy",
478+ description: "Deploy stuff",
479+ source: "local",
480+ },
481+ },
482+ },
483+ turnDone(),
484+ ]),
485+ new Spinner(),
486+ );
487+
488+ const plain = simulateTerminal(getCapturedStdout());
489+
490+ // All three calls rendered
491+ expect(plain).toContain(" $ ls src");
492+ expect(plain).toMatch(/^ {2}⇢/m);
493+ expect(plain).toMatch(/^ {2}←/m);
494+
495+ // All three results at 4-space indent
496+ expect(plain).toMatch(/^ {4}done.*index\.ts/m);
497+ expect(plain).toMatch(/^ {4}⇢/m);
498+ // readSkill result uses · (info) glyph
499+ expect(plain).toMatch(/^ {4}·/m);
500+ });
501+
502+ test("parallel calls: blank line separates second call from first call", async () => {
503+ captureStdout();
504+
505+ await renderTurn(
506+ eventsFrom([
507+ {
508+ type: "tool-call-start",
509+ toolName: "shell",
510+ toolCallId: "s1",
511+ args: { command: "echo one" },
512+ },
513+ {
514+ type: "tool-call-start",
515+ toolName: "shell",
516+ toolCallId: "s2",
517+ args: { command: "echo two" },
518+ },
519+ {
520+ type: "tool-result",
521+ toolName: "shell",
522+ toolCallId: "s1",
523+ isError: false,
524+ result: shellResult({ stdout: "one" }),
525+ },
526+ {
527+ type: "tool-result",
528+ toolName: "shell",
529+ toolCallId: "s2",
530+ isError: false,
531+ result: shellResult({ stdout: "two" }),
532+ },
533+ turnDone(),
534+ ]),
535+ new Spinner(),
536+ );
537+
538+ const plain = simulateTerminal(getCapturedStdout());
539+ const lines = plain.split("\n");
540+
541+ // There should be a blank line between the two tool call lines
542+ const idx1 = lines.findIndex((l) => l.includes("$ echo one"));
543+ const idx2 = lines.findIndex((l) => l.includes("$ echo two"));
544+ expect(idx2).toBeGreaterThan(idx1);
545+ // At least one blank line between them
546+ const between = lines.slice(idx1 + 1, idx2);
547+ expect(between.some((l) => l.trim() === "")).toBe(true);
548+ });
549+
550+ test("parallel calls with one error and one success", async () => {
551+ captureStdout();
552+
553+ await renderTurn(
554+ eventsFrom([
555+ {
556+ type: "tool-call-start",
557+ toolName: "shell",
558+ toolCallId: "e1",
559+ args: { command: "good-cmd" },
560+ },
561+ {
562+ type: "tool-call-start",
563+ toolName: "shell",
564+ toolCallId: "e2",
565+ args: { command: "bad-cmd" },
566+ },
567+ {
568+ type: "tool-result",
569+ toolName: "shell",
570+ toolCallId: "e1",
571+ isError: false,
572+ result: shellResult({ stdout: "ok" }),
573+ },
574+ {
575+ type: "tool-result",
576+ toolName: "shell",
577+ toolCallId: "e2",
578+ isError: true,
579+ result: "permission denied",
580+ },
581+ { type: "text-delta", delta: "Partial failure." },
582+ turnDone(),
583+ ]),
584+ new Spinner(),
585+ );
586+
587+ const plain = simulateTerminal(getCapturedStdout());
588+ expect(plain).toContain(" $ good-cmd");
589+ expect(plain).toContain(" $ bad-cmd");
590+ expect(plain).toMatch(/^ {4}done/m);
591+ expect(plain).toMatch(/^ {4}✖ permission denied/m);
592+ expect(plain).toContain("◆ Partial failure.");
593+ });
594+
595+ test("reasoning → parallel calls → reasoning → reply", async () => {
596+ captureStdout();
597+
598+ await renderTurn(
599+ eventsFrom([
600+ { type: "reasoning-delta", delta: "I'll check two files" },
601+ {
602+ type: "tool-call-start",
603+ toolName: "shell",
604+ toolCallId: "r1",
605+ args: { command: "cat a.ts" },
606+ },
607+ {
608+ type: "tool-call-start",
609+ toolName: "shell",
610+ toolCallId: "r2",
611+ args: { command: "cat b.ts" },
612+ },
613+ {
614+ type: "tool-result",
615+ toolName: "shell",
616+ toolCallId: "r1",
617+ isError: false,
618+ result: shellResult({ stdout: "content A" }),
619+ },
620+ {
621+ type: "tool-result",
622+ toolName: "shell",
623+ toolCallId: "r2",
624+ isError: false,
625+ result: shellResult({ stdout: "content B" }),
626+ },
627+ { type: "reasoning-delta", delta: "Now I understand" },
628+ { type: "text-delta", delta: "Here's my analysis." },
629+ turnDone(),
630+ ]),
631+ new Spinner(),
632+ );
633+
634+ const plain = simulateTerminal(getCapturedStdout());
635+
636+ // Reasoning block before tools
637+ expect(plain).toContain("· reasoning");
638+ expect(plain).toContain("I'll check two files");
639+
640+ // Both parallel calls
641+ expect(plain).toContain("$ cat a.ts");
642+ expect(plain).toContain("$ cat b.ts");
643+
644+ // Second reasoning block
645+ const reasoningCount = (plain.match(/· reasoning/g) ?? []).length;
646+ expect(reasoningCount).toBe(2);
647+ expect(plain).toContain("Now I understand");
648+
649+ // Final reply
650+ expect(plain).toContain("◆ Here's my analysis.");
Cut short: this commit's diff is over 1000 lines.