/** * Scenario Tests - Complex Workflows * Tests complex real-world usage patterns and edge cases */ import { describe, expect, it, } from "vitest"; import { createTestDb, } from "../db/test-db-factory.js"; import { MutableTree, } from "../mutable-tree.js"; import { NodeDB, } from "../storage/node-db.js"; import { stringToBytes, } from "../utils/bytes.js"; describe("Complex Workflows", () => { describe("Version Management", () => { it("should handle multiple version saves and loads", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Create 5 versions with distinct data const versionData: Map> = new Map(); for (let v = 1; v <= 5; v++) { const data = new Map(); for (let i = 0; i < 10; i++) { const key = `v${v}-key${i}`; const value = `v${v}-value${i}`; await tree.set(stringToBytes(key), stringToBytes(value)); data.set(key, value); } await tree.saveVersion(); versionData.set(BigInt(v), data); } // Load each version and verify its data for (let v = 1; v <= 5; v++) { const version = BigInt(v); const immutable = await tree.getImmutable(version); const expectedData = versionData.get(version)!; for (const [key, expectedValue] of expectedData.entries()) { const value = await immutable.get(stringToBytes(key)); expect(value).toEqual(stringToBytes(expectedValue)); } } }); it("should handle loading old version and continuing", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Create version 1 await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.saveVersion(); // Create version 2 await tree.set(stringToBytes("key2"), stringToBytes("value2")); await tree.saveVersion(); // Create version 3 await tree.set(stringToBytes("key3"), stringToBytes("value3")); await tree.saveVersion(); // Load version 1 await tree.loadVersion(1n); // Verify we're at version 1 expect(tree.getVersion()).toBe(1n); expect(await tree.get(stringToBytes("key1"))).toEqual(stringToBytes("value1")); expect(await tree.get(stringToBytes("key2"))).toBeNull(); expect(await tree.get(stringToBytes("key3"))).toBeNull(); // Add new data await tree.set(stringToBytes("key4"), stringToBytes("value4")); // Trying to save should fail because version 2 already exists with different hash await expect(tree.saveVersion()).rejects.toThrow(/version 2 was already saved to different hash/); }); it("should handle version deletion", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Create 5 versions for (let v = 1; v <= 5; v++) { await tree.set(stringToBytes(`key${v}`), stringToBytes(`value${v}`)); await tree.saveVersion(); } // Delete version 2 await tree.deleteVersion(2n); // Version 2 should no longer exist await expect(tree.getImmutable(2n)).rejects.toThrow(/version 2 does not exist/); // Other versions should still exist const v1 = await tree.getImmutable(1n); expect(await v1.get(stringToBytes("key1"))).toEqual(stringToBytes("value1")); const v3 = await tree.getImmutable(3n); expect(await v3.get(stringToBytes("key1"))).toEqual(stringToBytes("value1")); expect(await v3.get(stringToBytes("key2"))).toEqual(stringToBytes("value2")); expect(await v3.get(stringToBytes("key3"))).toEqual(stringToBytes("value3")); }); it("should handle deleteVersionsTo", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Create 10 versions for (let v = 1; v <= 10; v++) { await tree.set(stringToBytes(`key${v}`), stringToBytes(`value${v}`)); await tree.saveVersion(); } // Delete versions up to and including 7 await tree.deleteVersionsTo(7n); // Versions 1-7 should not exist for (let v = 1; v <= 7; v++) { await expect(tree.getImmutable(BigInt(v))).rejects.toThrow(); } // Versions 8-10 should still exist await tree.getImmutable(8n); await tree.getImmutable(9n); await tree.getImmutable(10n); }); }); describe("Concurrent Reads", () => { it("should handle multiple immutable tree reads concurrently", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Create version with data for (let i = 0; i < 100; i++) { await tree.set(stringToBytes(`key${i}`), stringToBytes(`value${i}`)); } await tree.saveVersion(); // Get multiple immutable trees const immutable1 = await tree.getImmutable(1n); const immutable2 = await tree.getImmutable(1n); const immutable3 = await tree.getImmutable(1n); // Read from all of them concurrently const reads = await Promise.all([ immutable1.get(stringToBytes("key0")), immutable2.get(stringToBytes("key50")), immutable3.get(stringToBytes("key99")), immutable1.get(stringToBytes("key25")), immutable2.get(stringToBytes("key75")), immutable3.get(stringToBytes("key10")), ]); expect(reads[0]).toEqual(stringToBytes("value0")); expect(reads[1]).toEqual(stringToBytes("value50")); expect(reads[2]).toEqual(stringToBytes("value99")); expect(reads[3]).toEqual(stringToBytes("value25")); expect(reads[4]).toEqual(stringToBytes("value75")); expect(reads[5]).toEqual(stringToBytes("value10")); }); it("should handle reads while mutations happen", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Create version 1 for (let i = 0; i < 50; i++) { await tree.set(stringToBytes(`key${i}`), stringToBytes(`v1-value${i}`)); } await tree.saveVersion(); // Get immutable tree for version 1 const immutable1 = await tree.getImmutable(1n); // Mutate tree (but don't save yet) for (let i = 0; i < 50; i++) { await tree.set(stringToBytes(`key${i}`), stringToBytes(`v2-value${i}`)); } // Reads from immutable1 should still return v1 data expect(await immutable1.get(stringToBytes("key0"))).toEqual(stringToBytes("v1-value0")); expect(await immutable1.get(stringToBytes("key25"))).toEqual(stringToBytes("v1-value25")); // Save version 2 await tree.saveVersion(); // Reads from immutable1 should STILL return v1 data expect(await immutable1.get(stringToBytes("key0"))).toEqual(stringToBytes("v1-value0")); expect(await immutable1.get(stringToBytes("key25"))).toEqual(stringToBytes("v1-value25")); // Get immutable2 and verify it has v2 data const immutable2 = await tree.getImmutable(2n); expect(await immutable2.get(stringToBytes("key0"))).toEqual(stringToBytes("v2-value0")); expect(await immutable2.get(stringToBytes("key25"))).toEqual(stringToBytes("v2-value25")); }); }); describe("Rollback", () => { it("should handle rollback after mutations", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Save version 1 await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.saveVersion(); // Make unsaved changes await tree.set(stringToBytes("key2"), stringToBytes("value2")); await tree.set(stringToBytes("key3"), stringToBytes("value3")); // Rollback tree.rollback(); // Unsaved changes should be gone expect(await tree.get(stringToBytes("key2"))).toBeNull(); expect(await tree.get(stringToBytes("key3"))).toBeNull(); expect(await tree.get(stringToBytes("key1"))).toEqual(stringToBytes("value1")); }); it("should handle multiple rollbacks", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Save version 1 await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.saveVersion(); // Changes and rollback #1 await tree.set(stringToBytes("key2"), stringToBytes("value2")); tree.rollback(); expect(await tree.get(stringToBytes("key2"))).toBeNull(); // Changes and rollback #2 await tree.set(stringToBytes("key3"), stringToBytes("value3")); tree.rollback(); expect(await tree.get(stringToBytes("key3"))).toBeNull(); // Original data should still be there expect(await tree.get(stringToBytes("key1"))).toEqual(stringToBytes("value1")); // Should be able to save after rollbacks await tree.set(stringToBytes("key4"), stringToBytes("value4")); await tree.saveVersion(); expect(await tree.get(stringToBytes("key4"))).toEqual(stringToBytes("value4")); }); }); describe("Error Recovery", () => { it("should handle remove on non-existent key", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.saveVersion(); // Remove non-existent key should return object with removed=false const result = await tree.remove(stringToBytes("key2")); expect(result.removed).toBe(false); expect(result.value).toBeNull(); // Tree should be unchanged expect(await tree.get(stringToBytes("key1"))).toEqual(stringToBytes("value1")); }); it("should handle get on non-existent version", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.saveVersion(); // Try to get immutable tree for non-existent version await expect(tree.getImmutable(999n)).rejects.toThrow(/version 999 does not exist/); }); it("should handle empty tree operations", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Operations on empty tree expect(await tree.get(stringToBytes("key1"))).toBeNull(); const removeResult = await tree.remove(stringToBytes("key1")); expect(removeResult.removed).toBe(false); expect(removeResult.value).toBeNull(); expect(tree.getRoot()).toBeNull(); // Save empty tree const { hash, } = await tree.saveVersion(); expect(hash).toBeNull(); // Empty tree has null hash // Load empty tree const immutable = await tree.getImmutable(1n); expect(immutable.getRoot()).toBeNull(); expect(immutable.size()).toBe(0n); }); }); describe("Complex Data Patterns", () => { it("should handle alternating inserts and deletes", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Alternate between insert and delete for (let i = 0; i < 100; i++) { if (i % 2 === 0) { await tree.set(stringToBytes(`key${i}`), stringToBytes(`value${i}`)); } else { // Delete the previous key await tree.remove(stringToBytes(`key${i - 1}`)); } } await tree.saveVersion(); // Tree should be empty (all inserts were deleted) expect(tree.getRoot()).toBeNull(); }); it("should handle duplicate key updates", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Update same key multiple times await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.set(stringToBytes("key1"), stringToBytes("value2")); await tree.set(stringToBytes("key1"), stringToBytes("value3")); await tree.saveVersion(); // Should have the last value expect(await tree.get(stringToBytes("key1"))).toEqual(stringToBytes("value3")); // Tree should have size 1 expect(tree.getRoot()?.size).toBe(1n); }); it("should handle sparse version history", async () => { const ctx = await createTestDb(); const ndb = new NodeDB(ctx.db, 10000, { }); await ndb.initialize(); const workingTree = await ndb.getImmutableTree(0n); const tree = new MutableTree(ndb, workingTree, true); // Create versions 1, 5, 10 (sparse) await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.saveVersion(); // v1 // Manually set version to create gaps (this tests the system's ability to handle sparse versions) await tree.set(stringToBytes("key5"), stringToBytes("value5")); await tree.saveVersion(); // v2 await tree.set(stringToBytes("key10"), stringToBytes("value10")); await tree.saveVersion(); // v3 // All versions should be accessible const v1 = await tree.getImmutable(1n); expect(await v1.get(stringToBytes("key1"))).toEqual(stringToBytes("value1")); const v2 = await tree.getImmutable(2n); expect(await v2.get(stringToBytes("key5"))).toEqual(stringToBytes("value5")); const v3 = await tree.getImmutable(3n); expect(await v3.get(stringToBytes("key10"))).toEqual(stringToBytes("value10")); }); }); });