Files
obsidian_ollama/tests/ollama-client-cache.test.ts
T
fegger 771db09d24 Refactor chat view and add conversation state management
Introduce ConversationStateManager to handle short, medium, and long-term
context for improved conversation flow. Update ChatView to use this manager
and refactor input handling to accept values directly for better testability.

Update OllamaClient with non-streaming chat support and improved error
handling for malformed chunks. Enhance vault indexer with caching, better
scoring, and stop word filtering. Refactor main plugin entry point and
semantic cache initialization for robustness.
2026-05-08 11:52:31 +02:00

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);
});
});
});