|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import type { IAIService, AIMessage, AIResult } from './ai-service'; |
| 3 | + |
| 4 | +describe('AI Service Contract', () => { |
| 5 | + it('should allow a minimal IAIService implementation with required methods', () => { |
| 6 | + const service: IAIService = { |
| 7 | + chat: async (_messages, _options?) => ({ content: '' }), |
| 8 | + complete: async (_prompt, _options?) => ({ content: '' }), |
| 9 | + }; |
| 10 | + |
| 11 | + expect(typeof service.chat).toBe('function'); |
| 12 | + expect(typeof service.complete).toBe('function'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should allow a full implementation with optional methods', () => { |
| 16 | + const service: IAIService = { |
| 17 | + chat: async () => ({ content: '' }), |
| 18 | + complete: async () => ({ content: '' }), |
| 19 | + embed: async () => [[]], |
| 20 | + listModels: async () => [], |
| 21 | + }; |
| 22 | + |
| 23 | + expect(service.embed).toBeDefined(); |
| 24 | + expect(service.listModels).toBeDefined(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should generate a chat completion', async () => { |
| 28 | + const service: IAIService = { |
| 29 | + chat: async (messages): Promise<AIResult> => { |
| 30 | + const lastMessage = messages[messages.length - 1]; |
| 31 | + return { |
| 32 | + content: `Echo: ${lastMessage.content}`, |
| 33 | + model: 'test-model', |
| 34 | + usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 }, |
| 35 | + }; |
| 36 | + }, |
| 37 | + complete: async () => ({ content: '' }), |
| 38 | + }; |
| 39 | + |
| 40 | + const messages: AIMessage[] = [ |
| 41 | + { role: 'system', content: 'You are a helpful assistant.' }, |
| 42 | + { role: 'user', content: 'Hello' }, |
| 43 | + ]; |
| 44 | + |
| 45 | + const result = await service.chat(messages); |
| 46 | + expect(result.content).toBe('Echo: Hello'); |
| 47 | + expect(result.model).toBe('test-model'); |
| 48 | + expect(result.usage?.totalTokens).toBe(15); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should generate a text completion', async () => { |
| 52 | + const service: IAIService = { |
| 53 | + chat: async () => ({ content: '' }), |
| 54 | + complete: async (prompt, options?): Promise<AIResult> => ({ |
| 55 | + content: `Completed: ${prompt}`, |
| 56 | + model: options?.model ?? 'default', |
| 57 | + }), |
| 58 | + }; |
| 59 | + |
| 60 | + const result = await service.complete('The sky is', { model: 'gpt-4', maxTokens: 50 }); |
| 61 | + expect(result.content).toContain('The sky is'); |
| 62 | + expect(result.model).toBe('gpt-4'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should generate embeddings', async () => { |
| 66 | + const service: IAIService = { |
| 67 | + chat: async () => ({ content: '' }), |
| 68 | + complete: async () => ({ content: '' }), |
| 69 | + embed: async (input) => { |
| 70 | + const texts = Array.isArray(input) ? input : [input]; |
| 71 | + return texts.map(() => [0.1, 0.2, 0.3]); |
| 72 | + }, |
| 73 | + }; |
| 74 | + |
| 75 | + const embeddings = await service.embed!('Hello world'); |
| 76 | + expect(embeddings).toHaveLength(1); |
| 77 | + expect(embeddings[0]).toEqual([0.1, 0.2, 0.3]); |
| 78 | + |
| 79 | + const batch = await service.embed!(['Hello', 'World']); |
| 80 | + expect(batch).toHaveLength(2); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should list available models', async () => { |
| 84 | + const service: IAIService = { |
| 85 | + chat: async () => ({ content: '' }), |
| 86 | + complete: async () => ({ content: '' }), |
| 87 | + listModels: async () => ['gpt-4', 'gpt-3.5-turbo', 'claude-3-sonnet'], |
| 88 | + }; |
| 89 | + |
| 90 | + const models = await service.listModels!(); |
| 91 | + expect(models).toHaveLength(3); |
| 92 | + expect(models).toContain('gpt-4'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments