// Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/corpusGrep.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, utimes, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import { corpusGrep, grepCues, parseGrepDate } from "./corpusGrep"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "corpus-grep-")); const paths = { transcriptsDir: ROOT, channelsDir: path.join(ROOT, "channels") } as Paths; test.after(() => rm(ROOT, { recursive: true, force: true })); const vtt = (cues: [number, number, string][]) => "WEBVTT\n\n" + cues .map(([a, b, t]) => { const ts = (s: number) => `00:${String(Math.floor(s / 60)).padStart(2, "0")}:${String(s % 60).padStart(2, "0")}.000`; return `${ts(a)} --> ${ts(b)}\n${t}\n`; }) .join("\n"); async function channel(slug: 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 })); } async function video( slug: string, id: string, opts: { date?: string; title: string; files?: Record; old?: boolean }, ) { const dir = path.join(paths.channelsDir, slug, "data", id); await mkdir(dir, { recursive: true }); if (opts.date) { const meta = path.join(dir, "metadata.info.json"); await writeFile(meta, JSON.stringify({ id, title: opts.title, upload_date: opts.date, duration: 600, webpage_url: `https://example.com/${id}` })); if (opts.old) { const t = new Date("2020-01-01T00:00:00Z"); await utimes(meta, t, t); } } for (const [name, body] of Object.entries(opts.files ?? {})) await writeFile(path.join(dir, name), body); } await channel("show"); // en-orig is the speaker's words; en a rewrite. The rule reads en-orig. await video("show", "a1", { date: "20261022", title: "Episode one", files: { "transcript.en-orig.vtt": vtt([ [0, 5, "hello and welcome"], [10, 15, "my dog Huck went to the vet"], [40, 45, "nothing here"], [70, 75, "Huck is doing better"], ]), "transcript.en.vtt": vtt([[0, 5, "a rewritten track that says Huck"]]), }, }); await video("show", "a2", { date: "20261023", title: "No transcript yet" }); await video("show", "a3", { date: "20251001", title: "Old one", old: true, files: { "transcript.en.vtt": vtt([[0, 5, "Huck in the old days"]]) }, }); await channel("posts", { sourceKind: "social", platform: "twitter" }); await channel("gone", { dataDir: "/nonexistent/archilyzer-corpus-grep" }); test("parseGrepDate takes both spellings and refuses a day that does not exist", () => { assert.equal(parseGrepDate("2026-10-21"), "20261021"); assert.equal(parseGrepDate("20261021"), "20261021"); assert.equal(parseGrepDate("2026-02-30"), null); assert.equal(parseGrepDate("yesterday"), null); }); test("grepCues: one hit per moment, at the cue the match begins in", () => { const cues = [ { start: 0, end: 5, text: "a" }, { start: 12, end: 15, text: "the word\nhere" }, { start: 20, end: 25, text: "b" }, { start: 50, end: 55, text: "word again" }, ]; const hits = grepCues(cues, /word/i, 30); assert.deepEqual( hits.map((h) => h.start), [12, 50], ); assert.equal(hits[0].text, "a the word here b"); }); test("corpus grep: date floor, caption-track rule, pending videos, guards", async () => { const r = await corpusGrep({ paths, pattern: /\bhuck\b/i, since: "20261021" }); assert.deepEqual(r.channels, ["show"]); // The legacy channel is skipped and named; the social one is left out. assert.deepEqual(r.skippedChannels.map((s) => s.slug), ["gone"]); assert.deepEqual( r.hits.map((h) => [h.date, h.slug, h.id, h.start, h.title]), [ ["2026-10-22", "show", "a1", 10, "Episode one"], ["2026-10-22", "show", "a1", 70, "Episode one"], ], ); assert.equal(r.hits[0].url, "https://example.com/a1"); assert.equal(r.videosInRange, 2); assert.equal(r.videosSearched, 1); assert.deepEqual(r.noTranscript.map((p) => [p.id, p.title, p.duration]), [["a2", "No transcript yet", 600]]); }); test("corpus grep: no date bound searches the old video too; --until bounds; limit counts all", async () => { const all = await corpusGrep({ paths, pattern: /huck/i, channels: ["show"], limit: 1 }); assert.equal(all.totalHits, 3); assert.equal(all.hits.length, 1); assert.equal(all.hits[0].id, "a3"); const until = await corpusGrep({ paths, pattern: /huck/i, channels: ["show"], until: "20251231" }); assert.deepEqual(until.hits.map((h) => h.id), ["a3"]); await assert.rejects(corpusGrep({ paths, pattern: /x/, channels: ["nope"] }), /Channel "nope" not found/); const named = await corpusGrep({ paths, pattern: /x/, channels: ["posts"] }); assert.match(named.skippedChannels[0].reason, /social/); });