/** * Tests for MutableTree.rollback() method * Covers rollback functionality to discard unsaved changes */ 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 - rollback()", () => { describe("basic functionality", () => { it("should rollback unsaved 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); // Save initial version await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Make unsaved changes await tree.set(new Uint8Array([2]), new Uint8Array([200])); expect(await tree.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); // Rollback tree.rollback(); // Unsaved changes should be gone expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await tree.get(new Uint8Array([2]))).toBeNull(); }); it("should rollback updates to existing keys", 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); // Save initial version await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Update existing key await tree.set(new Uint8Array([1]), new Uint8Array([111])); expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([111])); // Rollback tree.rollback(); // Should have original value expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); }); it("should rollback deletions", 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); // Save initial version await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Delete key await tree.remove(new Uint8Array([1])); expect(await tree.get(new Uint8Array([1]))).toBeNull(); // Rollback tree.rollback(); // Key should be restored expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); }); it("should rollback multiple operations", 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); // Save initial version await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); // Multiple unsaved operations await tree.set(new Uint8Array([1]), new Uint8Array([111])); // Update await tree.remove(new Uint8Array([2])); // Delete await tree.set(new Uint8Array([3]), new Uint8Array([300])); // Add // Verify unsaved state expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([111])); expect(await tree.get(new Uint8Array([2]))).toBeNull(); expect(await tree.get(new Uint8Array([3]))).toEqual(new Uint8Array([300])); // Rollback tree.rollback(); // All should be reverted 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(); }); }); describe("clean tree rollback", () => { it("should be no-op when no unsaved changes", 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); // Save version await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); const hash1 = tree.getHash(); // Rollback with no changes tree.rollback(); // Should be unchanged expect(tree.getHash()).toEqual(hash1); expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); }); it("should handle rollback on empty unsaved 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); // Rollback without any saves or changes tree.rollback(); // Should still be empty expect(tree.getVersion()).toBe(0n); expect(tree.getRoot()).toBeNull(); expect(tree.getSize()).toBe(0n); }); }); describe("multiple rollbacks", () => { it("should handle multiple rollbacks in sequence", 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); // Save version await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // First mutation and rollback await tree.set(new Uint8Array([2]), new Uint8Array([200])); tree.rollback(); expect(await tree.get(new Uint8Array([2]))).toBeNull(); // Second mutation and rollback await tree.set(new Uint8Array([3]), new Uint8Array([300])); tree.rollback(); expect(await tree.get(new Uint8Array([3]))).toBeNull(); // Original data should persist expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); }); it("should allow mutations after rollback", 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); // Save version await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Mutation and rollback await tree.set(new Uint8Array([2]), new Uint8Array([200])); tree.rollback(); // New mutation after rollback await tree.set(new Uint8Array([3]), new Uint8Array([300])); expect(await tree.get(new Uint8Array([3]))).toEqual(new Uint8Array([300])); // Save should work await tree.saveVersion(); expect(await tree.get(new Uint8Array([3]))).toEqual(new Uint8Array([300])); }); }); describe("version restoration", () => { it("should restore correct version after rollback", 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); // Save version 1 await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); expect(tree.getVersion()).toBe(1n); // Make changes await tree.set(new Uint8Array([2]), new Uint8Array([200])); // Rollback tree.rollback(); // Version should still be 1 expect(tree.getVersion()).toBe(1n); }); it("should restore tree size after rollback", 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); // Save version with 2 keys await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); expect(tree.getSize()).toBe(2n); // Add more keys await tree.set(new Uint8Array([3]), new Uint8Array([300])); await tree.set(new Uint8Array([4]), new Uint8Array([400])); expect(tree.getSize()).toBe(4n); // Rollback tree.rollback(); // Size should be back to 2 expect(tree.getSize()).toBe(2n); }); it("should restore tree hash after rollback", 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); // Save version await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); const savedHash = tree.getHash(); // Make changes (hash will differ) await tree.set(new Uint8Array([2]), new Uint8Array([200])); // Rollback tree.rollback(); // Hash should match saved hash expect(tree.getHash()).toEqual(savedHash); }); }); describe("interaction with save", () => { it("should not affect saved 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); // Save version 1 await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Save version 2 await tree.set(new Uint8Array([2]), new Uint8Array([200])); await tree.saveVersion(); // Make unsaved changes await tree.set(new Uint8Array([3]), new Uint8Array([300])); // Rollback tree.rollback(); // Version 2 should still be accessible const v2 = await tree.getImmutable(2n); expect(await v2.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await v2.get(new Uint8Array([2]))).toEqual(new Uint8Array([200])); }); it("should allow save after rollback", 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); // Save version 1 await tree.set(new Uint8Array([1]), new Uint8Array([100])); await tree.saveVersion(); // Unsaved changes and rollback await tree.set(new Uint8Array([2]), new Uint8Array([200])); tree.rollback(); // New changes after rollback await tree.set(new Uint8Array([3]), new Uint8Array([300])); // Save should work (version 2) const { version, } = await tree.saveVersion(); expect(version).toBe(2n); // Verify saved correctly expect(await tree.get(new Uint8Array([1]))).toEqual(new Uint8Array([100])); expect(await tree.get(new Uint8Array([2]))).toBeNull(); // Was rolled back expect(await tree.get(new Uint8Array([3]))).toEqual(new Uint8Array([300])); }); }); });