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<{ nodeDB: NodeDB cleanup: () => Promise }> { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); return { nodeDB, cleanup: ctx.cleanup, }; } /** * 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 - Basic Operations", () => { describe("constructor", () => { it("should create an empty immutable tree", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 1n); expect(tree.root).toBe(null); expect(tree.version).toBe(1n); expect(tree.skipFastStorageUpgrade).toBe(false); expect(tree.logger).toBe(null); await cleanup(); }); it("should create a tree with a root node", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.root).toBe(root); expect(tree.version).toBe(1n); await cleanup(); }); it("should accept skipFastStorageUpgrade parameter", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 1n, true); expect(tree.skipFastStorageUpgrade).toBe(true); await cleanup(); }); it("should accept logger parameter", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const mockLogger = { info: () => {}, error: () => {}, warn: () => {}, debug: () => {}, }; // eslint-disable-next-line @typescript-eslint/no-explicit-any const tree = new ImmutableTree(null, nodeDB, 1n, false, mockLogger as any); expect(tree.logger).toBe(mockLogger); await cleanup(); }); it("should freeze the tree to make it immutable", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 1n); expect(Object.isFrozen(tree)).toBe(true); await cleanup(); }); }); describe("getVersion", () => { it("should return the version", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 42n); expect(tree.getVersion()).toBe(42n); await cleanup(); }); it("should return version for tree with root", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.getVersion()).toBe(1n); await cleanup(); }); }); describe("getRoot", () => { it("should return null for empty tree", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 1n); expect(tree.getRoot()).toBe(null); await cleanup(); }); it("should return the root node", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.getRoot()).toBe(root); await cleanup(); }); }); describe("isEmpty", () => { it("should return true for empty tree", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 1n); expect(tree.isEmpty()).toBe(true); await cleanup(); }); it("should return false for tree with root", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.isEmpty()).toBe(false); await cleanup(); }); }); describe("immutability", () => { it("should not allow modification of root", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, nodeDB, 1n); expect(() => { // eslint-disable-next-line @typescript-eslint/no-explicit-any (tree as any).root = null; }).toThrow(); await cleanup(); }); it("should not allow modification of version", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 1n); expect(() => { // eslint-disable-next-line @typescript-eslint/no-explicit-any (tree as any).version = 2n; }).toThrow(); await cleanup(); }); it("should not allow modification of ndb", async () => { const { nodeDB: ndb, cleanup: cleanup1, } = await createTestNodeDB(); const tree = new ImmutableTree(null, ndb, 1n); const { nodeDB: newNdb, cleanup: cleanup2, } = await createTestNodeDB(); expect(() => { // eslint-disable-next-line @typescript-eslint/no-explicit-any (tree as any).ndb = newNdb; }).toThrow(); await cleanup1(); await cleanup2(); }); it("should not allow modification of skipFastStorageUpgrade", async () => { const { nodeDB: ndb, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, ndb, 1n); expect(() => { // eslint-disable-next-line @typescript-eslint/no-explicit-any (tree as any).skipFastStorageUpgrade = true; }).toThrow(); await cleanup(); }); }); describe("complex tree structure", () => { it("should create tree with multi-level structure", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); // Create a tree: // branch(b) // / \ // leaf(a) leaf(c) 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); // Add nodes to storage await addNodesToNodeDB(nodeDB, left, right, root); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.root).toBe(root); expect(tree.root.isLeaf()).toBe(false); expect(tree.root.size).toBe(2n); expect(tree.root.subtreeHeight).toBe(1); await cleanup(); }); }); }); describe("ImmutableTree - Metadata Methods", () => { describe("metadata methods", () => { describe("size", () => { it("should return 0 for empty tree", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 1n); expect(tree.size()).toBe(0n); await cleanup(); }); it("should return 1 for single leaf node", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.size()).toBe(1n); await cleanup(); }); it("should return correct size for multi-node tree", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); // Create tree with 2 nodes 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); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.size()).toBe(2n); await cleanup(); }); it("should return size from root node", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); root.size = 42n; // Manually set size const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.size()).toBe(42n); await cleanup(); }); }); describe("height", () => { it("should return 0 for empty tree", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 1n); expect(tree.height()).toBe(0); await cleanup(); }); it("should return 0 for single leaf node", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.height()).toBe(0); await cleanup(); }); it("should return 1 for two-level tree", async () => { const { nodeDB, cleanup, } = 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); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.height()).toBe(1); await cleanup(); }); it("should return correct height for deeper tree", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); // Create 3-level tree 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); // Branch at height 1 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); // Root at height 2 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); const tree = new ImmutableTree(root, nodeDB, 1n); expect(tree.height()).toBe(2); await cleanup(); }); }); describe("hash", () => { it("should return null for empty tree", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 1n); expect(tree.hash()).toBe(null); await cleanup(); }); it("should return hash for single leaf node", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree = new ImmutableTree(root, nodeDB, 1n); const hash = tree.hash(); expect(hash).not.toBe(null); expect(hash).toBeInstanceOf(Uint8Array); expect(hash?.length).toBe(32); // SHA-256 hash await cleanup(); }); it("should compute hash if not already computed", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); // Don't compute hash beforehand const tree = new ImmutableTree(root, nodeDB, 1n); expect(root.hash).toBe(null); // Not computed yet const hash = tree.hash(); expect(hash).not.toBe(null); expect(root.hash).not.toBe(null); // Now computed await cleanup(); }); it("should return cached hash if already computed", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); root.computeHash(1n); const tree = new ImmutableTree(root, nodeDB, 1n); const hash1 = tree.hash(); const hash2 = tree.hash(); expect(hash1).toBe(hash2); // Same instance (cached) await cleanup(); }); it("should return correct hash for multi-node tree", async () => { const { nodeDB, cleanup, } = 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); const tree = new ImmutableTree(root, nodeDB, 1n); const hash = tree.hash(); expect(hash).not.toBe(null); expect(hash).toBeInstanceOf(Uint8Array); expect(hash?.length).toBe(32); await cleanup(); }); it("should use tree version for hash computation", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const root = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); root.nodeKey = new NodeKey(1n, 1); const tree1 = new ImmutableTree(root, nodeDB, 1n); const hash1 = tree1.hash(); // Reset hash to force recomputation root.hash = null; const tree2 = new ImmutableTree(root, nodeDB, 2n); const hash2 = tree2.hash(); // Hashes should be different because version is different expect(hash1).not.toEqual(hash2); await cleanup(); }); }); describe("metadata consistency", () => { it("should return consistent metadata for same tree", async () => { const { nodeDB, cleanup, } = 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); const tree = new ImmutableTree(root, nodeDB, 1n); // Multiple calls should return same values expect(tree.size()).toBe(2n); expect(tree.size()).toBe(2n); expect(tree.height()).toBe(1); expect(tree.height()).toBe(1); expect(tree.isEmpty()).toBe(false); expect(tree.isEmpty()).toBe(false); const hash1 = tree.hash(); const hash2 = tree.hash(); expect(hash1).toBe(hash2); await cleanup(); }); }); }); }); describe("ImmutableTree - Clone", () => { describe("clone", () => { it("should create a shallow clone with same root and version", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const key = stringToBytes("key"); const value = stringToBytes("value"); const node = Node.newLeafNode(key, value); node.nodeKey = new NodeKey(1n, 1); node.computeHash(1n); const tree = new ImmutableTree(node, nodeDB, 1n); const cloned = tree.clone(); expect(cloned).not.toBe(tree); // Different instances expect(cloned.getRoot()).toBe(tree.getRoot()); // Same root reference expect(cloned.getVersion()).toBe(tree.getVersion()); expect(cloned.size()).toBe(tree.size()); await cleanup(); }); it("should clone empty tree", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 0n); const cloned = tree.clone(); expect(cloned).not.toBe(tree); expect(cloned.getRoot()).toBe(null); expect(cloned.getVersion()).toBe(0n); expect(cloned.isEmpty()).toBe(true); await cleanup(); }); it("should clone with skipFastStorageUpgrade flag", async () => { const { nodeDB, cleanup, } = await createTestNodeDB(); const tree = new ImmutableTree(null, nodeDB, 0n, true); const cloned = tree.clone(); expect(cloned).not.toBe(tree); expect(cloned.skipFastStorageUpgrade).toBe(true); await cleanup(); }); }); });