From 58a2dc860aa1848d7115faf7f3e3e6b822df4769 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Tue, 8 Sep 2026 07:57:30 +0000 Subject: [PATCH] test: isolate browser daemon state across free shards Co-Authored-By: OpenAI Codex --- scripts/test-free-shards.ts | 5 ++++ test/test-free-shards.test.ts | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/scripts/test-free-shards.ts b/scripts/test-free-shards.ts index 0dfb18397..62588a779 100755 --- a/scripts/test-free-shards.ts +++ b/scripts/test-free-shards.ts @@ -1223,6 +1223,11 @@ export async function runFreeShard( env.TMPDIR = childTmp; env.TEMP = childTmp; env.TMP = childTmp; + // CLI renders otherwise attach to the repo's shared .gstack/browse.json, + // even with distinct Chromium profiles. Concurrent shards and surviving + // daemons from prior runs can then replace or remove each other's state. + // Override inherited state too; the shard owns this directory's cleanup. + env.BROWSE_STATE_FILE = path.join(stateDir, '.gstack', 'browse.json'); // Per-shard Chromium profile (same isolation idea as TMPDIR): nine test // files launch in-process persistent contexts or daemons that default to // the SHARED ~/.gstack/chromium-profile, and two concurrent shards on one diff --git a/test/test-free-shards.test.ts b/test/test-free-shards.test.ts index 30c8ee0ac..a2e178638 100644 --- a/test/test-free-shards.test.ts +++ b/test/test-free-shards.test.ts @@ -321,6 +321,59 @@ describe('test-free-shards: strict shard execution', () => { fs.rmSync(captureDir, { recursive: true, force: true }); } }); + + test('concurrent shards isolate browser state and remove it on success or failure', async () => { + const captureDir = fs.mkdtempSync(path.join(os.tmpdir(), 'free-shard-browse-')); + const inheritedState = path.join(captureDir, 'host-browse.json'); + const parentState = process.env.BROWSE_STATE_FILE; + fs.writeFileSync(inheritedState, 'host daemon state'); + try { + const outcomes = await Promise.all([0, 3].map((exitCode, index) => { + const dump = path.join(captureDir, `shard-${index}.json`); + const script = ` + const fs = require('fs'); + const path = require('path'); + const { resolveConfig } = require(${JSON.stringify(path.join(ROOT, 'browse/src/config.ts'))}); + const config = resolveConfig(); + fs.mkdirSync(config.stateDir, { recursive: true }); + fs.writeFileSync(config.stateFile, ${JSON.stringify(String(index))}); + fs.writeFileSync(${JSON.stringify(dump)}, JSON.stringify({ + state: process.env.BROWSE_STATE_FILE, + resolvedState: config.stateFile, + tmp: process.env.TMPDIR, + profile: process.env.CHROMIUM_PROFILE, + written: fs.readFileSync(config.stateFile, 'utf8'), + })); + console.log(${JSON.stringify(SUMMARY_1)}); + process.exit(${exitCode}); + `; + return runFreeShard(['browser-state'], index + 1, 2, { + env: { ...process.env, BROWSE_STATE_FILE: inheritedState }, + commandFor: () => ({ command: process.execPath, args: ['-e', script] }), + quiet: true, + log: () => {}, + }); + })); + expect(outcomes.map((outcome) => outcome.status)).toEqual(['passed', 'failed']); + const seen = [0, 1].map((index) => JSON.parse(fs.readFileSync(path.join(captureDir, `shard-${index}.json`), 'utf8'))); + expect(seen[0].state).not.toBe(seen[1].state); + expect(seen[0].profile).not.toBe(seen[1].profile); + for (const [index, shard] of seen.entries()) { + expect(shard.state).not.toBe(inheritedState); + expect(shard.resolvedState).toBe(shard.state); + expect(shard.written).toBe(String(index)); + const ownedRoot = path.dirname(shard.tmp); + expect(path.relative(ownedRoot, shard.state)).toBe(path.join('.gstack', 'browse.json')); + expect(path.dirname(shard.profile)).toBe(ownedRoot); + expect(fs.existsSync(ownedRoot)).toBe(false); + expect(fs.existsSync(shard.state)).toBe(false); + } + expect(fs.readFileSync(inheritedState, 'utf8')).toBe('host daemon state'); + expect(process.env.BROWSE_STATE_FILE).toBe(parentState); + } finally { + fs.rmSync(captureDir, { recursive: true, force: true }); + } + }); }); describe('test-free-shards: output contract (log capture, quiet console, failure epilogue)', () => {