import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; /** * One fixture build per Playwright run instead of one per browser project. * * The fixture specs build a copy of the repository in `beforeAll`, and every * browser project runs that hook again: one e2e run made the same builds * three times, synchronously, while the other workers drove browsers. Those * builds took the CPU from the browsers next to them, and Firefox's first * render in a test then missed its 5 s expectation (FE-GATE-008, desktop and * runner). Builds are the same for every engine and no test writes into them. * * With `SHARED_FIXTURE_BUILD_ROOT` set (the e2e config's global setup sets it * for the run), the first worker that needs `key` builds it under that root and * the others wait for it and reuse it; the global teardown removes the root. * Unset (a Vitest test, another config), every call builds its own copy in a * new temp folder as before. */ export const SHARED_FIXTURE_BUILD_ROOT_VARIABLE = "SHARED_FIXTURE_BUILD_ROOT"; const POLL_MS = 200; const KEY = /^[a-z0-9][a-z0-9-]{0,63}$/u; export type SharedFixtureBuild = Readonly<{ /** The folder `create` built into. */ root: string; value: Value; /** Owned by the run (not removable by the spec) when true. */ shared: boolean; }>; type BuildRecord = Readonly<{ fingerprint: string; value: unknown }>; function hasCode(error: unknown, code: string): boolean { return typeof error === "object" && error !== null && "code" in error && error.code === code; } async function readRecord(file: string): Promise { try { return JSON.parse(await readFile(file, "utf8")) as BuildRecord; } catch (error) { if (hasCode(error, "ENOENT")) return undefined; throw error; } } async function readFailure(file: string): Promise { try { return await readFile(file, "utf8"); } catch (error) { if (hasCode(error, "ENOENT")) return undefined; throw error; } } /** * `create(root)` fills the empty folder `root` and returns a JSON value that * describes it (paths under `root`). `fingerprint` names what `create` builds; * a second caller of the same `key` with another fingerprint is refused. */ export async function sharedFixtureBuild( key: string, fingerprint: string, create: (root: string) => Promise, environment: NodeJS.ProcessEnv = process.env, ): Promise> { if (!KEY.test(key)) throw new Error(`Invalid fixture build key: ${key}`); const sharedRoot = environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE]; if (!sharedRoot) { const root = await mkdtemp(path.join(tmpdir(), `${key}-`)); try { return { root, value: await create(root), shared: false }; } catch (error) { await rm(root, { recursive: true, force: true }); throw error; } } const root = path.join(sharedRoot, key); const recordFile = `${root}.json`; const failureFile = `${root}.failed`; let owner = true; try { await mkdir(root); } catch (error) { if (!hasCode(error, "EEXIST")) throw error; owner = false; } if (owner) { let value: Value; try { value = await create(root); } catch (error) { await writeFile(failureFile, String(error instanceof Error ? error.stack : error)); throw error; } const temporary = `${recordFile}.${process.pid}.tmp`; await writeFile(temporary, JSON.stringify({ fingerprint, value })); await rename(temporary, recordFile); return { root, value, shared: true }; } // Another worker builds `key`; the spec's own hook timeout bounds this wait. for (;;) { const record = await readRecord(recordFile); if (record) { if (record.fingerprint !== fingerprint) { throw new Error( `Fixture build ${key} was built from another definition in this run.`, ); } return { root, value: record.value as Value, shared: true }; } const failure = await readFailure(failureFile); if (failure !== undefined) { throw new Error(`Fixture build ${key} failed in another worker:\n${failure}`); } await new Promise((resolve) => setTimeout(resolve, POLL_MS)); } } /** The e2e config's global setup: a shared root for this run, and its teardown. */ export async function startSharedFixtureBuilds( environment: NodeJS.ProcessEnv = process.env, ): Promise<() => Promise> { const root = await mkdtemp(path.join(tmpdir(), "e2e-fixture-builds-")); environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE] = root; return async () => { delete environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE]; await rm(root, { recursive: true, force: true }); }; }