diff --git a/test/e2e/mechanical.test.ts b/test/e2e/mechanical.test.ts index 3b21e1e92..edd3477d7 100644 --- a/test/e2e/mechanical.test.ts +++ b/test/e2e/mechanical.test.ts @@ -24,6 +24,29 @@ import { importFromContent } from '../../src/core/import-file.ts'; const skip = !hasDatabase(); const describeE2E = skip ? describe.skip : describe; +// HOME isolation. Several tests in this file shell out to `gbrain init` and +// `gbrain import` via Bun.spawnSync. `gbrain init` calls saveConfig() which +// writes to $HOME/.gbrain/config.json, and `gbrain import` writes a sync +// bookmark to the same directory. Without isolating $HOME, these tests +// clobber the user's real production gbrain config every time `bun run +// test:e2e` is executed. Sibling test/e2e/migration-flow.test.ts solved +// this with a module-level temp HOME; mirror that pattern here so the +// E2E suite stops mutating user state. +let _origHome: string | undefined; +let _tmpHome: string | undefined; +if (!skip) { + _origHome = process.env.HOME; + _tmpHome = mkdtempSync(join(tmpdir(), 'gbrain-e2e-mechanical-home-')); + process.env.HOME = _tmpHome; +} + +afterAll(() => { + if (skip) return; + if (_origHome === undefined) delete process.env.HOME; + else process.env.HOME = _origHome; + try { if (_tmpHome) rmSync(_tmpHome, { recursive: true, force: true }); } catch { /* best-effort */ } +}); + function makeCtx(opts: { remote?: boolean } = {}): OperationContext { return { engine: getEngine(),