// The umtool e2e suite's refusal of an inherited data root. // // Playwright hands the app server, the editor stub and every CLI a spec spawns // this shell's whole environment. The config confines what it sets (the // fixture's SONG_DIR, CHANNELS_DIR, ...), but a variable it does NOT set wins // by inheritance: REPORTS_DIR is read before the fixture's SONG_REPORTS_DIR // (lib/paths.mjs REPORTS_ROOT), and a spec's own spawn of a CLI with // `{ ...process.env }` carries CHANNELS_DIR and SONG_DIR along. On 2 Nov a run // that inherited them wrote fixture state into real projects under ~/reports. // // So a data root in the environment is refused, loudly and by name, unless it // already points inside the suite's own fixture. Unset (or empty) is the // normal case; the gate scripts `unset REPORTS_DIR CHANNELS_DIR SONG_DIR`. // // SONG_DIR IS NOT ONE OF THEM. It is the documented way to run the song-data // specs against a copy (AGENTS.md; e2e/fixtures/make-fixture.mjs), and only the // fixture builder reads it -- READ-ONLY, to derive the empty-state copy and link // the heavy audio; the app server is given the fixture's own SONG_DIR after it. import { realpathSync } from "node:fs"; import path from "node:path"; /** The variables that name a data root the suite must never inherit. */ export const INHERITED_DATA_ROOTS = ["REPORTS_DIR", "CHANNELS_DIR"]; /** A path with its links resolved (a worktree reached through a symlinked parent), else as given. */ const real = (p) => { const abs = path.resolve(p); try { return realpathSync(abs); } catch { // Missing: resolve the nearest parent that exists, and keep the rest as written. const parent = path.dirname(abs); return parent === abs ? abs : path.join(real(parent), path.basename(abs)); } }; const inside = (p, root) => { const rel = path.relative(real(root), real(p)); return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel)); }; /** * Every inherited data root that is not the fixture's, as `{ name, value }`. * @param {Record} env * @param {string} fixture the suite's fixture directory (e2e/.e2e-song) */ export function foreignDataRoots(env, fixture) { const root = fixture; return INHERITED_DATA_ROOTS.filter((name) => { const v = env[name]; return typeof v === "string" && v.trim() !== "" && !inside(v, root); }).map((name) => ({ name, value: env[name] })); } /** The refusal, naming each variable; null when there is nothing to refuse. */ export function dataRootRefusal(env, fixture) { const bad = foreignDataRoots(env, fixture); if (!bad.length) return null; return ( `umtool e2e refuses to run: ${bad.map((b) => `${b.name}=${b.value}`).join(", ")} ` + `${bad.length === 1 ? "is" : "are"} set in this shell and not the suite's fixture (${path.resolve(fixture)}). ` + `The app server and the CLIs the specs spawn inherit it and would read and write real data there ` + `(2 Nov: a run wrote fixture state into ~/reports). Unset it: \`unset ${bad.map((b) => b.name).join(" ")}\`.` ); }