import { afterAll, describe, it, expect } from "bun:test"; import { mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; const home = mkdtempSync(join(tmpdir(), "opencode-mem-test-")); const originalHome = process.env.HOME; const originalUserProfile = process.env.USERPROFILE; process.env.HOME = home; process.env.USERPROFILE = home; const { CONFIG, getAutoCaptureProviderStatus, hasAutoCaptureProviderConfig, isConfigured, isPlaceholderApiKey, normalizeAutoCleanupRetentionDays, normalizeAutoCaptureMaxContextBytes, normalizeEmbeddingIdleTimeoutMs, } = await import("../src/config.js"); afterAll(() => { process.env.HOME = originalHome; process.env.USERPROFILE = originalUserProfile; }); describe("config", () => { describe("CONFIG defaults", () => { it("should have a storagePath as a valid string", () => { expect(typeof CONFIG.storagePath).toBe("string"); expect(CONFIG.storagePath.length).toBeGreaterThan(0); }); it("should expose a valid memoryBackend", () => { expect(["database", "markdown"]).toContain(CONFIG.memoryBackend); }); it("should default to Xenova/nomic-embed-text-v1 embedding model", () => { expect(typeof CONFIG.embeddingModel).toBe("string"); }); it("should default embeddingUseTaskPrefixes to false", () => { expect(CONFIG.embeddingUseTaskPrefixes).toBe(false); }); it("should default embeddingPooling to mean", () => { expect(CONFIG.embeddingPooling).toBe("mean"); }); it("should leave embeddingDtype unset by default", () => { expect(CONFIG.embeddingDtype).toBeUndefined(); }); it("should keep the local embedding model loaded by default", async () => { // Cache-busted: CONFIG otherwise carries the developer's real user config. const fresh = await import(`../src/config.js?cachebust=${Math.random()}`); expect(fresh.CONFIG.embeddingIdleTimeoutMs).toBe(0); }); it("should normalize embeddingIdleTimeoutMs and allow 0 to disable", () => { expect(normalizeEmbeddingIdleTimeoutMs(0)).toBe(0); expect(normalizeEmbeddingIdleTimeoutMs(600000)).toBe(600000); expect(() => normalizeEmbeddingIdleTimeoutMs(-1)).toThrow(/embeddingIdleTimeoutMs/); expect(() => normalizeEmbeddingIdleTimeoutMs(1.5)).toThrow(/embeddingIdleTimeoutMs/); }); it("should have numeric embeddingDimensions", () => { expect(typeof CONFIG.embeddingDimensions).toBe("number"); expect(CONFIG.embeddingDimensions).toBeGreaterThan(0); }); it("should have similarityThreshold between 0 and 1", () => { expect(CONFIG.similarityThreshold).toBeGreaterThanOrEqual(0); expect(CONFIG.similarityThreshold).toBeLessThanOrEqual(1); }); it("should have positive maxMemories", () => { expect(CONFIG.maxMemories).toBeGreaterThan(0); }); it("should have webServerPort as a number", () => { expect(typeof CONFIG.webServerPort).toBe("number"); }); it("should have webServerHost as a string", () => { expect(typeof CONFIG.webServerHost).toBe("string"); }); it("should have maxVectorsPerShard as a positive number", () => { expect(CONFIG.maxVectorsPerShard).toBeGreaterThan(0); }); it("should have compaction settings", () => { expect(CONFIG.compaction).toBeDefined(); expect(typeof CONFIG.compaction.enabled).toBe("boolean"); expect(typeof CONFIG.compaction.memoryLimit).toBe("number"); }); it("should have chatMessage settings", () => { expect(CONFIG.chatMessage).toBeDefined(); expect(typeof CONFIG.chatMessage.enabled).toBe("boolean"); expect(typeof CONFIG.chatMessage.maxMemories).toBe("number"); expect(typeof CONFIG.chatMessage.excludeCurrentSession).toBe("boolean"); }); it("should have chatMessage.injectOn as 'first' or 'always'", () => { expect(["first", "always"]).toContain(CONFIG.chatMessage.injectOn); }); it("should default chatMessage.recall to relevant with default scope", () => { expect(CONFIG.chatMessage.recall).toBe("relevant"); expect(CONFIG.chatMessage.recallScope).toBe("default"); }); it("should default dreaming to disabled", () => { expect(CONFIG.dreaming.enabled).toBe(false); expect(CONFIG.dreaming.intervalHours).toBe(24); expect(CONFIG.dreaming.maxGroupsPerRun).toBe(5); expect(CONFIG.dreaming.trashRetentionDays).toBe(30); expect(CONFIG.dreaming.relatedThreshold).toBe(0.75); expect(CONFIG.dreaming.crossProject).toBe(false); }); it("should default update-on-capture to disabled", () => { expect(CONFIG.updateOnCaptureEnabled).toBe(false); expect(CONFIG.updateOnCaptureSimilarityThreshold).toBe(0.75); expect(CONFIG.updateOnCaptureMaxCandidates).toBe(5); }); it("should have boolean toggle settings", () => { expect(typeof CONFIG.autoCaptureEnabled).toBe("boolean"); expect(typeof CONFIG.injectProfile).toBe("boolean"); expect(typeof CONFIG.webServerEnabled).toBe("boolean"); expect(typeof CONFIG.autoCleanupEnabled).toBe("boolean"); expect(typeof CONFIG.deduplicationEnabled).toBe("boolean"); }); it("should default autoCaptureMaxContextBytes to 131072", () => { expect(CONFIG.autoCaptureMaxContextBytes).toBe(131072); }); it("should reject unsafe auto-capture context budgets", () => { expect(() => normalizeAutoCaptureMaxContextBytes(-1)).toThrow(); expect(() => normalizeAutoCaptureMaxContextBytes(1024)).toThrow(); expect(() => normalizeAutoCaptureMaxContextBytes(16 * 1024 * 1024 + 1)).toThrow(); }); it("should reject unsafe automatic cleanup retention periods", () => { expect(() => normalizeAutoCleanupRetentionDays(0)).toThrow(); expect(() => normalizeAutoCleanupRetentionDays(-1)).toThrow(); expect(() => normalizeAutoCleanupRetentionDays(1.5)).toThrow(); expect(() => normalizeAutoCleanupRetentionDays(Number.POSITIVE_INFINITY)).toThrow(); expect(normalizeAutoCleanupRetentionDays(1)).toBe(1); expect(normalizeAutoCleanupRetentionDays(30)).toBe(30); }); it("should expose memory scope config", () => { const defaultScope = CONFIG.memory.defaultScope ?? "project"; expect(["project", "all-projects"]).toContain(defaultScope); }); it("should have user profile settings as numbers", () => { expect(typeof CONFIG.userProfileAnalysisInterval).toBe("number"); expect(typeof CONFIG.userProfileDisplayPreferences).toBe("number"); expect(typeof CONFIG.userProfileDisplayPatterns).toBe("number"); expect(typeof CONFIG.userProfileDisplayWorkflows).toBe("number"); expect(typeof CONFIG.userProfileConfidenceDecayDays).toBe("number"); expect(typeof CONFIG.userProfileChangelogRetentionCount).toBe("number"); expect(CONFIG.userProfileStaleDays).toBe(2); expect(CONFIG.userProfileMinEvidenceForRetention).toBe(3); expect(CONFIG.userProfileAutoCleanupEnabled).toBe(true); expect(CONFIG.userProfileAutoCleanupInterval).toBe(100); expect(CONFIG.userProfileCleanupTimeoutMs).toBe(60000); }); it("should have toast settings as booleans", () => { expect(typeof CONFIG.showAutoCaptureToasts).toBe("boolean"); expect(typeof CONFIG.showUserProfileToasts).toBe("boolean"); expect(typeof CONFIG.showErrorToasts).toBe("boolean"); }); it("should not treat template manual API placeholders as usable auto-capture config", () => { expect( hasAutoCaptureProviderConfig({ ...CONFIG, opencodeProvider: undefined, opencodeModel: undefined, memoryModel: "gpt-4o-mini", memoryApiUrl: "https://api.openai.com/v1", memoryApiKey: "sk-...", }) ).toBe(false); }); it("should treat opencode provider settings as usable auto-capture config", () => { expect( hasAutoCaptureProviderConfig({ ...CONFIG, opencodeProvider: "anthropic", opencodeModel: "claude-haiku-4-5-20251001", memoryModel: undefined, memoryApiUrl: undefined, memoryApiKey: undefined, }) ).toBe(true); }); it("should report manual provider mode when model, API URL, and API key are configured", () => { expect( getAutoCaptureProviderStatus({ ...CONFIG, opencodeProvider: undefined, opencodeModel: undefined, memoryModel: "local-model", memoryApiUrl: "http://127.0.0.1:11434/v1", memoryApiKey: "local-api-key", }) ).toEqual({ ready: true, mode: "manual", issues: [] }); }); it("should report missing memoryApiKey when model and API URL are configured without a key", () => { expect( getAutoCaptureProviderStatus({ ...CONFIG, opencodeProvider: undefined, opencodeModel: undefined, memoryModel: "gpt-4o-mini", memoryApiUrl: "https://api.openai.com/v1", memoryApiKey: undefined, }) ).toEqual({ ready: false, issues: [ "opencodeProvider is not configured", "opencodeModel is not configured", "memoryApiKey is not configured", ], }); }); it("should report each missing manual provider field independently", () => { expect( getAutoCaptureProviderStatus({ ...CONFIG, opencodeProvider: undefined, opencodeModel: undefined, memoryModel: undefined, memoryApiUrl: undefined, memoryApiKey: "sk-...", }) ).toEqual({ ready: false, issues: [ "opencodeProvider is not configured", "opencodeModel is not configured", "memoryModel is not configured", "memoryApiUrl is not configured", "memoryApiKey contains a placeholder value", ], }); }); it("should expose the resolved auto-capture provider status on CONFIG", () => { expect(CONFIG.autoCaptureProviderStatus).toEqual(getAutoCaptureProviderStatus(CONFIG)); }); it("should default chatMessage.captureChildSessions to false", () => { expect(CONFIG.chatMessage.captureChildSessions).toBe(false); }); }); describe("isConfigured", () => { it("should return true", () => { expect(isConfigured()).toBe(true); }); it("should return a boolean", () => { expect(typeof isConfigured()).toBe("boolean"); }); }); describe("placeholder API key detection", () => { it("treats template API keys as placeholders", () => { expect(isPlaceholderApiKey("sk-...")).toBe(true); expect(isPlaceholderApiKey("sk-ant-...")).toBe(true); expect(isPlaceholderApiKey("gsk_...")).toBe(true); }); it("does not treat real-looking or absent API keys as placeholders", () => { expect(isPlaceholderApiKey("sk-test-realish")).toBe(false); expect(isPlaceholderApiKey(undefined)).toBe(false); }); }); });