import { renderToStaticMarkup } from 'react-dom/server' import { describe, expect, it, vi } from 'vitest' import { ReleaseTargetsView, libraryPollingActive } from './ReleaseTargets' import type { LibraryCaptureJob, LibraryJobRecord, LibraryMaintenanceView, LibraryOverview, LibrarySnapshot, LibrarySnapshotList, LibraryStageRow, } from './librarySnapshots' const internalSnapshot: LibrarySnapshot = { snapshot_id: 'f992d64ea45bf737a0721a465b5aef2b150ef7757157eb84f3aaf8b6972d98f9', scope: 'internal', knowledge_base_id: '0c84a098-d3ed-4b82-a165-e6de9691d6a8', tenant_id: 10000, created_at: '2026-10-28T03:34:57.314178223Z', validation_policy: 'artifact-acceptance-v1', format: 'weknora-library-snapshot/v1', compatibility: { weknora_version: 'v0.7.1', schema_version: 74, retriever_engine: 'elasticsearch_v8', embedding_model: 'text-embedding-v4', embedding_dimension: 1024, index_mapping_sha256: 'a'.repeat(64), }, parts: [ { kind: 'database', name: 'knowledges', sha256: 'b'.repeat(64), count: 4, bytes: 10654 }, { kind: 'index', name: 'chunks', sha256: 'c'.repeat(64), count: 122, bytes: 1624880 }, ], logical_bytes: 55309044, unique_bytes: 55309040, shared_bytes: 2, totals: { documents: 4, chunks: 38, index_documents: 122, resources: 21, wiki_pages: 31 }, } const externalSnapshot: LibrarySnapshot = { ...internalSnapshot, snapshot_id: '01343b0b310b3977c2fdda05f5b8a6272148c4f7bfa124a6c5944123cf0c51c3', scope: 'external', created_at: '2026-10-28T03:35:53.000000000Z', totals: { documents: 0, chunks: 0, index_documents: 0, resources: 0, wiki_pages: 1 }, } const runningJob: LibraryCaptureJob = { job_id: 'library-capture-internal-1', capture_job: 'lib-internal-20261028T033457', scope: 'internal', state: 'running', attempt: 1, last_error: '', requested_by: 'yaozhanghao', created_at: '2026-10-28T03:34:50Z', updated_at: '2026-10-28T03:34:55Z', } const page: LibrarySnapshotList = { available: true, reason: '', archive_root: '/var/lib/knowledge-governance/library', snapshots: [internalSnapshot, externalSnapshot], versions: 2, content_bytes: 55309693, blob_file_bytes: 55309693, blob_allocated_bytes: 55373824, unreferenced_blobs: 0, unreferenced_bytes: 0, manifest_file_bytes: 7544, manifest_allocated_bytes: 12288, capture_note: '', capture_available: true, capture_reason: '', } // 真实接口形状:生产段未知、闸口未开、没有持久作业。 const overview: LibraryOverview = { scope: 'internal', test_sealed: internalSnapshot, production: { state: 'unknown', reason: '还没有生产验证通过的生效版本记录' }, prebackup: { state: 'unknown', reason: '本次目标快照还没有生产预备份' }, diff: { state: 'unknown', reason: '生产与测试的文档级差异读取未接线', added: [], updated: [], removed: [], }, jobs: [], gate: { sync_allowed: false, reasons: [ '生产写入闸口关闭(PRODUCTION_RELEASE_REQUESTS_ENABLED=false)', '没有已核验的生产恢复演练证据(PRODUCTION_BACKUP_RESTORE_VERIFIED=false)', '目标非空且没有生效版本记录:先建立目标权威基线(维护隔离下捕获整库并逐类校验),或确认目标确实是空库', '本次目标快照还没有生产预备份', ], }, authorization: { state: 'none', reason: '没有与目标快照匹配的有效发布授权' }, // 测试夹具把两份归档登记为测试封存:默认筛选「已登记版本」会隐藏未登记文件, // 但仍可由用户切到「全部归档」显式查看。 versions: [ { id: 'ver-internal', kind: 'test_sealed', snapshot_id: internalSnapshot.snapshot_id, version_label: '封存 f992d64ea45b', source_job_id: '', created_at: internalSnapshot.created_at, }, { id: 'ver-external', kind: 'test_sealed', snapshot_id: externalSnapshot.snapshot_id, version_label: '封存 01343b0b310b', source_job_id: '', created_at: externalSnapshot.created_at, }, ], capture_available: true, now: '2026-10-28T03:40:00Z', } const persistedSeal: LibraryJobRecord = { id: 'libseal-lib-internal-20261028T033457', scope: 'internal', kind: 'seal', state: 'running', stage: 'export', request_digest: 'd'.repeat(64), queue_job_id: 'library-capture-internal-1', snapshot_id: '', target_snapshot_id: '', actor: 'yaozhanghao', error: '', recovery: '', created_at: '2026-10-28T03:34:50Z', updated_at: '2026-10-28T03:34:55Z', finished_at: null, stages: [ { stage: 'export', state: 'running', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '主机侧正在暂停写入者、导出并恢复', updated_at: '2026-10-28T03:34:55Z', }, ], } type ViewProps = Parameters[0] const base: ViewProps = { page, jobs: [], overview, loading: false, error: null, notice: null, scope: 'internal', onScope: vi.fn(), drawerOpen: false, onDrawer: vi.fn(), expandedSnapshot: null, onExpandSnapshot: vi.fn(), pendingCapture: false, canCapture: true, canWrite: true, canReconcileCapture: false, pendingReconcileCapture: '', onReconcileCapture: vi.fn(), canRestore: false, sync: { canSign: false, pendingGrant: false, onGrant: vi.fn(), canStart: false, pending: false, onStart: vi.fn() }, baseline: { canStart: false, pending: false, onStart: vi.fn() }, verification: { onAccepted: vi.fn(), onComplete: vi.fn() }, restore: null, restoreNotice: null, pendingRestore: false, pendingGrant: false, onGrantRestore: vi.fn(), onRestore: vi.fn(), maintenance: null, now: new Date('2026-10-28T03:35:00Z').getTime(), onRefresh: vi.fn(), onCapture: vi.fn(), onRecover: vi.fn(), } const render = (overrides: Partial = {}) => renderToStaticMarkup() describe('ReleaseTargetsView(确认稿布局)', () => { it('顶部只有两个知识库页签,且一次只显示选中库的作业区', () => { const html = render() expect(html).toContain('对内知识库') expect(html).toContain('对外知识库') expect(html).toContain('role="tab"') // 只显示一个主作业卡:选中库标题出现一次,另一个库不渲染作业卡 expect(html.match(/· 已封存,同步按条件放行/g)?.length).toBe(1) expect(html).not.toContain('对外知识库 · 尚无') }) it('未接入项显示“未接入/未知”,不用 0 / 无变化冒充', () => { const html = render() expect(html).toContain('生产当前') // 生产没有权威版本时必须写"非空且尚无权威版本",不能拿测试快照顶替, // 也不能把所有非 known 一律写成"未知"(空目标与读不到是另外两类)。 expect(html).toContain('目标非空,尚无权威版本') expect(html).toContain('尚无记录 · 生产与测试的文档级差异读取未接线') expect(html).toContain('未接入') }) it('“启动同步”保持禁用并逐条说明缺项,不把封存当成同步', () => { const html = render() expect(html).toMatch(/]*class="rel-primary"[^>]*disabled/) expect(html).toContain('生产写入闸口关闭') expect(html).toContain('没有已核验的生产恢复演练证据') expect(html).toContain('本次目标快照还没有生产预备份') // 授权也必须可见:没有授权时说明需要管理员签发 expect(html).toContain('无有效授权(需管理员按目标+快照签发)') expect(html).toContain('封存测试版本') }) it('没有当前作业时不把历史封存当成阶段完成', () => { const html = render() // 阶段条第一段必须是 idle(○),"✓" 只允许出现在准备清单,不出现在阶段条 const stageBar = html.slice(html.indexOf('aria-label="作业阶段"'), html.indexOf('rel-steps-note')) expect(stageBar).not.toContain('✓') expect(stageBar).toContain('本次作业尚未开始') expect(html).toContain('已有测试封存') expect(html).toContain('没有当前作业证据时不会把历史封存当作阶段完成') }) it('差异已计算时按文档身份列增改撤销,并标注比对的两个版本', () => { const html = render({ overview: { ...overview, diff: { state: 'computed', added: [{ id: 'doc-new-0001', title: '新增的说明', digest: 'a'.repeat(64) }], updated: [{ id: 'doc-upd-0002', title: '改过的说明', digest: 'b'.repeat(64) }], removed: [{ id: 'doc-del-0003', title: '撤销的说明', digest: 'c'.repeat(64) }], base_snapshot: 'prod00000000', target_snapshot: 'test00000000', wiki: { state: 'computed', added: 2, updated: 1, removed: 3, sample: [], note: '页面级差异', }, }, }, }) expect(html).toContain('3 条') expect(html).toContain('新增的说明 · doc-new-') expect(html).toContain('改过的说明 · doc-upd-') expect(html).toContain('撤销的说明 · doc-del-') expect(html).toContain('生产 prod00000000 → 测试 test00000000') expect(html).not.toContain('变更清单来自生产与测试版本对比') expect(html).toContain('Wiki 页面:新增 2 · 更新 1 · 撤销 3') expect(html).toContain('重算发生在源侧') }) it('准备信息注明快照时间与范围,不声称实时测试库已就绪', () => { const html = render() expect(html).toContain('依据 10/28 11:34 封存的测试库全库快照,不代表实时测试库已就绪') expect(html).toContain('同一份封存快照,不是实时 Wiki 队列状态') expect(html).toContain('列表只校验清单自身,字节级复核在同步/恢复执行前进行') }) it('版本与快照抽屉按 scope 列出全部历史,并把恢复条件交给接口判定', () => { const html = render({ drawerOpen: true, expandedSnapshot: internalSnapshot.snapshot_id, canRestore: true }) expect(html).toContain('版本与快照') expect(html).toContain('封存 f992d64ea45b') expect(html).toContain('测试封存') expect(html).toContain('恢复条件见下方') expect(html).toContain('恢复至该版本') expect(html).toMatch(/恢复至该版本[^<]*<\/button>/) expect(html).toContain('分项数量 / 字节 / 摘要(2 项)') expect(html).toContain('不支持只选择 Wiki 或某张数据表恢复') }) it('管理员在缺授权时看到签发入口,恢复按钮按接口给出的原因禁用', () => { const html = render({ drawerOpen: true, expandedSnapshot: internalSnapshot.snapshot_id, canRestore: true, restore: { allowed: false, reasons: ['没有针对该快照的恢复授权:先签发 restore 授权(绑定同一快照)'], scope: 'internal', snapshot_id: internalSnapshot.snapshot_id, authorization: { state: 'none' }, }, }) expect(html).toContain('签发恢复授权(30 分钟)') expect(html).toContain('没有针对该快照的恢复授权') expect(html).toMatch(/rel-danger[^>]*disabled/) // 不能再说"未接线":条件不满足要给出具体原因 expect(html).not.toContain('生产恢复未接线') }) it('条件全满足时恢复按钮可用,并写明授权有效期', () => { const html = render({ drawerOpen: true, expandedSnapshot: internalSnapshot.snapshot_id, canRestore: true, restore: { allowed: true, reasons: [], scope: 'internal', snapshot_id: internalSnapshot.snapshot_id, authorization: { state: 'active', authorized_by: 'yaozhanghao', expires_at: '2026-10-28T04:10:00Z' }, }, }) expect(html).toContain('条件已满足') expect(html).toContain('有效至') expect(html).not.toMatch(/rel-danger[^>]*disabled/) }) it('工程师看不到恢复入口,只看到权限说明', () => { const html = render({ drawerOpen: true, expandedSnapshot: internalSnapshot.snapshot_id, canRestore: false }) expect(html).toContain('按版本恢复仅管理员可发起') expect(html).not.toContain('签发恢复授权') }) it('抽屉只展示当前选中库的历史', () => { const html = render({ drawerOpen: true, scope: 'external', overviews: { internal: overview, external: { ...overview, scope: 'external', versions: [overview.versions![1]] }, }, }) const drawer = html.slice(html.indexOf('aria-label="版本与快照"')) expect(drawer).toContain('快照 01343b0b310b') expect(drawer).not.toContain('快照 f992d64ea45b') }) // E3:归档里同时躺着测试封存、目标基线与每次预备份。旧页面把每一行都写"测试封存", // 于是预备份/基线被当成了"可以发布的候选"。类型只能来自版本登记。 it('抽屉按版本登记标注每份归档,不把预备份/基线写成测试封存', () => { const html = render({ drawerOpen: true, overview: { ...overview, versions: [ { id: 'libver-sealed', kind: 'test_sealed', snapshot_id: internalSnapshot.snapshot_id, version_label: '', source_job_id: 'libseal-1', created_at: internalSnapshot.created_at, }, { id: 'libver-pre', kind: 'production_prebackup', snapshot_id: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', version_label: '', source_job_id: 'librestore-internal-1', created_at: '2026-10-29T16:59:39Z', }, ], }, page: { ...page, snapshots: [ internalSnapshot, { ...internalSnapshot, snapshot_id: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', created_at: '2026-10-29T16:59:39Z', }, ], }, }) expect(html).toContain('测试封存') expect(html).toContain('生产预备份') // 没有被登记过的归档文件也不能冒充测试封存 expect(html).not.toContain('测试封存 { const html = render({ drawerOpen: true, overview: { ...overview, prebackup: { state: 'known', version: { id: 'libver-pre', kind: 'production_prebackup', snapshot_id: '089676a09107ceab3bf5e88651f443da26e5d9b2d0de380840c5ec24941a07c2', version_label: '', source_job_id: 'librestore-internal-1793293164573204381', evidence: {}, created_by: 'worker', created_at: '2026-10-29T16:59:39Z', }, bound_to_target: false, }, }, }) expect(html).toContain('已登记预备份') expect(html).not.toContain('尚未创建') expect(html).toContain('作业 librestore-internal-1793293164573204381') expect(html).toContain('不是当前拟发布目标的快照') expect(html).toContain('最近一次生产预备份') expect(html).not.toContain('无记录(尚未为本次目标创建)') }) // E1:作业已经成功,页面不能还说"尚未启动/此时尚未运行"。 it('已完成的同步不再写“尚未启动”,阶段按本次作业打勾', () => { const succeeded: LibraryJobRecord = { ...persistedSeal, id: 'libsync-internal-1793292008635856030', kind: 'sync', state: 'succeeded', stage: 'finalize', snapshot_id: '4b720eb42fca75de40aa862f2b53d0751e648d760dafaca309aaee08771f2c73', target_snapshot_id: internalSnapshot.snapshot_id, finished_at: '2026-10-29T16:40:36Z', updated_at: '2026-10-29T16:40:36Z', stages: [ { stage: 'prebackup', state: 'succeeded', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '', updated_at: '2026-10-29T16:40:21Z', }, { stage: 'database', state: 'succeeded', total: 67, completed: 67, bytes_total: 88086, bytes_done: 88086, detail: '', updated_at: '2026-10-29T16:40:22Z', }, { stage: 'verify', state: 'succeeded', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '', updated_at: '2026-10-29T16:40:26Z', }, { stage: 'finalize', state: 'succeeded', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '已登记生效版本', updated_at: '2026-10-29T16:40:26Z', }, ], } const html = render({ overview: { ...overview, job: succeeded, jobs: [succeeded] } }) expect(html).not.toContain('尚未启动,不修改生产') expect(html).not.toContain('此时尚未运行') expect(html).toContain('生产同步已完成') expect(html).toContain('生产同步 · 已完成') const stageBar = html.slice(html.indexOf('aria-label="作业阶段"'), html.indexOf('rel-steps-note')) expect(stageBar).toContain('✓') }) // E4:基线/恢复不是"生产同步",也不是"封存进行中"。 it('建立基线进行中不写成同步中,也不写成封存进行中', () => { const baseline: LibraryJobRecord = { ...persistedSeal, id: 'libbase-internal-1', kind: 'baseline', state: 'running', stage: 'database', finished_at: null, } const html = render({ overview: { ...overview, job: baseline, jobs: [baseline] } }) expect(html).toContain('建立目标基线进行中') expect(html).toContain('建立目标基线作业') expect(html).toContain('建基线中') expect(html).not.toContain('封存进行中') expect(html).not.toContain('生产同步作业') expect(html).not.toContain('同步中') }) it('恢复作业用“按版本恢复”而不是“生产同步”,并说明已回到该快照', () => { const stages: LibraryStageRow[] = [ { stage: 'prebackup', state: 'succeeded', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '', updated_at: '2026-10-29T16:59:39Z', }, { stage: 'database', state: 'succeeded', total: 5, completed: 5, bytes_total: 7138, bytes_done: 7138, detail: '', updated_at: '2026-10-29T16:59:40Z', }, { stage: 'resources', state: 'succeeded', total: 1, completed: 1, bytes_total: 507, bytes_done: 507, detail: '', updated_at: '2026-10-29T16:59:40Z', }, { stage: 'index', state: 'succeeded', total: 2, completed: 2, bytes_total: 28563, bytes_done: 28563, detail: '', updated_at: '2026-10-29T16:59:40Z', }, { stage: 'wiki', state: 'succeeded', total: 10, completed: 10, bytes_total: 23144, bytes_done: 23144, detail: '', updated_at: '2026-10-29T16:59:40Z', }, { stage: 'verify', state: 'succeeded', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '', updated_at: '2026-10-29T16:59:40Z', }, { stage: 'finalize', state: 'succeeded', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '已登记生效版本', updated_at: '2026-10-29T16:59:40Z', }, ] const restoreRunning: LibraryJobRecord = { ...persistedSeal, id: 'librestore-internal-1793293164573204381', kind: 'restore', state: 'running', stage: 'wiki', target_snapshot_id: '4b720eb42fca75de40aa862f2b53d0751e648d760dafaca309aaee08771f2c73', finished_at: null, stages, } const running = render({ overview: { ...overview, job: restoreRunning, jobs: [restoreRunning] } }) expect(running).toContain('按版本恢复作业') expect(running).toContain('恢复到 4b720eb42fca') expect(running).toContain('恢复进行中(目标在维护中)') expect(running).not.toContain('生产同步作业') const restoreDone: LibraryJobRecord = { ...restoreRunning, state: 'succeeded', finished_at: '2026-10-29T16:59:51Z', } const html = render({ overview: { ...overview, job: restoreDone, jobs: [restoreDone] } }) expect(html).toContain('按版本恢复') expect(html).toContain('按版本恢复已完成') const stageBar = html.slice(html.indexOf('aria-label="作业阶段"'), html.indexOf('rel-steps-note')) expect(stageBar).toContain('目标已回到该快照') }) it('恢复验证使用自己的阶段,不把登记证据说成新版本生效', () => { const verifyJob: LibraryJobRecord = { ...persistedSeal, id: 'libverify-internal-1', kind: 'verify', state: 'running', stage: 'restore', stages: [ { stage: 'capture', state: 'succeeded', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '已捕获', updated_at: '2026-11-05T01:00:00Z', }, { stage: 'verify', state: 'succeeded', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '基线校验通过', updated_at: '2026-11-05T01:01:00Z', }, { stage: 'canary', state: 'succeeded', total: null, completed: null, bytes_total: null, bytes_done: null, detail: 'canary 通过', updated_at: '2026-11-05T01:02:00Z', }, { stage: 'restore', state: 'running', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '正在恢复', updated_at: '2026-11-05T01:03:00Z', }, { stage: 'finalize', state: 'pending', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '', updated_at: '2026-11-05T01:04:00Z', }, ], } const html = render({ overview: { ...overview, job: verifyJob, jobs: [verifyJob] } }) expect(html).toContain('恢复验证阶段') expect(html).toContain('捕获目标') expect(html).toContain('登记验证证据') expect(html).not.toContain('新版本生效') }) it('快照抽屉默认隐藏未登记归档,并给出全部归档入口', () => { const html = render({ drawerOpen: true, overview: { ...overview, versions: [] }, }) const drawer = html.slice(html.indexOf('aria-label="版本与快照"')) expect(drawer).toContain('已登记版本') expect(drawer).toContain('全部归档(含未登记)') expect(drawer).not.toContain('快照 f992d64ea45b') expect(drawer).toContain('当前筛选下没有已登记版本') }) it('统计数据只取当前作业快照,并按真实阶段状态展示', () => { const jobSnapshot: LibrarySnapshot = { ...internalSnapshot, snapshot_id: 'old-job-snapshot', totals: { documents: 4, chunks: 38, resources: 21, bindings: 4, wiki_pages: 31, wiki_folders: 21, wiki_log_entries: 11, }, } const job: LibraryJobRecord = { ...persistedSeal, id: 'libsync-old', kind: 'sync', target_snapshot_id: jobSnapshot.snapshot_id, stage: 'database', stages: [ { stage: 'database', state: 'running', total: null, completed: null, bytes_total: null, bytes_done: null, detail: '', updated_at: '2026-11-05T01:00:00Z', }, ], } const html = render({ page: { ...page, snapshots: [internalSnapshot, jobSnapshot] }, overview: { ...overview, job, jobs: [job], test_sealed: internalSnapshot }, }) expect(html).toContain('文档 4 · 分块 38 · 资源 21 · 绑定 4') expect(html).toContain('页面 31 · 目录 21 · 日志 11') expect(html).toContain('执行中;执行器尚未提供该分项明细') expect(html).not.toContain('已完成 0 / 0') }) it('封存已完成但终态未回写时,管理员看到核对入口;工程师不显示', () => { const reconciled: LibraryJobRecord = { ...persistedSeal, stages: [{ ...persistedSeal.stages[0], state: 'succeeded', detail: '已封存快照 f992d64ea45b' }], } const admin = render({ canReconcileCapture: true, onReconcileCapture: vi.fn(), overview: { ...overview, job: reconciled, jobs: [reconciled] }, }) expect(admin).toContain('核对封存结果') const engineer = render({ canReconcileCapture: false, overview: { ...overview, job: reconciled, jobs: [reconciled] }, }) expect(engineer).not.toContain('核对封存结果') }) it('系统误判的封存作业(已完成队列+成功导出)仍可由管理员核对;真实失败不显示', () => { const misclassified: LibraryJobRecord = { ...persistedSeal, state: 'failed', stage: '', snapshot_id: '', error: '队列中已无对应作业,业务作业未收到结果', recovery: '封存作业由 agent 恢复来源;确认来源 healthy 后可重发', stages: [{ ...persistedSeal.stages[0], state: 'succeeded' }], } const admin = render({ canReconcileCapture: true, overview: { ...overview, job: misclassified, jobs: [misclassified] }, }) expect(admin).toContain('核对封存结果') const realFailure = render({ canReconcileCapture: true, overview: { ...overview, job: { ...misclassified, error: '真实导出失败' }, jobs: [{ ...misclassified, error: '真实导出失败' }], }, }) expect(realFailure).not.toContain('核对封存结果') }) it('封存作业只显示只读导出步骤,不冒充生产备份/同步/生效或目标维护', () => { const html = render({ jobs: [runningJob], overview: { ...overview, job: persistedSeal, jobs: [persistedSeal] }, }) expect(html).toContain('测试库封存作业') // F06: 运行中主按钮只能查看当前任务,不能再次启动。 expect(html).toContain('查看当前任务') expect(html).not.toContain('▶ 启动同步') expect(html).toContain('只读导出测试版本') expect(html).toContain('只读导出测试库快照 → 不修改目标生产') expect(html).toContain('封存进行中(只读导出测试版本,不修改目标生产)') expect(html).toContain('封存范围') expect(html).toContain('主机侧正在暂停写入者、导出并恢复') expect(html).not.toContain('>备份生产<') expect(html).not.toContain('>同步数据与 Wiki<') expect(html).not.toContain('>生产验证<') expect(html).not.toContain('>生产生效<') expect(html).not.toContain('目标在维护中') expect(html).not.toContain('各类数据同步情况') expect(html).not.toContain('同步明细') }) it('失败封存保留失败阶段与原因,不切成生产步骤', () => { const failedSeal: LibraryJobRecord = { ...persistedSeal, state: 'failed', error: 'unverified legacy library chunk index coverage', stages: [{ ...persistedSeal.stages[0], state: 'failed', detail: '封存校验失败' }], } const html = render({ overview: { ...overview, job: failedSeal, jobs: [failedSeal] }, }) expect(html).toContain('只读导出测试版本') expect(html).toContain('封存校验失败') expect(html).toContain('unverified legacy library chunk index coverage') expect(html).toContain('测试封存失败') expect(html).not.toContain('目标在维护中') expect(html).not.toContain('>备份生产<') expect(html).not.toContain('>同步数据与 Wiki<') expect(html).not.toContain('>生产生效<') expect(html).not.toContain('目标在维护中') }) it('无权限时不给封存入口;不可发起时给出接口返回的真实原因与执行器身份', () => { expect(render({ canCapture: false, canWrite: false })).toContain('封存需要知识工程师及以上权限') const unavailable = render({ canCapture: false, canWrite: true, page: { ...page, capture_available: false }, overview: { ...overview, capture_available: false, capture_reason: '执行器指向的实例不匹配:agent="weknora071full",期望="kgmaint-weknora"', executor: { available: false, busy: false, project: 'weknora071full', source_app: 'WKNFull-app', observed_at: '2026-10-28T03:34:00Z', }, }, }) expect(unavailable).toContain('执行器指向的实例不匹配') expect(unavailable).toContain('执行器 weknora071full / WKNFull-app') expect(unavailable).toContain('观测于') // 通道可用/不可用都不许用写死的"未接线"糊过去 expect(unavailable).not.toContain('封存通道未接线') }) it('无封存时给出“尚无封存”而不是空表', () => { // 归档没有快照时接口也不会给 test_sealed:两处必须一致,否则页面会把 // 一个不存在的封存显示成"已封存"。 const html = render({ page: { ...page, snapshots: [] }, overview: { ...overview, test_sealed: undefined, test_sealed_reason: '还没有该目标的测试封存' }, }) expect(html).toContain('尚无测试封存') expect(html).toContain('还没有测试库封存') }) it('管理员在缺同步授权时看到签发入口,工程师只看到“需管理员签发”', () => { const admin = render({ sync: { canSign: true, pendingGrant: false, onGrant: vi.fn(), canStart: false, pending: false, onStart: vi.fn() }, }) expect(admin).toContain('签发同步授权(30 分钟)') // 授权入口不能顺手把执行也放行:闸口条件仍然逐条可见 expect(admin).toContain('生产写入闸口关闭') const engineer = render() expect(engineer).not.toContain('签发同步授权') expect(engineer).toContain('同步授权只能由管理员按目标+快照签发') }) it('目标非空又没有权威版本时,管理员看到建立基线入口;有生效版本时不显示', () => { const admin = render({ baseline: { canStart: true, pending: false, onStart: vi.fn() }, }) expect(admin).toContain('建立目标权威基线(维护隔离)') expect(admin).toContain('目标非空且没有生效版本记录') const known = render({ baseline: { canStart: true, pending: false, onStart: vi.fn() }, overview: { ...overview, production: { state: 'known', version: { id: 'v1', kind: 'production_effective', snapshot_id: 'a'.repeat(64), version_label: '基线 abc', source_job_id: 'libbase-1', evidence: {}, created_by: 'admin', created_at: '2026-10-29T09:00:00Z', }, }, }, }) // 有生效版本时不再提供基线入口(缺项说明文字里仍会提到它存在,所以只断言按钮) expect(known).not.toMatch(/建立目标权威基线(维护隔离)<\/button>/) }) it('生产生效版本就是当前测试封存时,主按钮是不可点的“已同步”,前进方向换成重新封存', () => { const html = render({ sync: { canSign: true, pendingGrant: false, onGrant: vi.fn(), canStart: true, pending: false, onStart: vi.fn() }, overview: { ...overview, gate: { sync_allowed: true, reasons: [] }, production: { state: 'known', version: { id: 'v-same', kind: 'production_effective', snapshot_id: internalSnapshot.snapshot_id, version_label: '', source_job_id: 'libsync-1', evidence: {}, created_by: 'admin', created_at: '2026-10-29T09:00:00Z', }, }, }, }) // 闸口放行也不许再把同一份内容同步一遍 expect(html).not.toContain('▶ 启动同步') expect(html).toMatch(/]*class="rel-primary rel-synced"[^>]*disabled[^>]*>✓ 已同步/) expect(html).toContain('生产已是当前测试封存') expect(html).toMatch(/class="rel-secondary"[^>]*>重新封存测试版本/) // 已同步时签发授权没有意义,不出现签发入口 expect(html).not.toContain('签发同步授权(30 分钟)') }) it('闸口全满足时“启动同步”真的可点,并给出执行说明', () => { const html = render({ sync: { canSign: false, pendingGrant: false, onGrant: vi.fn(), canStart: true, pending: false, onStart: vi.fn() }, overview: { ...overview, gate: { sync_allowed: true, reasons: [] } }, }) expect(html).toMatch(/]*class="rel-primary"[^>]*>▶ 启动同步/) expect(html).not.toMatch(/class="rel-primary"[^>]*disabled/) expect(html).toContain('先固定测试版本,再备份目标并写入') }) it('维护卡片区分“未运行维护”与“没有维护能力”', () => { const wired = render({ maintenance: { held: false, capability: { configured: true, missing: [], instance: 'kgseal-app', project: 'kgseal-weknora', writers: ['app'], ingress_service: 'ingress', }, }, }) expect(wired).toContain('未运行维护') expect(wired).toContain('维护能力已接线:实例 kgseal-app · 写入者 app') expect(wired).not.toContain('也没有维护能力配置') const broken = render({ maintenance: { held: false, capability: { configured: false, missing: ['LIBRARY_TARGET_WRITERS'] } }, }) expect(broken).toContain('能力未接线') expect(broken).toContain('维护能力未接线:缺少 LIBRARY_TARGET_WRITERS') // 接口没给能力信息时不能猜成"没有能力" const unknown = render({ maintenance: { held: false } }) expect(unknown).toContain('维护能力状态未知') expect(unknown).not.toContain('也没有维护能力配置') }) }) // 后端 /api/v1/library/overview 已经区分四类生产目标状态(library_flow.go unknownLibraryTarget、 // library_baseline.go:81 的空目标 409)。页面此前只判断 state === 'known',于是"已证空"和 // "读不到"都被写成"未知"、并给出必然 409 的"建立目标权威基线"入口。 describe('生产目标四类状态:known / 空 / 非空 / 不可读', () => { const baseline = { canStart: true, pending: false, onStart: vi.fn() } const emptyTarget: LibraryOverview['production'] = { state: 'unknown', empty_baseline: true, reason: '生产目标可读且为空:本次是首次发布,写入前仍会生成完整预备份', } const nonEmptyTarget: LibraryOverview['production'] = { state: 'unknown', reason: '目标非空且尚无权威版本', } const unreadableTarget: LibraryOverview['production'] = { state: 'unreachable', reason: '无法读取目标整库状态:请检查目标连接后重试,不能据此判定空库或建立基线', } const knownTarget: LibraryOverview['production'] = { state: 'known', version: { id: 'v-known', kind: 'production_effective', snapshot_id: 'c'.repeat(64), version_label: '生效 20261030-01', source_job_id: 'libsync-known', evidence: {}, created_by: 'admin', created_at: '2026-10-30T09:00:00Z', }, } it('空目标:写明"已读且为空/首次发布/写前仍预备份",且不给会 409 的建基线入口', () => { const html = render({ baseline, overview: { ...overview, production: emptyTarget, // 接口在空基线上不再给"建立基线"缺项(librarySyncGateWithGrant:657-661) gate: { sync_allowed: false, reasons: ['没有与目标快照匹配的有效发布授权'] }, }, }) expect(html).toContain('目标已读且为空') expect(html).toContain('首次发布') expect(html).toContain('写前仍会生成完整预备份') // 页面不能把"已证空"写成未知,也不能引导去建一个必然被 409 拒绝的基线。 expect(html).not.toContain('未知(尚未建立权威版本)') expect(html).not.toMatch(/建立目标权威基线(维护隔离)<\/button>/) }) it('非空且无生效版本:仍指向既有基线入口,不谎称空库', () => { const html = render({ baseline, overview: { ...overview, production: nonEmptyTarget, gate: { sync_allowed: false, reasons: [ '目标非空且没有生效版本记录:先建立目标权威基线(维护隔离下捕获整库并逐类校验),或确认目标确实是空库', ], }, }, }) expect(html).toContain('目标非空') expect(html).toMatch(/建立目标权威基线(维护隔离)<\/button>/) expect(html).not.toContain('目标已读且为空') }) it('不可读:照实说读取失败,既不认定空也不认定非空,更不引导建基线', () => { const html = render({ baseline, overview: { ...overview, production: unreadableTarget, gate: { sync_allowed: false, reasons: [unreadableTarget.reason!] }, }, }) expect(html).toContain('无法读取目标整库状态') expect(html).not.toContain('目标已读且为空') expect(html).not.toContain('目标非空') expect(html).not.toMatch(/建立目标权威基线(维护隔离)<\/button>/) }) it('known:显示权威生效版本与快照摘要,不再写未知', () => { const html = render({ baseline, overview: { ...overview, production: knownTarget } }) expect(html).toContain('生效 20261030-01') expect(html).toContain(knownTarget.version!.snapshot_id.slice(0, 12)) expect(html).not.toContain('未知(尚未建立权威版本)') expect(html).not.toMatch(/建立目标权威基线(维护隔离)<\/button>/) }) // 真正的一次挂载刷新由 ReleaseTargets.mount.test.tsx 覆盖:三次 renderToStaticMarkup // 只是三个互不相关的静态输入,不执行 load/effect/fetch,不能算刷新证据。 // F1:没有读到 overview 时,页面自己造的 {state:'unknown'} 与后端"已探测且非空" // 在 JSON 上长得一模一样。缺观测的三条真实路径都不能落进 nonempty 分支, // 也不能提供会返回 409 的建基线入口。 it('没有目标观测时不冒充非空:初始加载 / 整体读取失败 / 范围不匹配', () => { const cases: Array<[string, Partial, string]> = [ ['初始加载中', { loading: true, overview: null }, '正在读取目标状态'], ['初始 overview 请求失败', { error: '发布状态读取失败(HTTP 503)', overview: null }, '不能据此判定空或非空'], [ '选中对外但只有对内 overview', { scope: 'external', overview: { ...overview, scope: 'internal' } }, '还没有读到', ], ] for (const [name, overrides, expected] of cases) { const html = render({ baseline, ...overrides }) expect(html, name).toContain(expected) expect(html, name).not.toContain('目标非空') expect(html, name).not.toContain('目标已读且为空') expect(html, name).not.toMatch(/建立目标权威基线(维护隔离)<\/button>/) } }) it('刷新失败但保留旧观测时标成上次观测,不冒充刚验证', () => { const html = render({ baseline, error: '发布状态读取失败(HTTP 502)', overview: { ...overview, production: emptyTarget }, }) expect(html).toContain('目标已读且为空') expect(html).toContain('上次观测 · 刷新失败,可能已过期') expect(html).not.toContain('正在读取目标状态') expect(html).not.toMatch(/建立目标权威基线(维护隔离)<\/button>/) }) it('空目标但没有测试封存:启动同步保持禁用并说明缺候选', () => { const html = render({ baseline, overview: { ...overview, production: emptyTarget, test_sealed: undefined, test_sealed_reason: '还没有该目标的测试封存', }, }) expect(html).toMatch(/]*class="rel-primary"[^>]*disabled/) expect(html).toContain('尚无测试封存') }) }) // E5:轮询必须覆盖"投递 → 终态读回"。 // 旧实现只按封存队列的作业判断,于是点了同步/恢复/建立基线之后页面根本不刷新, // 看上去像"还没启动";要么反过来,一个失败作业被永远轮询下去。 describe('libraryPollingActive(轮询范围)', () => { const idle = { internal: null, external: null } as Record<'internal' | 'external', LibraryOverview | null> const withJob = (state: LibraryJobRecord['state'], kind: LibraryJobRecord['kind']) => ({ internal: { ...overview, job: { ...persistedSeal, state, kind }, jobs: [] }, external: null, }) as Record<'internal' | 'external', LibraryOverview | null> it('投递中就已经在轮询,不必等到作业出现在列表里', () => { expect(libraryPollingActive({ captures: [], overviews: idle, pending: true })).toBe(true) }) it('持久作业在飞(同步/恢复/基线)也轮询', () => { expect(libraryPollingActive({ captures: [], overviews: withJob('running', 'sync'), pending: false })).toBe(true) expect(libraryPollingActive({ captures: [], overviews: withJob('pending', 'restore'), pending: false })).toBe(true) expect(libraryPollingActive({ captures: [], overviews: withJob('running', 'baseline'), pending: false })).toBe(true) }) it('封存队列里的作业同样算在飞', () => { expect(libraryPollingActive({ captures: [runningJob], overviews: idle, pending: false })).toBe(true) }) it('终态必须停下来:失败作业不许被无限轮询', () => { expect(libraryPollingActive({ captures: [], overviews: withJob('succeeded', 'sync'), pending: false })).toBe(false) expect(libraryPollingActive({ captures: [], overviews: withJob('failed', 'sync'), pending: false })).toBe(false) expect(libraryPollingActive({ captures: [], overviews: withJob('cancelled', 'restore'), pending: false })).toBe( false ) expect( libraryPollingActive({ captures: [{ ...runningJob, state: 'failed' }], overviews: idle, pending: false }) ).toBe(false) }) }) // F12:按预备份恢复是独立队列作业(不产生新的 library_jobs 行), // 页面必须自己记住"还在等恢复",并在维护窗口关闭后停止。 describe('libraryPollingActive(恢复轮询)', () => { const idle = { internal: null, external: null } as Record<'internal' | 'external', LibraryOverview | null> it('恢复投递后即使没有任何作业行也要继续读回', () => { expect(libraryPollingActive({ captures: [], overviews: idle, pending: false, recoveryPending: true })).toBe(true) }) it('维护窗口仍被持有时继续读回(恢复/收尾未结束)', () => { const held = { internal: { held: true } as unknown as LibraryMaintenanceView, external: null, } expect(libraryPollingActive({ captures: [], overviews: idle, pending: false, maintenances: held })).toBe(true) }) it('维护窗口关闭且没有在飞动作时停止', () => { const released = { internal: { held: false } as unknown as LibraryMaintenanceView, external: null, } expect(libraryPollingActive({ captures: [], overviews: idle, pending: false, maintenances: released })).toBe(false) }) })