// The candidates inbox (UI notes item 14): a sweep's markdown read for its // proposals (real-shaped, anonymised lines in the /sweep bullet shape), the // store's one writer (a re-import changes nothing it holds; an unreadable file // is never overwritten), the place a row proposes, and Insert into a section // or a group through `timeline insert`'s writer -- one undo point, the marker // re-anchored, the group contiguous -- and Not this (recorded, hidden, kept). // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, readdir, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { CANDIDATES_FILE, addCandidates, candidateWindow, candidatesView, dismissCandidate, insertCandidate, normalizeProposal, parseCiteLink, parseSweepMarkdown, parseVerdict, readCandidates, } from "./candidates.mjs"; import { MANIFEST_NAME, insertIntoIn, manifestToken, removeEntry } from "./manifest.mjs"; import { sectionModel } from "./sectionEdit.mjs"; // The shapes the re-sweep files carry, anonymised: an archive link with %2F, // a twitch VOD id, ids with -- and a leading _, h:mm:ss, a title with ||, ?!? // and quotes in it, a "could replace", a NOTE in the why, a numbered Top 5 // restating bullets, a bullet with no date, and a set-aside list. const SWEEP = `# Re-sweep, Nov 2026: newer transcripts against the cut - Date: 1 Nov 2026. Notes for the operator; nothing in the project was edited. - Format of each bullet: \`[verdict] [title @ time](link) (upload date) "quote" — why\`. ## 1. As a streamer **As a host:** nothing found that is clearly her own first-person statement. - [strengthens fb1] [It's not the size, but how you use it... right guys?!? || Some Channel react @ 34:45](https://example-archive.pages.dev/?v=somestreamer%2FkHsAbcd3e0&t=2085) (2025-07-18) "I'm Sam. I'm a redhead who reacts to stuff a lot of times." — the same self-introduction fb1 gives ("the fiery redhead"), fifteen months before; a stock phrase - [strengthens j01] [The fight has begun! || Another react @ 43:14](https://example-archive.pages.dev/?v=somestreamer%2FFZvoVxld6Ts&t=2594) (2025-07-23) "I am just a simple country gamer." — same downplay again ## 2. Looks - [new claim] [The not-so-fabulous history of a streamer || Liquid react @ 4:44](https://example-archive.pages.dev/?v=somestreamer%2FW4pgGUq5ygQ&t=284) (2025-08-03) "I respect that looks sell." — she accepts the trade outright, then draws the line at 5:00-5:13 - [could replace s01] [It's pretty clear by now... @ 30:45](https://example-archive.pages.dev/?v=somestreamer%2FXCERAAfIePY&t=1845) (2025-08-27) "It's one of the big reasons why I use it is I need to protect my privacy." — earlier than s01 by 13 months; start the clip at 30:43 and stop at 30:57 ## 4. Why the voice - [new claim] [Working my way back to "normal" @ 5:02:46](https://example-archive.pages.dev/?v=somestreamer-twitch%2Fv2914211236&t=18166) (2026-10-29) "I didn't want my voice to be on stream so I got the changer" — a new window between cc1 and os1; NOTE: the transcript garbles a word, say it from the audio - [strengthens n02] [Looking back on my first year @ 10:16](https://example-archive.pages.dev/?v=somestreamer%2FoRsQnRc--aQ&t=616) "from day one I've used it and I've been open about it from day one" — no date on this one - [new claim] [Who wants to try? || Some react @ 6:48](https://example-archive.pages.dev/?v=somestreamer%2F_BjBoBICHaE&t=408) (2025-12-27) "maybe some of the other voices that I have might work well" — she keeps several ## Top 5 across all sections 1. [new claim] [Working my way back to "normal" @ 5:02:46](https://example-archive.pages.dev/?v=somestreamer-twitch%2Fv2914211236&t=18166) (2026-10-29) "I didn't want my voice to be on stream so I got the changer" 2. [strengthens fb1] [It's not the size, but how you use it... right guys?!? || Some Channel react @ 34:45](https://example-archive.pages.dev/?v=somestreamer%2FkHsAbcd3e0&t=2085) (2025-07-18) "I'm Sam." ## Read and set aside (not hers, or not safe to use) - \`zISdc4EwxJ4\` @ 15:29 ("I'm a host"): a child in her hypothetical about someone else. - [dropped] [A quoted tweet @ 1:00](https://example-archive.pages.dev/?v=somestreamer%2Fqmac00000&t=60) "read aloud" — a commenter she is quoting `; test("a sweep's bullets: verdict, ref, the recording and second, the date, the quote and why, the heading, the rank", () => { const { candidates: c, skipped } = parseSweepMarkdown(SWEEP, { file: "RESWEEP.md" }); assert.equal(c.length, 7); assert.deepEqual( c.map((x) => [x.channel, x.video, x.t]), [ ["somestreamer", "kHsAbcd3e0", 2085], ["somestreamer", "FZvoVxld6Ts", 2594], ["somestreamer", "W4pgGUq5ygQ", 284], ["somestreamer", "XCERAAfIePY", 1845], ["somestreamer-twitch", "v2914211236", 18166], ["somestreamer", "oRsQnRc--aQ", 616], ["somestreamer", "_BjBoBICHaE", 408], ], ); const [a, , n, r, tw, nd] = c; assert.equal(a.kind, "strengthens"); assert.equal(a.ref, "fb1"); assert.equal(a.title, "It's not the size, but how you use it... right guys?!? || Some Channel react"); assert.equal(a.date, "2025-07-18"); assert.equal(a.quote, "I'm Sam. I'm a redhead who reacts to stuff a lot of times."); assert.match(a.why, /^the same self-introduction fb1 gives \("the fiery redhead"\)/); assert.equal(a.heading, "As a streamer"); assert.equal(a.rank, 1); assert.equal(a.top, 2, "the Top 5 restating it marks it, adds nothing"); assert.equal(n.kind, "new claim"); assert.equal(n.ref, null); assert.equal(n.heading, "Looks"); assert.equal(r.kind, "could replace"); assert.equal(r.ref, "s01"); assert.equal(tw.title, 'Working my way back to "normal"'); assert.equal(tw.top, 1); assert.match(tw.why, /NOTE: the transcript garbles a word/); assert.equal(tw.heading, "Why the voice"); assert.equal(nd.date, null); assert.equal(nd.quote, "from day one I've used it and I've been open about it from day one"); assert.equal(nd.rank, 2); assert.ok(skipped.some((s) => /set-aside/.test(s.why)), "a bullet under a set-aside heading is not a proposal"); assert.ok(!c.some((x) => x.video === "zISdc4EwxJ4" || x.video === "qmac00000")); }); test("cite links, verdicts and an agent's proposal", () => { assert.deepEqual(parseCiteLink("https://x.pages.dev/?v=chan/vid-1&t=90"), { channel: "chan", video: "vid-1", t: 90 }); assert.deepEqual(parseCiteLink("https://www.youtube.com/watch?v=abcDEF12345&t=1m30s"), { channel: null, video: "abcDEF12345", t: 90 }); assert.deepEqual(parseCiteLink("https://youtu.be/abcDEF12345?t=42"), { channel: null, video: "abcDEF12345", t: 42 }); assert.equal(parseCiteLink("not a link"), null); assert.deepEqual(parseVerdict("0.91 strengthens ow2"), { kind: "strengthens", ref: "ow2", score: 0.91 }); assert.deepEqual(parseVerdict("new claim"), { kind: "new claim", ref: null, score: null }); const p = normalizeProposal({ link: "https://x.pages.dev/?v=chan%2Fvid&t=12", quote: " hello there ", verdict: "strengthens c01", section: "Two" }); assert.equal(p.channel, "chan"); assert.equal(p.t, 12); assert.equal(p.quote, "hello there"); assert.equal(p.ref, "c01"); assert.equal(p.heading, "Two"); assert.throws(() => normalizeProposal({ video: "v" }), /second/); assert.throws(() => normalizeProposal({ video: "v", t: 3, date: "3 May" }), /YYYY-MM-DD/); }); test("the window: from the sentence it starts in to a sentence end after the quote; no cues, the quote's length", () => { const cues = [ { start: 98, end: 100, text: "Before that." }, { start: 100, end: 103, text: "I respect that" }, { start: 103, end: 106, text: "looks sell." }, { start: 106, end: 110, text: "And then more words here." }, ]; assert.deepEqual(candidateWindow(101, "I respect that looks sell.", cues), { start: 100, end: 106, from: "the sentences around it" }); assert.deepEqual(candidateWindow(50, "a b c d", null), { start: 50, end: 54, from: "the quote's length (no cues)" }); }); const clip = (id, i, video = "v1") => ({ type: "clip", id, video, start: 10 * i, end: 10 * i + 8 }); const cut = () => ({ slug: "s", provenance: { channelSlug: "somestreamer" }, render: { width: 1920, height: 1080, chrome: { engine: "hyperframes", layout: "deck", deck: { markers: [{ after: "fb1", label: "As a streamer" }, { after: "j01", label: "Looks" }] } } }, timeline: [clip("c01", 0), clip("fb1", 1), clip("ow1", 2), clip("j01", 3), clip("s01", 4), clip("t01", 5)], groups: [{ id: "g1", section: 1, claim: "One claim", ids: ["ow1", "j01"] }], }); async function project() { const dir = await mkdtemp(path.join(tmpdir(), "umtool-candidates-")); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(cut(), null, 2) + "\n"); return { id: "cands", dir }; } const manifest = async (dir) => JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); test("insert into a section lands at its END, the marker re-anchored; into a group after its last clip, one of its clips", () => { const m = cut(); const r = insertIntoIn(m, "somestreamer/new1@100-105", { section: 0 }); assert.equal(r.after, "fb1"); assert.deepEqual(m.timeline.map((e) => e.id), ["c01", "fb1", "new1-100", "ow1", "j01", "s01", "t01"]); assert.equal(m.render.chrome.deck.markers[0].after, "new1-100", "the section's marker follows the new clip"); assert.equal(sectionModel(m).sectionOf.get("new1-100"), 0); const m2 = cut(); const g = insertIntoIn(m2, "somestreamer/new2@200-205", { group: "g1" }); assert.deepEqual(m2.timeline.map((e) => e.id), ["c01", "fb1", "ow1", "j01", "new2-200", "s01", "t01"]); assert.equal(m2.render.chrome.deck.markers[1].after, "new2-200"); assert.deepEqual(m2.groups[0].ids, ["ow1", "j01", "new2-200"]); assert.equal(g.groupsWritten, true); assert.throws(() => insertIntoIn(cut(), "a/b@1-3", { section: 9 }), /section must be 0–2/); assert.throws(() => insertIntoIn(cut(), "a/b@1-3", { group: "g9" }), /no group g9 \(g1\)/); assert.throws(() => insertIntoIn(cut(), "a/b@1-3", {}), /name one of them/); }); test("the store: import, a re-import changes nothing, the place each proposes, Insert into a section and a group, Not this", async () => { const p = await project(); const parsed = parseSweepMarkdown(SWEEP, { file: "RESWEEP.md" }).candidates; const r1 = await addCandidates(p.dir, parsed, { by: "sweep", now: "2026-11-02T00:00:00.000Z" }); assert.equal(r1.added.length, 7); const r2 = await addCandidates(p.dir, parsed, { by: "sweep" }); assert.equal(r2.added.length, 0); assert.equal(r2.kept.length, 7); const view = await candidatesView(p.dir); assert.deepEqual(view.counts, { proposed: 7, inserted: 0, dismissed: 0 }); assert.equal(view.candidates[0].top, 1, "the Top 5's first leads the list"); const byVideo = (v) => view.candidates.find((c) => c.video === v); assert.deepEqual(byVideo("kHsAbcd3e0").place, { section: 0, group: null, why: "fb1's section" }); assert.deepEqual(byVideo("FZvoVxld6Ts").place, { section: 1, group: "g1", why: "j01's group (g1)" }); assert.deepEqual(byVideo("W4pgGUq5ygQ").place, { section: 2, group: null, why: "the sweep's heading" }); assert.equal(byVideo("v2914211236").place.section, null); assert.deepEqual(view.sections.map((s) => s.label), [null, "As a streamer", "Looks"]); const a = byVideo("W4pgGUq5ygQ"); const token = await manifestToken(p.dir); const ins = await insertCandidate(p, a.id, { section: 2, token, cues: null }); assert.equal(ins.clip, "W4pgGUq5ygQ-284"); const m = await manifest(p.dir); assert.equal(m.timeline.at(-1).id, "W4pgGUq5ygQ-284", "the end of the last section"); const e = m.timeline.at(-1); assert.equal(e.channel, "somestreamer"); assert.equal(e.quote, "I respect that looks sell."); assert.equal(e.date, "2025-08-03"); assert.equal(e.cite, 284); assert.ok((await readdir(path.join(p.dir, "revisions"))).some((n) => n.includes("auto-before-insert")), "an undo point"); const b = byVideo("FZvoVxld6Ts"); await insertCandidate(p, b.id, { group: "g1", cues: null }); const m2 = await manifest(p.dir); assert.deepEqual(m2.groups[0].ids, ["ow1", "j01", "FZvoVxld6Ts-2594"]); assert.equal(m2.timeline[m2.timeline.findIndex((x) => x.id === "j01") + 1].id, "FZvoVxld6Ts-2594"); const d = byVideo("_BjBoBICHaE"); await dismissCandidate(p.dir, d.id, { note: "not her voice" }); await assert.rejects(insertCandidate(p, d.id, { section: 0, cues: null }), /set aside/); await assert.rejects(insertCandidate(p, a.id, { section: 0, cues: null }), /in the cut already/); await assert.rejects(insertCandidate(p, byVideo("oRsQnRc--aQ").id, { section: 0, token: "stale", cues: null }), /changed/i); const after = await readCandidates(p.dir); assert.equal(after.doc.candidates.length, 7, "nothing is ever deleted"); assert.equal(after.doc.candidates.find((x) => x.id === d.id).status, "dismissed"); assert.equal(after.doc.candidates.find((x) => x.id === d.id).dismissed.note, "not her voice"); assert.equal(after.doc.candidates.find((x) => x.id === a.id).inserted.clip, "W4pgGUq5ygQ-284"); // Proposing the set-aside moment again does not bring it back. await addCandidates(p.dir, [parsed.find((x) => x.video === "_BjBoBICHaE")]); assert.equal((await readCandidates(p.dir)).doc.candidates.find((x) => x.id === d.id).status, "dismissed"); const v2 = await candidatesView(p.dir); assert.deepEqual(v2.counts, { proposed: 4, inserted: 2, dismissed: 1 }); // Its clip taken out of the cut again (an Undo, a removal): the row waits again, and says what it was. await removeEntry(p.dir, "W4pgGUq5ygQ-284"); const v3 = await candidatesView(p.dir); const back = v3.candidates.find((x) => x.id === a.id); assert.equal(back.status, "proposed"); assert.equal(back.left, "W4pgGUq5ygQ-284"); assert.deepEqual(v3.counts, { proposed: 5, inserted: 1, dismissed: 1 }); const again = await insertCandidate(p, a.id, { section: 1, cues: null }); assert.equal((await readCandidates(p.dir)).doc.candidates.find((x) => x.id === a.id).inserted.clip, again.clip); }); test("an unreadable file is never overwritten: a write refuses, a read is empty with the reason", async () => { const p = await project(); await writeFile(path.join(p.dir, CANDIDATES_FILE), "{ not json"); const r = await readCandidates(p.dir); assert.equal(r.doc.candidates.length, 0); assert.match(r.error, /not JSON/); await assert.rejects(addCandidates(p.dir, [normalizeProposal({ video: "v", t: 1 })]), (e) => e.status === 409); assert.equal(await readFile(path.join(p.dir, CANDIDATES_FILE), "utf8"), "{ not json"); });