import { describe, it, expect, vi, afterEach } from 'vitest' import { parseAISettings, parseAIBatch, aiBatchProgress, parseAIStatus, parseAIWorkState, parseAIPreview, parseAIEvidence, } from './qaAITypes' const settings = { revision: 1, base_url: 'https://model.example.invalid/v1', model: 'fixture', key_configured: true, verification: 'verified', checked_at: '2026-10-19T00:00:00Z', automatic_evaluation: false, automatic_translation: false, storage_ready: true, } const batch = { id: 'fixture', kind: 'evaluation', state: 'active', total: 4, succeeded: 2, failed: 1, skipped: 1, running: 0, queued: 0, retry: 0, paused: 0, created_at: '2026-10-19T00:00:00Z', } describe('AI safe settings and honest batch accounting', () => { it('accepts only a write-only key status and rejects secret-bearing settings responses', () => { expect(parseAISettings(settings).key_configured).toBe(true) expect(() => parseAISettings({ ...settings, api_key: 'fixture-only' })).toThrow('响应无效') }) it('counts all terminal outcomes as finished without equating 100% with success', () => { const parsed = parseAIBatch(batch) expect(aiBatchProgress(parsed)).toEqual({ finished: 4, percent: 100 }) expect(parsed.failed).toBe(1) expect(() => parseAIBatch({ ...batch, total: 5 })).toThrow('响应无效') expect( aiBatchProgress(parseAIBatch({ ...batch, total: 0, succeeded: 0, failed: 0, skipped: 0 })).percent ).toBeNull() }) }) it('strictly accepts real task/evidence metadata and refuses partial preview counts', () => { const work = { id: 'work', record_id: 'record', kind: 'evaluation', state: 'running', stage: 'request', attempt: 1, error_code: '', skip_reason: '', started_at: '2026-10-19T00:00:00Z', updated_at: '2026-10-19T00:00:01Z', } const status = { settings, worker_ready: true, queues: [{ kind: 'evaluation', queued: 0, running: 1, retry: 0, failed: 0, paused: 0, succeeded: 0, skipped: 0 }], batches: [], recent_tasks: [work], recent_tasks_limited: false, } expect(parseAIStatus(status).recent_tasks[0].stage).toBe('request') expect(() => parseAIWorkState({ ...work, state: 'invented' })).toThrow('响应无效') const preview = { id: 'preview', kind: 'evaluation', scope_total: 4, eligible: 3, skipped: 1, config_revision: 1, expires_at: '2026-10-19T00:10:00Z', } expect(parseAIPreview(preview).eligible).toBe(3) expect(() => parseAIPreview({ ...preview, eligible: 2 })).toThrow('响应无效') const evidence = { ...work, state: 'succeeded', input_hash: 'a'.repeat(64), config_revision: 1, prompt_version: 'fixture', dictionary_version: 'fixture', actual_model: 'fixture', result: { fields: { product_category: { value: 'MG400', basis: 'semantic_only', reason: '原问题含型号', quotes: [{ source: 'question', text: 'MG400', reference_index: 0 }], }, }, }, applied_fields: { product_category: 'MG400' }, skipped_fields: {}, } expect(parseAIEvidence(evidence).result.fields?.product_category.reason).toBe('原问题含型号') }) afterEach(() => vi.unstubAllGlobals()) it('saves through the real admin API contract with explicit intent and no key on keep', async () => { const { saveAISettings } = await import('./qaAI') const fetcher = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: settings }), { status: 200 })) vi.stubGlobal('fetch', fetcher) const result = await saveAISettings({ base_revision: 1, base_url: settings.base_url, model: settings.model, key_action: 'keep', }) expect(result.key_configured).toBe(true) const [path, request] = fetcher.mock.calls[0] expect(path).toBe('/api/v1/qa-ops/ai/settings') expect(request.headers['X-Governance-Action']).toBe('qa-ai') const body = JSON.parse(request.body) expect(body.key_action).toBe('keep') expect(body).not.toHaveProperty('api_key') expect(body).not.toHaveProperty('actor') })