import { describe, expect, test } from "bun:test"; import { initTheme, type Theme } from "@mariozechner/pi-coding-agent"; import { visibleWidth } from "@mariozechner/pi-tui"; import type { DiffRenderPayload } from "../model"; import { normalizeToolPath, scrubLine } from "../model"; import { DiffRenderComponent } from "../render"; const theme = { fg: (_name: string, text: string) => text, bg: (_name: string, text: string) => text, bold: (text: string) => text, } as Theme; initTheme("dark"); const payload: DiffRenderPayload = { diff: [ "@@ -1,3 +1,3 @@", " 1 # TODO", "-2 bad", "+2 good", " 3 done", "@@ -9,2 +9,2 @@", "-9 expandaple", "+9 expandable", ].join("\n"), stats: { files: 1, additions: 2, deletions: 2, hunks: 2 }, }; describe("diff-render model", () => { test("normalizes @ paths", () => { expect(normalizeToolPath("@agent/extensions/TODO.md")).toBe("agent/extensions/TODO.md"); expect(normalizeToolPath("agent/extensions/TODO.md")).toBe("agent/extensions/TODO.md"); }); test("scrubs physical newlines", () => { expect(scrubLine("hello\n")).toBe("hello"); expect(scrubLine("a\nb")).toBe("a↵b"); }); }); describe("DiffRenderComponent", () => { test("collapsed summary is one padded line", () => { const lines = new DiffRenderComponent(payload, theme, false).render(60); expect(lines).toHaveLength(1); expect(visibleWidth(lines[0]!)).toBe(60); expect(lines[0]).toContain("1 file +2 -2 2 hunks"); }); test("expanded layout fits width and contains no physical newlines", () => { const lines = new DiffRenderComponent(payload, theme, true).render(80); for (const line of lines) { expect(line).not.toMatch(/[\r\n]/); expect(visibleWidth(line)).toBeLessThanOrEqual(80); } }); test("expanded layout does not duplicate file header", () => { const lines = new DiffRenderComponent(payload, theme, true).render(80); expect(lines.some((line) => line.includes("TODO.md"))).toBe(false); }); });