Files
knowledge-governance/web/src/QAOperations.test.tsx
T
姚章浩 0f89c82625
build / build (push) Successful in 1m7s
feat(qa): 详情面板顶部增加上一条和下一条
按当前筛选后的完整已载入顺序切换,边界自动禁用,后台续页到达后自动开放。
独立保存选中目标以支持加载及失败时切换,沿用取消/迟到响应隔离,关闭返回原表格焦点。
验证:详情首中尾/未知目标、加载/失败、100到101续页先红后绿;既有流水和看板范围回归、完整门禁及原生备份恢复通过。
2026-10-25 16:40:35 +08:00

326 lines
14 KiB
TypeScript

// @ts-expect-error The test runner provides Node; the browser bundle intentionally omits @types/node.
import { readFileSync } from 'node:fs'
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it, vi } from 'vitest'
import { QAOperationsTabs, QAOperationsView } from './QAOperations'
import type { QARecord, QARecordPage } from './qaOps'
import { restoreWorkspace } from './workspace'
const item: QARecord = {
id: 'fixture',
status: 'succeeded',
question: '<script>not executable</script>',
answer: '当前暂无已发布的权威知识。',
asked_at: '2026-10-14T03:00:00Z',
time_source: 'request_message.created_at',
asker: '测试人员',
trigger_person: '测试昵称',
department: '',
region: '',
references: [{ title: '测试引用' }],
media: [],
feedback: { asker_value: 'down', up: 0, down: 1 },
source_coverage: { verdict: 'covered', reason_code: 'covered_has_references', evaluator_version: 'v5' },
coverage: {
verdict: 'uncovered',
state: 'checked',
rule_version: 'governance-answer-substring-v1',
matched_text: '暂无已发布',
},
is_correct: false,
}
const page: QARecordPage = {
items: [item],
columns: ['answer', 'coverage', 'is_correct', 'asked_at', 'asker', 'question', 'feedback'],
total: 1,
has_more: false,
next_cursor: '',
as_of: '2026-10-14T04:00:00Z',
}
const props = {
page,
status: null,
loading: false,
error: null,
detail: null,
detailLoading: false,
detailError: null,
onSelect: vi.fn(),
onCloseDetail: vi.fn(),
onLoadMore: vi.fn(),
onRetry: vi.fn(),
}
describe('QA operations workbench', () => {
it('puts previous/next in the detail header and follows the entire filtered result, including loading and error states', () => {
const items = Array.from({ length: 101 }, (_, index) => ({ ...item, id: 'filtered-' + index }))
const cases = [
{ index: 0, previous: false, next: true },
{ index: 50, previous: true, next: true },
{ index: 100, previous: true, next: false },
{ index: -1, previous: false, next: false },
]
for (const state of ['ready', 'loading', 'failed']) {
for (const value of cases) {
const selected = items[value.index] ?? { ...item, id: 'not-in-this-filter' }
const html = renderToStaticMarkup(
<QAOperationsView
{...props}
page={{ ...page, items, total: items.length }}
detailID={selected.id}
detail={state === 'ready' ? selected : null}
detailLoading={state === 'loading'}
detailError={state === 'failed' ? '详情读取失败' : null}
/>
)
const drawer = html.slice(html.indexOf('<aside'))
const header = drawer.slice(0, drawer.indexOf('</header>'))
for (const [label, enabled] of [
['上一条', value.previous],
['下一条', value.next],
] as const) {
const button = header.match(new RegExp('<button([^>]*)>' + label + '</button>'))
expect(button, state + ': ' + selected.id + ': ' + label).not.toBeNull()
expect(button?.[1].includes('disabled=""')).toBe(!enabled)
}
}
}
})
it('enables next automatically when the background reader appends the next page', () => {
const items = Array.from({ length: 101 }, (_, index) => ({ ...item, id: 'filtered-' + index }))
for (const loaded of [100, 101]) {
const html = renderToStaticMarkup(
<QAOperationsView
{...props}
page={{ ...page, items: items.slice(0, loaded), total: 101, has_more: loaded < 101 }}
detail={items[99]}
loading={loaded < 101}
/>
)
const drawer = html.slice(html.indexOf('<aside'))
const button = drawer.slice(0, drawer.indexOf('</header>')).match(/<button([^>]*)>下一条<\/button>/)
expect(button).not.toBeNull()
expect(button?.[1].includes('disabled=""')).toBe(loaded === 100)
}
})
it('puts all five selectors in their own table header cells, including empty filtered results', () => {
const filtering = { filters: { coverage: '否' }, onFiltersChange: vi.fn() }
for (const value of [page, { ...page, items: [], total: 0, has_more: false }]) {
const html = renderToStaticMarkup(<QAOperationsView {...props} {...filtering} page={value} />)
const head = html.slice(html.indexOf('<thead>'), html.indexOf('</thead>'))
expect((head.match(/<select/g) ?? []).length).toBe(5)
for (const label of ['是否覆盖', '是否正确', '产品分类', '问题类型', '根因分析'])
expect(head).toContain('aria-label="筛选' + label + '"')
expect(html).toContain('清除字段筛选')
}
})
it('adds a leading serial column without changing business fields or virtual spacer geometry', () => {
const many = Array.from({ length: 200 }, (_, index) => ({ ...item, id: 'row-' + index }))
const html = renderToStaticMarkup(<QAOperationsView {...props} page={{ ...page, items: many, total: 200 }} />)
expect(html).toMatch(/<thead><tr><th[^>]*>序号<\/th><th>中文问题<\/th>/)
expect(html).toMatch(/data-qa-row="row-0"[^>]*><td[^>]*>1<\/td>/)
const rows = [...html.matchAll(/data-qa-row="row-(\d+)"[^>]*><td[^>]*>(\d+)<\/td>/g)]
expect(rows.length).toBeGreaterThan(1)
for (const row of rows) expect(Number(row[2])).toBe(Number(row[1]) + 1)
expect(html).toContain('colSpan="17"')
expect(html).toContain('aria-rowcount="201"')
})
it('keeps the first rows interactive while later batches load or fail', () => {
const prefix = { ...page, total: 2, has_more: true, next_cursor: 'next' }
const loading = renderToStaticMarkup(<QAOperationsView {...props} page={prefix} loading={true} />)
expect(loading).toContain('data-qa-row="fixture"')
expect(loading).toContain('后台加载中')
expect(loading).not.toContain('正在读取全部问答')
const failed = renderToStaticMarkup(<QAOperationsView {...props} page={prefix} error="明确续载失败" />)
expect(failed).toContain('未完整读取')
expect(failed).toContain('明确续载失败')
expect(failed).toContain('data-qa-row="fixture"')
})
it('edits five fields directly in the first row without a separate review or feedback form', () => {
const html = renderToStaticMarkup(
<QAOperationsView {...props} detail={{ ...item, record_origin: 'historical_excel' }} />
)
const drawer = html.slice(html.indexOf('<aside'))
expect((drawer.match(/<select/g) ?? []).length).toBe(5)
for (const copy of ['人工复核与留档', '保存复核', '复核理由', '用户反馈', '质量留档/人工复核未配置'])
expect(drawer).not.toContain(copy)
})
it('shows each persisted AI task in the existing operation cell and explains positive master coverage', () => {
const withTasks = {
...item,
background_tasks: [
{
id: 'w',
record_id: item.id,
kind: 'evaluation' as const,
state: 'running' as const,
stage: 'request',
attempt: 1,
error_code: '',
skip_reason: '',
started_at: '2026-10-19T00:00:00Z',
updated_at: '2026-10-19T00:00:01Z',
},
],
}
const detail = {
...item,
record_origin: 'dobotai_2_0' as const,
coverage: { state: 'checked' as const, verdict: 'covered' as const, rule_version: 'master-answer-substring-v2' },
}
const html = renderToStaticMarkup(
<QAOperationsView {...props} page={{ ...page, items: [withTasks] }} detail={detail} />
)
expect(html).toContain('字段分析:执行中')
expect(html).toContain('已覆盖:成功读取且非空答案未包含')
expect(html).toContain('字段结论与依据')
})
it('hides only the two requested advisory warnings, not real errors', () => {
const html = renderToStaticMarkup(
<QAOperationsView
{...props}
status={{
state: 'connected',
code: '',
warnings: ['database_temp_enabled', 'database_transport_unencrypted', 'fixture_other_warning'],
read_only: true,
source_version: '2.0',
channel: 'dingtalk',
}}
error="fixture real read failure"
/>
)
expect(html).not.toContain('数据库仍允许临时对象')
expect(html).not.toContain('数据库连接未启用 TLS')
expect(html).toContain('数据源有待审核的安全告警')
expect(html).toContain('fixture real read failure')
})
it('removes workflow personnel from details while preserving the record data', () => {
const detail = { ...item, trigger_person: JSON.stringify({ name: '示例姓名', unionId: 'fixture-union' }) }
const html = renderToStaticMarkup(<QAOperationsView {...props} detail={detail} />)
const drawer = html.slice(html.indexOf('<aside'))
expect(drawer).not.toContain('示例姓名')
expect(drawer).not.toContain('原始人员信息')
expect(drawer).not.toContain('fixture-union')
})
it('provides a row-level detail target and categorical color tags without adding business columns', () => {
const html = renderToStaticMarkup(<QAOperationsView {...props} />)
expect(html).toContain('data-qa-row="fixture"')
expect(html).toContain('双击行查看详情')
expect(html).toContain('qa-field-tag')
})
it('uses the four requested destinations in their fixed order', () => {
const html = renderToStaticMarkup(<QAOperationsTabs view="records" onChange={vi.fn()} />)
const labels = ['问题流水', '问题统计看板', '区域统计', '周报生成']
for (const label of labels) expect(html).toContain(label)
for (let i = 1; i < labels.length; i++) expect(html.indexOf(labels[i - 1])).toBeLessThan(html.indexOf(labels[i]))
expect(html).not.toContain('部门/区域保留源快照')
})
it('removes explanatory banners without hiding errors or any required column', () => {
const html = renderToStaticMarkup(<QAOperationsView {...props} />)
expect(html).not.toContain('本次读取')
expect(html).not.toContain('固定显示完整字段')
expect(html).not.toContain('DobotAI 2.0 / 钉钉')
})
it('shows all required columns even when empty and keeps false/downvotes visible', () => {
const html = renderToStaticMarkup(<QAOperationsView {...props} />)
expect(html).toContain('原始问题')
expect(html.indexOf('AI答案')).toBeLessThan(html.indexOf('是否覆盖'))
expect(html.indexOf('是否正确')).toBeLessThan(html.indexOf('提问时间'))
expect(html).toContain('否(点踩)')
expect(html).toContain('是否正确')
expect(html).toContain('<th>部门</th>')
expect([...html.matchAll(/<th>(.*?)<\/th>/g)].slice(0, 14).map((match) => match[1])).toEqual([
'中文问题',
'AI答案',
'是否覆盖',
'是否正确',
'产品分类',
'问题类型',
'根因分析',
'提问时间',
'提问人',
'原始问题',
'语言',
'流程触发人员信息',
'部门',
'区域',
])
expect(html).toContain('<th>产品分类</th>')
expect(html).not.toContain('<script>not executable</script>')
})
it('never shows a load-more control for the all-records table', () => {
const html = renderToStaticMarkup(
<QAOperationsView {...props} page={{ ...page, total: 2, has_more: true, next_cursor: 'next' }} />
)
expect(html).not.toContain('加载更多')
expect(html).not.toContain('空列按整个筛选结果隐藏')
})
it('distinguishes an unavailable source from a successful empty query', () => {
const html = renderToStaticMarkup(<QAOperationsView {...props} page={null} error="只读源不可用" />)
expect(html).toContain('role="alert"')
expect(html).toContain('只读源不可用')
expect(html).not.toContain('没有符合条件的问答')
})
it('shows complete source and governance evidence separately in the detail drawer', () => {
const html = renderToStaticMarkup(<QAOperationsView {...props} detail={item} />)
expect(html).toContain('role="dialog"')
expect(html).toContain('暂无已发布')
expect(html).toContain('源端覆盖(仅对照)')
expect(html).toContain('covered_has_references')
expect(html).toContain('测试引用')
})
it('restores the existing singleton workspace tab without creating another application', () => {
const restored = restoreWorkspace(JSON.stringify({ openTabs: ['dashboard', 'qaops'], activeTab: 'qaops' }))
expect(restored.openTabs).toEqual(['dashboard', 'qaops'])
expect(restored.activeTab).toBe('qaops')
})
})
it('opens a modeless right drawer with fixed leading quality fields and no redundant organization/personnel details', () => {
const html = renderToStaticMarkup(<QAOperationsView {...props} detail={{ ...item, record_origin: 'dobotai_2_0' }} />)
const drawer = html.slice(html.indexOf('<aside'))
expect(html).not.toContain('qa-ops-backdrop')
expect(drawer).toContain('aria-modal="false"')
expect(drawer).toContain('qa-detail-quality')
const labels = ['是否覆盖', '是否正确', '产品分类', '问题类型', '根因分析']
for (const label of labels) expect(drawer).toContain(label)
expect(drawer.indexOf('根因分析')).toBeLessThan(drawer.indexOf('原始问题'))
for (const label of [
'<dt>区域</dt>',
'当前组织关联',
'当前通讯录部门/区域',
'组织版本',
'源端组织快照',
'记录来源',
'问答状态',
'流程触发人员信息',
'原始人员信息',
])
expect(drawer).not.toContain(label)
})
it('names the ledger explicitly as 问题流水表', () => {
const html = renderToStaticMarkup(<QAOperationsTabs view="records" onChange={vi.fn()} />)
expect(html).toContain('>问题流水表</button>')
})
it('styles filters inside the header instead of reserving a separate query row', () => {
const css = readFileSync(new URL('./qaOps.css', import.meta.url), 'utf8')
expect(css).toContain('.qa-header-field-filter select')
expect(css).not.toContain('.qa-field-filters')
})
it('uses matching query/sync action sizing and keeps synchronization status outside the button baseline', () => {
const css = readFileSync(new URL('./qaOps.css', import.meta.url), 'utf8')
expect(css).toContain('.qa-ops-query .qa-query-action')
expect(css).toMatch(/\.qa-ops-query \.qa-query-action\s*\{[^}]*width:\s*96px/)
expect(css).toMatch(/\.qa-ops-query \.qa-mirror-sync-line\s*\{[^}]*margin-bottom:\s*0/)
})