import { after, test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; // Review fix 2 (lane/local-media): a library may not lie inside the corpus, a // saved-video store, a channel's media tier, a storage location, or the // checkout. One sentence each, at configure time and at use (preview). // Synthetic dirs; nothing outside the temp root is touched. const ROOT = mkdtempSync(path.join(tmpdir(), "local-media-location-")); const T = path.join(ROOT, "transcripts"); const DRIVE = path.join(ROOT, "drive"); const STORE = path.join(ROOT, "saved-videos"); const OWN_STORE = path.join(ROOT, "own-store"); const MEDIA = path.join(DRIVE, "media-tier", "other", "media"); const LOCATION = path.join(ROOT, "cold"); Object.assign(process.env, { TRANSCRIPTS_DIR: T, SAVED_VIDEOS_DIR: STORE, SETTINGS_FILE: path.join(ROOT, "settings.json"), ARCHILYZER_CONFIG_DIR: path.join(ROOT, "config"), }); after(() => rmSync(ROOT, { recursive: true, force: true })); writeFileSync(path.join(ROOT, "settings.json"), JSON.stringify({ storage: { locations: [{ id: "cold", label: "Cold drive", root: LOCATION, autoRepoint: false }], defaultLocationId: "cold" }, })); const json = (file: string, value: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, JSON.stringify(value)); }; json(path.join(T, "channels", "sample", "config.json"), { handling: "youtube", name: "Sample" }); json(path.join(T, "channels", "other", "config.json"), { handling: "youtube", name: "Other", mediaDir: MEDIA, savedVideosDir: OWN_STORE }); for (const d of [MEDIA, STORE, OWN_STORE, LOCATION, path.join(ROOT, "fine")]) mkdirSync(path.join(d, "lib"), { recursive: true }); mkdirSync(path.join(T, "channels", "other", "data", "x", "lib"), { recursive: true }); mkdirSync(path.join(T, "loose", "lib"), { recursive: true }); const { getPaths } = await import("../lib/paths"); const { libraryLocationProblem } = await import("./localMediaLocation"); const { configureLocalMedia } = await import("./configureLocalMedia"); const { previewLocalMedia } = await import("./localMediaImport"); const paths = getPaths(); test("each owned tree is refused with a sentence naming it; a directory outside all of them is fine", async () => { const cases: Array<[string, RegExp]> = [ [path.join(MEDIA, "lib"), /inside channel "other"'s media directory/], [path.join(OWN_STORE, "lib"), /inside channel "other"'s saved-video store/], [path.join(STORE, "lib"), /inside the saved-video store/], [path.join(T, "channels", "other", "data", "x", "lib"), /inside the corpus/], [path.join(T, "loose", "lib"), /inside the corpus/], [path.join(LOCATION, "lib"), /inside storage location "Cold drive"/], [path.join(paths.monorepoRoot, "editor"), /inside the Archilyzer checkout/], ]; for (const [dir, re] of cases) { const problem = await libraryLocationProblem(paths, dir); assert.ok(problem, dir); assert.match(problem!, re); assert.match(problem!, /; choose a directory outside it\.$/); } assert.equal(await libraryLocationProblem(paths, path.join(ROOT, "fine", "lib")), null); }); test("configure refuses a misplaced library and leaves the config as it was", async () => { const before = readFileSync(path.join(T, "channels", "sample", "config.json"), "utf8"); await assert.rejects(configureLocalMedia(paths, "sample", { sourceId: "library-1", directory: path.join(LOCATION, "lib") }), /inside storage location "Cold drive"/); assert.equal(readFileSync(path.join(T, "channels", "sample", "config.json"), "utf8"), before); }); test("a misplaced library written into the config by hand is refused at preview", async () => { json(path.join(T, "channels", "sample", "config.json"), { handling: "youtube", name: "Sample", localMedia: { sourceId: "library-1", directory: path.join(STORE, "lib") } }); await assert.rejects(previewLocalMedia(paths, "sample"), /inside the saved-video store/); }); // Re-review F1: a library that CONTAINS an owned tree is refused too. test("a library containing a channel's media dir, the store, a storage location or the checkout is refused", async () => { const cases: Array<[string, RegExp]> = [ [path.join(DRIVE, "media-tier"), /contains channel "other"'s media directory/], [ROOT, /contains channel "other"'s media directory/], [path.dirname(paths.monorepoRoot), /contains the Archilyzer checkout/], ]; for (const [dir, re] of cases) { const problem = await libraryLocationProblem(paths, dir); assert.ok(problem, dir); assert.match(problem!, re); assert.match(problem!, /; choose a directory that does not\.$/); } const { mkdirSync } = await import("node:fs"); const holder = path.join(ROOT, "holder"); mkdirSync(holder, { recursive: true }); const only = (root: string, what: RegExp) => libraryLocationProblem({ channelsDir: "/nonexistent-archilyzer-test/channels", savedVideosDir: root }, path.dirname(root), { channels: [] }) .then((p) => assert.match(p ?? "", what)); await only(path.join(holder, "store"), /contains the saved-video store/); assert.match(await libraryLocationProblem({ channelsDir: "/nonexistent-archilyzer-test/channels" }, path.dirname(LOCATION), { channels: [] }) ?? "", /contains storage location "Cold drive"/); });