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"; import { Exporter, } from "./exporter.js"; import type { ExportNode, } from "./types.js"; const stringToBytes = (s: string): Uint8Array => Buffer.from(s); /** * Helper to create a MutableTree for testing */ async function createTree(): Promise { const ctx = await createTestDb(); // Pre-populate storage version to enable fast storage const versionKey = new Uint8Array([0x6D, ...stringToBytes("storage_version")]); await ctx.db.set(versionKey, stringToBytes("1.1.0")); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); const workingTree = new ImmutableTree(null, nodeDB, 0n); return new MutableTree(nodeDB, workingTree); } describe("Exporter", () => { describe("Basic Export", () => { it("should export empty tree", async () => { const tree = await createTree(); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); const nodes: ExportNode[] = []; for await (const node of exporter) { nodes.push(node); } expect(nodes.length).toBe(0); }); it("should export single leaf node", async () => { const tree = await createTree(); await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); const nodes: ExportNode[] = []; for await (const node of exporter) { nodes.push(node); } expect(nodes.length).toBe(1); expect(nodes[0].key).toEqual(stringToBytes("key1")); expect(nodes[0].value).toEqual(stringToBytes("value1")); expect(nodes[0].version).toBe(1n); expect(nodes[0].height).toBe(0); // leaf node }); it("should export multiple nodes", async () => { const tree = await createTree(); await tree.set(stringToBytes("a"), stringToBytes("1")); await tree.set(stringToBytes("b"), stringToBytes("2")); await tree.set(stringToBytes("c"), stringToBytes("3")); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); const nodes: ExportNode[] = []; for await (const node of exporter) { nodes.push(node); } // Should have 3 leaf nodes + branch nodes expect(nodes.length).toBeGreaterThanOrEqual(3); // Find leaf nodes const leaves = nodes.filter(n => n.height === 0); expect(leaves.length).toBe(3); // Check keys are present const keys = leaves.map(n => new TextDecoder().decode(n.key)); expect(keys).toContain("a"); expect(keys).toContain("b"); expect(keys).toContain("c"); }); }); describe("Post-Order Traversal", () => { it("should export in post-order (LRN)", async () => { const tree = await createTree(); await tree.set(stringToBytes("a"), stringToBytes("1")); await tree.set(stringToBytes("b"), stringToBytes("2")); await tree.set(stringToBytes("c"), stringToBytes("3")); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); const nodes: ExportNode[] = []; for await (const node of exporter) { nodes.push(node); } // In post-order (LRN), children are exported before parents // So leaf nodes should come before branch nodes // Find the first branch node (height > 0) const firstBranchIdx = nodes.findIndex(n => n.height > 0); if (firstBranchIdx >= 0) { // All nodes before first branch should be leaves (height 0) for (let i = 0; i < firstBranchIdx; i++) { expect(nodes[i].height).toBe(0); } } }); it("should export children before parents", async () => { const tree = await createTree(); // Create a simple tree: (a, b) -> branch1, (branch1, c) -> root await tree.set(stringToBytes("a"), stringToBytes("1")); await tree.set(stringToBytes("b"), stringToBytes("2")); await tree.set(stringToBytes("c"), stringToBytes("3")); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); const nodes: ExportNode[] = []; const heights: number[] = []; for await (const node of exporter) { nodes.push(node); heights.push(node.height); } // In post-order, height should generally increase (children before parents) // Find max height (root) const maxHeight = Math.max(...heights); const rootIdx = heights.lastIndexOf(maxHeight); // All nodes before root should have height <= root height for (let i = 0; i < rootIdx; i++) { expect(heights[i]).toBeLessThanOrEqual(maxHeight); } }); }); describe("Export with Versions", () => { it("should export nodes with correct versions", async () => { const tree = await createTree(); // Version 1 await tree.set(stringToBytes("a"), stringToBytes("1")); await tree.saveVersion(); // Version 2 await tree.set(stringToBytes("b"), stringToBytes("2")); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); const nodes: ExportNode[] = []; for await (const node of exporter) { nodes.push(node); } // Should have nodes from both versions const versions = new Set(nodes.map(n => n.version)); expect(versions.has(1n)).toBe(true); expect(versions.has(2n)).toBe(true); }); it("should export specific version", async () => { const tree = await createTree(); // Version 1 await tree.set(stringToBytes("a"), stringToBytes("1")); await tree.saveVersion(); // Version 2 await tree.set(stringToBytes("b"), stringToBytes("2")); await tree.saveVersion(); // Export version 1 const immutable1 = await tree.getImmutable(1n); const exporter1 = immutable1.export(); const nodes1: ExportNode[] = []; for await (const node of exporter1) { nodes1.push(node); } // Should only have nodes from version 1 const leaves1 = nodes1.filter(n => n.height === 0); const keys1 = leaves1.map(n => new TextDecoder().decode(n.key)); expect(keys1).toContain("a"); expect(keys1).not.toContain("b"); }); }); describe("Export Large Tree", () => { it("should export tree with many nodes", async () => { const tree = await createTree(); // Add 100 nodes for (let i = 0; i < 100; i++) { const key = `key${i.toString().padStart(3, "0")}`; const value = `value${i}`; await tree.set(stringToBytes(key), stringToBytes(value)); } await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); const nodes: ExportNode[] = []; for await (const node of exporter) { nodes.push(node); } // Should have 100 leaf nodes const leaves = nodes.filter(n => n.height === 0); expect(leaves.length).toBe(100); // Should have branch nodes too const branches = nodes.filter(n => n.height > 0); expect(branches.length).toBeGreaterThan(0); // Total nodes should be leaves + branches expect(nodes.length).toBe(leaves.length + branches.length); }); }); describe("Next() API", () => { it("should support next() method", async () => { const tree = await createTree(); await tree.set(stringToBytes("a"), stringToBytes("1")); await tree.set(stringToBytes("b"), stringToBytes("2")); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); const nodes: ExportNode[] = []; let node = await exporter.next(); while (node !== null) { nodes.push(node); node = await exporter.next(); } expect(nodes.length).toBeGreaterThanOrEqual(2); }); it("should return null when exhausted", async () => { const tree = await createTree(); await tree.set(stringToBytes("a"), stringToBytes("1")); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); // Exhaust the exporter while ((await exporter.next()) !== null) { // continue } // Should return null const node = await exporter.next(); expect(node).toBeNull(); }); }); describe("Error Cases", () => { it("should throw error for nil tree", () => { expect(() => { // @ts-expect-error - testing invalid input new Exporter(null); }).toThrow("tree is nil"); }); }); describe("Export Order Verification", () => { it("should export all nodes including branches", async () => { const tree = await createTree(); // Create a tree with known structure await tree.set(stringToBytes("a"), stringToBytes("1")); await tree.set(stringToBytes("b"), stringToBytes("2")); await tree.set(stringToBytes("c"), stringToBytes("3")); await tree.set(stringToBytes("d"), stringToBytes("4")); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const exporter = immutable.export(); const nodes: ExportNode[] = []; for await (const node of exporter) { nodes.push(node); } // Should have at least 4 leaf nodes const leaves = nodes.filter(n => n.height === 0); expect(leaves.length).toBeGreaterThanOrEqual(4); // Should also have branch nodes (height > 0) const branches = nodes.filter(n => n.height > 0); expect(branches.length).toBeGreaterThan(0); // In post-order, leaves come before their parent branches // So the last node should be the root (highest height) const lastNode = nodes[nodes.length - 1]; const maxHeight = Math.max(...nodes.map(n => n.height)); expect(lastNode.height).toBe(maxHeight); }); }); });