import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; /** * WP-74: `playwright.config.ts` gives the backend a UNIQUE throwaway SQLite file * per `npm run e2e` invocation (`/big-register-e2e--.db`), * so Playwright has no webServer teardown hook to delete it once a run ends — this * sweeps them up instead, at the START of the NEXT run. * * Safe regardless of run ordering: `globalSetup` always executes AFTER `webServer` * has already started (Playwright's task order, not something a config can flip), * so THIS run's own file (`process.env['E2E_DB_PATH']`, set by the config module — * same node process, so the assignment is visible here) is always excluded. Every * OTHER matching file belongs to an invocation whose `dotnet run` process has * already exited, so deleting it can't race a live connection. */ export default function globalSetup(): void { const mine = process.env['E2E_DB_PATH']; const dir = os.tmpdir(); let entries: string[]; try { entries = fs.readdirSync(dir); } catch { return; // best-effort cleanup — a missing/unreadable temp dir isn't this run's problem } for (const name of entries) { if (!name.startsWith('big-register-e2e-')) continue; const base = name.replace(/(-shm|-wal)$/, ''); if (mine && base === path.basename(mine)) continue; // never this run's own file try { fs.unlinkSync(path.join(dir, name)); } catch { // best-effort — a file another leftover process still has open, or already // gone, is not worth failing this run's e2e suite over. } } }