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 { Node, } from "./node.js"; import { NodeDB, } from "./storage/node-db.js"; import { DEFAULT_OPTIONS, } from "./types/options.js"; describe("MutableTree - Balance Operations", () => { describe("Balance Operations", () => { it("should handle left-left case (single right rotation)", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); // Access private balance method type MutableTreePrivate = { balance(node: Node): Promise }; const privateTree = mutableTree as unknown as MutableTreePrivate; // Manually create unbalanced left-left tree with balance > 1: // c // / // b // / // a // This has balance = 2 (left height 1, right height -1) const leafA = Node.newLeafNode(Buffer.from("a"), Buffer.from("A")); const leafB = Node.newLeafNode(Buffer.from("b"), Buffer.from("B")); const branchBWithA = new Node(); branchBWithA.key = Buffer.from("b"); branchBWithA.subtreeHeight = 1; branchBWithA.size = 2n; branchBWithA.leftNode = leafA; branchBWithA.rightNode = leafB; branchBWithA.nodeKey = null; const rootNode = new Node(); rootNode.key = Buffer.from("c"); rootNode.subtreeHeight = 2; rootNode.size = 2n; rootNode.leftNode = branchBWithA; rootNode.rightNode = null; rootNode.nodeKey = null; const balanced = await privateTree.balance(rootNode); // After balancing with left-left case (right rotation): // Should have balance factor within -1 to 1 expect(balanced.getBalanceFactor()).toBeLessThanOrEqual(1); expect(balanced.getBalanceFactor()).toBeGreaterThanOrEqual(-1); // Verify tree has correct size (2 leaves: a and b) expect(balanced.size).toBe(2n); }); it("should handle right-right case (single left rotation)", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); // Access private balance method type MutableTreePrivate = { balance(node: Node): Promise }; const privateTree = mutableTree as unknown as MutableTreePrivate; // Manually create unbalanced right-right tree with balance < -1: // a // \ // b // \ // c // This has balance = -2 (left height -1, right height 1) const leafB = Node.newLeafNode(Buffer.from("b"), Buffer.from("B")); const leafC = Node.newLeafNode(Buffer.from("c"), Buffer.from("C")); const branchBWithC = new Node(); branchBWithC.key = Buffer.from("c"); branchBWithC.subtreeHeight = 1; branchBWithC.size = 2n; branchBWithC.leftNode = leafB; branchBWithC.rightNode = leafC; branchBWithC.nodeKey = null; const rootNode = new Node(); rootNode.key = Buffer.from("c"); rootNode.subtreeHeight = 2; rootNode.size = 2n; rootNode.leftNode = null; rootNode.rightNode = branchBWithC; rootNode.nodeKey = null; const balanced = await privateTree.balance(rootNode); // After balancing with right-right case (left rotation): // Should have balance factor within -1 to 1 expect(balanced.getBalanceFactor()).toBeLessThanOrEqual(1); expect(balanced.getBalanceFactor()).toBeGreaterThanOrEqual(-1); // Verify tree has correct size (2 leaves) expect(balanced.size).toBe(2n); }); it("should handle left-right case (double rotation)", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); // Access private balance method type MutableTreePrivate = { balance(node: Node): Promise }; const privateTree = mutableTree as unknown as MutableTreePrivate; // Manually create unbalanced left-right tree: // d // / // b // \ // c // Left child has negative balance, requiring double rotation const leafB = Node.newLeafNode(Buffer.from("b"), Buffer.from("B")); const leafC = Node.newLeafNode(Buffer.from("c"), Buffer.from("C")); const branchBWithC = new Node(); branchBWithC.key = Buffer.from("c"); branchBWithC.subtreeHeight = 1; branchBWithC.size = 2n; branchBWithC.leftNode = leafB; branchBWithC.rightNode = leafC; branchBWithC.nodeKey = null; const rootNode = new Node(); rootNode.key = Buffer.from("d"); rootNode.subtreeHeight = 2; rootNode.size = 2n; rootNode.leftNode = branchBWithC; rootNode.rightNode = null; rootNode.nodeKey = null; const balanced = await privateTree.balance(rootNode); // After balancing with left-right case (double rotation): // Should have balance factor within -1 to 1 expect(balanced.getBalanceFactor()).toBeLessThanOrEqual(1); expect(balanced.getBalanceFactor()).toBeGreaterThanOrEqual(-1); // Verify tree has correct size expect(balanced.size).toBe(2n); }); it("should handle right-left case (double rotation)", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); // Access private balance method type MutableTreePrivate = { balance(node: Node): Promise }; const privateTree = mutableTree as unknown as MutableTreePrivate; // Manually create unbalanced right-left tree: // c (root) // \ // c (branch) // / \ // b c (leaves) // Right child has positive balance, requiring double rotation const leafB = Node.newLeafNode(Buffer.from("b"), Buffer.from("B")); const leafC = Node.newLeafNode(Buffer.from("c"), Buffer.from("C")); const branchCWithB = new Node(); branchCWithB.key = Buffer.from("c"); branchCWithB.subtreeHeight = 1; branchCWithB.size = 2n; branchCWithB.leftNode = leafB; branchCWithB.rightNode = leafC; branchCWithB.nodeKey = null; const rootNode = new Node(); rootNode.key = Buffer.from("c"); rootNode.subtreeHeight = 2; rootNode.size = 2n; rootNode.leftNode = null; rootNode.rightNode = branchCWithB; rootNode.nodeKey = null; const balanced = await privateTree.balance(rootNode); // After balancing with right-left case (double rotation): // Should have balance factor within -1 to 1 expect(balanced.getBalanceFactor()).toBeLessThanOrEqual(1); expect(balanced.getBalanceFactor()).toBeGreaterThanOrEqual(-1); // Verify tree has correct size expect(balanced.size).toBe(2n); }); it("should not modify already balanced tree", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); // Create balanced tree: // 2 // / \ // 1 3 await mutableTree.set(Buffer.from("2"), Buffer.from("two")); await mutableTree.set(Buffer.from("1"), Buffer.from("one")); await mutableTree.set(Buffer.from("3"), Buffer.from("three")); // Access private balance method type MutableTreePrivate = { balance(node: Node): Promise }; const privateTree = mutableTree as unknown as MutableTreePrivate; const root = mutableTree.getRoot(); const balanced = await privateTree.balance(root!); // Should return same node (no rotation needed) expect(balanced).toBe(root); expect(balanced.getBalanceFactor()).toBeLessThanOrEqual(1); expect(balanced.getBalanceFactor()).toBeGreaterThanOrEqual(-1); }); it("should throw error when balancing persisted node", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); // Access private balance method type MutableTreePrivate = { balance(node: Node): Promise }; const privateTree = mutableTree as unknown as MutableTreePrivate; // Create a persisted node (with nodeKey) const persistedNode = new Node(); persistedNode.key = Buffer.from("test"); persistedNode.subtreeHeight = 0; persistedNode.size = 1n; persistedNode.value = Buffer.from("value"); persistedNode.nodeKey = { version: 1n, nonce: 1, }; // Attempting to balance persisted node should throw await expect(privateTree.balance(persistedNode)).rejects.toThrow( "unexpected balance() call on persisted node", ); }); it("should maintain all values after balancing", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); const mutableTree = new MutableTree(nodeDB, workingTree); // Create unbalanced tree const keys = [ "5", "4", "3", "2", "1", ]; for (const key of keys) { await mutableTree.set(Buffer.from(key), Buffer.from(key.toUpperCase())); } // Access private balance method type MutableTreePrivate = { balance(node: Node): Promise }; const privateTree = mutableTree as unknown as MutableTreePrivate; const root = mutableTree.getRoot(); const balanced = await privateTree.balance(root!); // Verify all values are still accessible const values: string[] = []; const collectLeaves = (node: Node | null): void => { if (!node) return; collectLeaves(node.leftNode); if (node.isLeaf() && node.value) { values.push(Buffer.from(node.value).toString()); } collectLeaves(node.rightNode); }; collectLeaves(balanced); values.sort(); expect(values).toEqual([ "1", "2", "3", "4", "5", ]); }); }); });