From 10adb5ca67fa387a99d65ba610c800a81eb748d0 Mon Sep 17 00:00:00 2001 From: garrytan Date: Mon, 2 Nov 2026 16:18:08 +0000 Subject: [PATCH] test: an exported live-brain URL must not reach a temp-HOME gbrain child (#3466) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- test/state-root-config-merge.test.ts | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/state-root-config-merge.test.ts b/test/state-root-config-merge.test.ts index a0e5cd73e..d6a1a25e5 100644 --- a/test/state-root-config-merge.test.ts +++ b/test/state-root-config-merge.test.ts @@ -163,4 +163,42 @@ describe('test preload hermeticity', () => { expect(r.stderr).toContain('test-setup: stripped inherited GSTACK_STATE_DIR; tests isolate state via GSTACK_HOME'); expect(r.stderr.match(/test-setup: stripped/g)?.length).toBe(2); }); + + test('bun test strips an inherited live-brain URL, so a temp-HOME gbrain child never sees it (#3466)', () => { + const live = path.join(tmp, 'live-gbrain'); + fs.mkdirSync(live, { recursive: true }); + fs.writeFileSync(path.join(live, 'config.json'), JSON.stringify({ database_url: 'postgresql://live-config.invalid/brain' })); + const childHome = path.join(tmp, 'child-home'); + fs.mkdirSync(childHome, { recursive: true }); + const probe = path.join(tmp, 'brain-probe.test.ts'); + fs.writeFileSync(probe, [ + "import { test, expect } from 'bun:test';", + "import { spawnSync } from 'child_process';", + "test('probe', () => {", + " const script = \"const { buildGbrainEnv } = require(\" + JSON.stringify(" + JSON.stringify(path.join(ROOT, 'lib', 'gbrain-exec.ts')) + ") + \"); const e = buildGbrainEnv(); console.log(['GBRAIN_DATABASE_URL','GBRAIN_DIRECT_DATABASE_URL','DATABASE_URL','GBRAIN_HOME'].map(k => k + '=' + (e[k] ?? '')).join(' '));\";", + " const r = spawnSync(process.execPath, ['--no-env-file', '--config=/dev/null', '-e', script], {", + " env: { ...process.env, HOME: " + JSON.stringify(childHome) + " }, encoding: 'utf-8', timeout: 30_000,", + " });", + " expect(r.stdout.trim()).toBe('GBRAIN_DATABASE_URL= GBRAIN_DIRECT_DATABASE_URL= DATABASE_URL= GBRAIN_HOME=');", + "});", + ].join('\n')); + const r = spawnSync('bun', ['test', '--preload', path.join(ROOT, 'test-setup.ts'), probe], { + cwd: tmp, + env: { + ...env, GSTACK_HOME: resolved, + GBRAIN_DATABASE_URL: 'postgresql://live-brain.invalid/brain', + GBRAIN_DIRECT_DATABASE_URL: 'postgresql://live-brain.invalid/direct', + DATABASE_URL: 'postgresql://live-brain.invalid/fallback', + GBRAIN_HOME: live, + }, + encoding: 'utf-8', + timeout: 60_000, + }); + expect(r.stderr).not.toContain('live-brain.invalid'); + expect(r.stderr).not.toContain('live-config.invalid'); + expect(r.status).toBe(0); + for (const name of ['GBRAIN_DATABASE_URL', 'GBRAIN_DIRECT_DATABASE_URL', 'DATABASE_URL', 'GBRAIN_HOME']) { + expect(r.stderr).toContain(`test-setup: stripped inherited ${name}; tests must not reach a live gbrain`); + } + }); });