import { readdirSync, readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { type EditorComponent, type EditorTheme, KeybindingsManager, type TUI, TUI_KEYBINDINGS, } from "@earendil-works/pi-tui"; import { afterEach, describe, expect, it } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; type Factory = ( tui: TUI, theme: EditorTheme, keybindings: never, ) => EditorComponent; const extensionPath = fileURLToPath(new URL("../index.ts", import.meta.url)); const theme = { borderColor: (text: string) => text, selectList: { selectedPrefix: (text: string) => text, selectedText: (text: string) => text, description: (text: string) => text, scrollInfo: (text: string) => text, noMatch: (text: string) => text, }, } as EditorTheme; describe("keysmash contract", () => { let session: PiSession | undefined; afterEach(async () => { await session?.dispose(); session = undefined; }); async function start( options: { current?: unknown; env?: Record } = {}, ) { session = await createPiSession({ extensionPaths: [extensionPath], ...(options.env ? { env: options.env } : {}), }); const runner = session.session.extensionRunner; const installed: Array = []; const notices: Array<{ message: string; type?: string }> = []; const ui = Object.create(runner.getUIContext()); ui.getEditorComponent = () => options.current; ui.setEditorComponent = (factory: Factory | undefined) => { installed.push(factory); }; ui.notify = (message: string, type?: string) => { notices.push({ message, ...(type ? { type } : {}) }); }; runner.setUIContext(ui, "tui"); await runner.emit({ type: "session_start", reason: "startup" }); return { runner, installed, notices }; } function build(factory: Factory | undefined): EditorComponent { if (!factory) throw new Error("no editor installed"); const tui = { terminal: { rows: 40, columns: 80 }, requestRender() {}, } as unknown as TUI; const keybindings = new KeybindingsManager(TUI_KEYBINDINGS); return factory(tui, theme, keybindings as never); } it("loads in real Pi extension runtime", async () => { session = await createPiSession({ extensionPaths: [extensionPath] }); expect(session.errors).toEqual([]); expect(session.extensions).toHaveLength(1); expect(session.extensions[0]!.path).toBe(extensionPath); }); it("installs a prompt editor that continues bullets", async () => { const { installed, notices } = await start(); expect(installed).toHaveLength(1); const editor = build(installed[0]); editor.setText("- first"); editor.handleInput("\n"); editor.handleInput("s"); expect(editor.getText()).toBe("- first\n- s"); expect(notices).toEqual([]); expect(session!.errors).toEqual([]); }); it("replaces its own editor after a reload", async () => { const first = await start(); const own = first.installed[0]!; await session!.dispose(); const second = await start({ current: own }); expect(second.installed).toHaveLength(1); expect(second.notices).toEqual([]); }); it("leaves another extension's editor alone and says so", async () => { const { installed, notices } = await start({ current: () => ({}) as EditorComponent, }); expect(installed).toEqual([]); expect(notices).toEqual([ expect.objectContaining({ type: "warning", message: expect.any(String) }), ]); }); it("does nothing without a terminal UI", async () => { session = await createPiSession({ extensionPaths: [extensionPath] }); const runner = session.session.extensionRunner; const installed: unknown[] = []; const ui = Object.create(runner.getUIContext()); ui.setEditorComponent = (factory: unknown) => installed.push(factory); runner.setUIContext(ui, "print"); await runner.emit({ type: "session_start", reason: "startup" }); expect(installed).toEqual([]); }); it("writes debug records without text content when enabled", async () => { const { installed, runner } = await start({ env: { PI_KEYSMASH_DEBUG: "1" }, }); const editor = build(installed[0]); editor.setText("- secret"); editor.handleInput("\n"); await runner.emit({ type: "session_shutdown", reason: "quit" }); const directory = path.join( session!.cwd, ".test-state", "pi-ext", "debug", "keysmash", ); const raw = readFileSync( path.join(directory, readdirSync(directory)[0]!), "utf8", ); const records = raw .trim() .split("\n") .map((line) => JSON.parse(line)); expect(records).toEqual( expect.arrayContaining([ expect.objectContaining({ event: "session.start" }), expect.objectContaining({ event: "edit.apply", action: "continue" }), expect.objectContaining({ event: "session.shutdown" }), ]), ); expect(raw).not.toContain("secret"); }); });