From 9f59e0514ed7754eb7ed7dd95656ac8a7e719a8b Mon Sep 17 00:00:00 2001 From: iceteaSA <171169159+iceteaSA@users.noreply.github.com> Date: Wed, 16 Sep 2026 16:23:32 +0200 Subject: [PATCH] fix(pi): isolate the test suite from the host config directory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `packages/pi` had no test preload, so its suite inherited the host's config directories. One test read the operator's live `~/.config/opencode/anthropic-auth.json` and passed because of what happened to be in it — on a machine without that file it failed, which is CI and anyone who clones the repo. Varying one variable at a time against the failing test on a developer machine: unsetting `HOME`, `XDG_CONFIG_HOME`, or `OPENCODE_CONFIG_DIR` each turned it red, while `PI_AGENT_DIR` did not. Pi's own agent dir is not the dependency; `getConfigDir()` in `packages/core/src/accounts.ts` is, and Pi tests reach core code that falls back to it. Pre-existing rather than caused by a recent change — a clean detached worktree at `main` fails identically under an empty `HOME`. `packages/core` and `packages/opencode` each received this preload on 2026-09-07; `packages/pi` was missed. The preload deliberately does not restore the host's original values. Restoring them in `afterEach` re-exposed host config mid-run: Bun runs `afterEach` inner-file-first and the preload's hook last, so a per-file `afterAll` saw them again — and `commands.test.ts` asserts `OPENCODE_ANTHROPIC_AUTH_STATE_FILE` is unset. Restoring is also unnecessary, since this is in-process mutation that cannot reach the operator's shell and every `beforeEach` re-isolates. --- packages/pi/bunfig.toml | 2 ++ packages/pi/src/tests/setup.ts | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 packages/pi/bunfig.toml create mode 100644 packages/pi/src/tests/setup.ts diff --git a/packages/pi/bunfig.toml b/packages/pi/bunfig.toml new file mode 100644 index 00000000..1c8dc619 --- /dev/null +++ b/packages/pi/bunfig.toml @@ -0,0 +1,2 @@ +[test] +preload = ["./src/tests/setup.ts"] diff --git a/packages/pi/src/tests/setup.ts b/packages/pi/src/tests/setup.ts new file mode 100644 index 00000000..4b03eec9 --- /dev/null +++ b/packages/pi/src/tests/setup.ts @@ -0,0 +1,35 @@ +import { afterEach, beforeEach } from 'bun:test' +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +let testDir: string | undefined +const HOST_PATH_ENV_VARS = [ + 'OPENCODE_CONFIG_DIR', + 'OPENCODE_ANTHROPIC_AUTH_FILE', + 'OPENCODE_ANTHROPIC_AUTH_STATE_FILE', + 'PI_AGENT_DIR', + 'PI_ANTHROPIC_AUTH_FILE', + 'PI_ANTHROPIC_AUTH_ROUTING_STATE_FILE', + 'PI_ANTHROPIC_AUTH_CACHEKEEP_REGISTRY_DIR', +] as const + +beforeEach(async () => { + testDir = await mkdtemp(join(tmpdir(), 'anthropic-auth-pi-test-')) + for (const key of HOST_PATH_ENV_VARS) delete process.env[key] + process.env.OPENCODE_CONFIG_DIR = join(testDir, 'opencode') + process.env.OPENCODE_ANTHROPIC_AUTH_FILE = join( + testDir, + 'anthropic-auth.json', + ) + process.env.OPENCODE_ANTHROPIC_AUTH_STATE_FILE = join( + testDir, + 'anthropic-auth-state.json', + ) + process.env.PI_AGENT_DIR = join(testDir, '.pi-agent') +}) + +afterEach(async () => { + if (testDir) await rm(testDir, { recursive: true, force: true }) + testDir = undefined +})