// The corpus tools in process, over the synthetic draft corpus read the way // the MCP's --local reads a composed public dir. 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 { CORPUS_TOOL_NAMES, CorpusToolError, createCorpusTools, type CorpusTools } from "./callTool"; import { fakeEditor } from "./editorOps.testkit"; let dir: string; let corpus: CorpusTools; before(async () => { dir = await writeDraftCorpus(await mkdtemp(path.join(os.tmpdir(), "mcp-calltool-"))); corpus = await createCorpusTools({ source: `local:${dir}` }); }); after(async () => { await corpus.close(); await rm(dir, { recursive: true, force: true }); }); test("the allowlist: read-only corpus tools only — no fetch_clip, enqueue, get_job or open_link", () => { const names = corpus.tools.map((t) => t.name); assert.deepEqual([...names].sort(), [...CORPUS_TOOL_NAMES].sort()); for (const n of ["fetch_clip", "enqueue", "get_job", "open_link"]) assert.ok(!names.includes(n), n); for (const t of corpus.tools) { assert.equal(typeof t.run, "function"); assert.equal(typeof t.description, "string"); const props = (t.inputSchema as { properties?: Record }).properties ?? {}; assert.ok(!("source" in props), `${t.name} shows no source`); assert.ok(!((t.inputSchema as { required?: string[] }).required ?? []).includes("source")); } }); test("a call outside the allowlist is refused before the server sees it", async () => { for (const n of ["fetch_clip", "enqueue", "get_job", "open_link", "nope"]) { await assert.rejects(corpus.call(n, {}), (e: Error) => e instanceof CorpusToolError && /not one of this run's read-only corpus tools/.test(e.message)); } }); test("search and transcript reads over the draft corpus, through the same dispatch Claude Code uses", async () => { const channels = await corpus.call("list_channels"); assert.match(channels, /synth-kitchen/); assert.match(channels, /synth-other/); const hits = await corpus.call("search_transcripts", { query: "second shop" }); assert.match(hits, /qa-long-03/); const t = await corpus.tools.find((x) => x.name === "get_transcript")!.run({ video_id: "bread-day-01", channel: "synth-kitchen" }, {}); assert.match(t, /I would never sell the bakery to a chain/); // Every result names its corpus — the forced one. assert.match(t, new RegExp(`\\(corpus: local:${dir.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`)); }); test("source is FORCED: an argument naming another corpus is overwritten", async () => { const out = await corpus.call("list_channels", { source: "remote:https://elsewhere.example" }); assert.match(out, /synth-kitchen/); assert.match(out, /\(corpus: local:/); assert.doesNotMatch(out, /elsewhere/); }); test("a tool's own error comes back as a CorpusToolError with its words", async () => { await assert.rejects( corpus.call("get_transcript", { video_id: "no-such-video" }), (e: Error) => e instanceof CorpusToolError && /no-such-video/.test(e.message), ); }); test("notes: list and read only; a reply is refused, and the schema offers neither reply nor its fields", async () => { const notes = corpus.tools.find((t) => t.name === "notes")!; const props = (notes.inputSchema as { properties: Record }).properties; assert.deepEqual(props.action!.enum, ["list", "read"]); for (const k of ["note_id", "text", "resolve"]) assert.ok(!(k in props), k); await assert.rejects(corpus.call("notes", { action: "reply", note_id: "n_1", text: "x" }), /list and read only/); // list goes through to umtool, as the MCP's notes does. const editor = fakeEditor(() => ({ status: 200, body: { notes: [] } }), { UMTOOL_URL: "http://umtool.test" }); const withNotes = await createCorpusTools({ source: `local:${dir}`, only: ["notes"], editorDeps: editor.deps }); try { await withNotes.call("notes", { action: "list" }).catch(() => ""); assert.ok(editor.sent.length >= 1); assert.ok(editor.sent.every((s) => s.method === "GET"), JSON.stringify(editor.sent)); } finally { await withNotes.close(); } }); test("only: a narrower set; a name outside the allowlist cannot be asked for", async () => { const two = await createCorpusTools({ source: `local:${dir}`, only: ["search_transcripts", "get_transcript"] }); try { assert.deepEqual(two.tools.map((t) => t.name), ["search_transcripts", "get_transcript"]); await assert.rejects(two.call("list_channels"), CorpusToolError); } finally { await two.close(); } await assert.rejects( createCorpusTools({ source: `local:${dir}`, only: ["fetch_clip" as never] }), /not a read-only corpus tool/, ); }); test("an aborted signal stops a call", async () => { const ac = new AbortController(); ac.abort(); await assert.rejects(corpus.tools[0]!.run({}, { signal: ac.signal })); });