import { describe, expect, it, } from "vitest"; import { createTestDb, } from "./db/test-db-factory.js"; import { Node, } from "./node.js"; import { NodeKey, } from "./node-key.js"; import { NodeDB, } from "./storage/node-db.js"; import { DEFAULT_OPTIONS, } from "./types/options.js"; import { stringToBytes, } from "./utils/bytes.js"; /** * Helper to create a NodeDB instance for testing */ async function createTestNodeDB(): Promise { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); return nodeDB; } /** * Helper to add nodes to NodeDB for testing */ async function addNodesToNodeDB(nodeDB: NodeDB, ...nodes: Node[]): Promise { for (const node of nodes) { if (!node.nodeKey) { throw new Error("Node must have nodeKey"); } // Compute hash if not already computed (required for serialization) if (!node.hash && node.nodeKey) { node.computeHash(node.nodeKey.version); } await nodeDB.saveNode(node); } } describe("Node - Basic Operations", () => { describe("newLeafNode", () => { it("should create a leaf node", async () => { const key = stringToBytes("test-key"); const value = stringToBytes("test-value"); const node = Node.newLeafNode(key, value); expect(node.key).toEqual(key); expect(node.value).toEqual(value); expect(node.subtreeHeight).toBe(0); expect(node.size).toBe(1n); expect(node.isLeaf()).toBe(true); }); it("should have null children", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); expect(node.leftNode).toBeNull(); expect(node.rightNode).toBeNull(); expect(node.leftNodeKey).toBeNull(); expect(node.rightNodeKey).toBeNull(); }); it("should have null hash initially", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); expect(node.hash).toBeNull(); }); }); describe("newBranchNode", () => { it("should create a branch node from two leaves", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("value-a")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("value-b")); const branch = Node.newBranchNode(left, right); expect(branch.isLeaf()).toBe(false); expect(branch.subtreeHeight).toBe(1); expect(branch.size).toBe(2n); expect(branch.key).toEqual(stringToBytes("b")); // Smallest key in right subtree expect(branch.leftNode).toBe(left); expect(branch.rightNode).toBe(right); }); it("should calculate height correctly", async () => { const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const leaf3 = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); const branch1 = Node.newBranchNode(leaf1, leaf2); // height = 1 const branch2 = Node.newBranchNode(branch1, leaf3); // height = 2 expect(branch1.subtreeHeight).toBe(1); expect(branch2.subtreeHeight).toBe(2); }); it("should calculate size correctly", async () => { const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const leaf3 = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); const leaf4 = Node.newLeafNode(stringToBytes("d"), stringToBytes("4")); const branch1 = Node.newBranchNode(leaf1, leaf2); // size = 2 const branch2 = Node.newBranchNode(leaf3, leaf4); // size = 2 const root = Node.newBranchNode(branch1, branch2); // size = 4 expect(branch1.size).toBe(2n); expect(branch2.size).toBe(2n); expect(root.size).toBe(4n); }); it("should not have value for branch nodes", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); expect(branch.value).toBeNull(); }); }); describe("isLeaf", () => { it("should return true for leaf nodes", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); expect(node.isLeaf()).toBe(true); }); it("should return false for branch nodes", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); expect(branch.isLeaf()).toBe(false); }); it("should be based on subtreeHeight", async () => { const node = new Node(); node.subtreeHeight = 0; expect(node.isLeaf()).toBe(true); node.subtreeHeight = 1; expect(node.isLeaf()).toBe(false); node.subtreeHeight = 5; expect(node.isLeaf()).toBe(false); }); }); describe("getNodeKey", () => { it("should return nodeKey bytes for v1 nodes", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = new NodeKey(100n, 5); const nodeKeyBytes = node.getNodeKey(); expect(nodeKeyBytes).toBeInstanceOf(Uint8Array); expect(nodeKeyBytes.length).toBe(12); expect(nodeKeyBytes).toEqual(node.nodeKey.toBytes()); }); it("should return hash for legacy nodes", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.isLegacy = true; node.hash = new Uint8Array(32).fill(0xAB); const nodeKey = node.getNodeKey(); expect(nodeKey).toEqual(node.hash); }); it("should throw if non-legacy node missing nodeKey", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = null; expect(() => node.getNodeKey()).toThrow("Node must have nodeKey"); }); it("should throw if legacy node missing hash", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.isLegacy = true; node.hash = null; expect(() => node.getNodeKey()).toThrow("Legacy node must have hash"); }); }); describe("getBalanceFactor", () => { it("should return 0 for balanced branch", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); expect(branch.getBalanceFactor()).toBe(0); }); it("should return positive for left-heavy branch", async () => { const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const leaf3 = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); const leftBranch = Node.newBranchNode(leaf1, leaf2); // height 1 const root = Node.newBranchNode(leftBranch, leaf3); // left height 1, right height 0 expect(root.getBalanceFactor()).toBe(1); }); it("should return negative for right-heavy branch", async () => { const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const leaf3 = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); const rightBranch = Node.newBranchNode(leaf2, leaf3); // height 1 const root = Node.newBranchNode(leaf1, rightBranch); // left height 0, right height 1 expect(root.getBalanceFactor()).toBe(-1); }); }); }); describe("Node - Advanced Operations", () => { describe("calcHeightAndSize", () => { it("should calculate height and size for branch with two leaves", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); // Manually corrupt height and size branch.subtreeHeight = 999; branch.size = 999n; const getter = await createTestNodeDB(); await branch.calcHeightAndSize(getter); expect(branch.subtreeHeight).toBe(1); // max(0, 0) + 1 expect(branch.size).toBe(2n); // 1 + 1 }); it("should calculate height and size for unbalanced tree", async () => { const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const leaf3 = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); const leftBranch = Node.newBranchNode(leaf1, leaf2); // height 1, size 2 const root = Node.newBranchNode(leftBranch, leaf3); // Manually corrupt root's height and size root.subtreeHeight = 999; root.size = 999n; const getter = await createTestNodeDB(); await root.calcHeightAndSize(getter); expect(root.subtreeHeight).toBe(2); // max(1, 0) + 1 expect(root.size).toBe(3n); // 2 + 1 }); it("should load children from storage if needed", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); left.nodeKey = new NodeKey(1n, 2); right.nodeKey = new NodeKey(1n, 3); const branch = new Node(); branch.key = left.key; branch.subtreeHeight = 999; branch.size = 999n; branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = left.nodeKey.toBytes(); branch.rightNodeKey = right.nodeKey.toBytes(); // Don't set leftNode/rightNode - they need to be loaded const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, left, right); await branch.calcHeightAndSize(getter); expect(branch.subtreeHeight).toBe(1); // max(0, 0) + 1 expect(branch.size).toBe(2n); // 1 + 1 }); it("should handle node with no children", async () => { const node = new Node(); node.key = stringToBytes("key"); node.subtreeHeight = 999; node.size = 999n; node.leftNode = null; node.rightNode = null; const getter = await createTestNodeDB(); await node.calcHeightAndSize(getter); expect(node.subtreeHeight).toBe(0); // max(-1, -1) + 1 expect(node.size).toBe(0n); // 0 + 0 }); }); describe("validate", () => { it("should validate correct leaf node", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = new NodeKey(1n, 1); const error = node.validate(); expect(error).toBeNull(); }); it("should validate correct branch node", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); left.nodeKey = new NodeKey(1n, 2); right.nodeKey = new NodeKey(1n, 3); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); const error = branch.validate(); expect(error).toBeNull(); }); it("should reject node with null key", async () => { const node = new Node(); node.key = null; const error = node.validate(); expect(error).toBe("key cannot be null"); }); it("should reject non-legacy node with null nodeKey", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = null; node.isLegacy = false; const error = node.validate(); expect(error).toBe("nodeKey cannot be null for non-legacy nodes"); }); it("should reject node with invalid version", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = new NodeKey(0n, 1); const error = node.validate(); expect(error).toBe("version must be greater than 0"); }); it("should reject node with negative height", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = new NodeKey(1n, 1); node.subtreeHeight = -1; const error = node.validate(); expect(error).toBe("height cannot be less than 0"); }); it("should reject leaf node without value", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = new NodeKey(1n, 1); node.value = null; const error = node.validate(); expect(error).toBe("leaf node must have a value"); }); it("should reject leaf node with size != 1", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = new NodeKey(1n, 1); node.size = 2n; const error = node.validate(); expect(error).toBe("leaf node must have size = 1"); }); it("should reject branch node with value", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.value = stringToBytes("invalid"); const error = branch.validate(); expect(error).toBe("branch node cannot have a value"); }); it("should reject branch node without children", async () => { const node = new Node(); node.key = stringToBytes("key"); node.nodeKey = new NodeKey(1n, 1); node.subtreeHeight = 1; node.size = 2n; const error = node.validate(); expect(error).toBe("branch node must have left child reference"); }); }); describe("toString", () => { it("should return string representation for leaf", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = new NodeKey(1n, 1); const str = node.toString(); expect(str).toContain("LEAF"); expect(str).toContain("h=0"); expect(str).toContain("s=1"); expect(str).toContain("v1n1"); }); it("should return string representation for branch", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); const str = branch.toString(); expect(str).toContain("BRANCH"); expect(str).toContain("h=1"); expect(str).toContain("s=2"); expect(str).toContain("v1n1"); }); }); describe("getLeftNode / getRightNode", () => { it("should return cached left node", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); const branch = Node.newBranchNode(left, right); const getter = await createTestNodeDB(); const result = await branch.getLeftNode(getter); expect(result).toBe(left); }); it("should load left node from storage", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); left.nodeKey = new NodeKey(1n, 2); const branch = new Node(); branch.subtreeHeight = 1; branch.leftNodeKey = left.nodeKey.toBytes(); branch.leftNode = null; // Not cached const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, left); const result = await branch.getLeftNode(getter); expect(result?.key).toEqual(left.key); }); it("should return null if no left child", async () => { const node = new Node(); node.leftNode = null; node.leftNodeKey = null; const getter = await createTestNodeDB(); const result = await node.getLeftNode(getter); expect(result).toBeNull(); }); }); describe("clone", () => { it("should throw error for leaf nodes", async () => { const leaf = Node.newLeafNode( stringToBytes("key"), stringToBytes("value"), ); const getter = await createTestNodeDB(); await expect(leaf.clone(getter)).rejects.toThrow("Cannot clone leaf node"); }); it("should clone branch node with cached children", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); const branch = Node.newBranchNode(left, right); branch.subtreeHeight = 1; branch.size = 2n; branch.hash = stringToBytes("hash"); branch.nodeKey = new NodeKey(1n, 1); const getter = await createTestNodeDB(); const cloned = await branch.clone(getter); // Different instances expect(cloned).not.toBe(branch); // Same structure expect(cloned.key).toBe(branch.key); // Same reference expect(cloned.subtreeHeight).toBe(branch.subtreeHeight); expect(cloned.size).toBe(branch.size); // Same children (shared references for copy-on-write) expect(cloned.leftNode).toBe(left); expect(cloned.rightNode).toBe(right); expect(cloned.leftNodeKey).toBe(branch.leftNodeKey); expect(cloned.rightNodeKey).toBe(branch.rightNodeKey); // Cleared hash and nodeKey expect(cloned.hash).toBe(null); expect(cloned.nodeKey).toBe(null); }); it("should load children from storage if needed", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); left.nodeKey = new NodeKey(1n, 2); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); right.nodeKey = new NodeKey(1n, 3); const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = left.nodeKey.toBytes(); branch.rightNodeKey = right.nodeKey.toBytes(); // Clear cached children to simulate loading from storage branch.leftNode = null; branch.rightNode = null; const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, left, right); const cloned = await branch.clone(getter); // Should have loaded children from storage expect(cloned.leftNode).toBe(left); expect(cloned.rightNode).toBe(right); }); it("should share child references (copy-on-write)", async () => { const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); const branch = Node.newBranchNode(left, right); const getter = await createTestNodeDB(); const cloned = await branch.clone(getter); // Same references to children expect(cloned.leftNode).toBe(branch.leftNode); expect(cloned.rightNode).toBe(branch.rightNode); // Modifying child in clone affects original (shared reference) // This is intentional for copy-on-write cloned.leftNode!.value = stringToBytes("modified"); expect(branch.leftNode!.value).toEqual(stringToBytes("modified")); }); }); });