import { test } from "node:test"; import assert from "node:assert/strict"; import { collapseDuplicates, type ClusterOf, type CollapsibleHit } from "./collapse"; const hit = (slug: string): CollapsibleHit => ({ slug, videoId: slug.split("/")[1], channelName: slug.split("/")[0], }); const index = ( entries: [string, ClusterOf][], ): ReadonlyMap => new Map(entries); test("an empty index collapses nothing and keeps every row", () => { const hits = [hit("a/1"), hit("b/2")]; const r = collapseDuplicates(hits, index([])); assert.equal(r.collapsed, 0); assert.equal(r.clusters, 0); assert.deepEqual(r.kept.map((h) => h.slug), ["a/1", "b/2"]); }); test("mirrors fold into the first match and are NAMED, never dropped", () => { const hits = [hit("a/1"), hit("b/2"), hit("c/3")]; const r = collapseDuplicates( hits, index([ ["a/1", { clusterId: "k", isCanonical: false }], ["b/2", { clusterId: "k", isCanonical: false }], ]), ); assert.equal(r.collapsed, 1); assert.equal(r.clusters, 1); assert.deepEqual(r.kept.map((h) => h.slug), ["a/1", "c/3"]); assert.deepEqual(r.kept[0].mirrors, [ { videoId: "2", channelName: "b", slug: "b/2" }, ]); }); test("a canonical member arriving LATER is promoted to the kept row", () => { const hits = [hit("a/1"), hit("b/2")]; const r = collapseDuplicates( hits, index([ ["a/1", { clusterId: "k", isCanonical: false }], ["b/2", { clusterId: "k", isCanonical: true }], ]), ); assert.equal(r.collapsed, 1); assert.deepEqual(r.kept.map((h) => h.slug), ["b/2"]); assert.deepEqual(r.kept[0].mirrors, [ { videoId: "1", channelName: "a", slug: "a/1" }, ]); }); test("an ABSENT canonical does not delete the surviving mirror", () => { // The cluster's canonical is "gone/9", which did not match. The first match // stays the representative rather than the row vanishing — a mirror is often // the only surviving copy of a deleted upload. const hits = [hit("a/1"), hit("b/2")]; const r = collapseDuplicates( hits, index([ ["a/1", { clusterId: "k", isCanonical: false }], ["b/2", { clusterId: "k", isCanonical: false }], ["gone/9", { clusterId: "k", isCanonical: true }], ]), ); assert.deepEqual(r.kept.map((h) => h.slug), ["a/1"]); }); test("three copies of one recording fold to one row carrying both mirrors", () => { const hits = [hit("a/1"), hit("b/2"), hit("c/3")]; const r = collapseDuplicates( hits, index([ ["a/1", { clusterId: "k", isCanonical: true }], ["b/2", { clusterId: "k", isCanonical: false }], ["c/3", { clusterId: "k", isCanonical: false }], ]), ); assert.equal(r.collapsed, 2); assert.equal(r.clusters, 1); assert.deepEqual(r.kept.map((h) => h.slug), ["a/1"]); assert.deepEqual( (r.kept[0].mirrors ?? []).map((m) => m.slug), ["b/2", "c/3"], ); }); test("collapsing does not mutate the input array", () => { const hits = [hit("a/1"), hit("b/2")]; collapseDuplicates( hits, index([ ["a/1", { clusterId: "k", isCanonical: false }], ["b/2", { clusterId: "k", isCanonical: false }], ]), ); assert.equal(hits.length, 2); assert.equal(hits[0].mirrors, undefined); });