import { mkdir, unlink, writeFile } from "node:fs/promises"; import { spawnSync } from "node:child_process"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { expect, it } from "vitest"; import { assertIsolatedOpenCodeSources, openCodeVerificationEnvironment, } from "../../../scripts/verify/clients/opencode-environment.mjs"; import { SELECTED_REGISTRATION_SETTINGS } from "../../../src/application/SetupRegistrationEnvironment.js"; import { AGENT_RESULT_BUDGET_ENVIRONMENT } from "../../../src/domain/agentResultBudget.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; const contextFor = (root: string) => ({ home: join(root, "account"), env: {}, platform: process.platform, systemPaths: { openCodeSystemDirectory: join(root, "system"), openCodeManagedPreferencesDirectory: join(root, "managed"), }, }); it.each(["v1", "v2"])( "isolates %s without changing its inherited environment", async (dialect) => { const lab = await createTestTempDirectory("rea-opencode-environment-"); const account = join(lab, "account"); const profile = join(lab, "selected"); const poisoned = [ "OPENCODE_CONFIG", "OPENCODE_CONFIG_CONTENT", "OPENCODE_CONFIG_DIR", AGENT_RESULT_BUDGET_ENVIRONMENT, ...SELECTED_REGISTRATION_SETTINGS, ]; const inherited = Object.fromEntries( poisoned.flatMap((key) => [ [key, "poison"], [ key.toLowerCase().replace(/^./u, (letter) => letter.toUpperCase()), "poison", ], ]), ); inherited.PATH = "fixture-path"; inherited.HOME = "caller-home"; inherited.KEEP_ME = "preserved"; const before = { ...inherited }; const environment = openCodeVerificationEnvironment({ inherited, account, profile, lab, dialect, runId: "fixture-run", }); expect(inherited).toEqual(before); expect(Object.values(environment)).not.toContain("poison"); expect(environment).toMatchObject({ HOME: account, USERPROFILE: account, PATH: "fixture-path", KEEP_ME: "preserved", OPENCODE_CONFIG_DIR: profile, REA_MCP_TOOL_CATALOG_PROFILE: "full", XDG_CONFIG_HOME: join(lab, "config"), XDG_CACHE_HOME: join(lab, "cache"), XDG_DATA_HOME: join(lab, "data"), XDG_STATE_HOME: join(lab, "state"), APPDATA: join(lab, "appdata", "roaming"), LOCALAPPDATA: join(lab, "appdata", "local"), REA_PROCESS_RUN_ID: "fixture-run", }); expect(environment.OPENCODE_PURE).toBe( dialect === "v1" ? "true" : undefined, ); }, ); it.each([ "opencode.json", "opencode.jsonc", ".opencode/opencode.json", ".opencode/opencode.jsonc", ])("refuses the ancestor layer %s without changing it", async (name) => { const root = await createTestTempDirectory("rea-opencode-ancestor-"); const workspace = join(root, "workspace"); await mkdir(workspace); await expect( assertIsolatedOpenCodeSources(workspace, "v2", contextFor(root)), ).resolves.toBeUndefined(); await mkdir(join(root, ".opencode"), { recursive: true }); const path = join(root, name); await writeFile(path, "{}"); await expect( assertIsolatedOpenCodeSources(workspace, "v2", contextFor(root)), ).rejects.toThrow(path); await expect( assertIsolatedOpenCodeSources(workspace, "v1", contextFor(root)), ).resolves.toBeUndefined(); await unlink(path); await expect( assertIsolatedOpenCodeSources(workspace, "v2", contextFor(root)), ).resolves.toBeUndefined(); }); it("preserves filesystem failures instead of treating them as missing layers", async () => { const root = await createTestTempDirectory("rea-opencode-ancestor-error-"); const file = join(root, "file"); await writeFile(file, "not a directory"); await expect( assertIsolatedOpenCodeSources( join(file, "workspace"), "v2", contextFor(root), ), ).rejects.toHaveProperty("code", "ENOTDIR"); }); it.each(["plugin", "plugins"])( "refuses an ancestor %s directory without a config file", async (name) => { const root = await createTestTempDirectory("rea-opencode-plugin-"); const workspace = join(root, "workspace"); await mkdir(workspace); const pluginDirectory = join(root, ".opencode", name); await mkdir(pluginDirectory, { recursive: true }); await writeFile( join(pluginDirectory, "example.ts"), "export default {};\n", ); await expect( assertIsolatedOpenCodeSources(workspace, "v2", contextFor(root)), ).rejects.toThrow(pluginDirectory); }, ); it.each(["opencode.json", "plugins/example.js"])( "refuses a system source at %s", async (name) => { const root = await createTestTempDirectory("rea-opencode-system-"); const workspace = join(root, "workspace"); await mkdir(workspace); const context = contextFor(root); const path = join(context.systemPaths.openCodeSystemDirectory, name); await mkdir(join(context.systemPaths.openCodeSystemDirectory, "plugins"), { recursive: true, }); await writeFile(path, name.endsWith(".js") ? "export default {};\n" : "{}"); await expect( assertIsolatedOpenCodeSources(workspace, "v2", context), ).rejects.toThrow( name.endsWith(".js") ? join(context.systemPaths.openCodeSystemDirectory, "plugins") : path, ); }, ); it.skipIf(process.platform !== "darwin")( "refuses machine-managed OpenCode preferences", async () => { const root = await createTestTempDirectory("rea-opencode-managed-"); const workspace = join(root, "workspace"); await mkdir(workspace); const context = contextFor(root); const path = join( context.systemPaths.openCodeManagedPreferencesDirectory, "ai.opencode.managed.plist", ); await mkdir(context.systemPaths.openCodeManagedPreferencesDirectory); await writeFile(path, "managed preferences"); await expect( assertIsolatedOpenCodeSources(workspace, "v2", context), ).rejects.toThrow(path); }, ); it("refuses discovered plugins before invoking the selected client", async () => { const root = await createTestTempDirectory("rea-opencode-preflight-"); const pluginDirectory = join(root, ".opencode", "plugins"); await mkdir(pluginDirectory, { recursive: true }); await writeFile(join(pluginDirectory, "example.ts"), "export default {};\n"); const script = fileURLToPath( new URL("../../../scripts/verify-opencode-client.mjs", import.meta.url), ); const repo = fileURLToPath(new URL("../../../", import.meta.url)); const result = spawnSync(process.execPath, [script, "scoped-chat"], { encoding: "utf8", timeout: 15_000, env: { ...process.env, TMPDIR: root, TMP: root, TEMP: root, REA_VERIFY_RUNTIME_ROOT: repo, REA_VERIFY_OPENCODE_DIALECT: "v2", REA_VERIFY_OPENCODE_COMMAND: join(root, "client-must-not-start"), }, }); expect(result.error).toBeUndefined(); expect(result.status).not.toBe(0); expect(result.stderr).toContain( "uncontrolled configuration or plugin source", ); expect(result.stderr).not.toContain("OpenCode preflight failed"); });