import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { afterEach, expect, it, vi } from "vitest"; const accessible = vi.hoisted(() => new Set()); vi.mock("node:fs/promises", async (importOriginal) => { const fs = await importOriginal(); return { ...fs, access(path: string) { if (accessible.has(path)) return fs.access(path); return Promise.reject( new Error(`Unexpected Chromium candidate: ${path}`), ); }, }; }); const { findChromium } = await import("../src/layout.ts"); let cache: string | undefined; let originalBrowserPath: string | undefined; let originalPath: string | undefined; afterEach(async () => { if (originalBrowserPath === undefined) delete process.env["PLAYWRIGHT_BROWSERS_PATH"]; else process.env["PLAYWRIGHT_BROWSERS_PATH"] = originalBrowserPath; if (originalPath === undefined) delete process.env["PATH"]; else process.env["PATH"] = originalPath; if (cache) await rm(cache, { recursive: true, force: true }); cache = undefined; accessible.clear(); }); it("finds Chromium installed by the Playwright E2E harness", async () => { cache = await mkdtemp(join(tmpdir(), "pi-canvas-playwright-")); const executable = join( cache, "chromium-1208", ...(process.platform === "win32" ? ["chrome-win64", "chrome.exe"] : process.platform === "darwin" ? [ `chrome-mac-${process.arch}`, "Google Chrome for Testing.app", "Contents", "MacOS", "Google Chrome for Testing", ] : [ process.arch === "arm64" ? "chrome-linux-arm64" : "chrome-linux64", "chrome", ]), ); await mkdir(dirname(executable), { recursive: true }); await writeFile(executable, ""); originalBrowserPath = process.env["PLAYWRIGHT_BROWSERS_PATH"]; originalPath = process.env["PATH"]; process.env["PLAYWRIGHT_BROWSERS_PATH"] = cache; process.env["PATH"] = ""; accessible.add(executable); expect(await findChromium()).toBe(executable); });