// A research case over a corpus on disk (controller/caseCorpus.ts): the scope resolved through a site's pool and // a site group, coverage per channel × year (listed, held, transcribed, untranscribed audio; the undated row; key // years), candidates over the en-orig track with the noise filters and counts, and the subject's uploads. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/caseCorpus.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import { normalizeDefinition } from "../lib/research/casePipeline"; import { caseCandidates, caseChannels, caseCoverage, subjectUploads } from "./caseCorpus"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "case-corpus-")); const paths = { transcriptsDir: ROOT, channelsDir: path.join(ROOT, "channels"), sitesDir: path.join(ROOT, "sites"), lmdbPath: path.join(ROOT, "index.mdb"), } as Paths; test.after(() => rm(ROOT, { recursive: true, force: true })); const ts = (s: number) => `00:${String(Math.floor(s / 60)).padStart(2, "0")}:${String(s % 60).padStart(2, "0")}.000`; const vtt = (cues: [number, string][]) => "WEBVTT\n\n" + cues.map(([a, t]) => `${ts(a)} --> ${ts(a + 3)}\n${t}\n`).join("\n"); async function channel(slug: string, playlist: string[] = [], config: Record = {}) { const dir = path.join(paths.channelsDir, slug); await mkdir(path.join(dir, "data"), { recursive: true }); await writeFile(path.join(dir, "config.json"), JSON.stringify({ handling: "youtube", name: slug, ...config })); if (playlist.length) await writeFile(path.join(dir, "playlist"), playlist.map((id) => `https://www.youtube.com/watch?v=${id}`).join("\n") + "\n"); } async function video(slug: string, id: string, date: string, title: string, files: Record = {}) { const dir = path.join(paths.channelsDir, slug, "data", id); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "metadata.info.json"), JSON.stringify({ id, title, upload_date: date, duration: 900, webpage_url: `https://www.youtube.com/watch?v=${id}` })); for (const [n, b] of Object.entries(files)) await writeFile(path.join(dir, n), b); } // Wren's own channel: two transcribed (en-orig beside a rewriting en), one with audio only, one listed and scanned, // one listed and never fetched. await channel("wren-own", ["o1", "o2", "o3", "o4", "o5"]); await video("wren-own", "o1", "20150310", "Opening day", { "transcript.en-orig.vtt": vtt([[10, "welcome to the lantern shop"], [40, "it is my shop"], [400, "goodbye"]]), "transcript.en.vtt": vtt([[10, "welcome to the candle store"]]), }); await video("wren-own", "o2", "20210305", "Closing the shop", { "transcript.en-orig.vtt": vtt([[20, "the lantern shop is closing"], [90, "wren here"]]) }); await video("wren-own", "o3", "20150601", "Audio only", { "audio.opus": "x".repeat(10) }); await writeFile(path.join(paths.channelsDir, "wren-own", "metadata-scan.json"), JSON.stringify({ version: 1, updatedAt: "", lastRun: null, entries: { o4: { title: "Scanned", description: "", uploadDate: "20160101", scannedAt: "x" } }, errors: {} })); // A talk show (coverage) on a site, in a site group; a posts channel; a channel that is not there. await channel("talk-show"); await video("talk-show", "t1", "20150320", "Talk", { "transcript.en.vtt": vtt([[100, "the lantern shop opened"], [300, "Wren Halloway came by the lantern shop"]]) }); await channel("x-posts", [], { sourceKind: "social", platform: "twitter" }); await mkdir(path.join(paths.sitesDir, "wrensite"), { recursive: true }); await writeFile( path.join(paths.sitesDir, "wrensite", "site.json"), JSON.stringify({ groups: [{ id: "default" }, { id: "talk" }], defaultGroupId: "default", channels: [{ slug: "talk-show", groupId: "talk" }, { slug: "x-posts" }] }), ); const def = normalizeDefinition( { subjects: [{ name: "Wren Halloway", aliases: ["Wren"], channels: ["wren-own"] }], scope: { channels: ["missing-one"], groups: ["wrensite:talk"], sites: ["wrensite"] }, eras: [ { id: "early", from: "2015-01-01", to: "2016-12-31", families: { shop: "lantern shop" } }, { id: "late", from: "2021-01-01", to: "2021-12-31", families: { shop: "lantern shop", name: "\\bwren\\b" }, strictFamilies: ["shop"] }, ], groups: [ { id: "own", label: "Her own", channels: "subject" }, { id: "others", label: "Coverage", channels: "coverage" }, ], keyDates: [{ date: "2021-03-01", label: "the shop closes" }], }, "wren", ); test("caseChannels: the scope's channels, a site's pool, a site group's members, the subject's own", async () => { const r = await caseChannels(paths, def); assert.deepEqual(r.slugs, ["missing-one", "talk-show", "wren-own", "x-posts"]); assert.deepEqual(r.problems, []); const bad = await caseChannels(paths, normalizeDefinition({ scope: { sites: ["nosuch"], groups: ["wrensite:nogroup"] } }, "wren")); assert.deepEqual(bad.problems, ["site nosuch: no site.json", "group wrensite:nogroup: no channel of wrensite is in it"]); }); test("caseCoverage: per channel × year, listed ⊇ held ⊇ transcribed; audio without a transcript; undated; key years", async () => { const r = await caseCoverage(paths, def); const own = r.rows.filter((x) => x.channel === "wren-own"); const y = (n: number) => own.find((x) => x.year === n); assert.deepEqual(y(2015), { channel: "wren-own", year: 2015, listed: 2, held: 2, transcribed: 1, untranscribedAudio: 1, key: true }); assert.deepEqual(y(2016), { channel: "wren-own", year: 2016, listed: 1, held: 0, transcribed: 0, untranscribedAudio: 0, key: true }); assert.deepEqual(y(2021), { channel: "wren-own", year: 2021, listed: 1, held: 1, transcribed: 1, untranscribedAudio: 0, key: true }); assert.deepEqual(y(0), { channel: "wren-own", year: 0, listed: 1, held: 0, transcribed: 0, untranscribedAudio: 0 }); assert.deepEqual(r.skipped, [ { slug: "missing-one", reason: "not in the corpus" }, { slug: "x-posts", reason: "a social channel: posts, not recordings" }, ]); }); test("caseCandidates: en-orig read, families per era, the noise filters, deterministic keys, counts before any read", async () => { const notes: string[] = []; const a = await caseCandidates(paths, def, { onNote: (l) => notes.push(l) }); assert.deepEqual(a.windows.map((w) => [w.key, w.group, w.track, w.era]), [ ["wren-own/o1@0", "own", "en-orig", "early"], ["wren-own/o2@0", "own", "en-orig", "late"], ["talk-show/t1@270", "others", "en", "early"], ]); // The rewriting `en` track is not what was read. assert.match(a.windows[0].text, /lantern shop/); assert.doesNotMatch(a.windows[0].text, /candle/); assert.equal(a.windows[0].url, "https://www.youtube.com/watch?v=o1"); // talk-show at 100 s names no subject: noise. o2's "wren" in the strict late era makes no window of its own. assert.equal(a.counts.noise.noSubject, 1); assert.equal(a.counts.windows, 3); assert.deepEqual(a.counts.byGroup, { own: 2, others: 1 }); assert.equal(a.counts.byFamily.shop, 3); assert.deepEqual(a.counts.scanned, { channels: 2, videos: 4, inEra: 4, withText: 3, noText: 1, undated: 0 }); assert.deepEqual(a.counts.skipped.map((s) => s.slug), ["missing-one", "x-posts"]); assert.ok(notes.some((n) => /^wren-own: 3 held, 2 windows$/.test(n))); const b = await caseCandidates(paths, def); assert.deepEqual(b.windows, a.windows, "the same corpus and definition give the same windows"); }); test("subjectUploads: held and listed-only uploads, captioned or not", async () => { const r = await subjectUploads(paths, def); assert.deepEqual(r.uploads.map((u) => [u.id, u.date, u.captioned]), [ ["o1", "20150310", true], ["o3", "20150601", false], ["o4", "20160101", false], ["o2", "20210305", true], ]); });