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 - Traversal Operations", () => { describe("get", () => { it("should find key in leaf node", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); const getter = await createTestNodeDB(); const result = await node.get(getter, stringToBytes("key")); expect(result.value).toEqual(stringToBytes("value")); expect(result.index).toBe(0n); }); it("should return null for non-existent key in leaf", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); const getter = await createTestNodeDB(); const result = await node.get(getter, stringToBytes("other")); expect(result.value).toBeNull(); }); it("should find key in left subtree", 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 = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = left.nodeKey.toBytes(); branch.rightNodeKey = right.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, left, right); const result = await branch.get(getter, stringToBytes("a")); expect(result.value).toEqual(stringToBytes("1")); expect(result.index).toBe(0n); }); it("should find key in right subtree with correct index", 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 = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = left.nodeKey.toBytes(); branch.rightNodeKey = right.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, left, right); const result = await branch.get(getter, stringToBytes("c")); expect(result.value).toEqual(stringToBytes("3")); expect(result.index).toBe(1n); // Adjusted for left subtree }); }); describe("getByIndex", () => { it("should get leaf by index 0", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); const getter = await createTestNodeDB(); const result = await node.getByIndex(getter, 0n); expect(result.key).toEqual(stringToBytes("key")); expect(result.value).toEqual(stringToBytes("value")); }); it("should return null for out of bounds index in leaf", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); const getter = await createTestNodeDB(); const result = await node.getByIndex(getter, 1n); expect(result.key).toBeNull(); expect(result.value).toBeNull(); }); it("should get item from left subtree by index", 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 = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = left.nodeKey.toBytes(); branch.rightNodeKey = right.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, left, right); const result = await branch.getByIndex(getter, 0n); expect(result.key).toEqual(stringToBytes("a")); expect(result.value).toEqual(stringToBytes("1")); }); it("should get item from right subtree by index", 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 = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = left.nodeKey.toBytes(); branch.rightNodeKey = right.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, left, right); const result = await branch.getByIndex(getter, 1n); expect(result.key).toEqual(stringToBytes("c")); expect(result.value).toEqual(stringToBytes("3")); }); }); describe("traverse", () => { it("should traverse leaf node", async () => { const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); const getter = await createTestNodeDB(); const visited: string[] = []; await node.traverse(getter, true, (n) => { visited.push(new TextDecoder().decode(n.key!)); return false; }); expect(visited).toEqual(["key"]); }); it("should traverse tree in ascending order", async () => { // Create tree: a, b, c, d 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")); leaf1.nodeKey = new NodeKey(1n, 4); leaf2.nodeKey = new NodeKey(1n, 5); leaf3.nodeKey = new NodeKey(1n, 6); leaf4.nodeKey = new NodeKey(1n, 7); const branch1 = Node.newBranchNode(leaf1, leaf2); const branch2 = Node.newBranchNode(leaf3, leaf4); branch1.nodeKey = new NodeKey(1n, 2); branch2.nodeKey = new NodeKey(1n, 3); branch1.leftNodeKey = leaf1.nodeKey.toBytes(); branch1.rightNodeKey = leaf2.nodeKey.toBytes(); branch2.leftNodeKey = leaf3.nodeKey.toBytes(); branch2.rightNodeKey = leaf4.nodeKey.toBytes(); const root = Node.newBranchNode(branch1, branch2); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = branch1.nodeKey.toBytes(); root.rightNodeKey = branch2.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, leaf1); await addNodesToNodeDB(getter, leaf2); await addNodesToNodeDB(getter, leaf3); await addNodesToNodeDB(getter, leaf4); await addNodesToNodeDB(getter, branch1); await addNodesToNodeDB(getter, branch2); const visited: string[] = []; await root.traverse(getter, true, (n) => { if (n.isLeaf()) { visited.push(new TextDecoder().decode(n.key!)); } return false; }); expect(visited).toEqual([ "a", "b", "c", "d", ]); }); it("should traverse tree in descending order", async () => { const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); leaf1.nodeKey = new NodeKey(1n, 2); leaf2.nodeKey = new NodeKey(1n, 3); const branch = Node.newBranchNode(leaf1, leaf2); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = leaf1.nodeKey.toBytes(); branch.rightNodeKey = leaf2.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, leaf1); await addNodesToNodeDB(getter, leaf2); const visited: string[] = []; await branch.traverse(getter, false, (n) => { if (n.isLeaf()) { visited.push(new TextDecoder().decode(n.key!)); } return false; }); expect(visited).toEqual(["b", "a"]); }); it("should stop traversal when callback returns true", async () => { const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); leaf1.nodeKey = new NodeKey(1n, 2); leaf2.nodeKey = new NodeKey(1n, 3); const branch = Node.newBranchNode(leaf1, leaf2); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = leaf1.nodeKey.toBytes(); branch.rightNodeKey = leaf2.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, leaf1); await addNodesToNodeDB(getter, leaf2); const visited: string[] = []; const stopped = await branch.traverse(getter, true, (n) => { if (n.isLeaf()) { visited.push(new TextDecoder().decode(n.key!)); return true; // Stop after first } return false; }); expect(stopped).toBe(true); expect(visited).toEqual(["a"]); }); }); describe("traverseInRange", () => { it("should traverse only nodes in range (inclusive)", 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")); leaf1.nodeKey = new NodeKey(1n, 4); leaf2.nodeKey = new NodeKey(1n, 5); leaf3.nodeKey = new NodeKey(1n, 6); leaf4.nodeKey = new NodeKey(1n, 7); const branch1 = Node.newBranchNode(leaf1, leaf2); const branch2 = Node.newBranchNode(leaf3, leaf4); branch1.nodeKey = new NodeKey(1n, 2); branch2.nodeKey = new NodeKey(1n, 3); branch1.leftNodeKey = leaf1.nodeKey.toBytes(); branch1.rightNodeKey = leaf2.nodeKey.toBytes(); branch2.leftNodeKey = leaf3.nodeKey.toBytes(); branch2.rightNodeKey = leaf4.nodeKey.toBytes(); const root = Node.newBranchNode(branch1, branch2); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = branch1.nodeKey.toBytes(); root.rightNodeKey = branch2.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, leaf1); await addNodesToNodeDB(getter, leaf2); await addNodesToNodeDB(getter, leaf3); await addNodesToNodeDB(getter, leaf4); await addNodesToNodeDB(getter, branch1); await addNodesToNodeDB(getter, branch2); const visited: string[] = []; await root.traverseInRange( getter, stringToBytes("b"), stringToBytes("c"), true, true, false, // pre-order (normal traversal) (n) => { if (n.isLeaf()) { visited.push(new TextDecoder().decode(n.key!)); } return false; }, ); expect(visited).toEqual(["b", "c"]); }); it("should traverse only nodes in range (exclusive)", 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")); leaf1.nodeKey = new NodeKey(1n, 3); leaf2.nodeKey = new NodeKey(1n, 4); leaf3.nodeKey = new NodeKey(1n, 5); const branch1 = Node.newBranchNode(leaf1, leaf2); branch1.nodeKey = new NodeKey(1n, 2); branch1.leftNodeKey = leaf1.nodeKey.toBytes(); branch1.rightNodeKey = leaf2.nodeKey.toBytes(); const root = Node.newBranchNode(branch1, leaf3); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = branch1.nodeKey.toBytes(); root.rightNodeKey = leaf3.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, leaf1); await addNodesToNodeDB(getter, leaf2); await addNodesToNodeDB(getter, leaf3); await addNodesToNodeDB(getter, branch1); const visited: string[] = []; await root.traverseInRange( getter, stringToBytes("a"), stringToBytes("c"), true, false, // exclusive false, // pre-order (normal traversal) (n) => { if (n.isLeaf()) { visited.push(new TextDecoder().decode(n.key!)); } return false; }, ); expect(visited).toEqual(["b"]); // Only 'b' is in range (a, c) exclusive }); it("should traverse from start with no end", 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")); leaf1.nodeKey = new NodeKey(1n, 3); leaf2.nodeKey = new NodeKey(1n, 4); leaf3.nodeKey = new NodeKey(1n, 5); const branch1 = Node.newBranchNode(leaf1, leaf2); branch1.nodeKey = new NodeKey(1n, 2); branch1.leftNodeKey = leaf1.nodeKey.toBytes(); branch1.rightNodeKey = leaf2.nodeKey.toBytes(); const root = Node.newBranchNode(branch1, leaf3); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = branch1.nodeKey.toBytes(); root.rightNodeKey = leaf3.nodeKey.toBytes(); const getter = await createTestNodeDB(); await addNodesToNodeDB(getter, leaf1); await addNodesToNodeDB(getter, leaf2); await addNodesToNodeDB(getter, leaf3); await addNodesToNodeDB(getter, branch1); const visited: string[] = []; await root.traverseInRange( getter, stringToBytes("b"), null, // No end true, true, false, // pre-order (normal traversal) (n) => { if (n.isLeaf()) { visited.push(new TextDecoder().decode(n.key!)); } return false; }, ); expect(visited).toEqual(["b", "c"]); }); }); });