import { describe, expect, it, } from "vitest"; import { createTestDb, } from "./db/test-db-factory.js"; import { ImmutableTree, } from "./immutable-tree.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("ImmutableTree - Get Operations", () => { describe("get operations", () => { describe("has", () => { it("should return false for empty tree", async () => { const ndb = await createTestNodeDB(); const tree = new ImmutableTree(null, ndb, 1n); const result = await tree.has(stringToBytes("key")); expect(result).toBe(false); }); it("should return true for existing key", async () => { const ndb = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, ndb, 1n); const result = await tree.has(stringToBytes("key")); expect(result).toBe(true); }); it("should return false for non-existing key", async () => { const ndb = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, ndb, 1n); const result = await tree.has(stringToBytes("other")); expect(result).toBe(false); }); it("should find key in multi-node tree", async () => { const ndb = await createTestNodeDB(); const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); left.nodeKey = new NodeKey(1n, 2); left.computeHash(1n); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); right.nodeKey = new NodeKey(1n, 3); right.computeHash(1n); const root = Node.newBranchNode(left, right); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = left.nodeKey.toBytes(); root.rightNodeKey = right.nodeKey.toBytes(); root.computeHash(1n); await addNodesToNodeDB(ndb, left, right); const tree = new ImmutableTree(root, ndb, 1n); expect(await tree.has(stringToBytes("a"))).toBe(true); expect(await tree.has(stringToBytes("c"))).toBe(true); expect(await tree.has(stringToBytes("b"))).toBe(false); }); }); describe("get", () => { it("should return null for empty tree", async () => { const ndb = await createTestNodeDB(); const tree = new ImmutableTree(null, ndb, 1n); const result = await tree.get(stringToBytes("key")); expect(result).toBe(null); }); it("should return value for existing key", async () => { const ndb = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, ndb, 1n); const result = await tree.get(stringToBytes("key")); expect(result).toEqual(stringToBytes("value")); }); it("should return null for non-existing key", async () => { const ndb = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, ndb, 1n); const result = await tree.get(stringToBytes("other")); expect(result).toBe(null); }); it("should get values from multi-node tree", async () => { const ndb = await createTestNodeDB(); const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); left.nodeKey = new NodeKey(1n, 2); left.computeHash(1n); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); right.nodeKey = new NodeKey(1n, 3); right.computeHash(1n); const root = Node.newBranchNode(left, right); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = left.nodeKey.toBytes(); root.rightNodeKey = right.nodeKey.toBytes(); root.computeHash(1n); await addNodesToNodeDB(ndb, left, right); const tree = new ImmutableTree(root, ndb, 1n); expect(await tree.get(stringToBytes("a"))).toEqual(stringToBytes("1")); expect(await tree.get(stringToBytes("c"))).toEqual(stringToBytes("3")); expect(await tree.get(stringToBytes("b"))).toBe(null); }); it("should get value from 3-level tree", async () => { const ndb = await createTestNodeDB(); // TODO: Properly constructed 3-level trees will be tested when // MutableTree is implemented (Phase 7), which builds trees correctly // through set operations. For now, manually constructing branches // with correct BST properties is complex. // // Create simple 3-level tree: root with left=branch1(a,c) and right=leaf(e) // This satisfies BST property: keys <= "c" go left, keys > "c" go right const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); leaf1.nodeKey = new NodeKey(1n, 4); leaf1.computeHash(1n); const leaf2 = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); leaf2.nodeKey = new NodeKey(1n, 5); leaf2.computeHash(1n); const leaf3 = Node.newLeafNode(stringToBytes("e"), stringToBytes("5")); leaf3.nodeKey = new NodeKey(1n, 6); leaf3.computeHash(1n); const branch1 = Node.newBranchNode(leaf1, leaf2); branch1.nodeKey = new NodeKey(1n, 2); branch1.leftNodeKey = leaf1.nodeKey.toBytes(); branch1.rightNodeKey = leaf2.nodeKey.toBytes(); // Override key to be right child's key for proper BST property branch1.key = leaf2.key; // "c" branch1.computeHash(1n); const root = Node.newBranchNode(branch1, leaf3); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = branch1.nodeKey.toBytes(); root.rightNodeKey = leaf3.nodeKey.toBytes(); // Override key to be right child's key for proper BST property root.key = leaf3.key; // "e" root.computeHash(1n); await addNodesToNodeDB(ndb, leaf1, leaf2, leaf3, branch1); const tree = new ImmutableTree(root, ndb, 1n); expect(await tree.get(stringToBytes("a"))).toEqual(stringToBytes("1")); expect(await tree.get(stringToBytes("c"))).toEqual(stringToBytes("3")); expect(await tree.get(stringToBytes("e"))).toEqual(stringToBytes("5")); }); }); describe("getWithIndex", () => { it("should return index 0 and null for empty tree", async () => { const ndb = await createTestNodeDB(); const tree = new ImmutableTree(null, ndb, 1n); const result = await tree.getWithIndex(stringToBytes("key")); expect(result.index).toBe(0n); expect(result.value).toBe(null); }); it("should return index and value for existing key", async () => { const ndb = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, ndb, 1n); const result = await tree.getWithIndex(stringToBytes("key")); expect(result.index).toBe(0n); expect(result.value).toEqual(stringToBytes("value")); }); it("should return correct indices for multi-node tree", async () => { const ndb = await createTestNodeDB(); const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); left.nodeKey = new NodeKey(1n, 2); left.computeHash(1n); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); right.nodeKey = new NodeKey(1n, 3); right.computeHash(1n); const root = Node.newBranchNode(left, right); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = left.nodeKey.toBytes(); root.rightNodeKey = right.nodeKey.toBytes(); root.computeHash(1n); await addNodesToNodeDB(ndb, left, right); const tree = new ImmutableTree(root, ndb, 1n); const resultA = await tree.getWithIndex(stringToBytes("a")); expect(resultA.index).toBe(0n); expect(resultA.value).toEqual(stringToBytes("1")); const resultC = await tree.getWithIndex(stringToBytes("c")); expect(resultC.index).toBe(1n); expect(resultC.value).toEqual(stringToBytes("3")); }); it("should return next index for non-existing key", async () => { const ndb = await createTestNodeDB(); const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); left.nodeKey = new NodeKey(1n, 2); left.computeHash(1n); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); right.nodeKey = new NodeKey(1n, 3); right.computeHash(1n); const root = Node.newBranchNode(left, right); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = left.nodeKey.toBytes(); root.rightNodeKey = right.nodeKey.toBytes(); root.computeHash(1n); await addNodesToNodeDB(ndb, left, right); const tree = new ImmutableTree(root, ndb, 1n); // Key "b" doesn't exist, should return index where it would be const result = await tree.getWithIndex(stringToBytes("b")); expect(result.index).toBe(1n); expect(result.value).toBe(null); }); }); describe("getByIndex", () => { it("should return null for empty tree", async () => { const ndb = await createTestNodeDB(); const tree = new ImmutableTree(null, ndb, 1n); const result = await tree.getByIndex(0n); expect(result).toBe(null); }); it("should return key-value for index 0 in single-node tree", async () => { const ndb = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, ndb, 1n); const result = await tree.getByIndex(0n); expect(result).not.toBe(null); expect(result?.key).toEqual(stringToBytes("key")); expect(result?.value).toEqual(stringToBytes("value")); }); it("should return null for out of bounds index", async () => { const ndb = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, ndb, 1n); const result = await tree.getByIndex(1n); expect(result).toBe(null); }); it("should return correct key-values by index in multi-node tree", async () => { const ndb = await createTestNodeDB(); const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); left.nodeKey = new NodeKey(1n, 2); left.computeHash(1n); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); right.nodeKey = new NodeKey(1n, 3); right.computeHash(1n); const root = Node.newBranchNode(left, right); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = left.nodeKey.toBytes(); root.rightNodeKey = right.nodeKey.toBytes(); root.computeHash(1n); await addNodesToNodeDB(ndb, left, right); const tree = new ImmutableTree(root, ndb, 1n); const result0 = await tree.getByIndex(0n); expect(result0).not.toBe(null); expect(result0?.key).toEqual(stringToBytes("a")); expect(result0?.value).toEqual(stringToBytes("1")); const result1 = await tree.getByIndex(1n); expect(result1).not.toBe(null); expect(result1?.key).toEqual(stringToBytes("c")); expect(result1?.value).toEqual(stringToBytes("3")); const result2 = await tree.getByIndex(2n); expect(result2).toBe(null); }); it("should work with 3-level tree", async () => { const ndb = await createTestNodeDB(); const leaf1 = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); leaf1.nodeKey = new NodeKey(1n, 4); leaf1.computeHash(1n); const leaf2 = Node.newLeafNode(stringToBytes("b"), stringToBytes("2")); leaf2.nodeKey = new NodeKey(1n, 5); leaf2.computeHash(1n); const leaf3 = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); leaf3.nodeKey = new NodeKey(1n, 6); leaf3.computeHash(1n); const branch1 = Node.newBranchNode(leaf1, leaf2); branch1.nodeKey = new NodeKey(1n, 2); branch1.leftNodeKey = leaf1.nodeKey.toBytes(); branch1.rightNodeKey = leaf2.nodeKey.toBytes(); branch1.computeHash(1n); const root = Node.newBranchNode(branch1, leaf3); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = branch1.nodeKey.toBytes(); root.rightNodeKey = leaf3.nodeKey.toBytes(); root.computeHash(1n); await addNodesToNodeDB(ndb, leaf1, leaf2, leaf3, branch1); const tree = new ImmutableTree(root, ndb, 1n); const result0 = await tree.getByIndex(0n); expect(result0?.key).toEqual(stringToBytes("a")); expect(result0?.value).toEqual(stringToBytes("1")); const result1 = await tree.getByIndex(1n); expect(result1?.key).toEqual(stringToBytes("b")); expect(result1?.value).toEqual(stringToBytes("2")); const result2 = await tree.getByIndex(2n); expect(result2?.key).toEqual(stringToBytes("c")); expect(result2?.value).toEqual(stringToBytes("3")); }); }); describe("get operations integration", () => { it("should have consistent results across all get operations", async () => { const ndb = await createTestNodeDB(); const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); left.nodeKey = new NodeKey(1n, 2); left.computeHash(1n); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); right.nodeKey = new NodeKey(1n, 3); right.computeHash(1n); const root = Node.newBranchNode(left, right); root.nodeKey = new NodeKey(1n, 1); root.leftNodeKey = left.nodeKey.toBytes(); root.rightNodeKey = right.nodeKey.toBytes(); root.computeHash(1n); await addNodesToNodeDB(ndb, left, right); const tree = new ImmutableTree(root, ndb, 1n); // Verify consistency for key "a" const hasA = await tree.has(stringToBytes("a")); const getA = await tree.get(stringToBytes("a")); const getWithIndexA = await tree.getWithIndex(stringToBytes("a")); const getByIndex0 = await tree.getByIndex(0n); expect(hasA).toBe(true); expect(getA).toEqual(stringToBytes("1")); expect(getWithIndexA.value).toEqual(stringToBytes("1")); expect(getWithIndexA.index).toBe(0n); expect(getByIndex0?.key).toEqual(stringToBytes("a")); expect(getByIndex0?.value).toEqual(stringToBytes("1")); }); }); }); });