import { afterEach, beforeEach, describe, it, expect } from 'bun:test'; // ─── BrowserManager basic unit tests ───────────────────────────── describe('BrowserManager defaults', () => { it('getConnectionMode defaults to launched', async () => { const { BrowserManager } = await import('../src/browser-manager'); const bm = new BrowserManager(); expect(bm.getConnectionMode()).toBe('launched'); }); it('getRefMap returns empty array initially', async () => { const { BrowserManager } = await import('../src/browser-manager'); const bm = new BrowserManager(); expect(bm.getRefMap()).toEqual([]); }); }); // ─── shouldEnableChromiumSandbox ───────────────────────────────── // // Pinning this is what prevents the "--no-sandbox" yellow infobar from // regressing on headed launches. Playwright auto-adds --no-sandbox when // chromiumSandbox !== true (playwright-core chromium.js:291-292), so all // three launch sites in browser-manager.ts must pass the policy this // helper computes. describe('shouldEnableChromiumSandbox', () => { const origPlatform = process.platform; const origCI = process.env.CI; const origContainer = process.env.CONTAINER; const origGetuid = process.getuid; beforeEach(() => { delete process.env.CI; delete process.env.CONTAINER; }); afterEach(() => { Object.defineProperty(process, 'platform', { value: origPlatform }); if (origCI === undefined) delete process.env.CI; else process.env.CI = origCI; if (origContainer === undefined) delete process.env.CONTAINER; else process.env.CONTAINER = origContainer; process.getuid = origGetuid; }); function setPlatform(p: NodeJS.Platform) { Object.defineProperty(process, 'platform', { value: p }); } it('darwin, no CI/CONTAINER/root → true', async () => { setPlatform('darwin'); process.getuid = (() => 501) as typeof process.getuid; const { shouldEnableChromiumSandbox } = await import('../src/browser-manager'); expect(shouldEnableChromiumSandbox()).toBe(true); }); it('linux, no CI/CONTAINER/root → true', async () => { setPlatform('linux'); process.getuid = (() => 1000) as typeof process.getuid; const { shouldEnableChromiumSandbox } = await import('../src/browser-manager'); expect(shouldEnableChromiumSandbox()).toBe(true); }); it('win32 → false (sandbox fails in Bun→Node→Chromium chain)', async () => { setPlatform('win32'); process.getuid = (() => 1000) as typeof process.getuid; const { shouldEnableChromiumSandbox } = await import('../src/browser-manager'); expect(shouldEnableChromiumSandbox()).toBe(false); }); it('linux + CI=1 → false', async () => { setPlatform('linux'); process.env.CI = '1'; process.getuid = (() => 1000) as typeof process.getuid; const { shouldEnableChromiumSandbox } = await import('../src/browser-manager'); expect(shouldEnableChromiumSandbox()).toBe(false); }); it('linux + CONTAINER=1 → false', async () => { setPlatform('linux'); process.env.CONTAINER = '1'; process.getuid = (() => 1000) as typeof process.getuid; const { shouldEnableChromiumSandbox } = await import('../src/browser-manager'); expect(shouldEnableChromiumSandbox()).toBe(false); }); it('linux + root (uid 0) → false', async () => { setPlatform('linux'); process.getuid = (() => 0) as typeof process.getuid; const { shouldEnableChromiumSandbox } = await import('../src/browser-manager'); expect(shouldEnableChromiumSandbox()).toBe(false); }); });