import { writeFileSync } from "node:fs"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { createFauxCore, fauxAssistantMessage } from "@earendil-works/pi-ai"; import { type ExtensionContext, initTheme, } from "@earendil-works/pi-coding-agent"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; import { __test } from "../index.ts"; const extensionPath = fileURLToPath(new URL("../index.ts", import.meta.url)); const masks = [ { name: "fast", model: "openai/gpt-4o", thinkingLevel: "low", shortcut: "alt+1", }, { name: "deep", model: "openai/gpt-4o-mini", thinkingLevel: "high", shortcut: "alt+2", }, { name: "missing", model: "absent/nope", thinkingLevel: "max", shortcut: "alt+3", }, ]; let session: PiSession | undefined; async function start(items: unknown[] = masks) { initTheme(undefined, false); session = await createPiSession({ extensionPaths: [extensionPath], userSettings: { masks: { items } }, }); const core = createFauxCore({ api: "openai", provider: "openai", models: [ { id: "gpt-4o", reasoning: true }, { id: "gpt-4o-mini", reasoning: true }, ], }); session.session.modelRuntime.registerProvider("openai", { apiKey: "test-key", api: "openai", baseUrl: "https://faux.invalid", streamSimple: core.streamSimple, models: ["gpt-4o", "gpt-4o-mini"].map((id) => ({ id, name: id, reasoning: true, input: ["text"] as ("text" | "image")[], cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: 200_000, maxTokens: 8192, })), }); const model = session.session.modelRuntime.getModel("openai", "gpt-4o"); if (!model) throw new Error("missing fixture model"); await session.session.setModel(model); session.session.setThinkingLevel("off"); return core; } function active() { if (!session) throw new Error("missing fixture session"); return session.session; } function extension() { const loaded = session?.extensions[0]; if (!loaded) throw new Error("missing Masks extension"); return loaded; } function context() { return active().extensionRunner.createCommandContext(); } function command() { const registered = extension().commands.get("masks"); if (!registered) throw new Error("missing Masks command"); return registered; } function shortcut( key: Parameters["shortcuts"]["get"]>[0], ) { const registered = extension().shortcuts.get(key); if (!registered) throw new Error("missing Masks shortcut"); return registered; } afterEach(async () => { vi.restoreAllMocks(); await session?.dispose(); session = undefined; }); describe("masks model and thinking contract", () => { it.each(["provider", "https://models.example/v1"])( "resolves exact registered identity for %s", async (provider) => { const id = "org/model"; await start([ { name: "nested", model: `${provider}/${id}`, thinkingLevel: "low" }, ]); const core = createFauxCore({ api: "openai", provider, models: [{ id, reasoning: true }], }); active().modelRuntime.registerProvider(provider, { apiKey: "test-key", api: "openai", baseUrl: "https://faux.invalid", streamSimple: core.streamSimple, models: core.models, }); await command().handler("nested", context()); expect(active().model?.provider).toBe(provider); expect(active().model?.id).toBe(id); expect(active().thinkingLevel).toBe("low"); }, ); it("switches from shortcut, picker, and named command", async () => { await start(); const select = vi .spyOn(context().ui, "select") .mockImplementation(async (_title, options) => options[1]); expect([...extension().commands.keys()]).toEqual(["masks"]); expect([...extension().shortcuts.keys()]).toEqual([ "alt+m", "alt+1", "alt+2", "alt+3", ]); await shortcut("alt+1").handler(context()); expect(active().model?.id).toBe("gpt-4o"); expect(active().thinkingLevel).toBe("low"); await shortcut("alt+m").handler(context()); expect(active().model?.id).toBe("gpt-4o-mini"); expect(active().thinkingLevel).toBe("high"); expect(select).toHaveBeenCalledWith("Masks", [ "fast openai/gpt-4o:low · alt+1", "deep openai/gpt-4o-mini:high · alt+2", "missing absent/nope:max · alt+3", ]); await command().handler("fast", context()); expect(active().model?.id).toBe("gpt-4o"); expect(active().thinkingLevel).toBe("low"); expect(session?.errors).toEqual([]); }); it.each([ "org/model", "https://models.example/v1/org/mod", "HTTPS://models.example/v1/org/model", "https://models.example/v1/org/model:high", ])( "does not substitute an invalid or unavailable identity: %s", async (ref) => { await start([{ name: "invalid", model: ref, thinkingLevel: "high" }]); const provider = "https://models.example/v1"; const core = createFauxCore({ api: "openai", provider, models: [{ id: "org/model", reasoning: true }], }); active().modelRuntime.registerProvider(provider, { apiKey: "test-key", api: "openai", baseUrl: "https://faux.invalid", streamSimple: core.streamSimple, models: core.models, }); const notify = vi.spyOn(context().ui, "notify"); await command().handler("invalid", context()); expect(active().model?.provider).toBe("openai"); expect(active().model?.id).toBe("gpt-4o"); expect(active().thinkingLevel).toBe("off"); expect(notify).toHaveBeenCalledWith( `Mask "invalid": model ${ref} not found`, "error", ); }, ); it("completes configured mask names", async () => { await start(); expect(command().getArgumentCompletions?.("de")).toEqual([ { value: "deep", label: "deep" }, ]); expect(command().getArgumentCompletions?.("unknown")).toBeNull(); }); it("leaves model and thinking unchanged on cancellation or unknown masks", async () => { await start(); vi.spyOn(context().ui, "select").mockResolvedValue(undefined); const notify = vi.spyOn(context().ui, "notify"); await command().handler("", context()); await command().handler("unknown", context()); await shortcut("alt+3").handler(context()); expect(active().model?.id).toBe("gpt-4o"); expect(active().thinkingLevel).toBe("off"); expect(notify.mock.calls).toEqual([ ['Unknown mask "unknown"', "error"], ['Mask "missing": model absent/nope not found', "error"], ]); }); it("warns when no masks are configured", async () => { await start([]); const notify = vi.spyOn(context().ui, "notify"); await command().handler("", context()); expect(notify).toHaveBeenCalledWith( "No masks configured in settings.json", "warning", ); }); it("does not change thinking when credentials reject the model", async () => { await start(); vi.spyOn(active().modelRuntime, "hasConfiguredAuth").mockReturnValue(false); const notify = vi.spyOn(context().ui, "notify"); await command().handler("deep", context()); expect(active().model?.id).toBe("gpt-4o"); expect(active().thinkingLevel).toBe("off"); expect(notify).toHaveBeenCalledWith( 'Mask "deep": no credentials for openai/gpt-4o-mini', "error", ); }); it("releases the switch guard after a model error", async () => { await start(); vi.spyOn(active(), "setModel").mockRejectedValueOnce(new Error("offline")); await expect(command().handler("deep", context())).rejects.toThrow( "offline", ); await command().handler("fast", context()); expect(active().thinkingLevel).toBe("low"); }); it("keeps overlapping model and thinking changes paired", async () => { await start(); const entered = Promise.withResolvers(); const release = Promise.withResolvers(); const setModel = active().setModel.bind(active()); vi.spyOn(active(), "setModel").mockImplementationOnce(async (model) => { entered.resolve(); await release.promise; return setModel(model); }); const notify = vi.spyOn(context().ui, "notify"); const first = command().handler("deep", context()); await entered.promise; try { await shortcut("alt+1").handler(context()); expect(notify).toHaveBeenCalledWith( "Mask switch already in progress", "warning", ); } finally { release.resolve(); } await first; expect(active().model?.id).toBe("gpt-4o-mini"); expect(active().thinkingLevel).toBe("high"); }); it("never changes tools or prompt instructions, including old config fields", async () => { await start([ { ...masks[1], tools: ["read"], instructions: "NEVER_INJECT_THIS" }, ]); const tools = active().getActiveToolNames(); const prompt = active().systemPrompt; await command().handler("deep", context()); expect(active().model?.id).toBe("gpt-4o-mini"); expect(active().thinkingLevel).toBe("high"); expect(active().getActiveToolNames()).toEqual(tools); expect(active().systemPrompt).toBe(prompt); expect([...extension().handlers.keys()]).toEqual([ "session_start", "session_shutdown", ]); }); it("leaves prompt templates to Pi without switching or rollback", async () => { const core = await start(); const filePath = join(session!.cwd, "review.md"); writeFileSync(filePath, "---\nmask: deep\ntools: [read]\n---\nReview."); active().resourceLoader.getPrompts = () => ({ prompts: [ { name: "review", description: "Review", content: "Review.", filePath, sourceInfo: { path: filePath, source: "project", scope: "project", origin: "top-level", }, }, ], diagnostics: [], }); core.setResponses([fauxAssistantMessage("done")]); await active().prompt("/review"); expect(core.state.callCount).toBe(1); expect(active().getLastAssistantText()).toBe("done"); expect(active().model?.id).toBe("gpt-4o"); expect(active().thinkingLevel).toBe("off"); expect(session?.errors).toEqual([]); }); it("fuzzy-matches model, thinking level and shortcut", () => { const parsed = __test.parseMasksSettings({ items: masks }); expect( __test.fuzzyMasks(parsed.config.items, "dhgh").map((mask) => mask.name), ).toEqual(["deep"]); expect(__test.fuzzyMasks(parsed.config.items, "")).toBe( parsed.config.items, ); expect(__test.describeMask(parsed.config.items[0]!)).toBe( "openai/gpt-4o:low · alt+1", ); }); it("selects a fuzzy picker result through keyboard input", async () => { const parsed = __test.parseMasksSettings({ items: masks }); let options: unknown; const selected = await __test.chooseFuzzyMask(parsed.config.items, { ui: { custom: async ( factory: ( tui: unknown, theme: unknown, keybindings: unknown, done: (value: string | undefined) => void, ) => { handleInput(data: string): void }, customOptions: unknown, ) => { options = customOptions; let result: string | undefined; const component = await factory( { requestRender() {} }, { bold: (text: string) => text, fg: (_color: string, text: string) => text, }, { matches: (data: string, action: string) => action === "tui.select.confirm" && data === "\r", }, (value: string | undefined) => { result = value; }, ); for (const key of ["d", "e", "e", "p", "\r"]) component.handleInput(key); return result; }, }, } as unknown as ExtensionContext); expect(selected?.name).toBe("deep"); expect(options).toEqual({ blocked: { kind: "question", message: "Choose a model and thinking mask", }, }); }); it("validates model/thinking presets while retaining usable entries", () => { const parsed = __test.parseMasksSettings({ items: [ masks[0], { ...masks[0], model: "provider/other" }, { ...masks[1], shortcut: "alt+1" }, { name: "bad", model: "missing-provider", thinkingLevel: "huge" }, { name: "old", model: "provider/org/model", thinkingLevel: "off", tools: [], instructions: 42, }, ], }); expect(parsed.config.items).toEqual([ masks[0], { ...masks[1], shortcut: undefined }, { name: "old", model: "provider/org/model", thinkingLevel: "off", shortcut: undefined, }, ]); expect(parsed.errors).toEqual([ 'masks.items[1].name duplicates "fast"', 'masks.items[2].shortcut duplicates "alt+1"', "masks.items[3].model must be provider/model", "masks.items[3].thinkingLevel is invalid", ]); }); });