import { renderToStaticMarkup } from 'react-dom/server' import { describe, expect, it } from 'vitest' import { MaintenanceCard, maintenanceSummary, wikiContributionLines } from './ReleaseTargets' import type { LibraryDiff, LibraryJobRecord, LibraryMaintenanceView } from './librarySnapshots' const job: LibraryJobRecord = { id: 'libsync-internal-1', scope: 'internal', kind: 'sync', state: 'failed', stage: 'rollback', request_digest: 'x', queue_job_id: 'q1', snapshot_id: '', target_snapshot_id: '', actor: 'admin', error: '写入生产 chunks 失败:连接被拒绝', recovery: '已按预备份回滚并校验', created_at: '2026-10-29T04:00:00Z', updated_at: '2026-10-29T04:02:00Z', finished_at: '2026-10-29T04:02:00Z', stages: [], } const heldInstance: LibraryMaintenanceView = { held: true, instance: 'kgmaint-weknora', instance_maintenance: { instance: 'kgmaint-weknora', owner_scope: 'internal', scope: 'internal', owner_job_id: 'libsync-internal-1', level: 'instance', services: ['app'], ingress_state: 'closed', state: 'held', acquired_at: '2026-10-29T04:00:00Z', updated_at: '2026-10-29T04:02:00Z', released_at: null, }, } describe('维护级别/进度/恢复错误', () => { it('实例级持有:显示级别、入口状态、进度与恢复错误,并允许按预备份恢复', () => { const summary = maintenanceSummary(heldInstance, job) expect(summary.level).toBe('实例级') expect(summary.held).toBe(true) expect(summary.ingress).toContain('已关闭') expect(summary.progress).toContain('回滚') expect(summary.progress).toContain('失败') expect(summary.recoveryError).toContain('写入生产 chunks 失败') expect(summary.recoveryNote).toContain('按预备份回滚') expect(summary.canRecover).toBe(true) const html = renderToStaticMarkup( undefined} /> ) for (const text of ['目标维护', '实例级', '入口 已关闭', '按预备份恢复', '写入生产 chunks 失败']) { expect(html).toContain(text) } }) it('没有窗口时如实说没有,不画成"已就绪"', () => { const summary = maintenanceSummary(null, undefined) expect(summary.level).toBe('未接入') expect(summary.held).toBe(false) expect(summary.canRecover).toBe(false) const html = renderToStaticMarkup( undefined} /> ) expect(html).toContain('暂无维护窗口') expect(html).not.toContain('按预备份恢复') }) it('已释放的窗口不再提供恢复入口', () => { const released: LibraryMaintenanceView = { held: false, instance_maintenance: { ...heldInstance.instance_maintenance!, released_at: '2026-10-29T04:05:00Z', state: 'recovered', }, } const summary = maintenanceSummary(released, job) expect(summary.held).toBe(false) expect(summary.canRecover).toBe(false) expect(summary.releasedAt).not.toBe('') }) }) describe('Wiki 贡献级差异展示', () => { const diff: LibraryDiff = { state: 'computed', added: [], updated: [], removed: [], wiki: { state: 'computed', added: 0, updated: 1, removed: 1, sample: [], note: '逐页比对', sources_removed: 2, pages: [ { slug: 'entity/fa-bu-zha-men', title: '发布闸门', status: 'updated', changed: ['正文', '来源文档', '引用分块'], sources_removed: ['doc-a'], citations_before: 2, citations_after: 1, }, { slug: 'entity/tu-pu-hui-tian-fu-wu', title: '图谱回填服务', status: 'removed', changed: ['整页撤销'], citations_before: 1, citations_after: 0, }, ], }, } it('共享页被更新时列出来源减少与引用变化,而不是只说"更新 1 页"', () => { const lines = wikiContributionLines(diff) expect(lines[0]).toContain('发布闸门') expect(lines[0]).toContain('来源 -1') expect(lines[0]).toContain('引用 2→1') expect(lines[1]).toContain('整页撤销') }) it('没有逐页明细时不编造明细', () => { expect(wikiContributionLines({ state: 'unknown', added: [], updated: [], removed: [] })).toEqual([]) }) })