|
| 1 | +/** |
| 2 | + * In-place retext keeps a row's paint node when only its state flips — most |
| 3 | + * importantly the gutter voice: a tool row that fails after being painted |
| 4 | + * pending must dim its gutter on the same node, not keep the live bronze. |
| 5 | + */ |
| 6 | +import { describe, expect, test } from "bun:test"; |
| 7 | +import { |
| 8 | + BoxRenderable, |
| 9 | + TextRenderable, |
| 10 | + parseColor, |
| 11 | + rgbToHex, |
| 12 | + type ColorInput, |
| 13 | +} from "@opentui/core"; |
| 14 | +import { withTestRenderer } from "./harness"; |
| 15 | +import { appendStreamRow, replaceStreamRowAt } from "./shell/chrome"; |
| 16 | +import { createAppShell } from "./shell/index"; |
| 17 | +import { transcriptRowChildren } from "./shell/transcript"; |
| 18 | +import { UI } from "./theme"; |
| 19 | +import { pushToolCall, pushToolResult } from "./tool-rows"; |
| 20 | +import type { StreamRow } from "./stream"; |
| 21 | + |
| 22 | +const SHELL_OPTS = { |
| 23 | + terminal: { columns: 100, rows: 24 }, |
| 24 | + wireKeys: false, |
| 25 | + run: "idle", |
| 26 | +} as const; |
| 27 | + |
| 28 | +const gutterOf = (node: unknown): TextRenderable => { |
| 29 | + if (!(node instanceof BoxRenderable)) throw new Error("row node is not a wrapper"); |
| 30 | + const [gutter] = node.getChildren(); |
| 31 | + if (!(gutter instanceof TextRenderable)) throw new Error("first child is not the gutter"); |
| 32 | + return gutter; |
| 33 | +}; |
| 34 | + |
| 35 | +/** The node normalizes fg to an RGBA; compare it to the hex token exactly. */ |
| 36 | +const fgIs = (gutter: TextRenderable, hex: string): boolean => |
| 37 | + rgbToHex(parseColor(gutter.fg as ColorInput)) === hex; |
| 38 | + |
| 39 | +describe("retext gutter voice", () => { |
| 40 | + test("a tool row that fails after being painted pending dims its gutter in place", async () => { |
| 41 | + await withTestRenderer( |
| 42 | + async (h) => { |
| 43 | + const shell = createAppShell(h.renderer, SHELL_OPTS); |
| 44 | + try { |
| 45 | + const rows: StreamRow[] = []; |
| 46 | + pushToolCall(rows, { |
| 47 | + name: "fetch", |
| 48 | + arguments: JSON.stringify({ url: "https://x.dev" }), |
| 49 | + }); |
| 50 | + const pending = rows[0]!; |
| 51 | + expect(pending.pending).toBe(true); |
| 52 | + appendStreamRow(shell, pending); |
| 53 | + await h.renderOnce(); |
| 54 | + const wrapper = transcriptRowChildren(shell)[0]; |
| 55 | + const gutter = gutterOf(wrapper); |
| 56 | + const fgToken = `${gutter.fg}`; |
| 57 | + expect(fgToken).not.toBe(UI.textDim); |
| 58 | + expect(fgIs(gutter, UI.textDim)).toBe(false); |
| 59 | + |
| 60 | + pushToolResult(rows, { name: "fetch", content: "", isError: true }); |
| 61 | + const failed = rows[0]!; |
| 62 | + expect(failed.failed).toBe(true); |
| 63 | + replaceStreamRowAt(shell, 0, failed); |
| 64 | + // Same paint node, same shape: the flip retexted rather than rebuilt. |
| 65 | + expect(transcriptRowChildren(shell)[0]).toBe(wrapper); |
| 66 | + expect(fgIs(gutter, UI.textDim)).toBe(true); |
| 67 | + await h.renderOnce(); |
| 68 | + } finally { |
| 69 | + shell.dispose(); |
| 70 | + } |
| 71 | + }, |
| 72 | + { width: 100, height: 24 }, |
| 73 | + ); |
| 74 | + }); |
| 75 | +}); |
0 commit comments