Files
knowledge-governance/web/src/WeKnoraMonitor.test.tsx
T
姚章浩 a1b46d2b7c feat(monitor): 需下架车道可逐条移除测试库知识 —— 补齐最后一个没有页面入口的流程
背景:来源侧被判「禁止导入」后,已建好的测试库知识不会自动消失;此前只有变更集
删除路径会入队 test.remove,页面既列不出条目也没有按钮,运维无法闭环(车道只有按
库计数)。

新增:
- GET /api/v1/test-publications?removal=required:条目级列表,判据与车道计数同源
  (superseded_at IS NULL / 有 knowledge_id / completed / retrieval_state NOT IN
  (disabling,disabled) / policy_state='blocked'),过滤是叠加而不是替换。
- POST /api/v1/test-publications/{id}/removal:engineer 级写路由,要求 Idempotency-Key,
  按条目幂等(operator-remove:<id>);置 disabling 并交既有 test.remove 工作流去删除
  远端知识并读回校验。错误映射 404/409(已移除)/409(不可移除)/501(未接线)。
- 界面:需下架车道逐条显示条目 + 「下架(从测试库移除)」,页内二次确认,成功提示,
  完成后计数归零。

修掉过程中踩到的两个真问题:
1. 二次确认的确认态原先经容器 props 传给视图,一次容器改动把三个 props 丢了 ——
   视图永远渲染原按钮、确认步骤静默消失,而所有静态单元测试仍然通过。改为把确认态
   放在视图内部;并把「取消」放在原按钮位置(真实指针连点两下只会落到取消)。
2. jobs 上有部分唯一索引 UNIQUE (business_change_set_id, business_version_id,
   business_target_kb_id, business_action) WHERE business_change_set_id IS NOT NULL。
   本路径给这三列写空串(非 NULL),('','','','remove') 对所有条目是同一个键 ——
   第一条之后每次 INSERT 都撞唯一索引、被 ON CONFLICT DO NOTHING 静默吞掉,留下
   「状态已 disabling、没有任务」的永久卡死条目,而车道恰好隐藏 disabling,运维也
   看不到。改为写 NULL(语义:无变更集身份)+ 冲突目标收敛到 idempotency_key +
   校验 RowsAffected()(0 行回查既有任务;查不到就报错,不提交无任务的 disabling)。
   卡死状态在重试时可自愈。

测试:internal/platform/postgres/test_publication_removal_integration_test.go(两条发布
各自入队、business_* 为 NULL、卡死自愈;把修复退回旧写法该测试即 FAIL,已实测)、
internal/httpapi/test_publication_removal_test.go(权限/幂等键/错误映射/未接线 501/列表
过滤)、前端 testPublications.test.ts + WeKnoraMonitor.test.tsx。

验收(纯界面,两条都走完):解析监控 → 需下架(2)→ 逐条「下架」+ 二次确认 →
retrieval_state=disabled(PDF 03:57:03Z、md 04:08:55Z),jobs completed(attempt 2,
首次 test_remove_pending=远端仍在处理,重试通过;工作流以 GetKnowledge 读回 404 判
成功)→ 车道需下架(0)。核验「确认前不执行」:首次点击后仍 unpublished、移除任务数仍 1。
2026-10-10 12:10:46 +08:00

100 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { renderToStaticMarkup } from 'react-dom/server'
import { describe,expect,it,vi } from 'vitest'
import { WeKnoraMonitorView } from './WeKnoraMonitor'
import type { MonitorFilePage,MonitorOverview,RunMetrics } from './monitor'
import type { TestPublicationPage } from './testPublications'
const overview:MonitorOverview={mode:'simulation',simulated:true,observed_at:'2026-09-08T12:00:00Z',host:{cpu_percent:42,memory_used_bytes:19971597926,memory_total_bytes:34359738368,disk_used_bytes:128849018880,disk_total_bytes:536870912000,gpu_state:'unknown',gpu_detail:'未接入真实GPU监控',simulated:true},services:[{key:'weknora',name:'WeKnora API',state:'healthy',detail:'模拟连接正常',latency_ms:86,observed_at:'2026-09-08T12:00:00Z',simulated:true},{key:'mineru',name:'MinerU',state:'healthy',detail:'模拟处理中2,队列5',latency_ms:24,observed_at:'2026-09-08T12:00:00Z',simulated:true},{key:'postgres',name:'PostgreSQL',state:'unknown',detail:'未接入真实监控',latency_ms:0,observed_at:'2026-09-08T12:00:00Z',simulated:true},{key:'redis',name:'Redis / MQ',state:'unknown',detail:'未接入真实监控',latency_ms:0,observed_at:'2026-09-08T12:00:00Z',simulated:true},{key:'oss',name:'OSS',state:'unknown',detail:'未接入真实监控',latency_ms:0,observed_at:'2026-09-08T12:00:00Z',simulated:true}],knowledge_bases:[{id:'kb-sim',name:'公司级技术支持知识库',visibility:'mixed',imported:5750,completed:4530,pending:450,processing:50,finalizing:714,failed:6,cancelled:0,planned:120,needs_removal:3,stale_planned:0,internal_imported:4200,internal_completed:3300,external_imported:1550,external_completed:1230}],pipeline:{fetch_pending:4,fetch_failed:1,fetch_missing:0,concurrency:2},health:[]}
const files:MonitorFilePage={items:[{name:'[模拟] 产品参数总表.xlsx',directory:'02|产品资料',visibility:'external',status:'finalizing',stage_detail:'基础检索可用,Wiki与图谱仍在生成',elapsed_seconds:2100,error_code:''}],total:1,limit:20,offset:0,sample_only:true}
const metrics:RunMetrics={observed_at:'2026-09-12T00:00:00Z',active_workers:2,worker_source:'pg_lease',queue:{pending:3,running:4,retry:1,failed:2,oldest_pending_age_seconds:120},short_window:{window_seconds:3600,jobs_started:50,jobs_completed:48,jobs_failed:6,success_rate:88.9,failure_rate:11.1,duration_avg_seconds:55,duration_p50_seconds:30,duration_p95_seconds:1800},long_window:{window_seconds:86400,jobs_started:500,jobs_completed:482,jobs_failed:18,success_rate:96.4,failure_rate:3.6,duration_avg_seconds:70,duration_p50_seconds:45,duration_p95_seconds:2400}}
const queue:TestPublicationPage={items:[{id:'tp-plan',title:'API.docx',ingest_state:'pending',processing_state:'planned',retrieval_state:'unpublished',target_scope: 'internal', change_set_id: '', change_type:'added',can_start:true,retry_count:0,last_error:'',can_retry:false,updated_at:'2026-09-09T10:01:00Z'},{id:'tp-fetch',title:'焊接手册.pdf',ingest_state:'pending',processing_state:'planned',retrieval_state:'unpublished',target_scope:'external',change_set_id:'',change_type:'added',can_start:false,retry_count:0,last_error:'',can_retry:false,updated_at:'2026-09-09T10:02:00Z'},{id:'tp-failed',title:'CR20A.pdf',ingest_state:'uploaded',processing_state:'failed',retrieval_state:'unpublished',target_scope: 'external', change_set_id: '', change_type:'updated',can_start:false,retry_count:1,last_error:'remote_parse_failed',can_retry:true,updated_at:'2026-09-09T10:00:00Z'}],total:3,limit:50,offset:0}
const plans=queue.items.filter((item)=>item.can_start)
const baseProps={overview,files,queue,plans,selectedKB:overview.knowledge_bases[0],status:'' as const,queueStatus:'' as const,queueScope:'' as const,loading:false,error:null as string|null,retryingIDs:[] as string[],starting:false,startMessage:'',onSelectKB:vi.fn(),onStatus:vi.fn(),onQueueStatus:vi.fn(),onQueueScope:vi.fn(),lane:'queue',onLaneChange:vi.fn(),metrics,onRetry:vi.fn(),onBatchRetry:vi.fn(),onStart:vi.fn(),onRefresh:vi.fn()}
describe('WeKnoraMonitorView',()=>{
it('filters the processing queue by target knowledge base and shows each task target',()=>{
const html=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} queueScope="internal" onQueueScope={vi.fn()} />)
expect(html).toContain('aria-label="队列目标知识库"')
expect(html).toContain('对内知识库</option>')
expect(html).toContain('对外知识库</option>')
expect(html).toContain('<option value="internal" selected="">对内知识库')
expect(html).toContain('目标知识库')
expect(html).toContain('scope-badge internal">对内')
expect(html).toContain('scope-badge external">对外')
})
it('treats the catalog annotation as the only decision: 待入库 lists plans with one 开始处理 button and no reason form',()=>{
const waiting=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane="waiting" onStart={vi.fn()} />)
expect(waiting).toContain('待入库(2)')
expect(waiting).toContain('开始处理(1)')
expect(waiting).toContain('并发上限(当前 2)')
expect(waiting).not.toContain('确认原因')
expect(waiting).not.toContain('操作人<input')
expect(waiting).toContain('正在从源头取回文件')
})
it('shows knowledge-base removal demand from blocked-but-live items',()=>{
const removal=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane="removal" />)
expect(removal).toContain('需下架(3)')
expect(removal).toContain('3 份仍在检索中')
const withItems = renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane="removal" removalItems={[{ id: 'tp-live', title: '相机线束固定孔位尺寸图.pdf', target_scope: 'internal', processing_state: 'completed', ingest_state: 'uploaded', retrieval_state: 'unpublished', source_item_id: 'si-1', source_item_version_id: 'siv-1', knowledge_id: 'knowledge-1', updated_at: '2026-10-10T03:00:00Z', created_at: '2026-10-10T02:00:00Z' } as never]} /> as never)
expect(withItems).toContain('相机线束固定孔位尺寸图.pdf')
expect(withItems).toContain('下架(从测试库移除)')
// 确认态在视图内部(不依赖容器传参):初始渲染必须是普通按钮,且不得出现确认/取消,
// 避免"点一下就直接执行"的降级。
expect(withItems).toContain('下架(从测试库移除)')
expect(withItems).not.toContain('确认下架(从测试库移除)')
})
it('shows inbox lanes for the knowledge-base diff view',()=>{
const attention=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='attention' />)
for(const text of ['需要你出手','CR20A.pdf','remote_parse_failed','失败阶段 文档解析','批量重试所选(1)','死信(重试次数用完,系统不再自动处理)','源头取件”=从钉钉/AList'])expect(attention).toContain(text)
const withFailures=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='attention' failures={[{id:'jf-1',job_id:'job-1',job_type:'artifact.fetch',error_code:'handler_error',attempt:8,max_attempts_at_failure:8,state:'open',created_at:'2026-09-15T01:52:53Z',doc_name:'ATOM整机出厂测试.adoc',last_error:'artifact_fetch_retryable: source_unavailable'}]} />)
expect(withFailures).toContain('「ATOM整机出厂测试.adoc」')
expect(withFailures).toContain('来源服务当时不可达')
const inflight=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='inflight' />)
for(const text of ['处理中','知识库解析进度','已导入 5750','待入库 120','对内 3300 / 4200','对外 1230 / 1550','源头取件 → Hash'])expect(inflight).toContain(text)
const queueView=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='queue' />)
expect(queueView).toContain('全部任务(3)')
expect(queueView).toContain('重试')
expect(queueView).toContain('1小时·成功')
expect(queueView).toContain('88.9%')
const nav=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='waiting' />)
for(const text of ['待入库','需人工处理','需下架','处理中','已完成','全部任务'])expect(nav).toContain(text)
})
it('does not show simulated success totals after a load error without data',()=>{
const html=renderToStaticMarkup(<WeKnoraMonitorView overview={null} files={null} selectedKB={null} status="" queueStatus="" queueScope="" loading={false} error="加载失败" onSelectKB={vi.fn()} onStatus={vi.fn()} onQueueStatus={vi.fn()} onQueueScope={vi.fn()} onRefresh={vi.fn()} />)
expect(html).toContain('加载失败')
expect(html).not.toContain('completed 0')
})
it('keeps attention items visible when they are outside the first queue page',()=>{
const firstPage:TestPublicationPage={items:[queue.items[0]],total:115,limit:50,offset:0}
const attentionItem=queue.items[2]
const html=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} queue={firstPage} attention={[attentionItem]} lane='attention' />)
expect(html).toContain('需要你出手(1)')
expect(html).toContain('CR20A.pdf')
expect(html).toContain('批量重试所选(1)')
})
it('offers to page through the full queue when more rows exist',()=>{
const firstPage:TestPublicationPage={items:queue.items,total:115,limit:50,offset:0}
const html=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} queue={firstPage} lane='queue' onLoadMore={vi.fn()} />)
expect(html).toContain('已显示 3 / 共 115')
expect(html).toContain('加载更多')
})
it('offers 重新入队 for a 源头取件 dead letter that carries no publication id',()=>{
// 取件死信没有 publication_id,所以不能走发布重试链路;没有这个按钮,一个终态
// 取件失败(例如钉钉会话过期)在界面上就永远救不回来。
const failures=[{id:'jf-fetch',job_id:'job-fetch',job_type:'artifact.fetch',error_code:'artifact_invalid',attempt:1,max_attempts_at_failure:8,state:'open' as const,created_at:'2026-10-08T03:20:35Z',doc_name:'关闭PC2远程桌面弹窗.docx',last_error:'artifact_fetch_rejected: http_401'}]
const html=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='attention' failures={failures} onRequeueFailure={vi.fn()} />)
expect(html).toContain('源头取件')
expect(html).toContain('关闭PC2远程桌面弹窗.docx')
expect(html).toContain('重新入队')
const busy=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='attention' failures={failures} requeueingIDs={['jf-fetch']} />)
expect(busy).toContain('重排中…')
expect(busy).not.toContain('>重新入队<')
})
it('does not offer 重新入队 for a 上传解析 dead letter without a publication id',()=>{
const failures=[{id:'jf-upload',job_id:'job-upload',job_type:'test.upload',error_code:'handler_error',attempt:8,max_attempts_at_failure:8,state:'open' as const,created_at:'2026-10-08T03:20:35Z',last_error:'remote_parse_failed'}]
const html=renderToStaticMarkup(<WeKnoraMonitorView {...baseProps} lane='attention' failures={failures} />)
expect(html).toContain('上传解析')
// 段落提示语里始终有“重新入队=再给它一次机会”,所以断言按钮本身不存在。
expect(html).not.toContain('>重新入队</button>')
})
})