import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { sha256Hex, buildMerkle, merkleProof, verifyProof, shardIndex, } from '../src/merkle.js'; describe('merkle', () => { it('one leaf is its own root', () => { const leaf = sha256Hex('only'); const t = buildMerkle([leaf]); assert.equal(t.root, leaf); assert.equal(verifyProof(leaf, merkleProof(t.layers, 0), t.root), true); }); it('three leaves: each proof verifies, unknown leaf does not', () => { const leaves = ['a', 'b', 'c'].map((s) => sha256Hex(s)); const t = buildMerkle(leaves); for (let i = 0; i < leaves.length; i += 1) { const proof = merkleProof(t.layers, i); assert.equal(verifyProof(leaves[i], proof, t.root), true); } assert.equal(verifyProof(sha256Hex('nope'), merkleProof(t.layers, 0), t.root), false); }); it('shards stably into three nodes', () => { const h = sha256Hex('x'); assert.equal(shardIndex(h, 3), shardIndex(h, 3)); assert.ok(shardIndex(h, 3) >= 0 && shardIndex(h, 3) < 3); }); });