// tests/ollama-client-cache.test.ts import { OllamaMessage, OllamaTool, CacheConfig } from '../src/types'; // Create mock functions for the cache service const mockInitialize = jest.fn().mockResolvedValue(undefined); const mockGetCache = jest.fn().mockResolvedValue(null); const mockSetCache = jest.fn().mockResolvedValue(undefined); const mockClearCache = jest.fn().mockResolvedValue(undefined); // Mock the semantic cache service BEFORE importing OllamaClient jest.mock('../src/semantic-cache', () => ({ SemanticCacheService: jest.fn().mockImplementation(() => ({ initialize: mockInitialize, getCache: mockGetCache, setCache: mockSetCache, clearCache: mockClearCache, })), })); import { OllamaClient } from '../src/ollama-client'; describe('OllamaClient', () => { const mockBaseUrl = 'http://localhost:11434'; const mockModel = 'llama3'; beforeEach(() => { mockInitialize.mockClear(); mockGetCache.mockClear(); mockSetCache.mockClear(); mockClearCache.mockClear(); }); describe('constructor', () => { it('should create cache service when enabled but not initialize it eagerly', () => { const cacheConfig: CacheConfig = { enabled: true, similarityThreshold: 0.85, collectionName: 'test_cache', embeddingModel: 'nomic-embed-text', chromaURL: 'http://localhost:8000', }; const client = new OllamaClient(mockBaseUrl, mockModel, undefined, cacheConfig); expect(client).toBeInstanceOf(OllamaClient); // Eager initialization was removed to avoid unhandled rejections; // initialization now happens via initializeCache() only. expect(mockInitialize).toHaveBeenCalledTimes(0); }); it('should not create cache service when disabled', () => { const cacheConfig: CacheConfig = { enabled: false, similarityThreshold: 0.85, collectionName: 'test_cache', embeddingModel: 'nomic-embed-text', chromaURL: 'http://localhost:8000', }; const client = new OllamaClient(mockBaseUrl, mockModel, undefined, cacheConfig); expect(client).toBeInstanceOf(OllamaClient); expect(mockInitialize).toHaveBeenCalledTimes(0); }); }); describe('clearCache', () => { it('should clear the cache when enabled', async () => { const cacheConfig: CacheConfig = { enabled: true, similarityThreshold: 0.85, collectionName: 'test_cache', embeddingModel: 'nomic-embed-text', chromaURL: 'http://localhost:8000', }; const client = new OllamaClient(mockBaseUrl, mockModel, undefined, cacheConfig); await client.clearCache(); expect(mockClearCache).toHaveBeenCalledTimes(1); }); it('should not clear cache when disabled', async () => { const cacheConfig: CacheConfig = { enabled: false, similarityThreshold: 0.85, collectionName: 'test_cache', embeddingModel: 'nomic-embed-text', chromaURL: 'http://localhost:8000', }; const client = new OllamaClient(mockBaseUrl, mockModel, undefined, cacheConfig); await client.clearCache(); expect(mockClearCache).toHaveBeenCalledTimes(0); }); }); });