import { renderToStaticMarkup } from 'react-dom/server' import { describe, expect, it, vi } from 'vitest' import { BackupScheduleView, backupDaysValid, backupMonthsValid, backupTimeValid, copyBackupPath, formatBackupBytes, inventoryAlerts, inventoryUsage, runMoment, storageAlerts, storageAlertSummary, storageUsage, } from './BackupScheduleSection' import type { StorageDiskObservation } from './BackupScheduleSection' import type { BackupInventory } from './backupInventory' import type { BackupSchedule } from './backupSchedule' const base: BackupSchedule = { enabled: true, schedule_time: '03:30', retention_days: 7, retention_months: 3, updated_by: 'yaozhanghao', updated_at: '2026-10-09T12:00:00Z', last_applied_at: '2026-10-09T12:00:00Z', last_applied_summary: '每天 03:30 · 保留 7 天 · 每月归档 3 份', host_configured: true, host_target: 'weknora-test', host_endpoint: '100.85.111.121:22', host_reachable: true, host_error: '', host_schedule_enabled: true, host_schedule_time: '03:30', host_keep_days: 7, host_keep_months: 3, host_cron_lines: [ '30 03 * * * root /bin/bash /opt/weknora-test/logical-backup.sh >> /srv/kb-backups/logs/logical-backup.log 2>&1', ], host_cron_service: 'active', host_script_present: true, last_run_stamp: '20261009T052909Z', last_run_created_at: '2026-10-09T05:29:09Z', last_run_duration_seconds: 33, last_run_total_bytes: 778_502_144, last_run_databases: ['weknora_test', 'dobotai_prod'], last_run_failed_databases: [], last_run_error: '', in_sync: true, applied: true, apply_error: '', running: false, running_started_at: '', run_last_error: '', } const GiB = 1024 * 1024 * 1024 const diskFixture = (overrides: Partial = {}): StorageDiskObservation => ({ total_bytes: 240 * GiB, free_bytes: 48 * GiB, backup_bytes: 62 * GiB, other_volume_backup_bytes: 0, quota_bytes: 100 * GiB, reserve_bytes: 10 * GiB, estimate_bytes: 12 * GiB, sampled_at: '2026-11-19T02:00:00Z', ...overrides, }) const inventoryFixture = (overrides: Partial = {}): BackupInventory => { const baseInventory: BackupInventory = { wired: true, root: '/var/lib/knowledge-governance/backup-host/rds-logical', host_root: '/srv/kb-backups/rds-logical', source: 'weknora-test:/srv/kb-backups/rds-logical', observed_at: '2026-11-19T03:30:10Z', error: '', mount: { container_root: '/', mount_verified: true, lives_on_container_root: false, read_only: true, evidence: '挂载设备 20 与容器根盘 10 不同:容量按挂载文件系统计量', }, capacity: { measured: true, total_bytes: 240 * GiB, free_bytes: 48 * GiB, available_bytes: 45 * GiB, used_bytes: 192 * GiB, }, batches: [ { stamp: '20261118T193001Z', path: '/var/lib/knowledge-governance/backup-host/rds-logical/20261118T193001Z', host_path: '/srv/kb-backups/rds-logical/20261118T193001Z', manifest_state: 'ok', manifest_path: '/var/lib/knowledge-governance/backup-host/rds-logical/20261118T193001Z/MANIFEST.json', kind: 'rds-logical-backup', created_at: '20261118T193001Z', started_at: '20261118T192908Z', duration_seconds: 53, manifest_total_bytes: 1_393_646_469, apparent_bytes: 1_393_646_469, actual_bytes: 1_500_000_000, file_count: 214, sizes_complete: true, databases: [ { name: 'weknora_v090prod', dir_name: 'weknora_v090prod.dir', path: '/var/lib/knowledge-governance/backup-host/rds-logical/20261118T193001Z/weknora_v090prod.dir', host_path: '/srv/kb-backups/rds-logical/20261118T193001Z/weknora_v090prod.dir', present: true, failed: false, in_manifest: true, apparent_bytes: 696_320, actual_bytes: 720_896, file_count: 12, }, { name: 'weknora_test', dir_name: 'weknora_test.dir', path: '/var/lib/knowledge-governance/backup-host/rds-logical/20261118T193001Z/weknora_test.dir', host_path: '/srv/kb-backups/rds-logical/20261118T193001Z/weknora_test.dir', present: true, failed: true, in_manifest: true, apparent_bytes: 10, actual_bytes: 4096, file_count: 1, }, ], }, { stamp: '20261117T193001Z', path: '/var/lib/knowledge-governance/backup-host/rds-logical/20261117T193001Z', host_path: '/srv/kb-backups/rds-logical/20261117T193001Z', manifest_state: 'invalid', manifest_path: '/var/lib/knowledge-governance/backup-host/rds-logical/20261117T193001Z/MANIFEST.json', manifest_error: '清单无法解析:unexpected end of JSON input', kind: '', created_at: '', started_at: '', duration_seconds: 0, manifest_total_bytes: 0, apparent_bytes: 2048, actual_bytes: 4096, file_count: 1, sizes_complete: true, databases: [ { name: 'weknora_archive', dir_name: 'weknora_archive.dir', path: '/var/lib/knowledge-governance/backup-host/rds-logical/20261117T193001Z/weknora_archive.dir', host_path: '/srv/kb-backups/rds-logical/20261117T193001Z/weknora_archive.dir', present: false, failed: false, in_manifest: false, apparent_bytes: 0, actual_bytes: 0, file_count: 0, }, ], }, ], totals: { batch_count: 2, database_count: 3, apparent_bytes: 1_393_648_517, actual_bytes: 1_500_004_096, latest_stamp: '20261118T193001Z', latest_actual_bytes: 1_500_000_000, }, incomplete_count: 1, skipped: [{ name: 'junk', reason: '不是批次目录' }], truncated: false, } return { ...baseInventory, ...overrides } } const view = ( overrides: Partial = {}, extra: Record = {}, disk: StorageDiskObservation | null = null ) => { const schedule = { ...base, ...overrides } return renderToStaticMarkup( ) } describe('BackupScheduleView', () => { it('按真实契约给出范围与读写方向,并把立即备份限定为整批', () => { const html = view() for (const text of [ '存储与备份', '数据库整库逻辑备份', '范围与读写方向', '备份执行主机 weknora-test 本机直连 RDS', '最近批次含 2 个数据库', 'weknora-test 本地备份根目录', '备份目录未接线或不可读,不在页面猜测', '整批 · 2 个数据库(weknora_test、dobotai_prod)', '100.85.111.121:22', '立即备份一次(整批 2 个数据库)', '不开放单库动作', '仅备份该库(不可用)', ]) expect(html).toContain(text) // 不存在把整批接口包装成单库操作的按钮文案。 expect(html).not.toContain('>立即备份') expect(html).not.toContain('生产 090') expect(html).not.toContain('weknora_v090prod') }) it('策略区分主机生效状态与编辑草稿,保留期与时间仍是真实可保存字段', () => { const html = view({}, { timeDraft: '05:10', daysDraft: '14' }) expect(html).toContain('主机生效:每天 03:30 · 保留 7 天 · 每月 3 份 · 与设置已一致') expect(html).toContain('最后下发(历史)') expect(html).toContain('只记录当时下发结果,不能代表当前主机计划或执行状态') expect(html).toContain('编辑草稿:每天 05:10 · 保留 14 天') expect(html).toContain('有未保存修改,主机仍按生效计划执行') expect(html).toContain('type="time"') expect(html).toContain('value="05:10"') expect(html).toContain('储存天数') expect(html).toContain('每月归档保留') expect(html).toContain('保存并下发到主机') expect(html).toContain('重新下发当前设置') }) it('目录/配额/预留没有后端字段:控件禁用并写明原因,不会假装可保存', () => { const html = view() expect(html).toContain('备份主机上的目录') expect(html).toContain('只读盘点未接线') expect(html).toContain('检查目录(不可用)') expect(html).toContain('备份总配额(GiB)') expect(html).toContain('磁盘预留(GiB)') expect(html).toContain('不会被保存') expect(html).toContain('保存只提交后端契约里的四项') expect(html).toContain('disabled') }) it('只读盘点:展示全部批次、MANIFEST 状态、子库完整路径与实际占盘', () => { const html = view({}, { inventory: inventoryFixture() }) for (const text of [ '备份记录', '2 批 · 只读盘点', '最近批次 20261118T193001Z', '清单完整', '清单损坏', '批次戳 20261118T193001Z', '实际占盘', 'weknora_v090prod', 'weknora_test', '已写入', '失败', '目录缺失', '/srv/kb-backups/rds-logical/20261118T193001Z', '/srv/kb-backups/rds-logical/20261118T193001Z/weknora_v090prod.dir', '/srv/kb-backups/rds-logical/20261117T193001Z/weknora_archive.dir', '复制子库目录', '清单无法解析', '有 1 个', 'junk(不是批次目录)', '恢复验证:未接线', ]) expect(html).toContain(text) expect(html).not.toContain('子库路径待接口返回') expect(html).not.toContain('复制目录(不可用)') }) it('没有只读盘点:不展示任何历史批次或伪造路径', () => { const html = view() expect(html).toContain('尚未读取只读盘点') expect(html).not.toContain('批次戳') expect(html).not.toContain('/srv/kb-backups/rds-logical') }) it('盘点未接线:说明与 SSH 计划读回独立,未读到不等于没有备份', () => { const html = view( {}, { inventory: inventoryFixture({ wired: false, error: '只读盘点未接线(BACKUP_HOST_READONLY_ROOT 未设置)', batches: [], capacity: { measured: false, total_bytes: 0, free_bytes: 0, available_bytes: 0, used_bytes: 0 }, }), } ) expect(html).toContain('只读盘点未接线') expect(html).toContain('未读到批次不等于没有备份') expect(html).toContain('文件盘点未接线') expect(html).not.toContain('/srv/kb-backups/rds-logical/20261118T193001Z') }) it('挂载可读但没有批次时说明不能据此判断成功/失败', () => { const html = view( { last_run_stamp: '', last_run_created_at: '', last_run_databases: [], last_run_total_bytes: 0, host_reachable: false, }, { inventory: inventoryFixture({ batches: [], totals: { batch_count: 0, database_count: 0, apparent_bytes: 0, actual_bytes: 0, latest_stamp: '', latest_actual_bytes: 0, }, }), } ) expect(html).toContain('挂载可读,但没有批次目录') expect(html).toContain('不能证明备份成功或失败') }) it('非管理员只读:没有保存/下发/立即备份按钮,只给当前设置', () => { const html = view({}, { isAdmin: false }) expect(html).toContain('定时备份设置仅管理员可修改') expect(html).toContain('立即备份仅管理员可触发') expect(html).not.toContain('保存并下发到主机') expect(html).not.toContain('立即备份一次(整批') }) it('无主机凭据:不能宣称远端计划停止,历史下发只作历史记录', () => { const html = view({ host_configured: false, host_reachable: false, host_error: '目标主机凭据未挂载(BACKUP_HOST_SSH_CONFIG / BACKUP_HOST_TARGET)', in_sync: false, }) expect(html).toContain('平台未挂载主机凭据(远端状态未知)') expect(html).toContain('无法确认当前主机计划') expect(html).toContain('远端 cron 可能仍在执行') expect(html).toContain('历史下发不能代替实时状态') expect(html).toContain('最后下发(历史)') expect(html).toContain('只记录当时下发结果,不能代表当前主机计划或执行状态') expect(html).toContain('无法比对(未读取主机计划)') expect(html).toContain('未探测(平台未挂载主机凭据)') expect(html).toContain('目标主机凭据未挂载') // 没读到主机就断言"不会执行"是同类误报。 expect(html).not.toContain('不会执行') expect(html).not.toContain('未接通备份执行主机') expect(html).toContain('disabled') }) it('主机已接线但尚未探测:区分"连不上"与"还没读",不按设置推定', () => { const html = view({ host_reachable: false, host_error: '', host_checked_at: '', in_sync: false }) expect(html).toContain('weknora-test · 尚未探测') expect(html).not.toContain('weknora-test · 连接失败') expect(html).toContain('尚未探测当前主机计划:不能按设置或历史下发推定') expect(html).toContain('尚未比对') expect(html).toContain('尚未探测主机,无法确认下一次执行') }) it('连不上主机:报连接失败与拨号地址,脚本状态写未知而不是"缺失"', () => { const html = view({ host_reachable: false, host_endpoint: '120.76.217.51:22', host_error: '目标主机 weknora-test 不可达(ssh: connect to host 120.76.217.51 port 22: Operation timed out);未执行任何主机侧脚本,脚本与计划状态未知(拨号地址 120.76.217.51:22)', in_sync: false, host_script_present: false, }) expect(html).toContain('weknora-test · 连接失败') expect(html).toContain('120.76.217.51:22') expect(html).toContain('未知(未连上主机,未执行主机侧脚本)') expect(html).not.toContain('缺失') expect(html).toContain('主机连不上:以下"主机生效"状态未知') expect(html).toContain('无法比对(主机不可达)') }) it('已连上且脚本真的不在:这时才显示"缺失"', () => { const html = view({ host_reachable: true, host_script_present: false, in_sync: false }) expect(html).toContain('weknora-test · 已连接') expect(html).toContain('缺失') expect(html).not.toContain('未知(未连上主机') }) it('待下发(主机上的计划与设置不一致)时提示', () => { const html = view({ host_schedule_time: '05:00', in_sync: false }) expect(html).toContain('待下发') expect(html).toContain('每天 05:00') }) it('在途备份:只报在途与开始时间,不造百分比', () => { const html = view({ running: true, running_started_at: '2026-10-09T12:30:00Z' }) expect(html).toContain('备份进行中…') expect(html).toContain('正在备份') expect(html).toContain('没有真实百分比接口') }) it('非法输入就地拦截,且保存按钮在未改动时不可点', () => { const html = view({}, { timeDraft: '25:99', daysDraft: '0' }) expect(html).toContain('定时时间需为 HH:MM(00:00~23:59),储存天数 1~365,每月归档 0~24。') expect(html).toContain('disabled') expect(backupTimeValid('03:30')).toBe(true) expect(backupTimeValid('24:00')).toBe(false) expect(backupDaysValid(0)).toBe(false) expect(backupDaysValid(365)).toBe(true) expect(backupMonthsValid(0)).toBe(true) expect(backupMonthsValid(25)).toBe(false) }) it('展示失败的库与最近一次读取错误', () => { const html = view({ last_run_failed_databases: ['rdsadmin'], last_run_error: '清单无法解析' }) expect(html).toContain('rdsadmin') expect(html).toContain('清单无法解析') }) it('最近一次备份不重复显示同一个时间戳', () => { expect(runMoment({ last_run_stamp: '20261009T060736Z', last_run_created_at: '20261009T060736Z' })).toBe('') expect(runMoment({ last_run_stamp: '20261009T060736Z', last_run_created_at: '' })).toBe('') expect(runMoment({ last_run_stamp: '20261009T060736Z', last_run_created_at: '2026-10-09T06:07:36Z' })).not.toBe('') }) it('体积格式化:MiB/GiB', () => { expect(formatBackupBytes(0)).toBe('—') expect(formatBackupBytes(1024 * 1024)).toBe('1.0 MiB') expect(formatBackupBytes(2 * 1024 * 1024 * 1024)).toBe('2.00 GiB') }) }) describe('整盘占用与告警', () => { it('没有只读盘点时:不显示示例容量,四条告警只报未知原因', () => { const html = view() expect(html).toContain('未接线 · 不显示估算容量') expect(html).toContain('容量来自只读挂载目录的 statfs 实测') expect(html).toContain('无法判定') expect(html).toContain('整盘总量 / 空闲 / 备份实际占盘:未接线') // 原型里的示例容量不能被当成产品事实带进来。 expect(html).not.toContain('总容量 240 GiB') expect(html).not.toContain('总容量 500 GiB') expect(html).not.toContain('可再写') }) it('四段色带按真实读数计算:系统其他、备份已用、配额内空闲、配额外空闲', () => { const usage = storageUsage(diskFixture()) expect(usage.system_other).toBe(130 * GiB) // 240 - 48 - 62 expect(usage.backup).toBe(62 * GiB) expect(usage.quota_free).toBe(38 * GiB) expect(usage.beyond_quota_free).toBe(10 * GiB) // 48 - 38 expect(usage.allowance).toBe(38 * GiB) const html = view({}, {}, diskFixture()) expect(html).toContain('系统其他占用 130 GiB') expect(html).toContain('备份已用 62 GiB') expect(html).toContain('配额内空闲 38 GiB') expect(html).toContain('配额外空闲 10 GiB') expect(html).toContain('四项均正常') }) it('磁盘不足:只剩磁盘告警,差值与预计新增都来自读数', () => { const alerts = storageAlerts(diskFixture({ free_bytes: 20 * GiB, quota_bytes: 80 * GiB })) expect(alerts.find((item) => item.code === 'disk_low')?.active).toBe(true) expect(alerts.find((item) => item.code === 'quota_low')?.active).toBe(false) expect(alerts.find((item) => item.code === 'both_low')?.active).toBe(false) expect(storageAlertSummary(alerts)).toBe('磁盘不足') const html = view({}, {}, diskFixture({ free_bytes: 20 * GiB, quota_bytes: 80 * GiB })) expect(html).toContain('差 2 GiB') }) it('配额不足:磁盘够、配额差 7 GiB', () => { const alerts = storageAlerts(diskFixture({ backup_bytes: 95 * GiB })) expect(alerts.find((item) => item.code === 'quota_low')?.active).toBe(true) expect(alerts.find((item) => item.code === 'disk_low')?.active).toBe(false) expect(alerts.find((item) => item.code === 'quota_low')?.detail).toContain('差 7 GiB') expect(storageAlertSummary(alerts)).toBe('配额不足') }) it('两者同时不足:第三条告警成立', () => { const alerts = storageAlerts(diskFixture({ free_bytes: 20 * GiB, backup_bytes: 95 * GiB })) expect(alerts.find((item) => item.code === 'both_low')?.active).toBe(true) expect(storageAlertSummary(alerts)).toContain('磁盘与配额同时不足') }) it('配额超出物理空闲:配额余量大于盘上空闲时单独告警', () => { const alerts = storageAlerts(diskFixture({ quota_bytes: 200 * GiB })) expect(alerts.find((item) => item.code === 'quota_over_physical')?.active).toBe(true) expect(alerts.find((item) => item.code === 'quota_over_physical')?.detail).toContain('超出部分画不进磁盘') }) it('缺下次新增体积时,磁盘/配额判定保持未知而不是默认正常', () => { const alerts = storageAlerts(diskFixture({ estimate_bytes: null })) expect(alerts.filter((item) => !item.known).map((item) => item.code)).toEqual(['disk_low', 'quota_low', 'both_low']) }) it('真实盘点:整盘色带取 statfs 容量与 st_blocks 实际占盘', () => { const inventory = inventoryFixture() const usage = inventoryUsage(inventory) expect(usage).not.toBeNull() expect(usage!.total).toBe(240 * GiB) expect(usage!.free).toBe(45 * GiB) expect(usage!.backup).toBe(1_500_004_096) expect(usage!.system_other).toBe(192 * GiB - 1_500_004_096) const alerts = inventoryAlerts(inventory) expect(alerts.find((item) => item.code === 'disk_low')?.known).toBe(true) expect(alerts.find((item) => item.code === 'disk_low')?.active).toBe(false) expect(alerts.find((item) => item.code === 'quota_low')?.known).toBe(false) const html = view({}, { inventory }) for (const text of [ '只读挂载实测', '系统其他占用', '备份实际占盘', '可用', '配额内/外 —(无配额字段)', '文件系统已用', '挂载为只读', '挂载设备 20 与容器根盘 10 不同', ]) expect(html).toContain(text) }) it('挂载落在容器根盘:不把容器容量当主机磁盘,磁盘告警保持未知', () => { const inventory = inventoryFixture({ mount: { container_root: '/', mount_verified: false, lives_on_container_root: true, read_only: true, evidence: '挂载设备与容器根盘相同(7):容量不代表备份主机磁盘', }, }) expect(inventoryUsage(inventory)).toBeNull() const alerts = inventoryAlerts(inventory) expect(alerts.find((item) => item.code === 'disk_low')?.known).toBe(false) expect(alerts.find((item) => item.code === 'disk_low')?.detail).toContain('容器根盘') const html = view({}, { inventory }) expect(html).toContain('容器根盘 · 不代表主机磁盘') expect(html).toContain('不据此判定备份主机磁盘') expect(html).not.toContain('备份实际占盘 1.40 GiB') }) it('复制路径:空值与不可用按钮不谎报成功', async () => { expect(await copyBackupPath('')).toBe(false) }) })