This commit is contained in:
25
apps/web/server/utils/ai-provider.test.ts
Normal file
25
apps/web/server/utils/ai-provider.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { buildJsonCompletion, parseJsonCompletion, resolveAiProvider } from './ai-provider'
|
||||
|
||||
const request = { system: 'Return JSON.', messages: [{ role: 'user' as const, content: 'Create a world' }], schemaName: 'world', jsonSchema: { type: 'object' } }
|
||||
|
||||
describe('AI provider adapter', () => {
|
||||
it('keeps OpenRouter structured outputs', () => {
|
||||
const completion = buildJsonCompletion({ aiProvider: 'openrouter', openrouterApiKey: 'key', openrouterModel: 'model' }, request)
|
||||
expect(completion.endpoint).toBe('https://openrouter.ai/api/v1/chat/completions')
|
||||
expect(completion.body.response_format).toEqual({ type: 'json_schema', json_schema: { name: 'world', strict: true, schema: { type: 'object' } } })
|
||||
})
|
||||
|
||||
it('uses official DeepSeek JSON mode', () => {
|
||||
const completion = buildJsonCompletion({ aiProvider: 'deepseek', deepseekApiKey: 'key', deepseekModel: 'deepseek-v4-flash' }, request)
|
||||
expect(completion.endpoint).toBe('https://api.deepseek.com/chat/completions')
|
||||
expect(completion.body.response_format).toEqual({ type: 'json_object' })
|
||||
expect(completion.body['thinking']).toEqual({ type: 'disabled' })
|
||||
})
|
||||
|
||||
it('rejects missing credentials and malformed responses', () => {
|
||||
expect(() => resolveAiProvider({ aiProvider: 'deepseek' })).toThrow('DEEPSEEK_API_KEY')
|
||||
expect(() => parseJsonCompletion({ choices: [] })).toThrow()
|
||||
expect(parseJsonCompletion({ choices: [{ message: { content: '{"ok":true}' } }] })).toEqual({ ok: true })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user