import { afterEach, beforeEach, describe, expect, test, vi } from "bun:test"; import * as fs from "node:fs/promises"; import * as os from "node:os"; import * as path from "node:path"; import { type ContextFile, contextFileCapability } from "@oh-my-pi/pi-coding-agent/capability/context-file"; import { resetSettingsForTest, Settings } from "@oh-my-pi/pi-coding-agent/config/settings"; import { initializeWithSettings, loadCapability } from "@oh-my-pi/pi-coding-agent/discovery"; import { __resetDirsFromEnvForTests, removeWithRetries, setAgentDir } from "@oh-my-pi/pi-utils"; function restoreEnvValue(key: string, value: string | undefined): void { if (value === undefined) { delete process.env[key]; delete Bun.env[key]; return; } process.env[key] = value; Bun.env[key] = value; } describe("disabledExtensions runtime filtering", () => { let tempDir = ""; let tempHomeDir = ""; let originalHome: string | undefined; let originalAgentDirEnv: string | undefined; let originalOmpProfileEnv: string | undefined; let originalPiProfileEnv: string | undefined; beforeEach(async () => { resetSettingsForTest(); originalAgentDirEnv = process.env.PI_CODING_AGENT_DIR; originalOmpProfileEnv = process.env.OMP_PROFILE; originalPiProfileEnv = process.env.PI_PROFILE; originalHome = process.env.HOME; tempHomeDir = await fs.mkdtemp(path.join(os.tmpdir(), "omp-disabled-ext-home-")); process.env.HOME = tempHomeDir; vi.spyOn(os, "homedir").mockReturnValue(tempHomeDir); setAgentDir(path.join(tempHomeDir, ".omp", "agent")); tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "omp-disabled-ext-")); await fs.mkdir(path.join(tempDir, ".omp"), { recursive: true }); await fs.writeFile(path.join(tempDir, ".omp", "AGENTS.md"), "# project instructions\n"); const settings = await Settings.init({ inMemory: true, cwd: tempDir, overrides: { disabledExtensions: ["context-file:project:AGENTS.md"], }, }); initializeWithSettings(settings); }); afterEach(async () => { resetSettingsForTest(); vi.restoreAllMocks(); restoreEnvValue("HOME", originalHome); restoreEnvValue("OMP_PROFILE", originalOmpProfileEnv); restoreEnvValue("PI_PROFILE", originalPiProfileEnv); restoreEnvValue("PI_CODING_AGENT_DIR", originalAgentDirEnv); __resetDirsFromEnvForTests(); await removeWithRetries(tempHomeDir); await removeWithRetries(tempDir); }); test("hides disabled context files from runtime loads by default", async () => { const result = await loadCapability(contextFileCapability.id, { cwd: tempDir }); expect(result.items).toHaveLength(0); }); test("can include disabled context files for dashboard-style loads", async () => { const result = await loadCapability(contextFileCapability.id, { cwd: tempDir, includeDisabled: true, }); expect(result.items).toHaveLength(1); expect(path.basename(result.items[0]!.path)).toBe("AGENTS.md"); }); });