import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; // WHERE A TEST SERVER'S DATA LIVES — one function, for the specs, the helpers // and the config (plans/e2e-speed.md S5: a server per worker needs every path a // spec touches to come from the worker's own root, not a literal). // // The root is the editor directory for the first worker (slot 0), and // `.e2e-workers/w` for the others (one server set per worker, started by // globalSetup — e2e/servers.ts); E2E_DATA_ROOT, when set, wins. Under it sit // the same names the suite has always used. `dev:test` / `start:test` read the // same variable (`${E2E_DATA_ROOT:-$(pwd)}`), so a server and the specs that // drive it agree by construction. One worker: the editor directory, exactly // the paths of before. // // The slot is Playwright's TEST_PARALLEL_INDEX (0 … workers−1, kept by a // restarted worker; not TEST_WORKER_INDEX, which grows with every restart). It // is set before the worker loads anything, so a spec's top-level constant // built from testPaths() is already its worker's. // // E2E_-prefixed on purpose, never TRANSCRIPTS_DIR / SETTINGS_FILE: a // `--share-data` worktree's `.worktree-env` sets TRANSCRIPTS_DIR to the REAL // corpus, and resetData() deletes this directory. const here = dirname(fileURLToPath(import.meta.url)); export const EDITOR_ROOT = resolve(here, ".."); export type TestPaths = { /** The data root: the editor directory, or E2E_DATA_ROOT. */ root: string; /** TRANSCRIPTS_DIR of the test server — what resetData() replaces. */ transcriptsDir: string; /** SETTINGS_FILE of the test server (and of the editor suite's export server). */ settingsFile: string; /** EXPORT_PUBLIC_DIR of the test server. */ exportPublicDir: string; /** EDITOR_CHANGELOG_FILE / EXPORT_CHANGELOG_FILE: the cut-release specs' disposable changelogs. */ editorChangelog: string; exportChangelog: string; }; export function testPathsFor(root: string): TestPaths { const transcriptsDir = join(root, "test-transcripts"); return { root, transcriptsDir, settingsFile: join(root, "test-settings.json"), exportPublicDir: join(transcriptsDir, ".export-public"), editorChangelog: join(root, "test-changelog.md"), exportChangelog: join(root, "test-export-changelog.md"), }; } /** The data root of a worker slot: the editor directory for slot 0. */ export function slotRoot(slot: number): string { return slot > 0 ? join(EDITOR_ROOT, ".e2e-workers", `w${slot}`) : EDITOR_ROOT; } /** This process's worker slot (0 outside a Playwright worker). */ export function currentSlot(env: Readonly> = process.env): number { const n = Number(env.TEST_PARALLEL_INDEX); return Number.isInteger(n) && n > 0 ? n : 0; } /** This process's test data paths (read per call, so a worker's env applies). */ export function testPaths(): TestPaths { const root = process.env.E2E_DATA_ROOT?.trim(); return testPathsFor(root ? resolve(root) : slotRoot(currentSlot())); }