// The shadow first, the corpus after (sources.mjs channelsLayersFor; rev 9, 6 Nov): a project's own channels tree // (`provenance.channelsDir`, a `.shadow-channels/`) read first, the real corpus behind it for anything it lacks -- // posts, cue files, cached windows -- each kind said once; off with `provenance.channelsFallback: false` or // CHANNELS_FALLBACK=0. Temp dirs only. import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { channelsFallbackOn, channelsLayersFor, firstLayerWith, resetChannelsFallbackNotes, resolveLocalSource, } from "./sources.mjs"; import { createCueSource } from "./cues.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-channels-layers-")); after(() => rm(ROOT, { recursive: true, force: true })); const PROJECT = path.join(ROOT, "project"); const SHADOW = path.join(PROJECT, ".shadow-channels"); const CORPUS = path.join(ROOT, "transcripts", "channels"); const put = async (file, body = "x") => { await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, body); }; test("channelsLayersFor: the project's tree, then the corpus -- unless the manifest or the environment turns it off", () => { const declared = { provenance: { channelsDir: ".shadow-channels" } }; assert.deepEqual(channelsLayersFor(PROJECT, declared, { fallback: CORPUS, env: {} }), [SHADOW, CORPUS]); assert.deepEqual(channelsLayersFor(PROJECT, {}, { shadowExists: true, fallback: CORPUS, env: {} }), [SHADOW, CORPUS]); assert.deepEqual(channelsLayersFor(PROJECT, {}, { fallback: CORPUS, env: {} }), [CORPUS], "no tree of its own: the corpus alone"); assert.deepEqual(channelsLayersFor(PROJECT, { provenance: { channelsDir: ".shadow-channels", channelsFallback: false } }, { fallback: CORPUS, env: {} }), [SHADOW]); assert.deepEqual(channelsLayersFor(PROJECT, declared, { fallback: CORPUS, env: { CHANNELS_FALLBACK: "0" } }), [SHADOW]); assert.equal(channelsFallbackOn({}, {}), true); }); test("firstLayerWith: the first tree holding the path -- a later one said ONCE per kind; none: the first (its own miss)", async () => { resetChannelsFallbackNotes(); await put(path.join(SHADOW, "him-x", "posts", "a.jsonl")); await put(path.join(CORPUS, "her-x", "posts", "a.jsonl")); const said = []; const log = (m) => said.push(m); assert.equal(await firstLayerWith([SHADOW, CORPUS], ["him-x", "posts"], { kind: "posts", log }), SHADOW); assert.equal(await firstLayerWith([SHADOW, CORPUS], ["her-x", "posts"], { kind: "posts", log }), CORPUS); assert.equal(await firstLayerWith([SHADOW, CORPUS], ["her-x", "posts"], { kind: "posts", log }), CORPUS); assert.equal(said.length, 1, "said once"); assert.match(said[0], /posts: her-x\/posts is not in .*shadow-channels -- read from .*transcripts\/channels/); assert.equal(await firstLayerWith([SHADOW, CORPUS], ["nobody-x", "posts"], { kind: "posts", log }), SHADOW); assert.equal(await firstLayerWith([SHADOW], ["her-x", "posts"], { kind: "posts", log }), SHADOW, "the fallback off: the shadow alone"); }); test("resolveLocalSource: a cached window the project's tree lacks is read from the corpus behind it (corpus tiers only)", async () => { resetChannelsFallbackNotes(); await put(path.join(CORPUS, "chan", "data", "vid9", "clips", "10.00-30.00.mp4")); const said = []; const hit = await resolveLocalSource({ video: "vid9", slug: "chan", from: 12, to: 20 }, { channelsDir: SHADOW, fallbackChannelsDir: CORPUS, log: (m) => said.push(m), probe: async () => null }); assert.equal(hit?.kind, "corpus-window"); assert.equal(hit.path, path.join(CORPUS, "chan", "data", "vid9", "clips", "10.00-30.00.mp4")); assert.match(said[0], /windows: chan\/vid9 12-20 is not in .* -- read from /); assert.equal(await resolveLocalSource({ video: "vid9", slug: "chan", from: 12, to: 20 }, { channelsDir: SHADOW, probe: async () => null }), null, "no fallback: the shadow alone, as before"); }); test("createCueSource channelsDirs: a cue file the project's tree lacks is read from the corpus behind it, said once", async () => { const cues = { cues: [{ start: 0, end: 1, text: "hi" }], duration: 1 }; await put(path.join(CORPUS, "chan", "data", "vid7", "transcript.cues.json"), JSON.stringify(cues)); await put(path.join(SHADOW, "chan", "data", "vid8", "transcript.cues.json"), JSON.stringify({ ...cues, duration: 2 })); const said = []; const src = createCueSource({ channelsDirs: [SHADOW, CORPUS], prefer: "local", log: (m) => said.push(m), cacheDir: path.join(ROOT, "cache") }); assert.equal((await src.load("chan", "vid8")).duration, 2, "the shadow's own"); assert.equal((await src.load("chan", "vid7")).duration, 1, "the corpus behind it"); await src.load("chan", "vid7"); assert.equal(said.filter((m) => /^cues:/.test(m)).length, 1); // The shadow alone (the fallback off): a miss, as before. const alone = createCueSource({ channelsDirs: [SHADOW], prefer: "local", cacheDir: path.join(ROOT, "cache") }); await assert.rejects(alone.load("chan", "vid7"), /no local cues for chan\/vid7/); }); test("cuePathFor: a clip's cue file from the corpus when the project's tree has none", async () => { const { cuePathFor } = await import("../lib/projects/report.mjs"); const m = { provenance: { channelSlug: "chan" } }; assert.equal(cuePathFor(m, { video: "vid8" }, SHADOW, CORPUS), path.join(SHADOW, "chan", "data", "vid8", "transcript.cues.json")); assert.equal(cuePathFor(m, { video: "vid7" }, SHADOW, CORPUS), path.join(CORPUS, "chan", "data", "vid7", "transcript.cues.json")); assert.equal(cuePathFor(m, { video: "vid7" }, SHADOW, null), path.join(SHADOW, "chan", "data", "vid7", "transcript.cues.json"), "off: the shadow's path"); });