import { describe, expect, it, } from "vitest"; import { createLogger, } from "../utils/logger.js"; import { createStatistics, DEFAULT_OPTIONS, mergeOptions, validateOptions, } from "./options.js"; describe("options", () => { describe("createStatistics", () => { it("should create statistics with all counters at zero", () => { const stats = createStatistics(); expect(stats.cacheHitCnt).toBe(0); expect(stats.cacheMissCnt).toBe(0); expect(stats.fastCacheHitCnt).toBe(0); expect(stats.fastCacheMissCnt).toBe(0); }); }); describe("DEFAULT_OPTIONS", () => { it("should have expected default values", () => { expect(DEFAULT_OPTIONS.sync).toBe(false); expect(DEFAULT_OPTIONS.initialVersion).toBeUndefined(); // Not set by default (matches Go) expect(DEFAULT_OPTIONS.flushThreshold).toBe(100_000); expect(DEFAULT_OPTIONS.asyncPruning).toBe(false); expect(DEFAULT_OPTIONS.cacheSize).toBe(10_000); expect(DEFAULT_OPTIONS.fastCacheSize).toBe(100_000); expect(DEFAULT_OPTIONS.skipFastStorageUpgrade).toBe(false); }); }); describe("mergeOptions", () => { it("should return defaults when no options provided", () => { const logger = createLogger(); const options = mergeOptions({ }, logger); expect(options.sync).toBe(DEFAULT_OPTIONS.sync); expect(options.initialVersion).toBe(DEFAULT_OPTIONS.initialVersion); expect(options.flushThreshold).toBe(DEFAULT_OPTIONS.flushThreshold); expect(options.asyncPruning).toBe(DEFAULT_OPTIONS.asyncPruning); expect(options.cacheSize).toBe(DEFAULT_OPTIONS.cacheSize); expect(options.fastCacheSize).toBe(DEFAULT_OPTIONS.fastCacheSize); expect(options.statistics).toBeDefined(); expect(options.logger).toBe(logger); }); it("should merge user options with defaults", () => { const logger = createLogger(); const options = mergeOptions({ sync: true, cacheSize: 5000, }, logger); expect(options.sync).toBe(true); expect(options.cacheSize).toBe(5000); expect(options.initialVersion).toBe(DEFAULT_OPTIONS.initialVersion); expect(options.flushThreshold).toBe(DEFAULT_OPTIONS.flushThreshold); }); it("should use provided logger over default", () => { const defaultLogger = createLogger("info"); const logger = createLogger("debug"); const options = mergeOptions({ logger, }, defaultLogger); expect(options.logger).toBe(logger); }); it("should use default logger if not provided in options", () => { const defaultLogger = createLogger("warn"); const options = mergeOptions({ }, defaultLogger); expect(options.logger).toBe(defaultLogger); }); it("should use provided statistics", () => { const logger = createLogger(); const stats = createStatistics(); stats.cacheHitCnt = 100; const options = mergeOptions({ statistics: stats, }, logger); expect(options.statistics).toBe(stats); expect(options.statistics.cacheHitCnt).toBe(100); }); it("should create new statistics if not provided", () => { const logger = createLogger(); const options = mergeOptions({ }, logger); expect(options.statistics).toBeDefined(); expect(options.statistics.cacheHitCnt).toBe(0); }); it("should throw if no logger provided", () => { expect(() => mergeOptions({ })).toThrow("Logger must be provided"); }); it("should allow all options to be customized", () => { const logger = createLogger("error"); const stats = createStatistics(); const options = mergeOptions({ sync: true, initialVersion: 10, flushThreshold: 50_000, asyncPruning: true, cacheSize: 5_000, fastCacheSize: 50_000, skipFastStorageUpgrade: true, logger, statistics: stats, }); expect(options.sync).toBe(true); expect(options.initialVersion).toBe(10); expect(options.flushThreshold).toBe(50_000); expect(options.asyncPruning).toBe(true); expect(options.cacheSize).toBe(5_000); expect(options.fastCacheSize).toBe(50_000); expect(options.skipFastStorageUpgrade).toBe(true); expect(options.logger).toBe(logger); expect(options.statistics).toBe(stats); }); }); describe("validateOptions", () => { it("should not throw for valid options", () => { expect(() => validateOptions({ })).not.toThrow(); expect(() => validateOptions({ initialVersion: 0, flushThreshold: 1, cacheSize: 0, fastCacheSize: 0, })).not.toThrow(); }); it("should throw for negative initialVersion", () => { expect(() => validateOptions({ initialVersion: -1, })).toThrow("initialVersion must be non-negative"); }); it("should throw for non-positive flushThreshold", () => { expect(() => validateOptions({ flushThreshold: 0, })).toThrow("flushThreshold must be positive"); expect(() => validateOptions({ flushThreshold: -1, })).toThrow("flushThreshold must be positive"); }); it("should throw for negative cacheSize", () => { expect(() => validateOptions({ cacheSize: -1, })).toThrow("cacheSize must be non-negative"); }); it("should throw for negative fastCacheSize", () => { expect(() => validateOptions({ fastCacheSize: -1, })).toThrow("fastCacheSize must be non-negative"); }); it("should allow zero cache sizes", () => { expect(() => validateOptions({ cacheSize: 0, fastCacheSize: 0, })).not.toThrow(); }); }); });