// The mirror scans in the MCP (release 31, lane D): get_video_metadata's // `copies` line and enumerate_matches' `copies: "only-here"`, read from a // local archive's transcripts/mirrors/; a published site has none and says so. // Synthetic only: the draft corpus, with `synth-other` scanned as a mirror of // `synth-kitchen`. import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { writeDraftCorpus } from "yt-dlp-transcript-common/testing/draft/corpus.mjs"; import { matchMirror } from "yt-dlp-transcript-common/lib/mirrors/match"; import { MIRROR_RULES, MIRROR_SCAN_FORMAT, MIRROR_SCAN_VERSION, type MirrorScanFile } from "yt-dlp-transcript-common/lib/mirrors/scanFile"; import { writeMirrorScan } from "yt-dlp-transcript-common/lib/mirrors/scanFile-server"; import { createCorpusTools, type CorpusTools } from "./callTool"; import { COPIES_NOT_AVAILABLE, describeCopies, loadMirrorScans, onlyHereIds } from "./mirrorCopies"; import { LocalSource, RemoteSource } from "./source"; let dir: string; let transcripts: string; let corpus: CorpusTools; const savedEnv = process.env.TRANSCRIPTS_DIR; const rec = (id: string, title: string, uploadDate: string, gone: "deleted" | null = null) => ({ id, title, uploadDate, duration: 3600, wasLive: true, gone, }); function scanFile(): MirrorScanFile { const r = matchMirror({ mirror: { slug: "synth-other", platform: "youtube", records: [rec("flat-loaf-01", "The flat loaf", "20260112"), rec("mirror-pie-09", "Pie stream", "20260210")], }, creators: [ { slug: "synth-kitchen", platform: "youtube", records: [rec("bread-day-01", "Bread day", "20260105"), rec("pie-stream-02", "Pie stream", "20260210", "deleted")], }, ], }); return { format: MIRROR_SCAN_FORMAT, version: MIRROR_SCAN_VERSION, mirror: "synth-other", of: ["synth-kitchen"], generatedAt: "2026-10-16T00:00:00.000Z", rules: { ...MIRROR_RULES }, ...r, }; } before(async () => { dir = await writeDraftCorpus(await mkdtemp(path.join(os.tmpdir(), "mcp-mirrors-"))); transcripts = await mkdtemp(path.join(os.tmpdir(), "mcp-mirrors-transcripts-")); process.env.TRANSCRIPTS_DIR = transcripts; await writeMirrorScan(transcripts, scanFile()); corpus = await createCorpusTools({ source: `local:${dir}` }); }); after(async () => { if (savedEnv === undefined) delete process.env.TRANSCRIPTS_DIR; else process.env.TRANSCRIPTS_DIR = savedEnv; await corpus.close(); await rm(dir, { recursive: true, force: true }); await rm(transcripts, { recursive: true, force: true }); }); test("the scans: read for a local source only", async () => { const local = await loadMirrorScans(new LocalSource(dir), transcripts); assert.equal(local?.length, 1); assert.equal(await loadMirrorScans(new RemoteSource("https://example.invalid"), transcripts), null); assert.deepEqual(onlyHereIds(local!), { ids: ["flat-loaf-01", "mirror-pie-09"], channels: ["synth-other"] }); }); test("the copies line: a mirror video, a creator's video held, nothing known, remote", () => { const scans = [scanFile()]; assert.equal(describeCopies(scans, "synth-other", "flat-loaf-01"), "- copies: a MIRROR copy — only copy; the creator's copies: none found"); assert.equal(describeCopies(scans, "synth-other", "mirror-pie-09"), "- copies: a MIRROR copy — only copy; the creator's copies: pie-stream-02 (synth-kitchen, deleted)"); assert.equal(describeCopies(scans, "synth-kitchen", "pie-stream-02"), "- copies: this video is deleted on its platform; a mirror holds it: mirror-pie-09 (synth-other)"); assert.equal(describeCopies(scans, "synth-kitchen", "bread-day-01"), ""); assert.equal(describeCopies(null, "synth-kitchen", "bread-day-01"), `- copies: ${COPIES_NOT_AVAILABLE}`); }); test("get_video_metadata carries the copies line", async () => { const text = await corpus.call("get_video_metadata", { video_id: "flat-loaf-01" }); assert.match(text, /- copies: a MIRROR copy — only copy; the creator's copies: none found/); const other = await corpus.call("get_video_metadata", { video_id: "bread-day-01" }); assert.doesNotMatch(other, /copies:/); }); test("enumerate_matches copies: only-here keeps the only-copy mirror videos", async () => { const all = await corpus.call("enumerate_matches", { query: ".", regex: true }); assert.match(all, /bread-day-01/); const only = await corpus.call("enumerate_matches", { query: ".", regex: true, copies: "only-here" }); assert.deepEqual([...only.matchAll(/^- ([^ |]+) \| /gm)].map((m) => m[1]), ["flat-loaf-01"]); assert.match(only, /copies: only-here — 2 mirror video\(s\) that are the only copy, from 1 mirror scan\(s\)/); const none = await corpus.call("enumerate_matches", { query: ".", regex: true, copies: "only-here", ids: ["bread-day-01"] }); assert.match(none, /^No matches/); await assert.rejects(corpus.call("enumerate_matches", { query: ".", copies: "everywhere" }), /copies must be "only-here"/); });