import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { createTestSession, type TestSession } from "../../../test/harness"; import { findNu } from "../runtime.ts"; const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); type Case = { user?: unknown; project?: unknown; env?: string; flag?: boolean; untrusted?: boolean; }; describe.runIf(Boolean(findNu()))("nushell.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-nushell-settings-")); const write = (dir: string, value: unknown) => { mkdirSync(dir, { recursive: true }); writeFileSync( path.join(dir, "settings.json"), JSON.stringify({ nushell: { 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], env: { PI_NUSHELL_ENABLE: input.env }, }); if (input.untrusted) t.session.settingsManager.setProjectTrusted(false); const runner = t.session.extensionRunner; if (input.flag !== undefined) runner.setFlagValue("nushell", input.flag); await runner.emit({ type: "session_start", reason: "startup" }); const warnings = t.events .uiCallsFor("notify") .filter((call) => call.args[1] === "warning") .map((call) => call.args[0]); return { active: t.session.getActiveToolNames().includes("nu"), warnings }; } it("registers --nushell without a default so omission stays distinct", async () => { const { active, warnings } = await start({}); expect(active).toBe(false); expect(warnings).toEqual([]); expect(t?.session.extensionRunner.getFlags().get("nushell")?.default).toBe( undefined, ); expect(t?.session.extensionRunner.getFlagValues().has("nushell")).toBe( false, ); }); it.each([ ["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], ["env false over project true", { project: true, env: "false" }, false], ["env 1 over user false", { user: false, env: "1" }, true], ["env 0 over user true", { user: true, env: "0" }, false], ["flag over env false", { env: "false", flag: true }, true], ["flag over project false", { project: false, flag: true }, true], [ "untrusted project ignored", { user: true, project: false, untrusted: true }, true, ], [ "untrusted project true ignored", { project: true, untrusted: true }, false, ], ] satisfies [string, Case, boolean][])( "%s", async (_name, input, expected) => { const { active, warnings } = await start(input); expect(active).toBe(expected); expect(warnings).toEqual([]); }, ); it.each([ [ "empty env", { user: true, env: "" }, "environment variable PI_NUSHELL_ENABLE", ], [ "string env", { user: true, env: "yes" }, "environment variable PI_NUSHELL_ENABLE", ], ["string project", { user: true, project: "true" }, "project settings"], ["null project", { user: true, project: null }, "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); // The harness emits session_start once during creation, then start() emits again. expect([...new Set(warnings)]).toEqual([ `Invalid nushell.enable from ${source}; tools unchanged`, ]); }, ); });