import { initTheme, Theme } from "@earendil-works/pi-coding-agent"; import { stripTerminalSequences, visibleWidth } from "@earendil-works/pi-tui"; import { beforeAll, describe, expect, it } from "vitest"; import { renderCall, renderResult } from "../src/render.ts"; import { failure, result } from "../src/tools.ts"; let theme: Theme; beforeAll(() => { initTheme("dark", false); const ink = "#dddddd"; theme = new Theme( { text: ink, thinkingXhigh: ink, accent: "#5f87ff", success: "#439343", warning: "#b89826", error: "#c84949", dim: ink, muted: ink, toolTitle: ink, } as unknown as ConstructorParameters[0], { selectedBg: "#000000", toolPendingBg: "#213141", toolSuccessBg: "#314151", toolErrorBg: "#415161", } as unknown as ConstructorParameters[1], "truecolor", { name: "dark", appearance: "dark" }, ); }); const context = { keyHint: (_key: string, description: string) => `KEY ${description}`, }; const quota = { fiveHourLeft: 93, weeklyLeft: 92, observedAt: 1 }; const widths = [40, 80, 160] as const; function lines(component: { render(width: number): string[] }, width: number) { const rows = component.render(width); for (const row of rows) { expect(visibleWidth(row)).toBeLessThanOrEqual(width); // Renderers inherit Pi's tool background: no resets or hardcoded backgrounds. expect(row).not.toMatch(/\x1b\[(?:0|49)?m|\x1b\[48[;:]/); } return stripTerminalSequences(rows.join("\n")); } function render( kind: Parameters[0], value: Parameters>[0], width: number, options: { expanded: boolean; isPartial?: boolean; isError?: boolean }, ) { return lines( renderResult(kind)( value, { expanded: options.expanded, isPartial: options.isPartial ?? false }, theme, { ...context, isError: options.isError ?? false }, ), width, ); } const flat = (text: string) => text.replace(/\s+/g, " "); const cases = { vision: result("red\n\nMiniMax vlm · est $0.01", { operation: "image-understanding", answer: "The square is \u001b[31mred\u001b[0m.\nSecond answer line.", accounting: { kind: "catalog-estimate", usd: 0.01 }, quota, }), speech: result("Saved MP3 → /tmp/out/speech.mp3", { operation: "speech", accounting: { kind: "catalog-estimate", usd: 0.00036, units: 6 }, providerMeasurements: { usage_characters: 6 }, artifact: { path: "/tmp/out/speech.mp3", format: "mp3", bytes: 2048 }, quota, }), video: result("H3 job ball · task t-1 · completed → /tmp/out/ball.mp4", { operation: "video", job: "ball", taskId: "t-1", state: "completed", requestedDuration: 4, resolution: "768P", accounting: { kind: "catalog-estimate", usd: 0.32 }, artifact: { path: "/tmp/out/user-approved.mp4", bytes: 12 }, providerMeasurements: { output_seconds: 4 }, quota: { weeklyLeft: 80, observedAt: 1 }, }), } as const; describe("MiniMax native tool rendering", () => { it.each(widths)("compact rows are bounded summaries at width %s", (width) => { const vision = flat( render("vision", cases.vision, width, { expanded: false }), ); expect(vision).toContain("✓ Answered"); expect(vision).toContain("est $0.01"); expect(vision).toContain("M Plan left 5h 93% · wk 92%"); expect(vision).not.toContain("Second answer line"); expect(vision).toContain("KEY to expand"); const speech = flat( render("speech", cases.speech, width, { expanded: false }), ); expect(speech).toContain("Saved MP3"); expect(speech).toContain("6 chars"); expect(speech).toContain("2.0 KB"); expect(speech).toContain("est $0.00036"); expect(speech).not.toContain("usage_characters"); const video = flat( render("video", cases.video, width, { expanded: false }), ); expect(video).toContain("ball · t-1 · completed"); expect(video).toContain("user-approved.mp4"); expect(video).toContain("4s 768P"); expect(video).toContain("M Plan left wk 80%"); expect(video).not.toContain("output_seconds"); }); it.each(widths)( "expanded rows show full readable details at width %s", (width) => { const vision = render("vision", cases.vision, width, { expanded: true }); expect(vision).toContain("Second answer line."); expect(vision).not.toContain("\u001b[31m"); expect(flat(vision)).toContain("kind: catalog-estimate"); expect(vision).not.toContain("KEY to expand"); const speech = flat( render("speech", cases.speech, width, { expanded: true }), ); expect(speech).toContain("usage_characters: 6"); expect(speech).not.toContain("{"); const video = flat( render("video", cases.video, width, { expanded: true }), ); expect(video).toContain("output_seconds: 4"); expect(video).toContain("path: /tmp/out/user-approved.mp4"); }, ); it.each(widths)( "lists owned jobs one per expanded line at width %s", (width) => { const value = result("H3 job a · task t-a · pending", { jobs: [ { job: "a", taskId: "t-a", state: "pending" }, { job: "b", state: "unknown" }, ], networkRequests: 0, }); expect( flat(render("video", value, width, { expanded: false })), ).toContain("2 owned H3 jobs"); const expanded = flat(render("video", value, width, { expanded: true })); expect(expanded).toContain("a · t-a · pending"); expect(expanded).toContain("b · unknown"); const empty = result("No owned H3 jobs in this session.", { jobs: [] }); const none = render("video", empty, width, { expanded: false }); expect(none).toContain("0 owned H3 jobs"); expect(none).not.toContain("KEY"); }, ); it.each(widths)( "partial progress and errors honor expansion at width %s", (width) => { expect( render("speech", result("", {}), width, { expanded: false, isPartial: true, }), ).toContain("Synthesizing speech"); const short = failure(new Error("Missing MiniMax M Plan key.")); const compactShort = render("vision", short, width, { expanded: false, isError: true, }); expect(flat(compactShort)).toContain("✗ Missing MiniMax M Plan key."); expect(compactShort).not.toContain("KEY"); const long = failure( new Error(`Long failure ${"detail ".repeat(40)}END`), ); const compactLong = flat( render("video", long, width, { expanded: false, isError: true }), ); expect(compactLong).toContain("…"); expect(compactLong).not.toContain("END"); expect(compactLong).toContain("KEY to expand"); const expandedLong = flat( render("video", long, width, { expanded: true, isError: true }), ); expect(expandedLong).toContain("END"); expect(expandedLong).not.toContain("KEY"); }, ); it.each(widths)( "malformed details fall back to model text at width %s", (width) => { const legacy = { content: [{ type: "text" as const, text: "Legacy line\nHidden more" }], details: undefined, }; const compact = render("speech", legacy, width, { expanded: false }); expect(compact).toContain("Legacy line"); expect(compact).not.toContain("Hidden more"); expect(compact).toContain("KEY to expand"); expect(render("speech", legacy, width, { expanded: true })).toContain( "Hidden more", ); }, ); it.each(widths)( "call rows show sanitized primary arguments at width %s", (width) => { const vision = flat( lines( renderCall("vision")( { source: "https://cdn.example/img/cat.png?signature=secret", prompt: `Describe\u001b]0;evil\u0007 ${"word ".repeat(30)}`, }, theme, ), width, ), ); expect(vision).toContain("MiniMax vision"); expect(vision).toContain("cat.png"); expect(vision).not.toContain("secret"); expect(vision).not.toContain("evil"); expect(vision).toContain("…"); const speech = flat( lines( renderCall("speech")( { text: "Hello there.", outputPath: "/tmp/a/b/hello.mp3" }, theme, ), width, ), ); expect(speech).toContain('"Hello there."'); expect(speech).toContain("→ hello.mp3"); expect(speech).not.toContain("/tmp/a/b"); const video = flat( lines( renderCall("video")( { action: "submit", job: "ball", duration: 6, resolution: "2K" }, theme, ), width, ), ); expect(video).toContain("submit ball 6s 2K"); expect( flat(lines(renderCall("capabilities")({}, theme), width)).trim(), ).toBe("MiniMax capabilities"); }, ); });