import { type ExtensionContext, initTheme, type Theme, } from "@earendil-works/pi-coding-agent"; import { type Component, getOsc8LinkAtColumn, type OverlayHandle, setCapabilityOverrides, type Terminal, Text, TuiAltScreen, visibleWidth, } from "@earendil-works/pi-tui"; import { describe, expect, it } from "vitest"; import { plainTheme } from "../__tests__/theme.ts"; import { icon } from "../src/look.ts"; import { PanelHost } from "../src/panel.ts"; import { EphemletStore } from "../src/store.ts"; class MemoryTerminal implements Terminal { columns = 120; rows = 32; kittyProtocolActive = false; input: (data: string) => void = () => {}; resize: () => void = () => {}; start(input: (data: string) => void, resize: () => void): void { this.input = input; this.resize = resize; } stop(): void {} async drainInput(): Promise {} write(): void {} moveBy(): void {} hideCursor(): void {} showCursor(): void {} clearLine(): void {} clearFromCursor(): void {} clearScreen(): void {} setTitle(): void {} setProgress(): void {} setProgramStatus(): void {} click(x: number, y: number): void { this.input(`\x1b[<0;${x + 1};${y + 1}M`); this.input(`\x1b[<0;${x + 1};${y + 1}m`); } } describe("real Pi fullscreen renderer integration", () => { it("renders Markdown, routes SGR mouse/keys and links, resizes, and removes only its own overlay", async () => { initTheme("dark", false); setCapabilityOverrides({ hyperlinks: true }); const terminal = new MemoryTerminal(); const opened: string[] = []; const tui = new TuiAltScreen(terminal, false, undefined, { mouse: true, openUrl: (url) => opened.push(url), }); const store = new EphemletStore(() => {}); const record = store.put({ title: "Report", purpose: "Progress", source: "[Markdown link](https://markdown.example)", }); store.view(record.id, true); const theme = plainTheme; let widget: (Component & { dispose?(): void }) | undefined; let handle: OverlayHandle | undefined; const show = tui.showOverlay.bind(tui); tui.showOverlay = (component, options) => { handle = show(component, options); return handle; }; const ctx = { mode: "tui", ui: { setWidget( _key: string, factory: | ((tui: TuiAltScreen, theme: Theme) => typeof widget) | undefined, ) { widget?.dispose?.(); widget = factory?.(tui, theme); }, notify() {}, }, } as unknown as ExtensionContext; const saved: string[] = []; const host = new PanelHost(store, () => ctx, { changed: () => host.refresh(false), close: (id) => { store.delete(id); host.refresh(); }, save: (id) => saved.push(id), hide: () => { store.view(store.selected, false); host.refresh(); }, answered() {}, }); const put = (source: string) => { store.put({ id: record.id, expectedRevision: store.get(record.id).revision, title: record.title, purpose: record.purpose, source, }); host.refresh(); tui.renderNow(); }; const screen = () => tui.getScreenLines().join("\n"); const findLink = (url: string) => { for (const [y, line] of tui.getScreenLines().entries()) { for (let x = 0; x < terminal.columns; x++) { if (getOsc8LinkAtColumn(line, x) === url) return { x, y }; } } return undefined; }; tui.addChild(new Text("Conversation", 0, 0)); tui.start(); try { host.refresh(true); tui.renderNow(); expect(screen()).toContain("Markdown link"); const bounds = handle?.getBounds(); if (!bounds) throw new Error("Overlay bounds unavailable"); // Markdown sits on the surface below the header, with one cell of padding. const link = findLink("https://markdown.example"); if (!link) throw new Error("Markdown link not rendered"); terminal.click(link.x, link.y); expect(opened).toEqual(["https://markdown.example"]); // A drag that starts on a link stays a text selection, not an activation. terminal.input(`\x1b[<0;${link.x + 1};${link.y + 1}M`); terminal.input(`\x1b[<32;${link.x + 3};${link.y + 1}M`); terminal.input(`\x1b[<0;${link.x + 3};${link.y + 1}m`); expect(opened).toEqual(["https://markdown.example"]); const header = tui.getScreenLines()[bounds.row] ?? ""; const saveIcon = icon("nerd", "save"); terminal.click( visibleWidth(header.slice(0, header.indexOf(saveIcon))), bounds.row, ); expect(saved).toEqual([record.id]); put( `${Array.from({ length: 30 }, (_, i) => `Paragraph ${i}`).join("\n\n")}\n\n界界 [A wrapping wide-character link label for verification](https://wrapped.example)`, ); host.refresh(true); expect(handle?.isFocused()).toBe(true); expect(host.getView(record.id)).toMatchObject({ available: true, revision: store.get(record.id).revision, stale: false, }); expect(screen()).toContain("Paragraph 0"); const beforePaging = screen(); for ( let page = 0; page < 8 && !findLink("https://wrapped.example"); page++ ) { terminal.input("\x1b[6~"); tui.renderNow(); } expect(screen()).not.toBe(beforePaging); expect(screen()).not.toContain("Paragraph 0"); const wrapped = findLink("https://wrapped.example"); if (!wrapped) throw new Error("Wrapped link not rendered after scrolling"); terminal.click(wrapped.x, wrapped.y); expect( opened.filter((url) => url === "https://wrapped.example"), ).toHaveLength(1); setCapabilityOverrides({ hyperlinks: false }); put("[Fallback link](https://fallback.example)"); expect(screen()).toContain("Fallback link"); expect(findLink("https://fallback.example")).toBeUndefined(); terminal.click(bounds.col + 2, bounds.row + 2); expect(opened).not.toContain("https://fallback.example"); terminal.columns = 40; terminal.rows = 50; terminal.resize(); tui.renderNow(); expect(handle?.getBounds()).toMatchObject({ col: 0, row: 0, width: 40 }); host.refresh(true); terminal.input("\x1be"); tui.renderNow(); expect(store.visible).toBe(false); expect(store.records.size).toBe(1); store.view(record.id, true); host.refresh(true); terminal.input("\x1b"); tui.renderNow(); expect(store.visible).toBe(false); expect(store.records.size).toBe(1); store.view(record.id, true); host.refresh(); terminal.rows = 6; terminal.resize(); tui.renderNow(); const tiny = host.getView(record.id); expect(tiny).toMatchObject({ available: true, height: 1, stale: false }); if (!tiny.available) throw new Error("Tiny frame missing"); expect(tiny.plainText).toContain("enlarge terminal"); terminal.rows = 50; terminal.resize(); tui.renderNow(); show(new Text("Another overlay", 0, 0), { width: 20, anchor: "bottom-left", }); host.dispose(); tui.renderNow(); expect(screen()).toContain("Another overlay"); } finally { host.dispose(); setCapabilityOverrides({}); tui.stop(); } }); });