Files
obsidian_ollama/tests/ollama-client-cache.test.ts
T

100 lines
3.0 KiB
TypeScript

// 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 initialize cache service when enabled', () => {
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);
expect(mockInitialize).toHaveBeenCalledTimes(1);
});
it('should not initialize 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);
});
});
});