import { renderToStaticMarkup } from 'react-dom/server' import { describe, expect, it, vi } from 'vitest' import { BackupScheduleView, backupDaysValid, backupMonthsValid, backupTimeValid, formatBackupBytes, runMoment, storageAlerts, storageAlertSummary, storageUsage, } from './BackupScheduleSection' import type { StorageDiskObservation } from './BackupScheduleSection' 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 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('最近批次只展示接口真实字段;子库路径/复制/恢复验证明确未接线', () => { const html = view({ last_run_failed_databases: ['dobotai_prod'] }) expect(html).toContain('备份记录') expect(html).toContain('接口只返回最近一批') expect(html).toContain('批次戳 20261009T052909Z') expect(html).toContain('耗时 33 秒') expect(html).toContain('742.4 MiB') expect(html).toContain('weknora_test') expect(html).toContain('dobotai_prod') expect(html).toContain('失败') expect(html).toContain('子库目录与单库字节未由接口返回') expect(html).toContain('复制目录(不可用)') expect(html).toContain('恢复验证:未接线') // 不拼造历史批次或示例子目录。 expect(html).not.toContain('/srv/kb-backups/rds-logical') expect(html).not.toContain('11 月 19 日 03:30') }) it('没有批次时说明不能据此判断成功/失败', () => { const html = view({ last_run_stamp: '', last_run_created_at: '', last_run_databases: [], last_run_total_bytes: 0, host_reachable: false, }) 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('后端目前没有「备份主机磁盘总量、空闲、备份目录占用」接口') 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']) }) })