/** * Tests for BatchWithFlusher implementation. */ import { beforeEach, describe, expect, it, } from "vitest"; import { AsyncBatchWithFlusher, BatchWithFlusher, DEFAULT_FLUSH_THRESHOLD, } from "./batch-with-flusher.js"; import { MemDB, } from "./mem-db.js"; import type { Database, } from "./types.js"; /** * Helper to create test data of specified size. */ function createData(size: number): Uint8Array { return new Uint8Array(size).fill(0x42); // Fill with 'B' } /** * Helper to create a key. */ function makeKey(n: number): Uint8Array { const key = new Uint8Array(2); const view = new DataView(key.buffer); view.setUint16(0, n, false); // big-endian return key; } describe("BatchWithFlusher", () => { let db: Database; beforeEach(() => { db = new MemDB(); }); describe("constructor", () => { it("should create with default threshold", () => { const batch = new BatchWithFlusher(db); expect(batch.getByteSize()).toBe(0); }); it("should create with custom threshold", () => { const batch = new BatchWithFlusher(db, 1000); expect(batch.getByteSize()).toBe(0); }); }); describe("basic operations", () => { it("should set values", () => { const batch = new BatchWithFlusher(db); const key = new Uint8Array([1]); const value = new Uint8Array([ 2, 3, 4, ]); batch.set(key, value); expect(batch.getByteSize()).toBe(4); // 1 + 3 }); it("should delete keys", () => { const batch = new BatchWithFlusher(db); const key = new Uint8Array([ 1, 2, 3, ]); batch.delete(key); expect(batch.getByteSize()).toBe(3); // key length }); it("should write to database", async () => { const batch = new BatchWithFlusher(db); const key1 = new Uint8Array([1]); const value1 = new Uint8Array([10]); const key2 = new Uint8Array([2]); const value2 = new Uint8Array([20]); batch.set(key1, value1); batch.set(key2, value2); // Values not visible before write expect(await db.get(key1)).toBeNull(); expect(await db.get(key2)).toBeNull(); await batch.write(); // Values visible after write expect(await db.get(key1)).toEqual(value1); expect(await db.get(key2)).toEqual(value2); // Size reset after write expect(batch.getByteSize()).toBe(0); }); it("should handle mixed set and delete", async () => { // Pre-populate database const existingKey = new Uint8Array([1]); const existingValue = new Uint8Array([100]); await db.set(existingKey, existingValue); const batch = new BatchWithFlusher(db); const newKey = new Uint8Array([2]); const newValue = new Uint8Array([200]); batch.delete(existingKey); batch.set(newKey, newValue); await batch.write(); expect(await db.get(existingKey)).toBeNull(); expect(await db.get(newKey)).toEqual(newValue); }); }); describe("auto-flush behavior", () => { it("should throw on sync flush requirement", () => { // Use small threshold to trigger flush const batch = new BatchWithFlusher(db, 10); const key = new Uint8Array([1]); const largeValue = createData(20); // Exceeds threshold // Should throw because sync flush is needed expect(() => batch.set(key, largeValue)).toThrow("Sync flush required"); }); }); describe("close behavior", () => { it("should close batch", () => { const batch = new BatchWithFlusher(db); batch.close(); expect(() => batch.set(new Uint8Array([1]), new Uint8Array([2]))).toThrow("BatchWithFlusher is closed"); expect(() => batch.delete(new Uint8Array([1]))).toThrow("BatchWithFlusher is closed"); }); it("should not write after close", async () => { const batch = new BatchWithFlusher(db); const key = new Uint8Array([1]); const value = new Uint8Array([2]); batch.set(key, value); batch.close(); await expect(batch.write()).rejects.toThrow("BatchWithFlusher is closed"); expect(await db.get(key)).toBeNull(); }); }); describe("writeSync", () => { it("should write synchronously", async () => { const batch = new BatchWithFlusher(db); const key = new Uint8Array([1]); const value = new Uint8Array([2]); batch.set(key, value); await batch.writeSync(); expect(await db.get(key)).toEqual(value); }); }); }); describe("AsyncBatchWithFlusher", () => { let db: Database; beforeEach(() => { db = new MemDB(); }); describe("constructor", () => { it("should create with default threshold", () => { const batch = new AsyncBatchWithFlusher(db); expect(batch.getByteSize()).toBe(0); }); it("should create with custom threshold", () => { const batch = new AsyncBatchWithFlusher(db, 1000); expect(batch.getByteSize()).toBe(0); }); }); describe("basic operations", () => { it("should set values", async () => { const batch = new AsyncBatchWithFlusher(db); const key = new Uint8Array([1]); const value = new Uint8Array([ 2, 3, 4, ]); await batch.set(key, value); await batch.write(); expect(await db.get(key)).toEqual(value); }); it("should delete keys", async () => { const key = new Uint8Array([1]); const value = new Uint8Array([2]); await db.set(key, value); const batch = new AsyncBatchWithFlusher(db); await batch.delete(key); await batch.write(); expect(await db.get(key)).toBeNull(); }); it("should handle multiple operations", async () => { const batch = new AsyncBatchWithFlusher(db); const operations = Array.from({ length: 10, }, (_, i) => ({ key: makeKey(i), value: new Uint8Array([i]), })); for (const { key, value, } of operations) { await batch.set(key, value); } await batch.write(); for (const { key, value, } of operations) { expect(await db.get(key)).toEqual(value); } }); it("should handle mixed set and delete", async () => { // Pre-populate const toDelete = Array.from({ length: 5, }, (_, i) => ({ key: makeKey(i), value: new Uint8Array([i]), })); for (const { key, value, } of toDelete) { await db.set(key, value); } const batch = new AsyncBatchWithFlusher(db); // Delete existing for (const { key, } of toDelete) { await batch.delete(key); } // Add new const toAdd = Array.from({ length: 5, }, (_, i) => ({ key: makeKey(i + 100), value: new Uint8Array([i + 100]), })); for (const { key, value, } of toAdd) { await batch.set(key, value); } await batch.write(); // Check deleted for (const { key, } of toDelete) { expect(await db.get(key)).toBeNull(); } // Check added for (const { key, value, } of toAdd) { expect(await db.get(key)).toEqual(value); } }); }); describe("auto-flush behavior", () => { it("should auto-flush when threshold exceeded", async () => { // Use small threshold (100 bytes) const batch = new AsyncBatchWithFlusher(db, 100); // Each entry is ~12 bytes (2 byte key + 10 byte value) const valueSize = 10; const value = createData(valueSize); // Add entries that will trigger flush const keys: Uint8Array[] = []; for (let i = 0; i < 20; i++) { const key = makeKey(i); keys.push(key); await batch.set(key, value); // After ~8-9 entries, should auto-flush // Check if early entries are already persisted if (i > 10) { // Earlier entries should be flushed const earlyKey = keys[0]; expect(await db.get(earlyKey)).toEqual(value); } } // Final write await batch.write(); // All entries should be persisted for (const key of keys) { expect(await db.get(key)).toEqual(value); } }); it("should handle large single value exceeding threshold", async () => { const batch = new AsyncBatchWithFlusher(db, 100); const key1 = makeKey(1); const smallValue = createData(10); await batch.set(key1, smallValue); const key2 = makeKey(2); const largeValue = createData(200); // Exceeds threshold await batch.set(key2, largeValue); // First key should be flushed before large value expect(await db.get(key1)).toEqual(smallValue); await batch.write(); expect(await db.get(key2)).toEqual(largeValue); }); it("should maintain correct size tracking", async () => { const batch = new AsyncBatchWithFlusher(db, 1000); let expectedSize = 0; const key1 = makeKey(1); const value1 = createData(50); await batch.set(key1, value1); expectedSize += key1.length + value1.length; const key2 = makeKey(2); const value2 = createData(75); await batch.set(key2, value2); expectedSize += key2.length + value2.length; // Note: Size might not match exactly due to async processing // but should be in the ballpark const size = batch.getByteSize(); expect(size).toBeGreaterThanOrEqual(0); expect(size).toBeLessThanOrEqual(expectedSize + 100); // Allow for overhead await batch.write(); expect(batch.getByteSize()).toBe(0); }); }); describe("concurrent operations", () => { it("should handle concurrent sets", async () => { const batch = new AsyncBatchWithFlusher(db); // Launch multiple concurrent set operations const promises = Array.from({ length: 100, }, (_, i) => batch.set(makeKey(i), new Uint8Array([i])), ); await Promise.all(promises); await batch.write(); // All values should be written for (let i = 0; i < 100; i++) { expect(await db.get(makeKey(i))).toEqual(new Uint8Array([i])); } }); it("should handle concurrent mixed operations", async () => { // Pre-populate some keys for (let i = 0; i < 50; i++) { await db.set(makeKey(i), new Uint8Array([i])); } const batch = new AsyncBatchWithFlusher(db); // Concurrent deletes and sets const promises: Promise[] = []; // Delete first 50 for (let i = 0; i < 50; i++) { promises.push(batch.delete(makeKey(i))); } // Add next 50 for (let i = 50; i < 100; i++) { promises.push(batch.set(makeKey(i), new Uint8Array([i]))); } await Promise.all(promises); await batch.write(); // Check results for (let i = 0; i < 50; i++) { expect(await db.get(makeKey(i))).toBeNull(); } for (let i = 50; i < 100; i++) { expect(await db.get(makeKey(i))).toEqual(new Uint8Array([i])); } }); }); describe("close behavior", () => { it("should close batch", async () => { const batch = new AsyncBatchWithFlusher(db); batch.close(); await expect(batch.set(new Uint8Array([1]), new Uint8Array([2]))).rejects.toThrow("AsyncBatchWithFlusher is closed"); await expect(batch.delete(new Uint8Array([1]))).rejects.toThrow("AsyncBatchWithFlusher is closed"); }); it("should not write after close", async () => { const batch = new AsyncBatchWithFlusher(db); const key = new Uint8Array([1]); const value = new Uint8Array([2]); await batch.set(key, value); batch.close(); await expect(batch.write()).rejects.toThrow("AsyncBatchWithFlusher is closed"); }); }); describe("large data test (similar to Go test)", { timeout: 20000, }, () => { it("should handle 10MB of data with auto-flushing", async () => { const batch = new AsyncBatchWithFlusher(db, DEFAULT_FLUSH_THRESHOLD); // 10KB per entry const valueSize = 10000; const value = createData(valueSize); // Write 1000 entries = 10MB total const numEntries = 1000; for (let i = 0; i < numEntries; i++) { const key = makeKey(i); await batch.set(key, value); } await batch.write(); // Verify all entries for (let i = 0; i < numEntries; i++) { const key = makeKey(i); const retrieved = await db.get(key); expect(retrieved).toEqual(value); } }); }); });