|
1 | 1 | // AIService integration tests |
2 | 2 |
|
3 | 3 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
4 | | -import { PerplexityClient, OpenAIClient, AICache, generateArtistSummary } from '@listentomore/ai'; |
| 4 | +import { OpenAIClient, AICache, generateArtistSummary } from '@listentomore/ai'; |
5 | 5 | import { createMockKV, setupFetchMock } from '../utils/mocks'; |
6 | 6 |
|
7 | 7 | describe('OpenAIClient', () => { |
@@ -159,79 +159,6 @@ describe('OpenAIClient', () => { |
159 | 159 | }); |
160 | 160 | }); |
161 | 161 |
|
162 | | -describe('PerplexityClient', () => { |
163 | | - let client: PerplexityClient; |
164 | | - |
165 | | - beforeEach(() => { |
166 | | - client = new PerplexityClient('test-api-key'); |
167 | | - }); |
168 | | - |
169 | | - describe('chatCompletion', () => { |
170 | | - it('sends chat completion request and returns response', async () => { |
171 | | - const response = { |
172 | | - choices: [{ message: { content: 'This is a response about Radiohead.' } }], |
173 | | - citations: ['https://en.wikipedia.org/wiki/Radiohead'], |
174 | | - }; |
175 | | - setupFetchMock([{ pattern: /api\.perplexity\.ai/, response }]); |
176 | | - |
177 | | - const result = await client.chatCompletion({ |
178 | | - model: 'sonar', |
179 | | - messages: [{ role: 'user', content: 'Tell me about Radiohead' }], |
180 | | - }); |
181 | | - |
182 | | - expect(result.content).toBe('This is a response about Radiohead.'); |
183 | | - expect(result.citations).toEqual(['https://en.wikipedia.org/wiki/Radiohead']); |
184 | | - }); |
185 | | - |
186 | | - it('preserves citation markers in response', async () => { |
187 | | - const response = { |
188 | | - choices: [{ message: { content: 'Radiohead [1] formed in 1985 [2] in Oxford.' } }], |
189 | | - citations: ['https://example.com', 'https://example2.com'], |
190 | | - }; |
191 | | - setupFetchMock([{ pattern: /api\.perplexity\.ai/, response }]); |
192 | | - |
193 | | - const result = await client.chatCompletion({ |
194 | | - model: 'sonar', |
195 | | - messages: [{ role: 'user', content: 'test' }], |
196 | | - }); |
197 | | - |
198 | | - // Citation markers are preserved for client-side transformation to superscript links |
199 | | - expect(result.content).toBe('Radiohead [1] formed in 1985 [2] in Oxford.'); |
200 | | - expect(result.citations).toEqual(['https://example.com', 'https://example2.com']); |
201 | | - }); |
202 | | - |
203 | | - it('throws error on API failure', async () => { |
204 | | - setupFetchMock([ |
205 | | - { |
206 | | - pattern: /api\.perplexity\.ai/, |
207 | | - response: { error: 'Unauthorized' }, |
208 | | - options: { status: 401, ok: false }, |
209 | | - }, |
210 | | - ]); |
211 | | - |
212 | | - await expect( |
213 | | - client.chatCompletion({ |
214 | | - model: 'sonar', |
215 | | - messages: [{ role: 'user', content: 'test' }], |
216 | | - }) |
217 | | - ).rejects.toThrow('Perplexity API error 401'); |
218 | | - }); |
219 | | - |
220 | | - it('handles empty citations', async () => { |
221 | | - const response = { |
222 | | - choices: [{ message: { content: 'Response without citations' } }], |
223 | | - }; |
224 | | - setupFetchMock([{ pattern: /api\.perplexity\.ai/, response }]); |
225 | | - |
226 | | - const result = await client.chatCompletion({ |
227 | | - model: 'sonar', |
228 | | - messages: [{ role: 'user', content: 'test' }], |
229 | | - }); |
230 | | - |
231 | | - expect(result.citations).toEqual([]); |
232 | | - }); |
233 | | - }); |
234 | | -}); |
235 | 162 |
|
236 | 163 | describe('AICache', () => { |
237 | 164 | let mockKV: KVNamespace; |
@@ -303,28 +230,37 @@ describe('AICache', () => { |
303 | 230 |
|
304 | 231 | describe('generateArtistSummary', () => { |
305 | 232 | let mockKV: KVNamespace; |
306 | | - let mockClient: PerplexityClient; |
| 233 | + let mockClient: OpenAIClient; |
307 | 234 | let cache: AICache; |
308 | 235 |
|
309 | 236 | beforeEach(() => { |
310 | 237 | mockKV = createMockKV(); |
311 | 238 | cache = new AICache(mockKV); |
312 | | - mockClient = new PerplexityClient('test-key'); |
| 239 | + mockClient = new OpenAIClient('test-key'); |
313 | 240 | }); |
314 | 241 |
|
315 | 242 | it('generates artist summary and caches result', async () => { |
316 | 243 | const response = { |
| 244 | + model: 'gpt-5-search-api', |
317 | 245 | choices: [ |
318 | 246 | { |
319 | 247 | message: { |
320 | | - content: |
321 | | - 'Radiohead is an English rock band. Their album {{OK Computer}} is considered a masterpiece. Similar artists include [[Portishead]].', |
| 248 | + content: 'Radiohead is an English rock band. Their album {{OK Computer}} is considered a masterpiece. Similar artists include [[Portishead]].', |
| 249 | + annotations: [ |
| 250 | + { |
| 251 | + type: 'url_citation', |
| 252 | + url_citation: { |
| 253 | + url: 'https://en.wikipedia.org/wiki/Radiohead', |
| 254 | + title: 'Radiohead - Wikipedia', |
| 255 | + }, |
| 256 | + }, |
| 257 | + ], |
322 | 258 | }, |
323 | 259 | }, |
324 | 260 | ], |
325 | | - citations: ['https://en.wikipedia.org/wiki/Radiohead'], |
| 261 | + usage: { prompt_tokens: 100, completion_tokens: 50, total_tokens: 150 }, |
326 | 262 | }; |
327 | | - setupFetchMock([{ pattern: /api\.perplexity\.ai/, response }]); |
| 263 | + setupFetchMock([{ pattern: /api\.openai\.com/, response }]); |
328 | 264 |
|
329 | 265 | const result = await generateArtistSummary('Radiohead', mockClient, cache); |
330 | 266 |
|
|
0 commit comments