import assert from "node:assert/strict"; import test from "node:test"; import { permutations, subsets, counts, jaccard, minimum, signature, reusedPayload, examples, affineCounterexample } from "./model.mjs"; test("225 nonempty subset pairs obey the exact collision identity across all 24 permutations", () => { const orders = permutations(["a", "b", "c", "d"]); const sets = subsets(["a", "b", "c", "d"]).slice(1); assert.equal(new Set(orders.map(p => p.join(""))).size, 24); let pairs = 0; for (const a of sets) for (const b of sets) { const shared = a.filter(item => b.includes(item)).length; const union = [...new Set([...a, ...b])].length; const matches = orders.filter(order => minimum(a, order) === minimum(b, order)).length; assert.equal(matches * union, shared * orders.length); assert.deepEqual(counts(a, b), { intersection: shared, union }); pairs++; } assert.equal(pairs, 225); }); test("256 subset pairs merge by coordinatewise minima, including empty identities", () => { const orders = permutations(["a", "b", "c", "d"]); const sets = subsets(["a", "b", "c", "d"]); let pairs = 0; for (const a of sets) for (const b of sets) { const sa = signature(a, orders), sb = signature(b, orders); assert.deepEqual(signature([...a, ...b], orders), sa.map((v, i) => Math.min(v, sb[i]))); pairs++; } assert.equal(pairs, 256); }); test("public example has two matching coordinates out of six", () => { const e = examples().agreement; assert.deepEqual(e.left, [0, 0, 0, 0, 1, 1]); assert.deepEqual(e.right, [0, 0, 1, 1, 0, 0]); assert.equal(e.matches, 2); assert.equal(e.coordinates, 6); assert.equal(e.jaccard, 1 / 3); }); test("set overlap cannot identify payload reuse, repetition or byte-stream order", () => { const rows = examples().payload; assert.deepEqual(rows.map(r => [r.reused, r.total]), [[1, 2], [9, 10], [3, 4]]); assert.ok(rows.every(r => r.jaccard === 1 / 3)); assert.equal(jaccard(["a", "c"], ["c", "a"]), 1); assert.deepEqual(signature(["a", "a", "b"], permutations(["a", "b"])), signature(["a", "b"], permutations(["a", "b"]))); }); test("identical shard scores leave merged similarity underdetermined", () => { const rows = examples().unions; assert.deepEqual(rows[0].local, rows[1].local); assert.deepEqual(rows.map(r => r.merged), [1, 1 / 2]); }); test("empty comparison is explicit, not a false collision identity", () => { assert.equal(jaccard([], []), null); assert.equal(jaccard([], ["a"]), 0); assert.deepEqual(reusedPayload([], [], {}), { reused: 0, total: 0, fraction: null }); }); test("malformed finite examples fail closed", () => { assert.throws(() => permutations(["a", "a"]), RangeError); assert.throws(() => subsets(Array.from({length: 8}, (_, i) => i)), RangeError); assert.throws(() => signature(["a"], []), RangeError); assert.throws(() => signature(["a"], [["a", "b"], ["a", "c"]]), RangeError); assert.throws(() => minimum(["c"], ["a", "b"]), RangeError); assert.throws(() => minimum(["a"], ["a", "a"]), RangeError); for (const n of [0, -1, 1.5, NaN, Infinity]) { assert.throws(() => reusedPayload([], ["a"], {a:n}), RangeError); } assert.throws(() => reusedPayload([], ["a", "a"], {a:Number.MAX_SAFE_INTEGER}), RangeError); }); test("pairwise-independent permutations need not give exact MinHash resemblance", () => { const result = affineCounterexample(); const universe = [0, 1, 2, 3, 4]; assert.equal(result.familySize, 20); assert.equal(new Set(result.orderings.map(order => order.join(","))).size, 20); for (const x of universe) for (const y of universe) if (x !== y) { const images = result.orderings.map(order => [order.indexOf(x), order.indexOf(y)]); assert.ok(images.every(([u, v]) => u !== v)); // Each distinct ordered output pair occurs exactly once out of 20. assert.equal(new Set(images.map(pair => pair.join(","))).size, 20); } assert.equal(result.matches, 7); assert.equal(result.jaccard, 1 / 3); assert.notEqual(result.matches * 3, result.familySize); });