import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, symlink } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { dataRootRefusal, foreignDataRoots } from "./e2eEnvGuard.mjs"; const FIX = "/w/umtool/e2e/.e2e-song"; test("nothing set, or set empty, is the normal case", () => { assert.equal(dataRootRefusal({}, FIX), null); assert.equal(dataRootRefusal({ REPORTS_DIR: "", CHANNELS_DIR: " " }, FIX), null); }); test("a root inside the fixture is the suite's own, and passes", () => { assert.equal(dataRootRefusal({ CHANNELS_DIR: `${FIX}/channels`, REPORTS_DIR: FIX }, FIX), null); }); test("an inherited REPORTS_DIR or CHANNELS_DIR anywhere else is refused, naming each variable", () => { const env = { REPORTS_DIR: "/home/u/reports", CHANNELS_DIR: "/home/u/transcripts/channels", HOME: "/home/u" }; assert.deepEqual(foreignDataRoots(env, FIX).map((b) => b.name), ["REPORTS_DIR", "CHANNELS_DIR"]); const msg = dataRootRefusal(env, FIX); assert.match(msg, /REPORTS_DIR=\/home\/u\/reports/); assert.match(msg, /CHANNELS_DIR=\/home\/u\/transcripts\/channels/); assert.match(msg, /unset REPORTS_DIR CHANNELS_DIR/); }); test("SONG_DIR elsewhere is accepted: the documented copy the fixture builder reads from, and nothing else sees", () => { assert.equal(dataRootRefusal({ SONG_DIR: "/elsewhere/song-copy/data" }, FIX), null); assert.deepEqual(foreignDataRoots({ SONG_DIR: "/elsewhere", REPORTS_DIR: "/home/u/reports" }, FIX).map((b) => b.name), ["REPORTS_DIR"]); }); test("a sibling whose name merely starts with the fixture's is not inside it", () => { assert.deepEqual(foreignDataRoots({ CHANNELS_DIR: `${FIX}-other/channels` }, FIX).map((b) => b.name), ["CHANNELS_DIR"]); assert.deepEqual(foreignDataRoots({ REPORTS_DIR: `${FIX}/../reports` }, FIX).map((b) => b.name), ["REPORTS_DIR"]); }); test("a worktree reached through a symlinked parent is its own fixture, either way round", async () => { const root = await mkdtemp(path.join(tmpdir(), "e2e-guard-")); try { const realFix = path.join(root, "real", "umtool", ".e2e-song"); await mkdir(path.join(realFix, "channels"), { recursive: true }); await symlink(path.join(root, "real"), path.join(root, "link")); const viaLink = path.join(root, "link", "umtool", ".e2e-song"); assert.equal(dataRootRefusal({ CHANNELS_DIR: path.join(viaLink, "channels") }, realFix), null); assert.equal(dataRootRefusal({ CHANNELS_DIR: path.join(realFix, "channels"), REPORTS_DIR: path.join(viaLink, "reports-not-yet") }, viaLink), null); assert.deepEqual(foreignDataRoots({ REPORTS_DIR: path.join(root, "real", "elsewhere") }, viaLink).map((b) => b.name), ["REPORTS_DIR"]); } finally { await rm(root, { recursive: true, force: true }); } });