import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { fauxProvider } from "@earendil-works/pi-ai"; import { afterEach, describe, expect, it } from "vitest"; import { createPiSession, type PiSession, } from "../../../test/fixture/create-pi-session.ts"; import { __test } from "../index.ts"; const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); const pairs = [{ executor: "openai/gpt-4o", advisor: "openai/gpt-4o" }]; interface NotifyCall { args: unknown[]; } /** * Install a recording UI context so `hasUI` is true (the extension sees an * interactive UI) and `ctx.ui.notify` calls are captured. */ function installRecordingUi(session: PiSession): NotifyCall[] { const calls: NotifyCall[] = []; session.session.extensionRunner.setUIContext( { ...session.session.extensionRunner.createContext().ui, notify: (...args: unknown[]) => calls.push({ args }), }, "print", ); return calls; } type Case = { user?: unknown; project?: unknown; untrusted?: boolean }; describe("angel settings resolution", () => { let session: PiSession | undefined; let root: string | undefined; afterEach(async () => { await session?.dispose(); session = undefined; if (root) rmSync(root, { recursive: true, force: true }); root = undefined; }); async function start(input: Case) { root = mkdtempSync(path.join(tmpdir(), "pi-ext-angel-settings-")); if ("project" in input) { mkdirSync(path.join(root, ".pi"), { recursive: true }); writeFileSync( path.join(root, ".pi", "settings.json"), JSON.stringify({ angel: input.project }), ); } session = await createPiSession({ cwd: root, extensionPaths: [extensionPath], ...("user" in input ? { userSettings: { angel: input.user } } : {}), }); if (input.untrusted) session.session.settingsManager.setProjectTrusted(false); // Angel activates only when its configured pairs resolve to real models; // provide openai/gpt-4o through a deterministic provider. const faux = fauxProvider({ provider: "openai", models: [{ id: "gpt-4o", maxTokens: 8_192 }], }); const registry = session.session.extensionRunner.createContext().modelRegistry; registry.registerProvider("openai", { api: faux.api, apiKey: "test-key", models: faux.models, streamSimple: (model, context, options) => faux.provider.streamSimple(model, context, options), }); await registry.refresh({ allowNetwork: false }); // Angel resolves its pair against the current model; make it openai/gpt-4o. const current = registry.find("openai", "gpt-4o"); if (!current) throw new Error("missing faux openai/gpt-4o model"); await session.session.setModel(current); const notifications = installRecordingUi(session); await session.session.extensionRunner.emit({ type: "session_start", reason: "startup", }); const warnings = notifications .filter((call) => call.args[1] === "warning") .map((call) => call.args[0]); return { active: session.session.getActiveToolNames().includes("angel"), warnings, }; } it.each([ ["default unconfigured", {}, false], ["user pairs", { user: { pairs } }, true], ["project pairs", { project: { pairs } }, true], [ "project enabled false merged over user pairs", { user: { pairs }, project: { enabled: false } }, false, ], [ "project enabled true merged over user enabled false", { user: { pairs, enabled: false }, project: { enabled: true } }, true, ], [ "project pairs array replaces user pairs", { user: { pairs }, project: { pairs: [] } }, false, ], [ "untrusted project ignored", { user: { pairs }, project: { enabled: false }, untrusted: true }, true, ], ] satisfies [string, Case, boolean][])( "%s", async (_name, input, expected) => { const { active, warnings } = await start(input); expect(active).toBe(expected); expect(warnings).toEqual([]); }, ); it.each([ [ "non-object project block", { user: { pairs }, project: true }, "Invalid angel from project settings: expected an object", ], [ "merged enabled string", { user: { pairs }, project: { enabled: "false" } }, "Invalid angel from merged project and user settings: enabled must be a boolean", ], [ "user thinkingLevel", { user: { pairs, thinkingLevel: "extreme" } }, "Invalid angel from user settings: thinkingLevel must be one of off, minimal, low, medium, high, xhigh, max", ], [ "user pair shape", { user: { pairs: [{ executor: "openai/gpt-4o" }] } }, "Invalid angel from user settings: pairs must be an array of { executor, advisor } strings", ], [ "user subagentExtensions", { user: { pairs, subagentExtensions: "web" } }, 'Invalid angel from user settings: subagentExtensions must be "all", "none", or a Pi source array', ], ] satisfies [string, Case, string][])( "rejects %s without falling through", async (_name, input, message) => { const { active, warnings } = await start(input); expect(active).toBe(false); expect(warnings).toEqual([`${message}; Angel unconfigured`]); }, ); }); describe("angel sub-agent extensions", () => { it("defaults sub-agent extension loading to none", () => { expect(__test.resolveSubagentExtensions(undefined)).toBeUndefined(); }); it("preserves unique Pi sources without resolving sibling paths", () => { expect( __test.resolveSubagentExtensions([ " ./custom-extension.ts ", "./custom-extension.ts", ]), ).toEqual(["./custom-extension.ts"]); }); it("preserves the all selection", () => { expect(__test.resolveSubagentExtensions("all")).toBe("all"); }); });