import { test } from "node:test"; import assert from "node:assert/strict"; import { pruneSelection, selectGroups } from "./selection"; import { buildSearchHandoff, type HandoffGroup } from "./aiHandoff"; test("pruneSelection drops slugs no longer present", () => { const next = pruneSelection(new Set(["a", "b", "c"]), new Set(["b", "c", "d"])); assert.deepEqual([...next].sort(), ["b", "c"]); }); test("pruneSelection keeps everything when all still present", () => { const next = pruneSelection(new Set(["a", "b"]), new Set(["a", "b", "x"])); assert.deepEqual([...next].sort(), ["a", "b"]); }); test("pruneSelection empties when nothing matches", () => { assert.equal(pruneSelection(new Set(["a"]), new Set(["z"])).size, 0); }); test("selectGroups filters to the selection, preserving result order", () => { const groups = [{ slug: "a" }, { slug: "b" }, { slug: "c" }]; assert.deepEqual( selectGroups(groups, new Set(["c", "a"])).map((g) => g.slug), ["a", "c"], ); assert.deepEqual(selectGroups(groups, new Set()), []); }); test("getGrounding('selection') = selectGroups routed through buildSearchHandoff", () => { const groups: HandoffGroup[] = [ { slug: "a", title: "A", channel: "Ch", uploadDate: "20240101", hits: [{ start: 5, text: "alpha line" }] as never }, { slug: "b", title: "B", channel: "Ch", uploadDate: "20240102", hits: [{ start: 9, text: "beta line" }] as never }, { slug: "c", title: "C", channel: "Ch", uploadDate: "20240103", hits: [{ start: 1, text: "gamma line" }] as never }, ]; const selected = new Set(["a", "c"]); const chosen = selectGroups(groups, selected); // Mirrors the session's selectionGrounding: the selected groups, full excerpts. const handoff = buildSearchHandoff(chosen, () => undefined, ["q"], { maxVideos: chosen.length, fullExcerptVideos: chosen.length, maxHitsPerVideo: 4, }); // Only the selected videos, in order, each carrying its excerpt. assert.deepEqual(handoff.videos.map((v) => v.key), ["a", "c"]); assert.equal(handoff.videos[0].snippets[0].text, "alpha line"); assert.equal(handoff.videos[1].snippets[0].text, "gamma line"); assert.equal(handoff.totalVideos, 2); });