Skip to content

test(e2e): isolate HOME in Setup Journey + Init Edge Cases blocks#205

Open
orendi84 wants to merge 1 commit intogarrytan:masterfrom
orendi84:fix/e2e-home-isolation
Open

test(e2e): isolate HOME in Setup Journey + Init Edge Cases blocks#205
orendi84 wants to merge 1 commit intogarrytan:masterfrom
orendi84:fix/e2e-home-isolation

Conversation

@orendi84
Copy link
Copy Markdown
Contributor

The bug

The E2E: Setup Journey block in test/e2e/mechanical.test.ts runs gbrain init --non-interactive --url $DATABASE_URL in a subprocess that inherits the real HOME env var. gbrain init calls saveConfig() (src/commands/init.ts:198) which writes to ~/.gbrain/config.json in whatever homedir() returns - i.e. the developer's real home directory.

Run this exact command from CONTRIBUTING.md:

DATABASE_URL=postgresql://postgres:postgres@localhost:5434/gbrain_test bun run test:e2e

...and your real ~/.gbrain/config.json gets silently overwritten with:

{"engine":"postgres","database_url":"postgresql://postgres:postgres@localhost:5434/gbrain_test"}

Any Supabase URL and API keys that were in the config are gone. The gbrain CLI then fails with 28P01 password authentication failed on the next real-brain query.

I hit this on my 13k-page brain today running migrations. Cost ~90 minutes of debugging before we realized tests had clobbered the config.

The fix

Override process.env.HOME to a fresh mkdtempSync() dir in beforeAll, restore in afterAll, and pass HOME explicitly to spawned subprocesses. This is the same pattern already used in test/e2e/migration-flow.test.ts (see lines 62-73, 80-90) - that file is the only other e2e test that had the discipline to isolate HOME correctly.

Applied the same fix defensively to E2E: Init Edge Cases even though its current test strips DATABASE_URL before spawning. If a future test in that block calls gbrain init with a URL, the isolation is already there.

Scope

  • One file changed: test/e2e/mechanical.test.ts
  • +36 lines, -4 lines
  • Zero production code changes
  • Zero new dependencies (mkdtempSync, rmSync, tmpdir, join already imported)
  • Test behavior unchanged, just safer re: developer's real config

Verification

Locally: file builds clean via bun build, matches the migration-flow.test.ts pattern which is already known-good in the existing E2E suite.

The Setup Journey block runs `gbrain init --non-interactive --url $DATABASE_URL`
in a subprocess that inherits the real HOME. On init, gbrain calls saveConfig()
which writes the test URL into the developer's real ~/.gbrain/config.json,
clobbering their prod Supabase URL + any API keys stored there.

On a developer machine running `DATABASE_URL=postgresql://postgres:postgres@localhost:5434/gbrain_test bun run test:e2e`
(the exact command in CONTRIBUTING.md), the test replaces the live config with:
  {"engine":"postgres","database_url":"postgresql://postgres:postgres@localhost:5434/gbrain_test"}

Same latent bug in Init Edge Cases (though the one existing test there strips
DATABASE_URL so it doesn't actually hit saveConfig, applied the fix defensively).

Fix: override process.env.HOME to a fresh mkdtempSync() dir in beforeAll, restore
in afterAll, and pass HOME explicitly to spawned subprocesses. Matches the
pattern used in test/e2e/migration-flow.test.ts (the only other e2e test that
had the discipline to do this correctly).

No production code changes. No new dependencies. +36/-4 lines.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant