/** * Tests for debug/introspection methods * Tests the toString() and other debug methods added for API completion item #21 */ import { beforeEach, describe, expect, it, } from "vitest"; import { createTestDb, type TestDbContext, } 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 type { Options, } from "../types/options.js"; import type { Statistics, } from "../types/options.js"; describe("Debug/Introspection Methods", () => { let ctx: TestDbContext; let stats: Statistics; let opts: Options; beforeEach(async () => { ctx = await createTestDb(); stats = { cacheHitCnt: 0, cacheMissCnt: 0, fastCacheHitCnt: 0, fastCacheMissCnt: 0, }; opts = { cacheSize: 100, fastCacheSize: 100, statistics: stats, }; }); afterEach(async () => { await ctx.cleanup(); }); // Helper function to create a MutableTree function createTree(): MutableTree { const ndb = new NodeDB(ctx.db, 1000, opts); const workingTree = new ImmutableTree(null, ndb, 0n, true); // skipFastStorageUpgrade = true return new MutableTree(ndb, workingTree, true); // skipFastStorageUpgrade = true } describe("ImmutableTree.toString()", () => { it("should return empty tree representation for empty tree", async () => { const tree = createTree(); const immutable = tree.getWorkingTree(); const str = await immutable.toString(); expect(str).toBe("Tree{}"); }); it("should return single key-value pair", async () => { const tree = createTree(); await tree.set(new TextEncoder().encode("key1"), new TextEncoder().encode("value1")); await tree.saveVersion(); // Get from working tree after save (it will iterate over the saved data) const immutable = tree.getWorkingTree(); const str = await immutable.toString(); // Keys and values are hex encoded expect(str).toContain("6b657931"); // "key1" in hex expect(str).toContain("76616c756531"); // "value1" in hex expect(str).toContain("Tree{"); expect(str).toContain("}"); }); it("should return multiple key-value pairs in sorted order", async () => { const tree = createTree(); await tree.set(new TextEncoder().encode("zebra"), new TextEncoder().encode("val1")); await tree.set(new TextEncoder().encode("apple"), new TextEncoder().encode("val2")); await tree.set(new TextEncoder().encode("mango"), new TextEncoder().encode("val3")); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const str = await immutable.toString(); expect(str).toContain("Tree{"); expect(str).toContain("}"); // Should contain all keys (hex encoded) expect(str).toContain("6170706c65"); // "apple" in hex expect(str).toContain("6d616e676f"); // "mango" in hex expect(str).toContain("7a65627261"); // "zebra" in hex // Check order - apple should come before mango, mango before zebra const appleIndex = str.indexOf("6170706c65"); const mangoIndex = str.indexOf("6d616e676f"); const zebraIndex = str.indexOf("7a65627261"); expect(appleIndex).toBeLessThan(mangoIndex); expect(mangoIndex).toBeLessThan(zebraIndex); }); it("should handle binary keys and values", async () => { const tree = createTree(); const binaryKey = new Uint8Array([ 0x00, 0x01, 0xff, 0xfe, ]); const binaryValue = new Uint8Array([ 0xde, 0xad, 0xbe, 0xef, ]); await tree.set(binaryKey, binaryValue); await tree.saveVersion(); const immutable = tree.getWorkingTree(); const str = await immutable.toString(); expect(str).toContain("0001fffe"); // binary key in hex expect(str).toContain("deadbeef"); // binary value in hex }); it("should work after version is saved", async () => { const tree = createTree(); await tree.set(new TextEncoder().encode("key1"), new TextEncoder().encode("val1")); await tree.saveVersion(); // Get the saved immutable tree const immutable = tree.getWorkingTree(); const str = await immutable.toString(); expect(str).toContain("6b657931"); // "key1" in hex expect(str).toContain("76616c31"); // "val1" in hex }); }); describe("MutableTree.toString()", () => { it("should return tree state with version, size, and hash", async () => { const tree = createTree(); await tree.set(new TextEncoder().encode("key1"), new TextEncoder().encode("value1")); await tree.saveVersion(); const str = tree.toString(); expect(str).toContain("MutableTree{"); expect(str).toContain("version=1"); expect(str).toContain("size=1"); expect(str).toContain("hash="); expect(str).toContain("}"); }); it("should show null hash for empty tree", async () => { const tree = createTree(); const str = tree.toString(); expect(str).toContain("hash=null"); expect(str).toContain("size=0"); }); it("should update after mutations", async () => { const tree = createTree(); await tree.set(new TextEncoder().encode("key1"), new TextEncoder().encode("val1")); const str1 = tree.toString(); expect(str1).toContain("size=1"); await tree.set(new TextEncoder().encode("key2"), new TextEncoder().encode("val2")); const str2 = tree.toString(); expect(str2).toContain("size=2"); // Hash should be different expect(str1).not.toBe(str2); }); }); describe("NodeDB.toString()", () => { it("should return database state information", async () => { const ndb = new NodeDB(ctx.db, 1000, opts); const str = ndb.toString(); expect(str).toContain("NodeDB State"); expect(str).toContain("Storage Version:"); expect(str).toContain("Latest Version:"); expect(str).toContain("First Version:"); expect(str).toContain("Version Range:"); expect(str).toContain("Has Fast Storage:"); }); it("should show version range after saving versions", async () => { const ndb = new NodeDB(ctx.db, 1000, opts); const workingTree = new ImmutableTree(null, ndb, 0n, true); const tree = new MutableTree(ndb, workingTree, true); await tree.set(new TextEncoder().encode("key1"), new TextEncoder().encode("val1")); await tree.saveVersion(); await tree.set(new TextEncoder().encode("key2"), new TextEncoder().encode("val2")); await tree.saveVersion(); const str = ndb.toString(); expect(str).toContain("Latest Version: 2"); expect(str).toContain("First Version: 1"); expect(str).toContain("Version Range: [1, 2]"); }); it("should show cache statistics when available", async () => { const ndb = new NodeDB(ctx.db, 1000, opts); const workingTree = new ImmutableTree(null, ndb, 0n, true); const tree = new MutableTree(ndb, workingTree, true); // Perform operations to generate cache stats await tree.set(new TextEncoder().encode("key1"), new TextEncoder().encode("val1")); await tree.saveVersion(); // Load version to trigger cache operations await tree.loadVersion(1n); const str = ndb.toString(); expect(str).toContain("Cache Statistics:"); expect(str).toContain("Node Cache Hits:"); expect(str).toContain("Node Cache Misses:"); }); it("should calculate cache hit rates", async () => { const ndb = new NodeDB(ctx.db, 1000, opts); const workingTree = new ImmutableTree(null, ndb, 0n, true); const tree = new MutableTree(ndb, workingTree, true); // Generate some cache activity await tree.set(new TextEncoder().encode("key1"), new TextEncoder().encode("val1")); await tree.saveVersion(); await tree.set(new TextEncoder().encode("key2"), new TextEncoder().encode("val2")); await tree.saveVersion(); // Load versions multiple times to generate hits/misses await tree.loadVersion(1n); await tree.loadVersion(2n); await tree.loadVersion(1n); // Should have some cache hits const str = ndb.toString(); // Should show hit rate if there were accesses if (stats.cacheHitCnt + stats.cacheMissCnt > 0) { expect(str).toContain("Node Cache Hit Rate:"); expect(str).toContain("%"); } }); it("should show fast storage status", async () => { const ndb = new NodeDB(ctx.db, 1000, opts); const str = ndb.toString(); // Default should be true for new databases with version >= 1.1.0 expect(str).toContain("Has Fast Storage:"); }); }); describe("Node.toString()", () => { it("should show node details", async () => { const tree = createTree(); await tree.set(new TextEncoder().encode("testkey"), new TextEncoder().encode("testval")); const root = tree.getRoot(); expect(root).not.toBeNull(); const str = root!.toString(); expect(str).toContain("Node["); expect(str).toContain("LEAF"); expect(str).toContain("key="); expect(str).toContain("value="); expect(str).toContain("h=0"); // Leaf height is 0 expect(str).toContain("s=1"); // Size is 1 }); it("should distinguish between leaf and branch nodes", async () => { const tree = createTree(); // Create multiple nodes to get a branch await tree.set(new TextEncoder().encode("a"), new TextEncoder().encode("val1")); await tree.set(new TextEncoder().encode("z"), new TextEncoder().encode("val2")); const root = tree.getRoot(); expect(root).not.toBeNull(); const str = root!.toString(); // Root should be a branch with 2 children expect(str).toContain("Node["); expect(str).toContain("BRANCH"); expect(str).toContain("s=2"); }); }); });