/** * ConfigManager Tests */ import { ConfigManager } from '../config'; // Mock Configstore jest.mock('configstore', () => { return jest.fn().mockImplementation((_name, defaults) => { let data = { ...defaults }; return { all: data, get: jest.fn((key: string) => { const keys = key.split('.'); let value: any = data; for (const k of keys) { value = value?.[k]; } return value; }), set: jest.fn((key: string, value: any) => { if (key.includes('.')) { const keys = key.split('.'); const lastKey = keys.pop()!; let target: any = data; for (const k of keys) { target = target[k]; } target[lastKey] = value; } else { data[key] = value; } }), clear: jest.fn(() => { data = {}; }), path: '/mock/config/path.json', }; }); }); describe('ConfigManager', () => { let configManager: ConfigManager; beforeEach(() => { configManager = new ConfigManager(); }); describe('getConfig', () => { it('should return full configuration', () => { const config = configManager.getConfig(); expect(config).toHaveProperty('ollama'); expect(config).toHaveProperty('preferences'); }); }); describe('getOllamaConfig', () => { it('should return Ollama configuration', () => { const ollamaConfig = configManager.getOllamaConfig(); expect(ollamaConfig).toHaveProperty('baseUrl'); expect(ollamaConfig).toHaveProperty('model'); expect(ollamaConfig).toHaveProperty('timeout'); }); it('should have default values', () => { const ollamaConfig = configManager.getOllamaConfig(); expect(ollamaConfig.baseUrl).toBe('http://localhost:11434'); expect(ollamaConfig.model).toBe('deepseek-coder:33b'); expect(ollamaConfig.timeout).toBe(60000); }); }); describe('setOllamaConfig', () => { it('should update Ollama configuration partially', () => { configManager.setOllamaConfig({ model: 'codellama:34b' }); const config = configManager.getOllamaConfig(); expect(config.model).toBe('codellama:34b'); expect(config.baseUrl).toBe('http://localhost:11434'); // Unchanged }); it('should update multiple Ollama properties', () => { configManager.setOllamaConfig({ model: 'llama2:13b', baseUrl: 'http://localhost:8080', }); const config = configManager.getOllamaConfig(); expect(config.model).toBe('llama2:13b'); expect(config.baseUrl).toBe('http://localhost:8080'); }); }); describe('getPreferences', () => { it('should return preferences configuration', () => { const prefs = configManager.getPreferences(); expect(prefs).toHaveProperty('autoContext'); expect(prefs).toHaveProperty('maxContextFiles'); expect(prefs).toHaveProperty('theme'); }); it('should have default preference values', () => { const prefs = configManager.getPreferences(); expect(prefs.autoContext).toBe(true); expect(prefs.maxContextFiles).toBe(10); expect(prefs.theme).toBe('auto'); }); }); describe('setPreference', () => { it('should update individual preference', () => { configManager.setPreference('autoContext', false); const prefs = configManager.getPreferences(); expect(prefs.autoContext).toBe(false); }); it('should update maxContextFiles preference', () => { configManager.setPreference('maxContextFiles', 20); const prefs = configManager.getPreferences(); expect(prefs.maxContextFiles).toBe(20); }); it('should update theme preference', () => { configManager.setPreference('theme', 'dark'); const prefs = configManager.getPreferences(); expect(prefs.theme).toBe('dark'); }); }); describe('getConfigPath', () => { it('should return config file path', () => { const path = configManager.getConfigPath(); expect(path).toBe('/mock/config/path.json'); }); }); describe('getUser', () => { it('should return user configuration if set', () => { const user = configManager.getUser(); expect(user).toBeUndefined(); // Default has no user }); }); describe('setUser', () => { it('should set user configuration', () => { const userData = { id: 'user123', email: 'test@example.com', apiKey: 'key123', }; configManager.setUser(userData); // Verify through mock that set was called expect(configManager.getUser).toBeDefined(); }); }); describe('reset', () => { it('should reset configuration to defaults', () => { // Change some config configManager.setOllamaConfig({ model: 'test-model' }); // Reset should be callable expect(() => configManager.reset()).not.toThrow(); }); }); });