// R6.1: what a triage's tier 0 lists the scope with, and how its term query // is bounded. A channel of three shard pages, oldest first and the newest on // the last page (the order a scan reads them in): a capped scan never reaches // the newest — said, and parsed back as partial with what it did not reach — // while `listRecordings` (the manifests, dated by the summaries index or, for // a record the index lacks, its own page) lists every recording, and an // enumeration restricted to `ids` reads only the pages holding them. import { after, before, 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 { enumerateOf } from "yt-dlp-transcript-common/lib/report/draft/tasks/trace"; import { createCorpusTools, type CorpusTools } from "./callTool"; import { countPhrases, enumerate, listRecordings, type Enumerated } from "./enumerate"; import { LocalSource } from "./source"; let dir: string; let corpus: CorpusTools; const rec = (id: string, date: string, lines: string[]) => ({ slug: `big/${id}`, id, channelSlug: "big", channel: "Big Channel (Mirror)", title: `Title ${id}`, uploadDate: date, webpageUrl: `https://video.example/watch?v=${id}`, cues: lines.map((text, i) => ({ start: i * 5, end: i * 5 + 4.8, text })), }); const PAGES = [ [rec("old-1", "20240101", ["the contest"]), rec("old-2", "20240201", ["flour"]), rec("old-3", "20240301", ["the contest again"])], [rec("old-4", "20250101", ["water"]), rec("old-5", "20250201", ["the contest once more"])], [rec("new-1", "20261012", ["a co-host joined the show"]), rec("new-2", "20261013", ["the contest results"])], ]; before(async () => { dir = await mkdtemp(path.join(os.tmpdir(), "mcp-list-")); await writeFile(path.join(dir, "corpus.json"), JSON.stringify({ channels: [{ slug: "big", name: "Big Channel (Mirror)", videoCount: 7 }] })); const ch = path.join(dir, "transcripts", "big"); await mkdir(ch, { recursive: true }); const slugToPage: Record = {}; PAGES.forEach((p, i) => p.forEach((r) => (slugToPage[r.id] = i))); await writeFile(path.join(ch, "manifest.json"), JSON.stringify({ version: 1, channelSlug: "big", pageCount: PAGES.length, maxPageBytes: 1e6, generatedAt: "2026-10-14", slugToPage })); for (const [i, p] of PAGES.entries()) await writeFile(path.join(ch, `page-${String(i).padStart(4, "0")}.json`), JSON.stringify(p)); // The summaries index dates all but new-2 (a record it lacks is dated from its page). const sum = path.join(dir, "summaries"); await mkdir(sum, { recursive: true }); const rows = PAGES.flat().filter((r) => r.id !== "new-2").map((r) => ({ slug: r.slug, id: r.id, channelSlug: "big", title: r.title, uploadDate: r.uploadDate })); await writeFile(path.join(sum, "manifest.json"), JSON.stringify({ version: 3, totalCount: rows.length, pageSize: 1000, pageCount: 1, generatedAt: "2026-10-14", channels: [{ slug: "big", name: "Big Channel (Mirror)", count: 7 }] })); await writeFile(path.join(sum, "page-0000.json"), JSON.stringify(rows)); corpus = await createCorpusTools({ source: `local:${dir}` }); }); after(async () => { await corpus.close(); await rm(dir, { recursive: true, force: true }); }); const ok = (e: Enumerated | { error: string }): Enumerated => { if ("error" in e) throw new Error(e.error); return e; }; test("a capped scan never reaches the newest: the tool says so, and its coverage parses back whole (a name with parentheses, the channel it stopped inside)", async () => { const e = ok(await enumerate(new LocalSource(dir), { query: ".", regex: true, max_pages: 2 })); assert.equal(e.result.truncated, true); assert.deepEqual(e.result.hits.map((h) => h.videoId), ["old-1", "old-2", "old-3", "old-4", "old-5"]); const text = await corpus.call("enumerate_matches", { query: ".", regex: true, max_pages: 2 }); const parsed = enumerateOf(text); assert.equal(parsed.partial, true); assert.equal(parsed.stopped, "Big Channel (Mirror) (stopped at page 2 of 3)"); }); test("listRecordings lists every recording, uncapped, dated (the index, else the record's own page); a date range; an unknown channel is said, never 'nothing'", async () => { const src = new LocalSource(dir); const all = await listRecordings(src, { channels: ["big"] }); assert.equal(all.complete, true, all.problems.join("; ")); assert.deepEqual(all.rows.map((r) => r.id).sort(), PAGES.flat().map((r) => r.id).sort()); // new-2 is dated from its own page, whose cues say it has text; with no stats shipped the rest do not say (null). assert.deepEqual(all.rows.find((r) => r.id === "new-2"), { id: "new-2", channel: "Big Channel (Mirror)", channelSlug: "big", date: "2026-10-13", title: "Title new-2", hasTranscript: true }); assert.equal(all.rows.find((r) => r.id === "old-1")?.hasTranscript, null); const recent = await listRecordings(src, { channels: ["big"], date_from: "2026-10-07" }); assert.deepEqual(recent.rows.map((r) => r.id).sort(), ["new-1", "new-2"]); const unknown = await listRecordings(src, { channels: ["big", "nowhere"] }); assert.equal(unknown.complete, false); assert.deepEqual(unknown.unreached, ["nowhere"]); assert.match(unknown.problems[0]!, /no channel "nowhere" in this corpus/); // Through the corpus tools (what the editor's triage gets). const viaTools = await corpus.listRecordings({ channels: ["big"] }); assert.equal(viaTools.rows.length, 7); }); test("enumerate with `ids` reads only the pages holding them — bounded, so a scan the scope would cap is complete", async () => { const read: string[] = []; const e = ok(await enumerate(new LocalSource(dir), { query: "contest", ids: ["new-2", "old-1", "gone-9"], max_pages: 2 }, { onRecord: (r) => read.push(r.id) })); assert.equal(e.result.truncated, false); assert.deepEqual(e.result.hits.map((h) => h.videoId), ["old-1", "new-2"]); assert.deepEqual(read.sort(), ["new-2", "old-1"], "only the ids' records are matched"); assert.deepEqual(e.result.coverage.onlyIds, { asked: 3, found: 2 }); assert.equal(e.result.coverage.pagesPlanned, 2, "pages 0 and 2, never page 1"); const text = await corpus.call("enumerate_matches", { query: "contest", ids: ["new-2"] }); assert.match(text, /restricted to 1 recording id\(s\): 1 held by a channel in scope/); assert.deepEqual(enumerateOf(text).ids, ["new-2"]); }); test("R6.2 listRecordings says which recordings have transcript text: the stats index's flag, or a page-read record's own cues", async () => { const d = await mkdtemp(path.join(os.tmpdir(), "mcp-list-text-")); try { await writeFile(path.join(d, "corpus.json"), JSON.stringify({ channels: [{ slug: "big", name: "Big", videoCount: 4 }] })); const ch = path.join(d, "transcripts", "big"); await mkdir(ch, { recursive: true }); const recs = [ rec("has-1", "20261001", ["words"]), { ...rec("bare-1", "20261002", []), cues: undefined }, // listed before its transcript lands rec("has-2", "20261003", ["more words"]), // not in the index: read from its page { ...rec("bare-2", "20261004", []), cues: undefined }, // not in the index, no cues on its page ]; await writeFile(path.join(ch, "manifest.json"), JSON.stringify({ version: 1, channelSlug: "big", pageCount: 1, maxPageBytes: 1e6, generatedAt: "2026-10-14", slugToPage: Object.fromEntries(recs.map((r) => [r.id, 0])) })); await writeFile(path.join(ch, "page-0000.json"), JSON.stringify(recs)); const sum = path.join(d, "summaries"); await mkdir(sum, { recursive: true }); const indexed = recs.filter((r) => r.id === "has-1" || r.id === "bare-1"); await writeFile(path.join(sum, "manifest.json"), JSON.stringify({ version: 3, totalCount: 2, pageSize: 1000, pageCount: 1, generatedAt: "2026-10-14", channels: [{ slug: "big", name: "Big", count: 2 }] })); await writeFile(path.join(sum, "page-0000.json"), JSON.stringify(indexed.map((r) => ({ slug: r.slug, id: r.id, channelSlug: "big", title: r.title, uploadDate: r.uploadDate })))); const stats = path.join(d, "stats"); await mkdir(stats, { recursive: true }); await writeFile(path.join(stats, "manifest.json"), JSON.stringify({ version: 6, generatedAt: "2026-10-14", totalCount: 2, pageCount: 1, maxPageBytes: 1e6, channels: [{ slug: "big", name: "Big", count: 2 }] })); await writeFile(path.join(stats, "page-0000.json"), JSON.stringify(indexed.map((r) => ({ slug: r.slug, id: r.id, channelSlug: "big", hasTranscript: r.id === "has-1", cueCount: r.id === "has-1" ? 1 : null })))); const l = await listRecordings(new LocalSource(d), { channels: ["big"] }); assert.equal(l.complete, true, l.problems.join("; ")); assert.deepEqual(Object.fromEntries(l.rows.map((r) => [r.id, r.hasTranscript])), { "has-1": true, "bare-1": false, "has-2": true, "bare-2": false }); } finally { await rm(d, { recursive: true, force: true }); } }); test("countPhrases (R6.3): each phrase counted over the whole listed scope in one restricted scan — recordings, lines, examples newest first; a phrase is literal, whole words", async () => { const src = new LocalSource(dir); const c = await countPhrases(src, { phrases: ["the contest", "co-host", "flour", "contest.*", "nowhere at all"], channels: ["big"] }); assert.equal(c.complete, true, c.problems.join("; ")); assert.equal(c.listed, 7); assert.deepEqual( Object.fromEntries(Object.entries(c.counts).map(([p, x]) => [p, [x.recordings, x.lines]])), { "the contest": [4, 4], "co-host": [1, 1], flour: [1, 1], "contest.*": [0, 0], "nowhere at all": [0, 0] }, ); assert.deepEqual(c.counts["the contest"]!.examples.map((x) => x.id), ["new-2", "old-5", "old-3", "old-1"]); assert.deepEqual(c.counts["co-host"]!.examples[0], { id: "new-1", channel: "Big Channel (Mirror)", channelSlug: "big", date: "2026-10-12", title: "Title new-1", start: 0, text: "a co-host joined the show" }); // Through the corpus tools, as the editor's jobs call it. const t = await corpus.countPhrases({ phrases: ["the contest"], channels: ["big"], date_from: "2025-01-01" }); assert.equal(t.counts["the contest"]!.recordings, 2); });