import { describe, expect, it, } from "vitest"; import { createTestDb, } from "../db/test-db-factory.js"; import { DEFAULT_OPTIONS, } from "../types/options.js"; import { stringToBytes, } from "../utils/bytes.js"; import { NodeDB, } from "./node-db.js"; describe("NodeDB", () => { describe("constructor and initialization", () => { it("should create a NodeDB instance", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); expect(nodeDB).toBeInstanceOf(NodeDB); expect(nodeDB.getStorageVersion()).toBe("1.0.0"); expect(nodeDB.getFirstVersion()).toBe(0n); expect(nodeDB.getLatestVersion()).toBe(0n); }); it("should initialize with custom cache size", async () => { const ctx = await createTestDb(); const cacheSize = 5000; const nodeDB = new NodeDB(ctx.db, cacheSize, DEFAULT_OPTIONS); expect(nodeDB).toBeInstanceOf(NodeDB); }); it("should initialize storage version from database", async () => { const ctx = await createTestDb(); // Pre-populate storage version 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(); expect(nodeDB.getStorageVersion()).toBe("1.1.0"); }); it("should use default storage version if not in database", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); expect(nodeDB.getStorageVersion()).toBe("1.0.0"); }); }); describe("storage version", () => { it("should report fast storage not upgraded for default version", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); expect(nodeDB.hasUpgradedToFastStorage()).toBe(false); }); it("should report fast storage upgraded for 1.1.0", async () => { const ctx = await createTestDb(); // Pre-populate storage version 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(); expect(nodeDB.hasUpgradedToFastStorage()).toBe(true); }); it("should report fast storage upgraded for 1.1.0-123", async () => { const ctx = await createTestDb(); // Pre-populate storage version with fast cache version const versionKey = new Uint8Array([0x6D, ...stringToBytes("storage_version")]); await ctx.db.set(versionKey, stringToBytes("1.1.0-123")); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.initialize(); expect(nodeDB.hasUpgradedToFastStorage()).toBe(true); expect(nodeDB.getStorageVersion()).toBe("1.1.0-123"); }); }); describe("version accessors", () => { it("should get first version", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); expect(nodeDB.getFirstVersion()).toBe(0n); }); it("should get latest version", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); expect(nodeDB.getLatestVersion()).toBe(0n); }); }); describe("saveNode and getNode", () => { it("should save and load a leaf node", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); // Import Node and NodeKey classes const { Node, } = await import("../node.js"); const { NodeKey, } = await import("../node-key.js"); // Create a leaf node const key = stringToBytes("testkey"); const value = stringToBytes("testvalue"); const node = Node.newLeafNode(key, value); node.nodeKey = new NodeKey(1n, 1); node.computeHash(1n); // Save node await nodeDB.saveNode(node); // Load node const loadedNode = await nodeDB.getNode(node.getNodeKey()); expect(loadedNode).not.toBeNull(); expect(loadedNode?.isLeaf()).toBe(true); expect(loadedNode?.key).toEqual(key); expect(loadedNode?.value).toEqual(value); expect(loadedNode?.nodeKey?.toBytes()).toEqual(node.nodeKey.toBytes()); }); it("should save and load a branch node", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); // Import Node and NodeKey classes const { Node, } = await import("../node.js"); const { NodeKey, } = await import("../node-key.js"); // Create leaf nodes const left = Node.newLeafNode(stringToBytes("a"), stringToBytes("1")); left.nodeKey = new NodeKey(1n, 2); left.computeHash(1n); const right = Node.newLeafNode(stringToBytes("c"), stringToBytes("3")); right.nodeKey = new NodeKey(1n, 3); right.computeHash(1n); // Create branch node const branch = Node.newBranchNode(left, right); branch.nodeKey = new NodeKey(1n, 1); branch.leftNodeKey = left.nodeKey.toBytes(); branch.rightNodeKey = right.nodeKey.toBytes(); branch.computeHash(1n); // Save all nodes await nodeDB.saveNode(left); await nodeDB.saveNode(right); await nodeDB.saveNode(branch); // Load branch node const loadedBranch = await nodeDB.getNode(branch.getNodeKey()); expect(loadedBranch).not.toBeNull(); expect(loadedBranch?.isLeaf()).toBe(false); expect(loadedBranch?.size).toBe(2n); expect(loadedBranch?.subtreeHeight).toBe(1); }); it("should return null for non-existent node", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); const nodeKey = new Uint8Array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ]); const node = await nodeDB.getNode(nodeKey); expect(node).toBeNull(); }); it("should use cache on second get", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); // Import Node and NodeKey classes const { Node, } = await import("../node.js"); const { NodeKey, } = await import("../node-key.js"); // Create and save a node const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); node.nodeKey = new NodeKey(1n, 1); node.computeHash(1n); await nodeDB.saveNode(node); // First get - from database const firstGet = await nodeDB.getNode(node.getNodeKey()); expect(firstGet).not.toBeNull(); // Second get - from cache const secondGet = await nodeDB.getNode(node.getNodeKey()); expect(secondGet).not.toBeNull(); // Should be same instance (from cache) expect(secondGet).toBe(firstGet); }); it("should throw error for empty nodeKey", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await expect(nodeDB.getNode(new Uint8Array(0))).rejects.toThrow( "NodeDB.getNode() requires nodeKey", ); }); it("should throw error when saving node without nodeKey", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); // Import Node class const { Node, } = await import("../node.js"); // Create node without nodeKey const node = Node.newLeafNode(stringToBytes("key"), stringToBytes("value")); // Don't set nodeKey // Node.getNodeKey() will throw before NodeDB check await expect(nodeDB.saveNode(node)).rejects.toThrow( "Node must have nodeKey", ); }); }); describe("saveFastNode and getFastNode", () => { it("should save and load a fast node", async () => { const ctx = await createTestDb(); // Set storage version to 1.1.0 for 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(); // Import FastNode class const { FastNode, } = await import("./fast-node.js"); // Create a fast node const key = stringToBytes("testkey"); const value = stringToBytes("testvalue"); const version = 42n; const fastNode = new FastNode(key, value, version); // Save fast node await nodeDB.saveFastNode(fastNode); // Load fast node const loadedFastNode = await nodeDB.getFastNode(key); expect(loadedFastNode).not.toBeNull(); expect(loadedFastNode?.key).toEqual(key); expect(loadedFastNode?.value).toEqual(value); expect(loadedFastNode?.versionLastUpdatedAt).toBe(version); }); it("should return null for non-existent fast node", async () => { const ctx = await createTestDb(); // Set storage version to 1.1.0 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 key = stringToBytes("nonexistent"); const fastNode = await nodeDB.getFastNode(key); expect(fastNode).toBeNull(); }); it("should use cache on second get", async () => { const ctx = await createTestDb(); // Set storage version to 1.1.0 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(); // Import FastNode class const { FastNode, } = await import("./fast-node.js"); // Create and save a fast node const key = stringToBytes("key"); const value = stringToBytes("value"); const fastNode = new FastNode(key, value, 1n); await nodeDB.saveFastNode(fastNode); // First get - from database const firstGet = await nodeDB.getFastNode(key); expect(firstGet).not.toBeNull(); // Second get - from cache const secondGet = await nodeDB.getFastNode(key); expect(secondGet).not.toBeNull(); // Should be same instance (from cache) expect(secondGet).toBe(firstGet); }); it("should throw error if fast storage not enabled", async () => { const ctx = await createTestDb(); // Default storage version is 1.0.0 (no fast storage) const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); const { FastNode, } = await import("./fast-node.js"); const fastNode = new FastNode(stringToBytes("key"), stringToBytes("value"), 1n); await expect(nodeDB.saveFastNode(fastNode)).rejects.toThrow( "Fast storage not enabled", ); await expect(nodeDB.getFastNode(stringToBytes("key"))).rejects.toThrow( "Fast storage not enabled", ); }); it("should throw error for empty key", async () => { const ctx = await createTestDb(); // Set storage version to 1.1.0 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(); await expect(nodeDB.getFastNode(new Uint8Array(0))).rejects.toThrow( "NodeDB.getFastNode() requires key", ); }); }); describe("close", () => { it("should close the database", async () => { const ctx = await createTestDb(); const nodeDB = new NodeDB(ctx.db, 1000, DEFAULT_OPTIONS); await nodeDB.close(); // After close, database operations should fail await expect(ctx.db.get(stringToBytes("test"))).rejects.toThrow(); }); }); });