import { renderToStaticMarkup } from 'react-dom/server' import { describe, expect, it, vi } from 'vitest' import { ReleaseTargetsView } from './ReleaseTargets' import type { LibraryCaptureJob, LibraryJobRecord, LibraryOverview, LibrarySnapshot, LibrarySnapshotList, } 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: '没有与目标快照匹配的有效发布授权' }, 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, 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() }, 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('生产当前') // 生产没有权威版本时必须写未知,不能拿测试快照顶替 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' }) expect(html).toContain('封存 01343b0b310b') expect(html).not.toContain('封存 f992d64ea45b') }) it('同步中布局显示四类数据卡与明细,且不编造百分比', () => { const html = render({ jobs: [runningJob], overview: { ...overview, job: persistedSeal, jobs: [persistedSeal] }, }) expect(html).toContain('测试库封存作业') expect(html).toContain('数据库 · 文档与分块') expect(html).toContain('原文件 · 图片 · 附件') expect(html).toContain('检索索引 · 向量') expect(html).toContain('Wiki · 完整快照') expect(html).toContain('同步明细') // 唯一允许出现的百分比是免责声明里的「100% ≠ 作业完成」,没有任何进度主张 expect(html.match(/[1-9]\d*%/g) ?? []).toEqual(['100%']) expect(html).toContain('文件传输 100% ≠ 作业完成') expect(html).toContain('不编造百分比') // 没有真实总量时只写"未知总量",不画百分比 expect(html).toContain('未知总量') expect(html).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: 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('也没有维护能力配置') }) })