/** * Tests for MutableTree.saveVersion() method * Covers basic version saving, node persistence, and idempotency */ import { describe, expect, it, } from "vitest"; import { MemDB, } from "./db/mem-db.js"; 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 - saveVersion()", () => { describe("basic version saving", () => { it("should save first version", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.set(new Uint8Array([2]), new Uint8Array([200])); const result = await tree.saveVersion(); expect(result.version).toBe(1n); // First save is version 1 (Go default) expect(result.hash).not.toBeNull(); expect(result.hash?.length).toBe(32); }); it("should save multiple versions incrementally", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); // Version 1 (first save) await tree.set(new Uint8Array([1]), new Uint8Array([100])); const v1 = await tree.saveVersion(); expect(v1.version).toBe(1n); // Version 2 await tree.set(new Uint8Array([2]), new Uint8Array([200])); const v2 = await tree.saveVersion(); expect(v2.version).toBe(2n); // Version 3 await tree.set(new Uint8Array([3]), new Uint8Array([300])); const v3 = await tree.saveVersion(); expect(v3.version).toBe(3n); // Hashes should differ expect(Buffer.from(v1.hash!).equals(Buffer.from(v2.hash!))).toBe(false); expect(Buffer.from(v2.hash!).equals(Buffer.from(v3.hash!))).toBe(false); }); it("should save empty root", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); const result = await tree.saveVersion(); expect(result.version).toBe(1n); // First save is version 1 expect(result.hash).toBeNull(); }); it("should persist nodes to storage", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.set(new Uint8Array([3]), new Uint8Array([300])); await tree.saveVersion(); // Verify root is saved (version 1) const rootKey = await ndb.getRoot(1n); expect(rootKey).not.toBeNull(); expect(rootKey?.length).toBeGreaterThan(0); // Verify we can load the root node const rootNode = await ndb.getNode(rootKey!); expect(rootNode).not.toBeNull(); expect(rootNode!.key).not.toBeNull(); }); }); describe("idempotency", () => { it("should allow saving same version twice with same hash", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new Uint8Array([1]), new Uint8Array([100])); const v1 = await tree.saveVersion(); const v2 = await tree.saveVersion(); expect(v1.version).toBe(v2.version); expect(Buffer.from(v1.hash!).equals(Buffer.from(v2.hash!))).toBe(true); }); it("should error when saving same version with different hash", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // First tree - save version 1 const workingTree1 = new ImmutableTree(null, ndb, 0n); const tree1 = new MutableTree(ndb, workingTree1, true); await tree1.set(new Uint8Array([1]), new Uint8Array([100])); await tree1.saveVersion(); // Second tree - try to save different content to version 1 const workingTree2 = new ImmutableTree(null, ndb, 0n); const tree2 = new MutableTree(ndb, workingTree2, true); await tree2.set(new Uint8Array([2]), new Uint8Array([200])); await expect(tree2.saveVersion()).rejects.toThrow( /version 1 was already saved to different hash/, ); }); it("should allow idempotent save of empty root", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); const v1 = await tree.saveVersion(); const v2 = await tree.saveVersion(); expect(v1.version).toBe(v2.version); expect(v1.hash).toBeNull(); expect(v2.hash).toBeNull(); }); }); describe("node key assignment", () => { it("should assign node keys to all nodes", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.set(new Uint8Array([3]), new Uint8Array([300])); await tree.saveVersion(); // Get root and verify it has a node key const root = tree.getRoot(); expect(root).not.toBeNull(); expect(root!.nodeKey).not.toBeNull(); expect(root!.nodeKey!.version).toBe(1n); // Version 1 expect(root!.nodeKey!.nonce).toBeGreaterThan(0); }); it("should assign unique nonces to nodes", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); // Create a tree with multiple nodes await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.set(new Uint8Array([3]), new Uint8Array([300])); await tree.set(new Uint8Array([4]), new Uint8Array([400])); await tree.saveVersion(); // After save, child references are cleared (memory optimization) // We need to load them from storage to check nonces const root = tree.getRoot(); expect(root).not.toBeNull(); expect(root!.nodeKey).not.toBeNull(); // Collect all nonces by loading nodes from storage const nonces = new Set(); nonces.add(root!.nodeKey!.nonce); // Load left and right children if they exist if (root!.leftNodeKey) { const leftNode = await ndb.getNode(root!.leftNodeKey); if (leftNode && leftNode.nodeKey) { nonces.add(leftNode.nodeKey.nonce); } } if (root!.rightNodeKey) { const rightNode = await ndb.getNode(root!.rightNodeKey); if (rightNode && rightNode.nodeKey) { nonces.add(rightNode.nodeKey.nonce); } } // All nonces should be unique (we have at least 3 nodes: root + 2 children) expect(nonces.size).toBeGreaterThan(1); }); }); describe("version tracking", () => { it("should update latest version after save", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); expect(ndb.getLatestVersion()).toBe(0n); // Initial state, no versions saved // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); expect(ndb.getLatestVersion()).toBe(1n); // Version 1 was saved await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); expect(ndb.getLatestVersion()).toBe(2n); // Version 2 was saved }); it("should track version existence", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); expect(await ndb.versionExists(1n)).toBe(false); await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); expect(await ndb.versionExists(1n)).toBe(true); // Version 1 was saved expect(await ndb.versionExists(2n)).toBe(false); }); }); describe("mutations after save", () => { it("should allow mutations after saveVersion", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Should be able to mutate after save await tree.set(new Uint8Array([2]), new Uint8Array([200])); const value = await tree.get(new Uint8Array([2])); expect(value).toEqual(new Uint8Array([200])); }); it("should not affect previous version after new mutations", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Mutate tree await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); // Version 1 should still have its original root const v1Root = await ndb.getRoot(1n); const v1Node = await ndb.getNode(v1Root!); expect(v1Node).not.toBeNull(); }); }); describe("hash computation", () => { it("should compute hash for all nodes", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.set(new Uint8Array([2]), new Uint8Array([200])); const result = await tree.saveVersion(); expect(result.hash).not.toBeNull(); expect(result.hash!.length).toBe(32); // Verify root has hash const root = tree.getRoot(); expect(root!.hash).not.toBeNull(); expect(root!.hash!.length).toBe(32); }); it("should produce deterministic hashes", async () => { const db1 = new MemDB(); const ndb1 = new NodeDB(db1, 1000, DEFAULT_OPTIONS); await ndb1.initialize(); const workingTree1 = await ndb1.getImmutableTree(0n); const tree1 = new MutableTree(ndb1, workingTree1, true); await tree1.set(new Uint8Array([1]), new Uint8Array([100])); await tree1.set(new Uint8Array([2]), new Uint8Array([200])); const hash1 = (await tree1.saveVersion()).hash; const db2 = new MemDB(); const ndb2 = new NodeDB(db2, 1000, DEFAULT_OPTIONS); await ndb2.initialize(); const workingTree2 = await ndb2.getImmutableTree(0n); const tree2 = new MutableTree(ndb2, workingTree2, true); await tree2.set(new Uint8Array([1]), new Uint8Array([100])); await tree2.set(new Uint8Array([2]), new Uint8Array([200])); const hash2 = (await tree2.saveVersion()).hash; expect(Buffer.from(hash1!).equals(Buffer.from(hash2!))).toBe(true); }); it("should produce different hashes for different data", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create fresh tree with version 0 (matches Go's zero value for int64) const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new Uint8Array([1]), new Uint8Array([100])); const hash1 = (await tree.saveVersion()).hash; await tree.set(new Uint8Array([2]), new Uint8Array([200])); const hash2 = (await tree.saveVersion()).hash; expect(Buffer.from(hash1!).equals(Buffer.from(hash2!))).toBe(false); }); }); });