import { describe, expect, it, } from "vitest"; import { createTestDb, } from "./db/test-db-factory.js"; import { ImmutableTree, } from "./immutable-tree.js"; import { MutableTree, } from "./mutable-tree.js"; import { NodeDB, } from "./storage/node-db.js"; import { DEFAULT_OPTIONS, } from "./types/options.js"; describe("MutableTree - Basic", () => { describe("constructor and basic structure", () => { it("should create a MutableTree instance", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree).toBeInstanceOf(MutableTree); expect(mutableTree.getVersion()).toBe(0n); expect(mutableTree.getWorkingVersion()).toBe(1n); expect(mutableTree.getSize()).toBe(0n); }); it("should create with skipFastStorageUpgrade flag", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n, true); const mutableTree = new MutableTree(nodeDB, workingTree, true); expect(mutableTree).toBeInstanceOf(MutableTree); }); it("should clone working tree as lastSaved", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); // LastSaved should have same version as working tree initially expect(mutableTree.getHash()).toBe(null); // Both empty, so null hash }); }); describe("isWorkingTreeEmpty", () => { it("should return true for empty tree", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree.isWorkingTreeEmpty()).toBe(true); }); it("should return false for non-empty tree", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); // Create a tree with a root node const { Node, } = await import("./node.js"); const { NodeKey, } = await import("./node-key.js"); const key = new TextEncoder().encode("testkey"); const value = new TextEncoder().encode("testvalue"); const node = Node.newLeafNode(key, value); node.nodeKey = new NodeKey(1n, 1); node.computeHash(1n); const workingTree = new ImmutableTree(node, nodeDB, 1n, false); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree.isWorkingTreeEmpty()).toBe(false); expect(mutableTree.getSize()).toBe(1n); }); }); describe("version accessors", () => { it("should get version from working tree", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 5n); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree.getVersion()).toBe(5n); }); it("should calculate working version as version + 1", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 5n); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree.getWorkingVersion()).toBe(6n); }); }); describe("hash accessors", () => { it("should return null hash for empty lastSaved", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree.getHash()).toBe(null); }); it("should return null working hash for empty tree", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree.getWorkingHash()).toBe(null); }); it("should compute working hash for non-empty tree", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); // Create a tree with a root node const { Node, } = await import("./node.js"); const { NodeKey, } = await import("./node-key.js"); const key = new TextEncoder().encode("testkey"); const value = new TextEncoder().encode("testvalue"); const node = Node.newLeafNode(key, value); node.nodeKey = new NodeKey(1n, 1); node.computeHash(1n); const workingTree = new ImmutableTree(node, nodeDB, 1n, false); const mutableTree = new MutableTree(nodeDB, workingTree); const hash = mutableTree.getWorkingHash(); expect(hash).not.toBe(null); expect(hash).toBeInstanceOf(Uint8Array); expect(hash!.length).toBe(32); // SHA256 hash }); it("should compute working hash with unsaved changes from set operations", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); // Set some keys without saving await mutableTree.set(new TextEncoder().encode("key1"), new TextEncoder().encode("value1")); await mutableTree.set(new TextEncoder().encode("key2"), new TextEncoder().encode("value2")); await mutableTree.set(new TextEncoder().encode("key3"), new TextEncoder().encode("value3")); // getWorkingHash should work with unsaved nodes (nodes without nodeKey) const hash1 = mutableTree.getWorkingHash(); expect(hash1).not.toBe(null); expect(hash1).toBeInstanceOf(Uint8Array); expect(hash1!.length).toBe(32); // SHA256 hash // Add another key and verify hash changes await mutableTree.set(new TextEncoder().encode("key4"), new TextEncoder().encode("value4")); const hash2 = mutableTree.getWorkingHash(); expect(hash2).not.toBe(null); expect(hash2!.length).toBe(32); // Hashes should be different expect(Buffer.from(hash1!).equals(Buffer.from(hash2!))).toBe(false); // Verify hash is consistent on multiple calls without changes const hash3 = mutableTree.getWorkingHash(); expect(Buffer.from(hash2!).equals(Buffer.from(hash3!))).toBe(true); }); }); describe("tree accessors", () => { it("should get working tree", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 5n); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree.getWorkingTree()).toBe(workingTree); }); it("should get root from working tree", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree.getRoot()).toBe(null); }); it("should get size from working tree", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); expect(mutableTree.getSize()).toBe(0n); }); }); });