import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { getModel } from "@earendil-works/pi-ai/compat"; import { afterEach, describe, expect, it, vi } from "vitest"; import { createTestSession, type TestSession } from "../../../test/harness"; const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); type Case = { user?: unknown; project?: unknown; untrusted?: boolean }; describe("fast.enable resolution", () => { let t: TestSession | undefined; let root: string | undefined; afterEach(() => { t?.dispose(); t = undefined; if (root) rmSync(root, { recursive: true, force: true }); root = undefined; }); async function start(input: Case) { root = mkdtempSync(path.join(tmpdir(), "pi-ext-fast-settings-")); const write = (dir: string, value: unknown) => { mkdirSync(dir, { recursive: true }); writeFileSync( path.join(dir, "settings.json"), JSON.stringify({ fast: { enable: value } }), ); }; if ("user" in input) write(path.join(root, ".test-home/.pi/agent"), input.user); if ("project" in input) write(path.join(root, ".pi"), input.project); t = await createTestSession({ cwd: root, extensions: [extensionPath] }); if (input.untrusted) t.session.settingsManager.setProjectTrusted(false); const model = getModel("openai-codex", "gpt-5.5"); if (!model) throw new Error("missing openai-codex test model"); // The harness pins an openai model; fast only activates for openai-codex. vi.spyOn(t.session, "model", "get").mockReturnValue(model); const session = t; await session.session.extensionRunner.emit({ type: "session_start", reason: "startup", }); await vi.waitFor(() => expect(session.events.uiCallsFor("setStatus").length).toBeGreaterThan(0), ); const status = session.events.uiCallsFor("setStatus").at(-1)?.args; const warnings = session.events .uiCallsFor("notify") .filter((call) => call.args[1] === "warning") .map((call) => call.args[0]); return { active: status?.[1] !== undefined, warnings }; } it.each([ ["default off", {}, false], ["user true", { user: true }, true], ["project false over user true", { user: true, project: false }, false], ["project true over user false", { user: false, project: true }, true], [ "untrusted project ignored", { user: true, project: 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([ ["string project", { user: true, project: "true" }, "project settings"], ["numeric user", { user: 1 }, "user settings"], ] satisfies [string, Case, string][])( "rejects %s without falling through", async (_name, input, source) => { const { active, warnings } = await start(input); expect(active).toBe(false); expect(warnings.at(-1)).toBe( `Invalid fast.enable from ${source}; fast mode off`, ); }, ); });