/** * Tests for MutableTree.getImmutable() method * Covers loading immutable snapshots of specific versions */ 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 - getImmutable()", () => { describe("basic functionality", () => { it("should load immutable tree at specific version", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create and save versions 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(); // Version 1 await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); // Version 2 // Get immutable tree at version 1 const immutableV1 = await tree.getImmutable(1n); expect(immutableV1.getVersion()).toBe(1n); expect(await immutableV1.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await immutableV1.get(new Uint8Array([2]))).toBeNull(); // Not in version 1 }); it("should load immutable tree at latest version", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); 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(); // Version 1 await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); // Version 2 // Get immutable tree at version 2 (latest) const immutableV2 = await tree.getImmutable(2n); expect(immutableV2.getVersion()).toBe(2n); expect(await immutableV2.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await immutableV2.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); }); it("should load empty tree version", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); await tree.saveVersion(); // Version 1 (empty) await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Version 2 // Get immutable tree at version 1 (empty) const immutableV1 = await tree.getImmutable(1n); expect(immutableV1.getVersion()).toBe(1n); expect(immutableV1.isEmpty()).toBe(true); expect(immutableV1.getRoot()).toBeNull(); expect(await immutableV1.get(new Uint8Array([1]))).toBeNull(); }); }); describe("isolation", () => { it("should return independent immutable tree instances", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); 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(); // Version 1 // Get immutable tree twice const immutable1 = await tree.getImmutable(1n); const immutable2 = await tree.getImmutable(1n); // Should be different instances expect(immutable1).not.toBe(immutable2); // But should have same data expect(immutable1.getVersion()).toBe(immutable2.getVersion()); expect(await immutable1.get(new Uint8Array([1]))).toEqual( await immutable2.get(new Uint8Array([1])), ); }); it("should not be affected by subsequent mutations", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); 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(); // Version 1 // Get immutable tree at version 1 const immutableV1 = await tree.getImmutable(1n); // Mutate working tree await tree.set(new Uint8Array([1]), new Uint8Array([111])); // Update await tree.set(new Uint8Array([2]), new Uint8Array([200])); // Add await tree.saveVersion(); // Version 2 // Immutable tree should still have old data expect(await immutableV1.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await immutableV1.get(new Uint8Array([2]))).toBeNull(); }); it("should not affect working tree", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); 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(); // Version 1 await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); // Version 2 // Get immutable tree at version 1 await tree.getImmutable(1n); // Working tree should still be at version 2 expect(tree.getVersion()).toBe(2n); expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await tree.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); }); }); describe("multiple versions", () => { it("should load different versions correctly", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); // Create multiple versions await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Version 1 await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); // Version 2 await tree.set(new Uint8Array([3]), new Uint8Array([300])); await tree.saveVersion(); // Version 3 // Load all versions const v1 = await tree.getImmutable(1n); const v2 = await tree.getImmutable(2n); const v3 = await tree.getImmutable(3n); // Verify version 1 expect(v1.getVersion()).toBe(1n); expect(await v1.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await v1.get(new Uint8Array([2]))).toBeNull(); expect(await v1.get(new Uint8Array([3]))).toBeNull(); // Verify version 2 expect(v2.getVersion()).toBe(2n); expect(await v2.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await v2.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); expect(await v2.get(new Uint8Array([3]))).toBeNull(); // Verify version 3 expect(v3.getVersion()).toBe(3n); expect(await v3.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await v3.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); expect(await v3.get(new Uint8Array([3]))).toEqual(new Uint8Array([300])); }); it("should handle updates and deletes across versions", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); // Version 1: Add key1 await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Version 2: Update key1, add key2 await tree.set(new Uint8Array([1]), new Uint8Array([111])); await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); // Version 3: Remove key1, add key3 await tree.remove(new Uint8Array([1])); await tree.set(new Uint8Array([3]), new Uint8Array([300])); await tree.saveVersion(); // Load versions const v1 = await tree.getImmutable(1n); const v2 = await tree.getImmutable(2n); const v3 = await tree.getImmutable(3n); // Verify version 1 expect(await v1.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await v1.get(new Uint8Array([2]))).toBeNull(); expect(await v1.get(new Uint8Array([3]))).toBeNull(); // Verify version 2 expect(await v2.get(new Uint8Array([1]))).toEqual(new Uint8Array([111])); // Updated expect(await v2.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); expect(await v2.get(new Uint8Array([3]))).toBeNull(); // Verify version 3 expect(await v3.get(new Uint8Array([1]))).toBeNull(); // Removed expect(await v3.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); expect(await v3.get(new Uint8Array([3]))).toEqual(new Uint8Array([300])); }); }); describe("error cases", () => { it("should error when getting non-existent version", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); 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(); // Version 1 // Try to get version 5 (doesn't exist) await expect(tree.getImmutable(5n)).rejects.toThrow(); }); it("should error when no versions exist", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); // Try to get version 1 (no versions saved yet) await expect(tree.getImmutable(1n)).rejects.toThrow(); }); }); describe("read operations", () => { it("should support all read operations on immutable tree", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); // Create tree with multiple keys 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(); const immutable = await tree.getImmutable(1n); // Test get expect(await immutable.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); // Test has expect(await immutable.has(new Uint8Array([1]))).toBe(true); expect(await immutable.has(new Uint8Array([99]))).toBe(false); // Test metadata expect(immutable.size()).toBe(3n); expect(immutable.height()).toBeGreaterThan(0); expect(immutable.hash()).not.toBeNull(); }); }); });