// Research boards (R2) over the e2e fixture corpus, as the routes run them: a // pin reads the cite from the corpus and keeps it as a snapshot; re-verify // reads it again and says what drifted (and where `en` and `en-orig` // differ); the export converts as a /sweep report (common's converter); the // hand-offs plan, and confirm -- an article as a seeded report-draft sweep // the editor is asked for, a video as a scaffolded project. // // Run with: pnpm --filter umtool exec tsx --test lib/research/boards.test.ts import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, rm } from "node:fs/promises"; import http from "node:http"; import type { AddressInfo } from "node:net"; import os from "node:os"; import path from "node:path"; let tmp: string; let pub: string; let source: string; let editor: http.Server; const asked: { url: string; body: Record }[] = []; // The modules read their env at import, so they are imported after it is set. let lib: { boards: typeof import("./boards"); handoff: typeof import("./handoff"); fixture: typeof import("../../e2e/fixtures/research-fixture.mjs"); sweepToReport: typeof import("yt-dlp-transcript-common/lib/report/convertSweep").sweepToReport; }; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-boards-")); const fixture = await import("../../e2e/fixtures/research-fixture.mjs"); pub = fixture.makeResearchFixture({ dest: tmp, channels: path.join(tmp, "channels") }).pub; source = `local:${pub}`; fixture.makeBoardFixture({ reportsRoot: path.join(tmp, "reports"), pub }); // A stand-in editor: the policy, and report-draft (it records what it was asked). editor = http.createServer((req, res) => { let raw = ""; req.on("data", (c) => (raw += c)); req.on("end", () => { asked.push({ url: req.url ?? "", body: raw ? JSON.parse(raw) : {} }); res.setHeader("content-type", "application/json"); if (req.url?.startsWith("/api/ops/settings")) { res.end(JSON.stringify({ ok: true, value: { routes: { sweep: "code" }, models: { api: "claude-sonnet-5-5" }, caps: { sweep: 4, max: 10 } } })); } else if (req.url === "/api/ops/report-draft") { res.end(JSON.stringify({ ok: true, jobId: "job-1", iteration: 1 })); } else { res.statusCode = 404; res.end(JSON.stringify({ error: "no route" })); } }); }); await new Promise((r) => editor.listen(0, "127.0.0.1", r)); process.env.ARCHILYZER_EDITOR_URL = `http://127.0.0.1:${(editor.address() as AddressInfo).port}`; process.env.WORKER_TOKEN = "t"; process.env.TRANSCRIPT_LOCAL_DIR = pub; process.env.CHANNELS_DIR = path.join(tmp, "channels"); process.env.SITES_DIR = path.join(tmp, "sites"); process.env.ARTICLES_DIR = path.join(tmp, "articles"); process.env.REPORTS_DIR = path.join(tmp, "reports"); process.env.SETTINGS_FILE = path.join(tmp, "settings.json"); delete process.env.TRANSCRIPT_HUB_URL; lib = { boards: await import("./boards"), handoff: await import("./handoff"), fixture, sweepToReport: (await import("yt-dlp-transcript-common/lib/report/convertSweep")).sweepToReport, }; }); after(async () => { editor.close(); await rm(tmp, { recursive: true, force: true }); }); test("a pin reads the cue from the corpus: the words, the cite, the house citation; twice is one quote", async () => { const { boards } = lib; const c = await boards.createCase({ query: "lighthouse budget", scope: { source } }); assert.equal(c.id, "lighthouse-budget"); const search = { session: "00112233aabbccdd", query: "lighthouse budget", total: 8, complete: true, notReached: [] }; // 1213 s: the nearest cue starts at 1212 (a snippet's second is its cue's). const r = await boards.pinQuote(c.id, { source, channel: "harbor-radio", video: "hr-2021a", t: 1213, search }); assert.equal(r.duplicate, false); assert.equal(r.quote.text, "who signed off on the lighthouse budget"); assert.equal(r.quote.cite.t, 1212); assert.equal(r.quote.cite.end, 1215.8); assert.equal(r.quote.cite.url, "https://archive.example/?v=harbor-radio%2Fhr-2021a&t=1212"); assert.equal(r.quote.cite.citation, '"who signed off on the lighthouse budget" [Harbor Radio 52: storm season @ 20:12](https://archive.example/?v=harbor-radio%2Fhr-2021a&t=1212)'); assert.equal(r.case.search?.session, search.session); const again = await boards.pinQuote(c.id, { source, channel: "harbor-radio", video: "hr-2021a", t: 1212 }); assert.equal(again.duplicate, true); // Several cues as one quote. const two = await boards.pinQuote(c.id, { source, channel: "night-desk", video: "nd-2023", t: 400, until: 404 }); assert.match(two.quote.text, /^the keeper never saw the lighthouse budget \S/); // A Rumble mirror keeps the editor's directory id beside the archive's. const rumble = await boards.pinQuote(c.id, { source, channel: "harbor-radio-rumble", video: "v2efgh", t: 32 }); assert.equal(rumble.quote.cite.dirVideo, undefined, "the fixture's URL slug is the title, not an id: nothing mapped"); // A local-only channel is marked. const lo = await boards.pinQuote(c.id, { source, channel: "candace-owens", video: "lo-2025", t: 12 }); assert.equal(lo.quote.cite.localOnly, true); await assert.rejects(boards.pinQuote(c.id, { source, channel: "night-desk", video: "nope", t: 1 }), /no recording nope/); await assert.rejects(boards.pinQuote(c.id, { source: "local:/elsewhere", channel: "night-desk", video: "nd-2023", t: 1 }), (e: Error & { status?: number }) => e.status === 400 || e.status === 404); }); test("re-verify: an intact quote is ok, a changed one drifted with old and new, en vs en-orig flagged", async () => { const { boards, fixture } = lib; const { case: doc, results } = await boards.verifyCase(fixture.BOARD_CASE); const by = Object.fromEntries(results.map((r) => [r.id, r.verify])); assert.equal(by.q1.status, "drifted"); assert.equal(by.q1.now, fixture.DRIFTED_QUOTE.now); assert.equal(doc.quotes.find((q) => q.id === "q1")!.text, fixture.DRIFTED_QUOTE.was, "never silently updated"); assert.equal(by.q2.status, "ok"); assert.equal(by.q2.nowAt, undefined, "where it was pinned"); assert.equal(by.q2.message, undefined); assert.equal(by.q2.enOrig, undefined); assert.equal(by.q3.status, "ok", "the en words are what the en track says"); assert.deepEqual(by.q3.enOrig, { text: fixture.EN_REWRITE.orig, quoted: "en", other: fixture.EN_REWRITE.en }); // The operator takes the current words: the old kept. const took = await boards.patchQuote(fixture.BOARD_CASE, "q1", { useCurrentWords: true }); assert.equal(took.quote.text, fixture.DRIFTED_QUOTE.now); assert.equal(took.quote.history?.[0].text, fixture.DRIFTED_QUOTE.was); }); test("the export is cited markdown that converts as a /sweep report: one citation per quote, verbatim", async () => { const { boards, sweepToReport } = lib; const { markdown } = await boards.exportCase("lighthouse-budget"); assert.match(markdown, /^# lighthouse budget\n/); assert.match(markdown, /## Coverage\n\nScope: Local corpus, every channel\. Searched for “lighthouse budget”: 8 recordings, complete\. Read 0 of 8\. 4 pinned\./); const converted = await sweepToReport(markdown, { id: "lighthouse-budget" }); assert.deepEqual(converted.problems, [], JSON.stringify(converted.problems)); const cites = Object.values((converted.report as { citations: Record }).citations); assert.equal(cites.length, 4); assert.ok(cites.some((c) => c.quote === "who signed off on the lighthouse budget" && c.id === "hr-2021a"), JSON.stringify(cites)); }); test("hand-off to an article: refused for a local-only board; else a plan, then a seeded sweep the editor is asked for", async () => { const { boards, handoff } = lib; const refused = await handoff.planArticle("lighthouse-budget"); assert.match(refused.plan.refusals.join(" "), /local-only channel/); await assert.rejects(handoff.confirmArticle("lighthouse-budget", {}), /local-only/); const doc = await boards.readCase("lighthouse-budget"); const lo = doc.quotes.find((q) => q.cite.localOnly)!; await boards.deleteQuote("lighthouse-budget", lo.id); const { plan, seed } = await handoff.planArticle("lighthouse-budget"); assert.deepEqual(plan.refusals, []); assert.equal(plan.article, "lighthouse-budget"); assert.equal(plan.question, "lighthouse budget"); assert.deepEqual(plan.channels.sort(), ["harbor-radio", "harbor-radio-rumble", "night-desk"]); assert.equal(plan.model, "claude-sonnet-5-5"); assert.equal(plan.capUsd, 4); assert.match(plan.warnings.join(" "), /routes sweeps to Claude Code; a seeded sweep runs the API agent/); const before = asked.length; const out = await handoff.confirmArticle("lighthouse-budget", {}); assert.deepEqual(out, { href: "/articles/lighthouse-budget", jobId: "job-1", iteration: 1 }); const sent = asked.slice(before).find((a) => a.url === "/api/ops/report-draft")!.body; assert.equal(sent.task, "sweep"); assert.equal(sent.seed, seed); assert.equal(sent.source, source); assert.equal((await boards.readCase("lighthouse-budget")).handoffs[0].href, "/articles/lighthouse-budget"); }); test("hand-off to a video: a plan, then a project whose timeline is the board's cue spans", async () => { const { boards, handoff } = lib; const plan = await handoff.planVideo("lighthouse-budget"); assert.deepEqual(plan.refusals, []); assert.equal(plan.siteOrigin, "https://archive.example"); assert.equal(plan.clips.length, 3); assert.deepEqual(plan.clips[0], { id: "c00", channel: "harbor-radio", video: "hr-2021a", start: 1212, end: 1215.8, quote: "who signed off on the lighthouse budget" }); const out = await handoff.confirmVideo("lighthouse-budget", {}); assert.equal(out.href, "/projects/lighthouse-budget"); const manifest = JSON.parse(await readFile(path.join(tmp, "reports", "lighthouse-budget", "video.manifest.json"), "utf8")); assert.equal(manifest.provenance.siteOrigin, "https://archive.example"); assert.deepEqual(manifest.timeline.map((c: { id: string; cite: number }) => [c.id, c.cite]), [["c00", 1212], ["c01", 400], ["c02", 32]]); const readme = await readFile(path.join(tmp, "reports", "lighthouse-budget", "README.md"), "utf8"); assert.match(readme, /## Clips from the research board “lighthouse budget” \(c:lighthouse-budget\) — review each/); const again = await handoff.planVideo("lighthouse-budget"); assert.match(again.refusals.join(" "), /already exists/); assert.equal((await boards.readCase("lighthouse-budget")).handoffs.length, 2); }); test("R3: a sweep on the case's scope — no seed, the policy's route (Claude Code's runs the API agent); confirmed, it answers the run's page", async () => { const { boards, handoff } = lib; // The stand-in policy routes sweeps to Claude Code: a session's, so the API agent runs it, and the plan says so. const { plan } = await handoff.planScopeSweep("lighthouse-budget"); assert.equal(plan.seeded, false); assert.equal(plan.seed.chars, 0); assert.deepEqual(plan.refusals, []); assert.match(plan.warnings.join(" "), /routes sweeps to Claude Code, which runs in a session; this sweep runs the API agent on claude-sonnet-5-5/); assert.equal(plan.model, "claude-sonnet-5-5"); assert.equal(plan.capUsd, 4); assert.deepEqual(plan.channels.sort(), ["harbor-radio", "harbor-radio-rumble", "night-desk"]); // A local model named for it: no cap, no extractor. const local = (await handoff.planScopeSweep("lighthouse-budget", { model: "qwen3:8b" })).plan; assert.deepEqual([local.model, local.capUsd, local.extractor ?? null], ["qwen3:8b", null, null]); const before = asked.length; const out = await handoff.confirmScopeSweep("lighthouse-budget", {}); assert.deepEqual(out, { href: "/research/runs/lighthouse-budget/1", jobId: "job-1", iteration: 1 }); const sent = asked.slice(before).find((a) => a.url === "/api/ops/report-draft")!.body; assert.equal(sent.task, "sweep"); assert.equal(sent.seed, undefined, "no seed: the scope alone"); assert.equal(sent.model, "claude-sonnet-5-5"); assert.equal(sent.question, "lighthouse budget"); assert.equal(sent.source, source); const h = (await boards.readCase("lighthouse-budget")).handoffs.at(-1)!; assert.match(h.what!, /^a sweep on the scope: lighthouse-budget i1, job job-1$/); });