// The compact transcript's grouping (src/tui/transcript.ts): a pure view over the items. import { expect, test } from "bun:test" import type { Item } from "../src/tui/state.ts" import { groupRounds, patchStats, roundLine, roundTrouble, rowIdOf, summarize, type Round } from "../src/tui/transcript.ts" const tool = (id: string, name: string, o: Partial> = {}): Item => ({ kind: "tool", id, name, args: {}, status: "done", ...o }) const text = (id: string): Item => ({ kind: "assistant", id, text: "words", streaming: false }) const thought = (id: string, streaming = false): Item => ({ kind: "reasoning", id, text: "hm", streaming, started: 0, ms: 100 }) const shape = (items: Item[], o?: { showReasoning?: boolean }) => groupRounds(items, o).map((r) => (r.kind === "round" ? `[${r.tools.map((t) => t.id).join(",")}${r.thoughts.length ? `|${r.thoughts.map((t) => t.id).join(",")}` : ""}]` : r.item.id)) test("the calls between two pieces of text are one round; text, a prompt, a notice end it", () => { const items: Item[] = [{ kind: "user", id: "u1", text: "go" }, text("a1"), tool("t1", "read"), tool("t2", "read"), tool("t3", "bash"), text("a2"), tool("t4", "edit"), { kind: "notice", id: "n1", text: "x", level: "info" }, tool("t5", "bash"), text("a3")] expect(shape(items)).toEqual(["u1", "a1", "[t1,t2,t3]", "a2", "[t4]", "n1", "[t5]", "a3"]) // The items are not changed, and grouping twice gives the same rows. const before = JSON.stringify(items) expect(shape(items)).toEqual(shape(items)) expect(JSON.stringify(items)).toBe(before) }) test("a finished thought between two calls is folded into the round; one before it, after it, or still streaming is a row", () => { expect(shape([thought("r0"), tool("t1", "read"), thought("r1"), tool("t2", "bash"), thought("r2"), text("a1")])).toEqual(["r0", "[t1,t2|r1]", "r2", "a1"]) // Thinking now: the round ends where the thought begins, and the thought shows. expect(shape([tool("t1", "read"), thought("r1", true)])).toEqual(["[t1]", "r1"]) // Opened with ctrl+r, every thought is a row of its own. expect(shape([tool("t1", "read"), thought("r1"), tool("t2", "bash")], { showReasoning: true })).toEqual(["[t1]", "r1", "[t2]"]) }) test("a call the transcript does not draw is no row and ends no round", () => { expect(shape([tool("t1", "read"), tool("p1", "plan_update"), tool("t2", "bash")])).toEqual(["[t1,t2]"]) expect(shape([tool("p1", "plan_write")])).toEqual([]) }) test("every item is found again in its row: itself, or its round", () => { const rows = groupRounds([text("a1"), tool("t1", "read"), thought("r1"), tool("t2", "bash"), text("a2")]) const map = rowIdOf(rows) expect([...map]).toEqual([["a1", "a1"], ["t1", "t1"], ["t2", "t1"], ["r1", "t1"], ["a2", "a2"]]) }) const round = (tools: Item[]): Pick => ({ tools: tools as Round["tools"] }) const DIFF = "--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1,1 +1,2 @@\n-old\n+new\n+more\n" test("a round's line: the count, the kinds in order with ×n, the time together", () => { const s = summarize(round([tool("1", "read", { ms: 400 }), tool("2", "bash", { ms: 9000 }), tool("3", "read", { ms: 100 }), tool("4", "edit", { ms: 2500, diff: DIFF, title: "src/a.ts" }), tool("5", "read", { ms: 0 }), tool("6", "bash", { ms: 1000 }), tool("7", "edit", { ms: 1000, diff: DIFF, title: "src/b.ts" })])) expect(roundLine(s)).toBe("7 tools · read ×3 · bash ×2 · edit ×2 · 14.0s") expect(s.changes).toEqual([ { id: "4", path: "src/a.ts", added: 2, removed: 1 }, { id: "7", path: "src/b.ts", added: 2, removed: 1 }, ]) expect(roundTrouble(s)).toEqual({ failed: "", denied: "" }) expect(roundLine(summarize(round([tool("1", "bash", { ms: 61_000 })])))).toBe("1 tool · bash · 1m 01s") // A stored session kept no durations: none is made up. expect(roundLine(summarize(round([tool("1", "read"), tool("2", "read")])))).toBe("2 tools · read ×2") }) test("a failure and a refusal are never inside a number only: each is counted beside the line and listed", () => { const s = summarize(round([tool("1", "read", { ms: 10 }), tool("2", "bash", { status: "error", ms: 20, note: "exit 3" }), tool("3", "bash", { status: "denied", note: "denied by a rule" }), tool("4", "edit", { status: "error", ms: 5, diff: DIFF })])) expect(roundTrouble(s)).toEqual({ failed: "2 failed", denied: "1 denied" }) expect(s.failed.map((t) => t.id)).toEqual(["2", "4"]) expect(s.denied.map((t) => t.id)).toEqual(["3"]) // A change that failed is not a change. expect(s.changes).toEqual([]) }) test("a running call stays in sight, and the round has no total until it ends", () => { const s = summarize(round([tool("1", "read", { ms: 10 }), tool("2", "bash", { status: "running" })])) expect(s.running.map((t) => t.id)).toEqual(["2"]) expect(roundLine(s)).toBe("2 tools · read · bash") }) test("the line fits the columns it is given: the count and the time stay, the kinds give way", () => { const s = summarize(round([tool("1", "read", { ms: 400 }), tool("2", "bash", { ms: 9000 }), tool("3", "grep", { ms: 100 }), tool("4", "edit", { ms: 2500 }), tool("5", "web_fetch", { ms: 2000 })])) expect(roundLine(s, 80)).toBe("5 tools · read · bash · grep · edit · web_fetch · 14.0s") expect(roundLine(s, 40)).toBe("5 tools · read · bash · grep · … · 14.0s") expect(roundLine(s, 20)).toBe("5 tools · … · 14.0s") expect(roundLine(s, 16)).toBe("5 tools · 14.0s") for (let width = 16; width <= 80; width++) expect(Bun.stringWidth(roundLine(s, width))).toBeLessThanOrEqual(width) }) test("a multi-file patch as stat lines", () => { const patch = `diff --git a/a.txt b/a.txt\n--- a/a.txt\n+++ b/a.txt\n@@ -1,1 +1,2 @@\n-x\n+y\n+z\ndiff --git a/d/b.txt b/d/b.txt\n--- a/d/b.txt\n+++ b/d/b.txt\n@@ -1,2 +1,1 @@\n-one\n-two\n+one\n` expect(patchStats(patch)).toEqual([ { path: "a.txt", added: 2, removed: 1 }, { path: "d/b.txt", added: 1, removed: 2 }, ]) })