import { test } from "node:test"; import assert from "node:assert/strict"; import { dHash, duplicatesOf, hamming, hashesOf, THUMB_BYTES, THUMB_H, THUMB_W } from "./dhash"; const flat = (v: number) => new Uint8Array(THUMB_BYTES).fill(v); const ramp = (dir: 1 | -1) => { const a = new Uint8Array(THUMB_BYTES); for (let r = 0; r < THUMB_H; r++) for (let c = 0; c < THUMB_W; c++) a[r * THUMB_W + c] = dir === 1 ? c * 10 : 250 - c * 10; return a; }; test("dHash: brightness-proof, and a picture that moves changes it", () => { assert.equal(dHash(flat(10)), dHash(flat(200))); assert.equal(dHash(ramp(-1)).length, ((THUMB_W - 1) * THUMB_H) / 4); assert.equal(hamming(dHash(ramp(-1)), dHash(ramp(1))), (THUMB_W - 1) * THUMB_H); assert.equal(hamming(dHash(flat(1)), dHash(flat(1))), 0); assert.throws(() => dHash(new Uint8Array(3))); }); test("hamming: Infinity when two hashes cannot be compared", () => { assert.equal(hamming("ab", "abc"), Infinity); assert.equal(hamming("", ""), Infinity); assert.equal(hamming("f0", "0f"), 8); }); test("hashesOf: one hash per thumbnail in a rawvideo stream", () => { const raw = Buffer.concat([Buffer.from(flat(5)), Buffer.from(ramp(-1)), Buffer.alloc(7)]); const hs = hashesOf(raw); assert.equal(hs.length, 2); assert.equal(hs[1], dHash(ramp(-1))); }); test("duplicatesOf: a duplicate of the last READ frame is skipped; a run is read again every N", () => { const A = "0000"; const B = "ffff"; const out = duplicatesOf([A, A, A, B, B, A, A, A, A, A], { maxBits: 0, rereadEvery: 3 }); assert.deepEqual(out, [null, 0, 0, null, 3, null, 5, 5, null, 8]); // Near enough under maxBits. assert.deepEqual(duplicatesOf(["0000", "0001", "0003"], { maxBits: 1, rereadEvery: 0 }), [null, 0, null]); });