// Release 38 lane C's stages over the e2e fixture case (e2e/fixtures/case-stages-fixture.mjs), as the routes run // them, against the e2e suite's own stand-in editor routes (editor-stub-case-stages.mjs, editor-stub-import-page.mjs): // verify (both tracks lit, a correction that keeps the reader's words, the private-name gate, the audio check), // leads (counts, never-archive as a hash, archive on no site, context withheld), evidence (an excluded target, a // capture, a forum attachment marked unverified, attached to a finding) and the export's sections. // // Run with: pnpm --filter umtool exec tsx --test lib/research/case-stages.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 editor: http.Server; // The case stages' stub records {path, body}; the intake stub (capture-posts) records {route, ...body}. const asked: ({ path: string; body: Record } & { route?: string })[] = []; let lib: { verify: typeof import("./case-verify"); leads: typeof import("./case-leads"); evidence: typeof import("./case-evidence"); findings: typeof import("./case-findings-store"); fixture: typeof import("../../e2e/fixtures/case-stages-fixture.mjs"); }; let CASE: string; let caseDir: string; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-case-stages-")); const research = await import("../../e2e/fixtures/research-fixture.mjs"); const fixture = await import("../../e2e/fixtures/case-stages-fixture.mjs"); const channels = path.join(tmp, "channels"); const { pub } = research.makeResearchFixture({ dest: tmp, channels }); const made = fixture.makeCaseStagesFixture({ reportsRoot: path.join(tmp, "reports"), channels, pub }); CASE = made.id; caseDir = made.dir; const { caseStagesRoute } = await import("../../e2e/fixtures/editor-stub-case-stages.mjs"); const { importPageRoute } = await import("../../e2e/fixtures/editor-stub-import-page.mjs"); // capture-posts is the intake stub's (shared with U4's Add post). const { intakeRoute } = await import("../../e2e/fixtures/editor-stub-intake.mjs"); const json = (res: http.ServerResponse, status: number, body: unknown) => { res.writeHead(status, { "content-type": "application/json" }); res.end(JSON.stringify(body)); }; const readBody = (req: http.IncomingMessage, then: (b: Record | null) => void) => { let raw = ""; req.on("data", (c) => (raw += c)); req.on("end", () => then(raw ? JSON.parse(raw) : null)); }; const ctx = { channels, json, readBody, push: (r: { path: string; body: Record }) => asked.push(r as (typeof asked)[number]) }; editor = http.createServer((req, res) => { const url = new URL(req.url ?? "/", "http://stub"); if (caseStagesRoute(req, res, url, ctx)) return; if (importPageRoute(req, res, url, ctx)) return; if (intakeRoute(req, res, url, ctx)) return; json(res, 404, { ok: false, error: `no route ${url.pathname}` }); }); 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 = 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.SONG_REPORTS_DIR = path.join(tmp, "reports", "song"); lib = { verify: await import("./case-verify"), leads: await import("./case-leads"), evidence: await import("./case-evidence"), findings: await import("./case-findings-store"), fixture, }; }); after(async () => { await new Promise((r) => editor.close(r)); await rm(tmp, { recursive: true, force: true }); }); const wait = (ms: number) => new Promise((r) => setTimeout(r, ms)); test("verify: the findings, a forum one flagged; both tracks side by side with the quote lit where it lands", async () => { const v = await lib.verify.verifyView(CASE); assert.deepEqual(v.rows.map((r) => r.row), ["f1", "f2", "f3"]); assert.deepEqual(v.rows.map((r) => r.status), ["unverified", "unverified", "unverified"]); assert.deepEqual(v.rows.map((r) => r.forum), [false, false, true]); const c = await lib.verify.findingCaptions(CASE, "f1"); assert.deepEqual(c.tracks.map((t) => t.track), ["en-orig", "en"], "en-orig first"); const [orig, en] = c.tracks; assert.equal(en.holds, true, "the quote is the en track's words"); assert.equal(orig.holds, false); assert.equal(orig.matched, 4, "en-orig says 'called in': four of five words"); const lit = (t: (typeof c.tracks)[number]) => t.cues.flatMap((q) => q.parts.filter((p) => p.hit).map((p) => p.text)).join(" "); assert.equal(lit(en), "the lighthouse keeper rang in"); assert.equal(lit(orig), "the lighthouse keeper in"); const post = await lib.verify.findingCaptions(CASE, "f3"); assert.equal(post.tracks.length, 1); assert.match(post.tracks[0].label, /^forum post #3 by deckhand$/); assert.equal(post.tracks[0].holds, true); }); test("verify: corrected by hand keeps the reader's words, once; the private-name gate; status rules", async () => { await assert.rejects(lib.verify.setVerify(CASE, "f1", { status: "verified", quote: "the lighthouse keeper called in" }), /status corrected/); await assert.rejects(lib.verify.setVerify(CASE, "f1", { status: "fine" }), /not a status/); await assert.rejects( lib.verify.setVerify(CASE, "f1", { status: "corrected", note: `asked ${lib.fixture.PRIVATE_NAME} about it` }), (e: Error & { status?: number }) => e.status === 409 && /privacy gate blocked the findings: 1 private-name hit in 1 item \(f1\)/.test(e.message) && !e.message.includes(lib.fixture.PRIVATE_NAME), ); const r = await lib.verify.setVerify(CASE, "f1", { status: "corrected", quote: "the lighthouse keeper called in", note: "en-orig and the audio agree" }); assert.equal(r.quote, "the lighthouse keeper called in"); assert.deepEqual(r.verify?.original, { quote: "the lighthouse keeper rang in", speaker: "host" }); assert.equal(r.verify?.how, "hand"); const again = await lib.verify.setVerify(CASE, "f1", { status: "corrected", quote: "the lighthouse keeper called in tonight" }); assert.deepEqual(again.verify?.original?.quote, "the lighthouse keeper rang in", "the first reader's words, never the last correction's"); const { rows } = await lib.findings.readFindings(CASE); assert.equal(rows.length, 3, "rewritten whole, nothing lost"); const q = await lib.verify.setVerify(CASE, "f3", { status: "quarantined", note: "a forum joke until shown otherwise" }); assert.equal(q.status, "quarantined"); }); test("verify: the audio check transcribes the held window through the editor, while paused when asked, and diffs it", async () => { const before = asked.length; const a = await lib.verify.audioStep(CASE, "f2", { whilePaused: true }); assert.equal(a.state, "transcribing"); const req = asked.slice(before).find((r) => r.path === "/api/ops/transcribe")!; assert.match(String(req.body.path), /harbor-radio\/data\/hr-2022a\/audio\.m4a$/); assert.equal(req.body.start, 2039); assert.equal(req.body.end, 2074); assert.equal(req.body.whilePaused, true); let step: import("./case-verify").AudioAnswer = a; for (let i = 0; i < 20 && step.state === "transcribing"; i++) { await wait(200); step = await lib.verify.audioStep(CASE, "f2", { job: (step as { job: string }).job }); } assert.equal(step.state, "done"); if (step.state !== "done") return; assert.equal(step.audio.matched, 9); assert.equal(step.audio.words, 9); assert.equal(step.audio.worker, "stub parakeet"); assert.ok(step.ops.every((o: { op: string }) => o.op === "same")); const row = (await lib.verify.verifyView(CASE)).rows.find((r) => r.row === "f2")!; assert.equal(row.verify?.audio?.matched, 9, "kept on the finding"); assert.equal(row.status, "unverified", "the check informs; a person sets the status"); const v = await lib.verify.setVerify(CASE, "f2", { status: "verified", how: "audio" }); assert.equal(v.verify?.how, "audio"); assert.equal(v.verify?.audio?.matched, 9, "a status keeps the audio check"); }); test("leads: counted by post, the forum's own links left out, a private name withheld", async () => { const v = await lib.leads.leadsView(CASE); assert.equal(v.channel, lib.fixture.FORUM, "the scope's posts channel"); assert.equal(v.posts, 7); const counts = Object.fromEntries(v.leads.map((l) => [l.key, l.count])); assert.deepEqual(counts, { "x:@harborwatch": 3, "harbor-radio.example/archive": 2, "x:@tidewatcher": 2, "newgrounds:pocketfox": 1, "youtube:@harborradio": 1, }); assert.equal(v.withheld, 1, "the private name's account is counted, never listed"); assert.ok(!JSON.stringify(v).toLowerCase().includes(lib.fixture.PRIVATE_NAME.toLowerCase())); const ctx = await lib.leads.leadContext(CASE, lib.fixture.FORUM, "x:@harborwatch"); assert.equal(ctx.snippets.length, 3); assert.deepEqual(ctx.snippets.map((s) => s.position), [1, 2, 4]); }); test("leads: never-archive feeds the name as a hash and keeps the lead only as it; archive creates on no site", async () => { const r = await lib.leads.decideLead(CASE, { key: "x:@tidewatcher", disposition: "never-archive", role: "the account the forum blames", url: "https://x.com/TideWatcher", count: 2 }); assert.equal(r.lead.key, `hash:${lib.fixture.nameHash("TideWatcher")}`); assert.equal(r.lead.url, ""); const pipeline = JSON.parse(await readFile(path.join(caseDir, "pipeline.json"), "utf8")); assert.ok(pipeline.privateNames.sha256.includes(lib.fixture.nameHash("TideWatcher"))); assert.equal(pipeline.subjects[0].name, "Harbor Radio", "the rest of the definition kept"); const leadsText = await readFile(path.join(caseDir, "leads.json"), "utf8"); assert.ok(!/tidewatcher/i.test(leadsText), "no file holds the name"); const v = await lib.leads.leadsView(CASE); assert.equal(v.withheld, 2); assert.ok(!v.leads.some((l) => l.key === "x:@tidewatcher")); assert.equal(v.leads.filter((l) => l.withheld).length, 1); // Its snippet elsewhere now withholds it too. const ctx = await lib.leads.leadContext(CASE, lib.fixture.FORUM, "x:@harborwatch"); assert.ok(!JSON.stringify(ctx).toLowerCase().includes("tidewatcher")); await assert.rejects(lib.leads.decideLead(CASE, { key: r.lead.key, disposition: "watch" }), /stays never-archive/); await assert.rejects(lib.leads.decideLead(CASE, { key: "x:@harborwatch", role: `${lib.fixture.PRIVATE_NAME}'s friend` }), /privacy gate blocked the lead's role note/); const before = asked.length; const a = await lib.leads.decideLead(CASE, { key: "x:@harborwatch", disposition: "archive", url: "https://x.com/HarborWatch", count: 3 }); const create = asked.slice(before).find((x) => x.path === "/api/ops/create-channel")!; assert.equal((create.body.fields as { url: string }).url, "https://x.com/HarborWatch"); assert.equal("sites" in create.body, false, "X on no site, by rule"); assert.equal(create.body.fetchPostsNow, true); assert.equal(a.lead.channel, a.created?.slug); const roleOnly = await lib.leads.decideLead(CASE, { key: "x:@harborwatch", role: "posts the council minutes" }); assert.equal(roleOnly.lead.disposition, "archive", "a role alone leaves the disposition as it was"); assert.equal(roleOnly.lead.channel, a.lead.channel); assert.equal(roleOnly.created, undefined, "nothing is created twice"); const undecided = await lib.leads.decideLead(CASE, { key: "youtube:@harborradio", disposition: "watch", url: "https://www.youtube.com/@HarborRadio" }); assert.equal(undecided.lead.disposition, "watch"); assert.equal((await lib.leads.decideLead(CASE, { key: "youtube:@harborradio", disposition: null })).lead.disposition, undefined); const older = await lib.leads.startOlderWalk(CASE, "x:@harborwatch"); assert.equal(older.slug, a.lead.channel); assert.deepEqual(asked[asked.length - 1].body, { slug: a.lead.channel, older: true }); }); test("evidence: an excluded target, a capture at a date, a forum attachment marked unverified, attached to a finding", async () => { const added = await lib.evidence.addTargets(CASE, [lib.fixture.EXCLUDED_URL, lib.fixture.TARGET_URL], ["2014-03-04"]); assert.deepEqual(added.map((r) => [r.id, r.at, r.state]), [["e1", "20140304", "planned"], ["e2", "20140304", "planned"]]); const gone = await lib.evidence.lookupTarget(CASE, "e1"); assert.equal(gone.state, "excluded"); assert.equal(gone.label, "excluded from the Wayback Machine (cause unknown)"); await assert.rejects(lib.evidence.captureTarget(CASE, "e1", { slug: lib.fixture.PAGES }), /excluded from the Wayback Machine/); const found = await lib.evidence.lookupTarget(CASE, "e2"); assert.match(found.message ?? "", /2 captures; nearest 2014-03-04/); const q = await lib.evidence.captureTarget(CASE, "e2", { slug: lib.fixture.PAGES }); assert.equal(q.state, "queued"); let row = q; for (let i = 0; i < 20 && row.state === "queued"; i++) { await wait(200); row = await lib.evidence.refreshEvidence(CASE, "e2"); } assert.equal(row.state, "captured"); assert.equal(row.captureTs, "20140304120000"); assert.equal(row.label, "Wayback capture, 2014-03-04"); const f = await lib.evidence.addForumAttachment(CASE, lib.fixture.FORUM, "70003"); assert.equal(f.label, "forum attachment, post #3, unverified"); assert.deepEqual(asked.filter((x) => x.route === "capture-posts").at(-1), { route: "capture-posts", slug: lib.fixture.FORUM, ids: ["70003"], shots: true, media: true }); let fr = f; for (let i = 0; i < 20 && fr.state === "queued"; i++) { await wait(200); fr = await lib.evidence.refreshEvidence(CASE, f.id); } assert.equal(fr.state, "captured"); assert.deepEqual(fr.files, ["minutes.png", "shot.png"]); const att = await lib.evidence.attachEvidence(CASE, f.id, "f3"); assert.deepEqual(att.finding.evidence, [{ kind: "forum-attachment", url: lib.fixture.FORUM_POSTS[2].url, evidence: f.id, post: "70003", position: 3 }]); const v = await lib.verify.verifyView(CASE); assert.deepEqual(v.rows.find((r) => r.row === "f3")?.evidenceLabels, ["forum attachment, post #3, unverified"]); const md = lib.evidence.evidenceSection(await lib.evidence.readEvidence(CASE)).join("\n"); assert.match(md, /^## Evidence\n/); assert.match(md, /- excluded from the Wayback Machine \(cause unknown\) — http:\/\/gone\.harbor-radio\.example\/, asked for 2014-03-04/); assert.match(md, /- Wayback capture, 2014-03-04 — \[http:\/\/harbor-radio\.example\/archive\]\(https:\/\/web\.archive\.org\/web\/20140304120000\/http:\/\/harbor-radio\.example\/archive\)/); assert.match(md, /- forum attachment, post #3, unverified — \[the post\]\(.+post-70003\), 2 files captured \(for f3\)/); });