/** * Tests for MutableTree.loadVersion() method * Covers loading versions, error cases, and version validation */ 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 - loadVersion()", () => { describe("basic loading", () => { it("should load latest version when targetVersion is 0", 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 await tree.set(new Uint8Array([3]), new Uint8Array([300])); await tree.saveVersion(); // Version 3 // Load version 0 (should load latest = 3) const latestVersion = await tree.loadVersion(0n); expect(latestVersion).toBe(3n); expect(tree.getVersion()).toBe(3n); // Verify data is from version 3 expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await tree.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); expect(await tree.get(new Uint8Array([3]))).toEqual(new Uint8Array([300])); }); it("should load 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 await tree.set(new Uint8Array([3]), new Uint8Array([300])); await tree.saveVersion(); // Version 3 // Load version 2 const latestVersion = await tree.loadVersion(2n); expect(latestVersion).toBe(3n); // Still returns latest expect(tree.getVersion()).toBe(2n); // But working version is 2 // Verify data is from version 2 expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await tree.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); expect(await tree.get(new Uint8Array([3]))).toBeNull(); // Not in version 2 }); it("should load first 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 // Load version 1 const latestVersion = await tree.loadVersion(1n); expect(latestVersion).toBe(2n); // Returns latest expect(tree.getVersion()).toBe(1n); // Working version is 1 // Verify data is from version 1 expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await tree.get(new Uint8Array([2]))).toBeNull(); // Not in version 1 }); it("should load empty tree version", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create and save empty version 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 // Load version 1 (empty) const latestVersion = await tree.loadVersion(1n); expect(latestVersion).toBe(2n); expect(tree.getVersion()).toBe(1n); // Tree should be empty expect(await tree.get(new Uint8Array([1]))).toBeNull(); expect(tree.getRoot()).toBeNull(); }); }); describe("error cases", () => { it("should error when loading non-existent version", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create and save version 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 load version 5 (doesn't exist) await expect(tree.loadVersion(5n)).rejects.toThrow( /wanted to load target 5 but only found up to 1/, ); }); it("should error when loading with no versions saved", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create tree but don't save const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); // Try to load version 1 (doesn't exist) await expect(tree.loadVersion(1n)).rejects.toThrow( /no versions found while trying to load 1/, ); }); it("should return 0 when loading version 0 with no versions saved", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create tree but don't save const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); // Load version 0 (should return 0 for empty tree) const latestVersion = await tree.loadVersion(0n); expect(latestVersion).toBe(0n); expect(tree.getVersion()).toBe(0n); }); it("should return 0 when loading negative version with no versions saved", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await ndb.initialize(); // Create tree but don't save const workingTree = new ImmutableTree(null, ndb, 0n); const tree = new MutableTree(ndb, workingTree, true); // Load version -1 (negative means latest, which is 0 for empty tree) const latestVersion = await tree.loadVersion(-1n); expect(latestVersion).toBe(0n); expect(tree.getVersion()).toBe(0n); }); }); describe("mutations after load", () => { it("should allow mutations after loading 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 // Load version 1 await tree.loadVersion(1n); // Should be able to mutate await tree.set(new Uint8Array([3]), new Uint8Array([300])); expect(await tree.get(new Uint8Array([3]))).toEqual(new Uint8Array([300])); // Save new version - this will be version 2 since we loaded version 1 // In Go IAVL, after loading version 1, the next save is version 1+1 = 2 // If version 2 already exists with different data, it will error // This test expects the error because version 2 has different data await expect(tree.saveVersion()).rejects.toThrow( /version 2 was already saved to different hash/, ); }); it("should clear unsaved changes when loading 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 // Make unsaved changes await tree.set(new Uint8Array([2]), new Uint8Array([200])); expect(await tree.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); // Load version 1 (should discard unsaved changes) await tree.loadVersion(1n); // Unsaved changes should be gone expect(await tree.get(new Uint8Array([2]))).toBeNull(); expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); }); }); describe("version isolation", () => { it("should isolate loaded version from later versions", 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([1]), new Uint8Array([111])); // Update await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); // Version 2 // Load version 1 await tree.loadVersion(1n); // Should see version 1 data expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await tree.get(new Uint8Array([2]))).toBeNull(); // Load version 2 await tree.loadVersion(2n); // Should see version 2 data expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([111])); expect(await tree.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); }); }); });