import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import type { ExtensionUIContext } from "@earendil-works/pi-coding-agent"; import type { TUI } from "@earendil-works/pi-tui"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; import { ICONS_SETTING } from "../index.ts"; import { icon } from "../src/look.ts"; import { resolveSetting } from "../src/pi-ext-settings.ts"; import { plainTheme } from "./theme.ts"; let box: PiSession | undefined; let root: string | undefined; afterEach(async () => { await box?.session.extensionRunner.emit({ type: "session_shutdown", reason: "quit", }); await box?.dispose(); box = undefined; if (root) await rm(root, { recursive: true, force: true }); root = undefined; }); async function start(options: { user?: unknown; project?: unknown; trusted?: boolean; env?: string; }) { root = await mkdtemp(join(tmpdir(), "ephemlets-settings-")); await mkdir(join(root, ".pi")); await writeFile( join(root, ".pi", "settings.json"), JSON.stringify({ ephemlets: { icons: options.project } }), ); box = await createPiSession({ cwd: root, projectTrusted: options.trusted ?? false, extensionPaths: [fileURLToPath(new URL("../index.ts", import.meta.url))], userSettings: { ephemlets: { icons: options.user } }, env: { PI_EPHEMLETS_ICONS: options.env }, }); return box; } describe("ephemlets icon setting", () => { it.each([ ["default", {}, "default", "nerd"], ["user", { user: "unicode" }, "user", "unicode"], ["env beats user", { user: "nerd", env: "unicode" }, "env", "unicode"], [ "trusted project beats user", { user: "nerd", project: "unicode", trusted: true }, "project", "unicode", ], [ "untrusted project is never read", { user: "nerd", project: "unicode", trusted: false }, "user", "nerd", ], ])("resolves %s", async (_name, options, source, value) => { const session = await start(options); const resolved = resolveSetting( { getFlag: () => undefined }, session.session.extensionRunner.createContext(), ICONS_SETTING, ); expect(resolved).toEqual({ ok: true, source, value }); }); it("renders the chosen glyphs and warns about invalid values without falling through", async () => { const session = await start({ user: "unicode", env: "emoji" }); const notify = vi.fn(); const lines: string[][] = []; const tui = { terminal: { columns: 120, rows: 32 }, requestRender() {}, } as unknown as TUI; await session.session.bindExtensions({ mode: "tui", uiContext: { ...session.session.extensionRunner.getUIContext(), notify, setWidget(_key: string, factory: unknown) { if (typeof factory === "function") lines.push(factory(tui, plainTheme).render(80)); }, } as ExtensionUIContext, }); expect(notify.mock.calls[0]?.[1]).toBe("warning"); expect(String(notify.mock.calls[0]?.[0])).toContain("ephemlets.icons"); const tool = session.extensions[0]!.tools.get("ephemlet")!.definition; await tool.execute( "icons", { action: "put", title: "Report", source: "# Report" }, undefined, undefined, session.session.extensionRunner.createToolContext("icons", undefined), ); expect(lines.at(-1)?.[0]).toContain(icon("nerd", "bell")); }); });