Files
knowledge-governance/web/src/qaOps.test.ts
T

67 lines
2.9 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { fetchQARecords, parseQARecord, parseQARecordPage } from './qaOps'
export const fixtureRecord = {
id: 'test-turn',
status: 'succeeded',
question: 'How to configure CR5?',
answer: '当前暂无已发布的权威知识,暂不建议操作。',
asked_at: '2026-10-14T03:00:00Z',
time_source: 'request_message.created_at',
asker: '测试人员',
trigger_person: '回调昵称',
department: '',
region: '',
references: [{ title: '测试引用' }],
media: [],
source_coverage: { verdict: 'covered', reason_code: 'covered_has_references', evaluator_version: 'v5' },
feedback: { asker_value: 'down', up: 0, down: 1 },
coverage: {
state: 'checked',
verdict: 'uncovered',
rule_version: 'governance-answer-substring-v1',
matched_text: '暂无已发布',
},
}
const fixturePage = {
items: [fixtureRecord],
columns: ['answer', 'coverage', 'asked_at', 'asker', 'question', 'trigger_person', 'feedback'],
total: 1,
has_more: false,
next_cursor: '',
as_of: '2026-10-14T04:00:00Z',
}
afterEach(() => vi.unstubAllGlobals())
describe('QA operations data contract', () => {
it('keeps governance coverage separate from source coverage and downvotes', () => {
const item = parseQARecord(fixtureRecord)
expect(item.coverage.verdict).toBe('uncovered')
expect(item.source_coverage.verdict).toBe('covered')
expect(item.feedback.asker_value).toBe('down')
expect(parseQARecordPage(fixturePage).columns).not.toContain('department')
})
it('rejects malformed rows and pagination rather than rendering an empty success', () => {
expect(() => parseQARecord({ ...fixtureRecord, answer: null })).toThrow('响应无效')
expect(() => parseQARecordPage({ ...fixturePage, has_more: true })).toThrow('响应无效')
expect(() => parseQARecordPage({ ...fixturePage, columns: ['unapproved_secret_column'] })).toThrow('响应无效')
})
it('uses only the governance API and preserves keyword text', async () => {
const mocked = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: fixturePage }), { status: 200 }))
vi.stubGlobal('fetch', mocked)
await fetchQARecords({ start: '2026-10-14T00:00:00Z', end: '2026-10-15T00:00:00Z', keyword: '%_CR5', cursor: '' })
const url = String(mocked.mock.calls[0][0])
expect(url.startsWith('/api/v1/qa-ops/records?')).toBe(true)
expect(new URL(url, 'http://localhost').searchParams.get('keyword')).toBe('%_CR5')
expect(url).not.toContain('tenant')
})
it('surfaces source errors, never silently replacing them with no questions', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: '只读权限校验未通过' }), { status: 503 }))
)
await expect(fetchQARecords({ start: '', end: '', keyword: '', cursor: '' })).rejects.toThrow('只读权限校验未通过')
})
})