// Release 36 lane D: the assistant's labels and its answer to a comment — the // pure parts (lib/clips/suggest.mjs) over the synthetic reactions project. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { askClipsOf, askNoteOf, askPrompt, parseAskAnswer, parseSuggestAnswer, readSuggestions, roleLines, suggestionsDocOf, suggestionsState, suggestItems, suggestPrompt, wordsOfFile, writeSuggestions, } from "./suggest.mjs"; import { labelAnswer, reactionProject, writeReactionProject, PROJECT_NOTE } from "../../testing/clips/reactionProject.mjs"; const fixture = () => { const p = reactionProject(); return { ...p, wordList: wordsOfFile(p.words) }; }; test("a proposal in words: each line marked by who speaks, what the finder heard, capped", () => { const { reactions, wordList } = fixture(); const items = suggestItems(reactions, wordList); assert.deepEqual(items.map((i) => i.id), ["p001", "p002", "p003", "p004", "p005"]); const p001 = items[0]!; assert.deepEqual(p001.kinds, ["laugh"]); // 88–99: the subject talks to 95, the host laughs and talks after. assert.deepEqual(p001.lines.map((l) => l.role), ["subject", "host"]); assert.match(p001.lines[1]!.text, /^haha/); assert.deepEqual(items.find((i) => i.id === "p005")!.kinds, ["chat"]); // Only the ids asked. assert.deepEqual(suggestItems(reactions, wordList, { ids: ["p003"] }).map((i) => i.id), ["p003"]); // A long span is cut at the cap, with an ellipsis. const long = roleLines({ words: wordList, turns: reactions.turns, speakers: reactions.speakers }, 0, 600, 80); assert.ok(long.map((l) => l.text).join(" ").length <= 84); assert.match(long.at(-1)!.text, /…$/); const pr = suggestPrompt(items.slice(0, 2)); assert.match(pr.user, /\[p001\] 1:28–1:39 \(11 s\) · heard: laugh\nSUBJECT: n\d+/); assert.match(pr.user, /\nHOST: haha/); assert.match(pr.system, /at most six words/); }); test("a model's answer: labels kept, a malformed answer refused, a label over six words refused (never cut down)", () => { const ids = ["p001", "p002", "p003"]; assert.deepEqual(parseSuggestAnswer({ labels: [] }, ids), { ok: false, error: "the answer is not { items: [...] }" }); assert.equal(parseSuggestAnswer(null, ids).ok, false); const r = parseSuggestAnswer( { items: [ { id: "p001", label: '"The badge reveal."', why: "The host\nloses it.", score: 9 }, { id: "[p002]", label: "a label with far too many words in it", why: "x", score: 5 }, { id: "p003", label: "flat", why: "Nothing lands.", score: 11 }, { id: "p009", label: "never asked", why: "x", score: 3 }, { id: "p001", label: "a second answer", why: "x", score: 1 }, ], }, ids, ); assert.ok(r.ok); assert.deepEqual(r.items, [{ id: "p001", label: "The badge reveal", why: "The host loses it.", score: 9 }]); assert.deepEqual( r.refused.map((x) => [x.id, x.error]), [ ["p002", "a label of 9 words (at most 6)"], ["p003", "a score of 11 (1 to 10)"], ["p009", "an id that was not asked"], ], ); assert.deepEqual(r.missing, []); const partial = parseSuggestAnswer({ items: [{ id: "p001", label: "one", why: "w", score: 2 }] }, ids); assert.ok(partial.ok); assert.deepEqual(partial.missing, ["p002", "p003"]); }); test("the file: ranked by the model's score; merged over the old file's current labels; stale when the proposals moved", async () => { const { reactions } = fixture(); const at = "2026-10-19T01:00:00.000Z"; const first = suggestionsDocOf({ model: "qwen3:8b", at, reactions, fresh: labelAnswer(["p001", "p002", "p003", "p004", "p005"]).items }); // labelAnswer scores 2, 3, 4, 5, 6: the last is the best. assert.deepEqual(Object.entries(first.items).map(([id, it]) => [id, it.rank]), [["p005", 1], ["p004", 2], ["p003", 3], ["p002", 4], ["p001", 5]]); assert.equal(first.proposedAt, reactions.proposedAt); assert.deepEqual(first.items.p001, { label: "the p001 moment", why: "Something happens in p001.", rank: 5, score: 2, start: 88, end: 99 }); assert.deepEqual(suggestionsState(first, reactions), { stale: false, why: null, passNewer: false, staleIds: [], missing: [] }); // A tie on the model's score goes to the finder's score. const tie = suggestionsDocOf({ model: "m", at, reactions, fresh: ["p003", "p001"].map((id) => ({ id, label: id, why: "w", score: 5 })) }); assert.deepEqual(Object.keys(tie.items), ["p001", "p003"]); // A later proposal pass: the file is stale; a proposal moved: that one is. const later = { ...reactions, proposedAt: "2026-10-20T00:00:00.000Z" }; assert.deepEqual(suggestionsState(first, later), { stale: true, why: "the proposals were made again after these labels", passNewer: true, staleIds: [], missing: [] }); const moved = { ...reactions, proposals: reactions.proposals.map((p: { id: string; start: number }) => (p.id === "p002" ? { ...p, start: p.start + 2 } : p)).filter((p: { id: string }) => p.id !== "p005") }; assert.deepEqual(suggestionsState(first, moved), { stale: true, why: "2 proposals changed since these labels", passNewer: false, staleIds: ["p005", "p002"], missing: [] }); // Labelling only p003 again keeps the others' current labels and drops the stale ones. const again = suggestionsDocOf({ model: "claude-sonnet-5-5", at, reactions: moved, fresh: [{ id: "p003", label: "now the best", why: "w", score: 10 }], prev: first }); assert.deepEqual(Object.keys(again.items), ["p003", "p004", "p001"]); assert.equal(again.items.p003!.rank, 1); assert.equal(again.model, "claude-sonnet-5-5"); // The writer refuses what is not the shape; the reader is lenient about absence, not about junk. const dir = await mkdtemp(path.join(os.tmpdir(), "clip-suggest-")); try { assert.deepEqual(await readSuggestions(dir), { doc: null, error: null }); await assert.rejects(writeSuggestions(dir, { model: "m", at, items: { p001: { label: "x", why: "y", rank: 0 } } }), /not a suggestions doc/); await writeSuggestions(dir, first); assert.deepEqual((await readSuggestions(dir)).doc, first); assert.match(await readFile(path.join(dir, "clip-suggestions.json"), "utf8"), /"promptVersion": "clips-suggest-1"/); await writeFile(path.join(dir, "clip-suggestions.json"), "{"); assert.match(String((await readSuggestions(dir)).error), /not JSON/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("Ask: the note on a clip, the clip and its next one of the same recording, the prompt with times", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "clip-ask-")); try { const { manifest } = writeReactionProject(dir, { note: { clip: "r002", text: "start it where he laughs" } }); const notes = JSON.parse(await readFile(path.join(dir, "notes.json"), "utf8")); const n = askNoteOf(notes, PROJECT_NOTE); assert.ok("note" in n); assert.equal(n.note.clip, "r002"); assert.deepEqual(askNoteOf(notes, "n_none0000"), { error: "no note n_none0000" }); const clips = askClipsOf(manifest, "r002"); assert.ok(!("error" in clips)); assert.deepEqual([clips.prev?.id, clips.clip.id, clips.next?.id], ["r001", "r002", "r003"]); // A card between, or another recording: no merge. const other = { ...manifest, timeline: [manifest.timeline[1], { ...manifest.timeline[2], video: "other" }] }; const o = askClipsOf(other, "r002"); assert.ok(!("error" in o) && o.next === null && o.nextAny?.id === "r003"); const { reactions, words } = reactionProject(); const pr = askPrompt({ note: n.note, clips, words: wordsOfFile(words), turns: reactions.turns, speakers: reactions.speakers }); assert.match(pr.user, /The comment on clip r002 \(88\.0–99\.0 s\): start it where he laughs/); assert.match(pr.user, /The next clip: r003, 333\.0–344\.0 s/); assert.match(pr.user, /\[95\.\d–\d+\.\d\] HOST: haha/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("Ask's answer: a trim within reach kept, one too far dropped, a merge only with a next clip, a malformed answer refused", () => { const { manifest } = reactionProject(); const clips = askClipsOf(manifest, "r002"); assert.ok(!("error" in clips)); const basis = { cutStart: 88, cutEnd: 99 }; assert.deepEqual(parseAskAnswer({ reply: "Start at the laugh.", proposal: { kind: "trim", from: 94.6, to: 99 } }, clips), { ok: true, reply: "Start at the laugh.", proposal: { kind: "trim", clip: "r002", from: 94.6, to: 99, basis }, dropped: null, }); const far = parseAskAnswer({ reply: "x", proposal: { kind: "trim", from: 20, to: 99 } }, clips); assert.ok(far.ok && far.proposal === null && /more than 30 s/.test(String(far.dropped))); const same = parseAskAnswer({ reply: "x", proposal: { kind: "trim", from: 88, to: 99 } }, clips); assert.ok(same.ok && same.proposal === null && /leaves the clip where it is/.test(String(same.dropped))); assert.deepEqual(parseAskAnswer({ reply: "Join them.", proposal: { kind: "merge", from: 0, to: 0 } }, clips), { ok: true, reply: "Join them.", proposal: { kind: "merge", clip: "r002", with: "r003", basis }, dropped: null, }); const last = askClipsOf(manifest, "r003"); assert.ok(!("error" in last)); const noNext = parseAskAnswer({ reply: "Join them.", proposal: { kind: "merge" } }, last); assert.ok(noNext.ok && noNext.proposal === null && /no next clip/.test(String(noNext.dropped))); const cut = parseAskAnswer({ reply: "Leave it out.", proposal: { kind: "cut", from: 0, to: 0 } }, clips); assert.ok(cut.ok && cut.proposal?.kind === "cut"); const none = parseAskAnswer({ reply: "It works.", proposal: { kind: "none", from: 0, to: 0 } }, clips); assert.ok(none.ok && none.proposal === null && none.dropped === null); assert.deepEqual(parseAskAnswer({ answer: "x" }, clips), { ok: false, error: "the answer is not { reply, proposal }" }); assert.equal(parseAskAnswer({ reply: " " }, clips).ok, false); });