|
| 1 | +import { generateText } from 'ai'; |
| 2 | + |
| 3 | +import { VercelProvider } from '../src/VercelProvider'; |
| 4 | + |
| 5 | +// Mock Vercel AI SDK |
| 6 | +jest.mock('ai', () => ({ |
| 7 | + generateText: jest.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +describe('VercelProvider', () => { |
| 11 | + let mockModel: any; |
| 12 | + let provider: VercelProvider; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + mockModel = { name: 'test-model' }; |
| 16 | + provider = new VercelProvider(mockModel, {}); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('createAIMetrics', () => { |
| 20 | + it('creates metrics with success=true and token usage', () => { |
| 21 | + const mockResponse = { |
| 22 | + usage: { |
| 23 | + promptTokens: 50, |
| 24 | + completionTokens: 50, |
| 25 | + totalTokens: 100, |
| 26 | + }, |
| 27 | + }; |
| 28 | + |
| 29 | + const result = VercelProvider.createAIMetrics(mockResponse); |
| 30 | + |
| 31 | + expect(result).toEqual({ |
| 32 | + success: true, |
| 33 | + usage: { |
| 34 | + total: 100, |
| 35 | + input: 50, |
| 36 | + output: 50, |
| 37 | + }, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('creates metrics with success=true and no usage when usage is missing', () => { |
| 42 | + const mockResponse = {}; |
| 43 | + |
| 44 | + const result = VercelProvider.createAIMetrics(mockResponse); |
| 45 | + |
| 46 | + expect(result).toEqual({ |
| 47 | + success: true, |
| 48 | + usage: undefined, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('handles partial usage data', () => { |
| 53 | + const mockResponse = { |
| 54 | + usage: { |
| 55 | + promptTokens: 30, |
| 56 | + // completionTokens and totalTokens missing |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + const result = VercelProvider.createAIMetrics(mockResponse); |
| 61 | + |
| 62 | + expect(result).toEqual({ |
| 63 | + success: true, |
| 64 | + usage: { |
| 65 | + total: 0, |
| 66 | + input: 30, |
| 67 | + output: 0, |
| 68 | + }, |
| 69 | + }); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('invokeModel', () => { |
| 74 | + it('invokes Vercel AI generateText and returns response', async () => { |
| 75 | + const mockResponse = { |
| 76 | + text: 'Hello! How can I help you today?', |
| 77 | + usage: { |
| 78 | + promptTokens: 10, |
| 79 | + completionTokens: 15, |
| 80 | + totalTokens: 25, |
| 81 | + }, |
| 82 | + }; |
| 83 | + |
| 84 | + (generateText as jest.Mock).mockResolvedValue(mockResponse); |
| 85 | + |
| 86 | + const messages = [{ role: 'user' as const, content: 'Hello!' }]; |
| 87 | + |
| 88 | + const result = await provider.invokeModel(messages); |
| 89 | + |
| 90 | + expect(generateText).toHaveBeenCalledWith({ |
| 91 | + model: mockModel, |
| 92 | + messages: [{ role: 'user', content: 'Hello!' }], |
| 93 | + }); |
| 94 | + |
| 95 | + expect(result).toEqual({ |
| 96 | + message: { |
| 97 | + role: 'assistant', |
| 98 | + content: 'Hello! How can I help you today?', |
| 99 | + }, |
| 100 | + metrics: { |
| 101 | + success: true, |
| 102 | + usage: { |
| 103 | + total: 25, |
| 104 | + input: 10, |
| 105 | + output: 15, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('handles response without usage data', async () => { |
| 112 | + const mockResponse = { |
| 113 | + text: 'Hello! How can I help you today?', |
| 114 | + }; |
| 115 | + |
| 116 | + (generateText as jest.Mock).mockResolvedValue(mockResponse); |
| 117 | + |
| 118 | + const messages = [{ role: 'user' as const, content: 'Hello!' }]; |
| 119 | + |
| 120 | + const result = await provider.invokeModel(messages); |
| 121 | + |
| 122 | + expect(result).toEqual({ |
| 123 | + message: { |
| 124 | + role: 'assistant', |
| 125 | + content: 'Hello! How can I help you today?', |
| 126 | + }, |
| 127 | + metrics: { |
| 128 | + success: true, |
| 129 | + usage: undefined, |
| 130 | + }, |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('getModel', () => { |
| 136 | + it('returns the underlying Vercel AI model', () => { |
| 137 | + const model = provider.getModel(); |
| 138 | + expect(model).toBe(mockModel); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe('createVercelModel', () => { |
| 143 | + it('creates OpenAI model for openai provider', async () => { |
| 144 | + const mockAiConfig = { |
| 145 | + model: { name: 'gpt-4', parameters: {} }, |
| 146 | + provider: { name: 'openai' }, |
| 147 | + enabled: true, |
| 148 | + tracker: {} as any, |
| 149 | + toVercelAISDK: jest.fn(), |
| 150 | + }; |
| 151 | + |
| 152 | + // Mock the dynamic import |
| 153 | + jest.doMock('@ai-sdk/openai', () => ({ |
| 154 | + openai: jest.fn().mockReturnValue(mockModel), |
| 155 | + })); |
| 156 | + |
| 157 | + const result = await VercelProvider.createVercelModel(mockAiConfig); |
| 158 | + expect(result).toBe(mockModel); |
| 159 | + }); |
| 160 | + |
| 161 | + it('throws error for unsupported provider', async () => { |
| 162 | + const mockAiConfig = { |
| 163 | + model: { name: 'test-model', parameters: {} }, |
| 164 | + provider: { name: 'unsupported' }, |
| 165 | + enabled: true, |
| 166 | + tracker: {} as any, |
| 167 | + toVercelAISDK: jest.fn(), |
| 168 | + }; |
| 169 | + |
| 170 | + await expect(VercelProvider.createVercelModel(mockAiConfig)).rejects.toThrow( |
| 171 | + 'Unsupported Vercel AI provider: unsupported', |
| 172 | + ); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('create', () => { |
| 177 | + it('creates VercelProvider with correct model and parameters', async () => { |
| 178 | + const mockAiConfig = { |
| 179 | + model: { |
| 180 | + name: 'gpt-4', |
| 181 | + parameters: { |
| 182 | + temperature: 0.7, |
| 183 | + maxTokens: 1000, |
| 184 | + }, |
| 185 | + }, |
| 186 | + provider: { name: 'openai' }, |
| 187 | + enabled: true, |
| 188 | + tracker: {} as any, |
| 189 | + toVercelAISDK: jest.fn(), |
| 190 | + }; |
| 191 | + |
| 192 | + // Mock the dynamic import |
| 193 | + jest.doMock('@ai-sdk/openai', () => ({ |
| 194 | + openai: jest.fn().mockReturnValue(mockModel), |
| 195 | + })); |
| 196 | + |
| 197 | + const result = await VercelProvider.create(mockAiConfig); |
| 198 | + |
| 199 | + expect(result).toBeInstanceOf(VercelProvider); |
| 200 | + expect(result.getModel()).toBeDefined(); |
| 201 | + }); |
| 202 | + }); |
| 203 | +}); |
0 commit comments