|
1 | | -import type { LaunchOptions, PuppeteerNode } from "puppeteer-core"; |
2 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import { EventEmitter } from "node:events"; |
| 2 | +import type { Browser, LaunchOptions, PuppeteerNode } from "puppeteer-core"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
3 | 4 | import { launchManagedBrowser } from "./launch.js"; |
4 | 5 |
|
5 | 6 | const SEATBELT_LAUNCH_FAILURE = [ |
@@ -28,3 +29,43 @@ describe("a browser launch a sandbox refuses", () => { |
28 | 29 | await expect(launchManagedBrowser(failingPuppeteer(other), {})).rejects.toThrow(other); |
29 | 30 | }); |
30 | 31 | }); |
| 32 | + |
| 33 | +const fakeBrowser = () => ({ process: () => new EventEmitter() }) as unknown as Browser; |
| 34 | + |
| 35 | +function recordingPuppeteer(failures: string[]) { |
| 36 | + const launches: LaunchOptions[] = []; |
| 37 | + const puppeteer = { |
| 38 | + launch: async (options: LaunchOptions) => { |
| 39 | + launches.push(options); |
| 40 | + const failure = failures.shift(); |
| 41 | + if (failure) throw new Error(failure); |
| 42 | + return fakeBrowser(); |
| 43 | + }, |
| 44 | + } as unknown as PuppeteerNode; |
| 45 | + return { puppeteer, launches }; |
| 46 | +} |
| 47 | + |
| 48 | +describe("a browser inside an agent's macOS seatbelt", () => { |
| 49 | + const platform = process.platform; |
| 50 | + afterEach(() => { |
| 51 | + vi.unstubAllEnvs(); |
| 52 | + Object.defineProperty(process, "platform", { value: platform }); |
| 53 | + }); |
| 54 | + |
| 55 | + it("starts as one process over a pipe when Codex names its seatbelt", async () => { |
| 56 | + vi.stubEnv("CODEX_SANDBOX", "seatbelt"); |
| 57 | + Object.defineProperty(process, "platform", { value: "darwin" }); |
| 58 | + const { puppeteer, launches } = recordingPuppeteer([]); |
| 59 | + await launchManagedBrowser(puppeteer, { args: ["--headless"] }); |
| 60 | + expect(launches).toHaveLength(1); |
| 61 | + expect(launches[0]).toMatchObject({ pipe: true, args: ["--headless", "--single-process"] }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("relaunches as one process over a pipe when an unnamed sandbox refuses the mach service", async () => { |
| 65 | + vi.stubEnv("CODEX_SANDBOX", ""); |
| 66 | + const { puppeteer, launches } = recordingPuppeteer([SEATBELT_LAUNCH_FAILURE]); |
| 67 | + await expect(launchManagedBrowser(puppeteer, { args: ["--headless"] })).resolves.toBeDefined(); |
| 68 | + expect(launches.map((options) => options.pipe ?? false)).toEqual([false, true]); |
| 69 | + expect(launches[1]!.args).toEqual(["--headless", "--single-process"]); |
| 70 | + }); |
| 71 | +}); |
0 commit comments