UNPKG

cmte

Version:

Design by Committee™ except it's just you and LLMs

87 lines (85 loc) 2.9 kB
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { createLLMClient, getLLMClient } from "../factory.js"; import { ClaudeAdapter } from "../claude-adapter.js"; import { LocalLLMAdapter } from "../local-llm-adapter.js"; // Mock logger vi.mock('../../../utils/logger.js', () => ({ logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() } })); // Store original env const originalEnv = process.env; describe('LLM Factory', () => { beforeEach(() => { // Reset environment before each test process.env = { ...originalEnv }; process.env.NODE_ENV = 'test'; process.env.ANTHROPIC_API_KEY = 'test-key'; process.env.LOCAL_LLM_URL = 'http://localhost:1234'; process.env.LOCAL_LLM_MODEL = 'test-model'; }); afterEach(() => { // Restore original env process.env = originalEnv; }); describe('createLLMClient', () => { it('should create a Claude client by default', () => { const client = createLLMClient(); expect(client).toBeInstanceOf(ClaudeAdapter); }); it('should create a Local LLM client when provider is local', () => { const client = createLLMClient({ provider: 'local' }); expect(client).toBeInstanceOf(LocalLLMAdapter); }); it('should throw error for unsupported provider', () => { expect(() => createLLMClient({ provider: 'unsupported' })).toThrow('Unsupported LLM provider: unsupported'); }); it('should use default model when not specified', () => { const client = createLLMClient(); expect(client.config.model).toBe('claude-3-7-sonnet-latest'); }); it('should use provided model when specified', () => { const client = createLLMClient({ model: 'custom-model' }); expect(client.config.model).toBe('custom-model'); }); it('should use environment model when available', () => { process.env.DEFAULT_MODEL = 'env-model'; const client = createLLMClient(); expect(client.config.model).toBe('env-model'); }); }); describe('getLLMClient', () => { it('should return Claude client when useLocalLLM is false', () => { const client = getLLMClient(false); expect(client).toBeInstanceOf(ClaudeAdapter); }); it('should return Local LLM client when useLocalLLM is true', () => { const client = getLLMClient(true); expect(client).toBeInstanceOf(LocalLLMAdapter); }); it('should pass through additional config', () => { const client = getLLMClient(false, { model: 'custom-model' }); expect(client.config.model).toBe('custom-model'); }); it('should override provider based on useLocalLLM', () => { const client = getLLMClient(true, { provider: 'anthropic' }); expect(client).toBeInstanceOf(LocalLLMAdapter); }); }); });