- Migration: app_settings table with admin-only RLS (ANTHROPIC_API_KEY, VOYAGE_API_KEY) - lib/settings.ts: getApiKey() reads DB first, falls back to env var - lib/claude/client.ts: factory createAnthropicClient(apiKey) replaces singleton - lib/claude/embed.ts: optional apiKey param, falls back to env - 3 Claude AI routes + similar route: fetch key from settings before calling AI - incidents/route.ts: fire-and-forget embed reads VOYAGE key from settings - GET/POST /api/settings: admin-only masked key management endpoint - /hse/settings page + ApiKeyForm client component Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FFDuBhMKvoWjrWT3ZnGmmr
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest'
|
|
|
|
vi.mock('@/lib/supabase/server', () => ({
|
|
createClient: vi.fn().mockResolvedValue({
|
|
auth: {
|
|
getUser: vi.fn().mockResolvedValue({ data: { user: { id: 'user-1' } }, error: null }),
|
|
},
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/lib/claude/client', () => ({
|
|
createAnthropicClient: vi.fn().mockReturnValue({
|
|
messages: {
|
|
create: vi.fn().mockResolvedValue({
|
|
content: [{
|
|
type: 'tool_use',
|
|
name: 'assess_quality',
|
|
input: { score: 8, passes: true, feedback: 'Clear description.', suggestions: [] },
|
|
}],
|
|
}),
|
|
},
|
|
}),
|
|
}))
|
|
|
|
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 Claude', 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')
|
|
})
|
|
})
|