Files
adminandClaude Sonnet 4.6 d10c690c12 feat: switch embeddings from Voyage AI to Google Gemini text-embedding-004
- embedText: call Gemini REST API (768-dim) instead of Voyage (1024-dim)
- Migration: drop+recreate incidents.embedding as vector(768), update
  match_incidents function, swap VOYAGE_API_KEY → GOOGLE_AI_API_KEY in app_settings
- Settings UI: relabel to "Google AI API Key (Embeddings)"
- All call sites updated (incidents POST, similar GET)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CPf5Rc8QPx6V8KLEEgfKEQ
2026-07-13 06:50:35 +08:00

38 lines
1.3 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
describe('embedText', () => {
beforeEach(() => {
process.env.GOOGLE_AI_API_KEY = 'test-key'
})
afterEach(() => {
vi.restoreAllMocks()
})
it('returns 768-element embedding array', async () => {
const mockEmbedding = Array.from({ length: 768 }, (_, i) => i / 768)
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({ embedding: { values: mockEmbedding } }),
}))
const { embedText } = await import('@/lib/claude/embed')
const result = await embedText('forklift hit racking in zone B')
expect(result).toHaveLength(768)
expect(result[0]).toBeCloseTo(0)
expect(result[767]).toBeCloseTo(767 / 768)
})
it('throws on non-ok response', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false, status: 401 }))
const { embedText } = await import('@/lib/claude/embed')
await expect(embedText('test')).rejects.toThrow('Gemini embed failed: 401')
})
it('throws when GOOGLE_AI_API_KEY is missing', async () => {
delete process.env.GOOGLE_AI_API_KEY
vi.resetModules()
const { embedText } = await import('@/lib/claude/embed')
await expect(embedText('test')).rejects.toThrow('GOOGLE_AI_API_KEY')
})
})