- lib/claude/client.ts: replace Anthropic SDK with openai package pointed at DeepSeek baseURL
- 4 AI routes: port tool definitions, tool_choice, and output parsing to OpenAI function-calling format
- Drop thinking:{type:'adaptive'} (no DeepSeek equivalent); model string → deepseek-chat
- settings/route.ts: add DEEPSEEK_API_KEY to ALLOWED_KEYS
- migration: seed DEEPSEEK_API_KEY placeholder row in app_settings
- tests: update 3 AI route tests to mock createDeepSeekClient + OpenAI response shape
Voyage AI embedding path untouched (DeepSeek has no embeddings endpoint).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CPf5Rc8QPx6V8KLEEgfKEQ
76 lines
2.4 KiB
TypeScript
76 lines
2.4 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest'
|
|
|
|
const qualityOutput = { score: 8, passes: true, feedback: 'Clear description.', suggestions: [] }
|
|
|
|
vi.mock('@/lib/supabase/server', () => ({
|
|
createClient: vi.fn().mockResolvedValue({
|
|
auth: {
|
|
getUser: vi.fn().mockResolvedValue({ data: { user: { id: 'user-1' } }, error: null }),
|
|
},
|
|
from: vi.fn().mockReturnValue({
|
|
select: vi.fn().mockReturnThis(),
|
|
eq: vi.fn().mockReturnThis(),
|
|
gte: vi.fn().mockResolvedValue({ count: 0 }),
|
|
}),
|
|
rpc: vi.fn().mockResolvedValue({ error: null }),
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/lib/claude/client', () => ({
|
|
createDeepSeekClient: vi.fn().mockReturnValue({
|
|
chat: {
|
|
completions: {
|
|
create: vi.fn().mockResolvedValue({
|
|
choices: [{
|
|
message: {
|
|
tool_calls: [{
|
|
type: 'function',
|
|
function: {
|
|
name: 'assess_quality',
|
|
arguments: JSON.stringify(qualityOutput),
|
|
},
|
|
}],
|
|
},
|
|
}],
|
|
}),
|
|
},
|
|
},
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/lib/settings', () => ({
|
|
getApiKey: vi.fn().mockResolvedValue('sk-test-key'),
|
|
}))
|
|
|
|
describe('POST /api/incidents/ai/quality-check', () => {
|
|
it('returns 422 when description is missing', async () => {
|
|
const { POST } = await import('@/app/api/incidents/ai/quality-check/route')
|
|
const req = new Request('http://localhost/api/incidents/ai/quality-check', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ incident_type: 'injury' }),
|
|
})
|
|
const res = await POST(req as never)
|
|
expect(res.status).toBe(422)
|
|
})
|
|
|
|
it('returns quality assessment from DeepSeek', async () => {
|
|
const { POST } = await import('@/app/api/incidents/ai/quality-check/route')
|
|
const req = new Request('http://localhost/api/incidents/ai/quality-check', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
description: 'Forklift operator collided with racking in zone B at 14:30, injuring left arm.',
|
|
incident_type: 'injury',
|
|
}),
|
|
})
|
|
const res = await POST(req as never)
|
|
expect(res.status).toBe(200)
|
|
const body = await res.json()
|
|
expect(body).toHaveProperty('score')
|
|
expect(body).toHaveProperty('passes')
|
|
expect(body).toHaveProperty('feedback')
|
|
expect(body).toHaveProperty('suggestions')
|
|
})
|
|
})
|