|
| 1 | +import type { LaunchOptions, PuppeteerNode } from "puppeteer-core"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { |
| 4 | + _resetAutoBrowserGpuModeCacheForTests, |
| 5 | + _resetBrowserPoolForTests, |
| 6 | + _setPuppeteerForTests, |
| 7 | + acquireBrowser, |
| 8 | + drainBrowserPool, |
| 9 | +} from "./browserManager.js"; |
| 10 | + |
| 11 | +vi.mock("./chromeStartup.js", async (importActual) => ({ |
| 12 | + ...(await importActual<typeof import("./chromeStartup.js")>()), |
| 13 | + explainChromeLaunchFailure: async (error: unknown) => error, |
| 14 | +})); |
| 15 | + |
| 16 | +const chrome = () => ({ |
| 17 | + connected: true, |
| 18 | + newPage: vi.fn().mockResolvedValue({ evaluate: vi.fn() }), |
| 19 | + version: vi.fn().mockResolvedValue("HeadlessChrome/152.0.7977.30"), |
| 20 | + close: vi.fn().mockResolvedValue(undefined), |
| 21 | + disconnect: vi.fn(), |
| 22 | + process: () => ({ kill: vi.fn(), killed: false }), |
| 23 | +}); |
| 24 | + |
| 25 | +const denied = new Error( |
| 26 | + "Failed to launch: bootstrap_check_in org.chromium.Chromium.MachPortRendezvousServer: Permission denied (1100)", |
| 27 | +); |
| 28 | + |
| 29 | +describe("engine Chrome launch inside an agent seatbelt", () => { |
| 30 | + beforeEach(() => { |
| 31 | + vi.stubEnv("PRODUCER_HEADLESS_SHELL_PATH", process.execPath); |
| 32 | + vi.spyOn(console, "log").mockImplementation(() => {}); |
| 33 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + await drainBrowserPool(); |
| 38 | + _resetBrowserPoolForTests(); |
| 39 | + _resetAutoBrowserGpuModeCacheForTests(); |
| 40 | + _setPuppeteerForTests(undefined); |
| 41 | + vi.unstubAllEnvs(); |
| 42 | + vi.restoreAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("retries once in one process over a pipe when the mach service is refused", async () => { |
| 46 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 47 | + const launch = vi.fn<(options: LaunchOptions) => Promise<ReturnType<typeof chrome>>>(); |
| 48 | + launch.mockRejectedValueOnce(denied).mockResolvedValue(chrome()); |
| 49 | + _setPuppeteerForTests({ launch } as unknown as PuppeteerNode); |
| 50 | + const lease = await acquireBrowser(["--no-sandbox"], { forceScreenshot: true }); |
| 51 | + await lease.release(); |
| 52 | + expect(launch).toHaveBeenCalledTimes(2); |
| 53 | + const [first, second] = launch.mock.calls.map(([options]) => options); |
| 54 | + expect(first?.args).not.toContain("--single-process"); |
| 55 | + expect(second?.pipe).toBe(true); |
| 56 | + expect(second?.args).toContain("--single-process"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("launches one process over a pipe at once when the host names a seatbelt", async () => { |
| 60 | + vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 61 | + vi.stubEnv("CODEX_SANDBOX", "seatbelt"); |
| 62 | + const launch = vi.fn(async (_options: LaunchOptions) => chrome()); |
| 63 | + _setPuppeteerForTests({ launch } as unknown as PuppeteerNode); |
| 64 | + const lease = await acquireBrowser(["--no-sandbox"], { forceScreenshot: true }); |
| 65 | + await lease.release(); |
| 66 | + const calls = launch.mock.calls.map(([options]) => options); |
| 67 | + expect(calls).toHaveLength(1); |
| 68 | + expect(calls[0]?.pipe).toBe(true); |
| 69 | + expect(calls[0]?.args).toContain("--single-process"); |
| 70 | + }); |
| 71 | + |
| 72 | + it.each([ |
| 73 | + ["a refused mach service", denied], |
| 74 | + ["a pipe launch that closed", Object.assign(new Error("closed"), { name: "TargetCloseError" })], |
| 75 | + ])("on Linux launches once and keeps %s as the failure", async (_, failure) => { |
| 76 | + vi.spyOn(process, "platform", "get").mockReturnValue("linux"); |
| 77 | + vi.stubEnv("CODEX_SANDBOX", "seatbelt"); |
| 78 | + const launch = vi.fn<(options: LaunchOptions) => Promise<ReturnType<typeof chrome>>>(); |
| 79 | + launch.mockRejectedValue(failure); |
| 80 | + _setPuppeteerForTests({ |
| 81 | + launch, |
| 82 | + defaultArgs: vi.fn().mockResolvedValue([]), |
| 83 | + } as unknown as PuppeteerNode); |
| 84 | + const error = await acquireBrowser(["--no-sandbox"], { forceScreenshot: true }).catch((e) => e); |
| 85 | + expect(launch).toHaveBeenCalledTimes(1); |
| 86 | + expect(launch.mock.calls[0]?.[0].args).not.toContain("--single-process"); |
| 87 | + expect(error).toBe(failure); |
| 88 | + }); |
| 89 | +}); |
0 commit comments