Files
knowledge-governance/web/src/backupSchedule.test.ts
T
姚章浩 e34306b2db
build / build (push) Failing after 18s
fix(ui): 已释放窗口不再渲染 held 文案,版本页不再把 500 说成未接线
发布管理:maintenanceSummary 只看 job.state === 'failed' 就给 held 处置文案;
窗口已 released 的旧作业(libsync-internal-1793399248431728863)因此自相矛盾。
改为仅 held 时给 recoveryError,已释放时给 releasedFailure(如实记录失败,不提供强制成功)。

版本记录:LoadVersionHistorySyncState 把可空的 finished_at 扫进 time.Time;
部署重启触发的同步在"进行中"时该列为 NULL,扫描失败使整页 500
(nginx access log:GET /api/v1/version-history?limit=30 → 500,74 字节错误体)。
改为指针接值再判空。前端同时区分"未配置/加载失败"并显示服务端 code。

RED/GREEN:新增 Go 集成用例(NULL finished_at)、两条前端用例;
make verify 16/16 通过(local 档案)。
2026-11-01 19:34:31 +08:00

88 lines
2.8 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { fetchBackupSchedule, fetchBackupScheduleHost, runBackupNow, saveBackupSchedule } from './backupSchedule'
const payload = {
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: '',
last_applied_summary: '',
host_configured: true,
host_target: 'weknora-test',
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 …'],
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: 778502144,
last_run_databases: ['weknora_test'],
last_run_failed_databases: [],
last_run_error: '',
in_sync: true,
applied: true,
apply_error: '',
running: false,
running_started_at: '',
run_last_error: '',
}
const jsonResponse = (body: unknown, status = 200) =>
new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } })
afterEach(() => {
vi.unstubAllGlobals()
})
describe('backupSchedule 客户端', () => {
it('解析计划三要素与主机回执', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async () => jsonResponse({ data: payload }))
)
const schedule = await fetchBackupSchedule()
expect(schedule.schedule_time).toBe('03:30')
expect(schedule.retention_days).toBe(7)
expect(schedule.host_cron_lines).toHaveLength(1)
expect(schedule.last_run_failed_databases).toEqual([])
})
it('缺计划字段就报错,绝不把默认值伪装成真实设置', async () => {
const broken = { ...payload } as Record<string, unknown>
delete broken.retention_days
vi.stubGlobal(
'fetch',
vi.fn(async () => jsonResponse({ data: broken }))
)
await expect(fetchBackupScheduleHost()).rejects.toThrow('存储与备份响应缺少计划字段')
})
it('保存失败时把后端的中文原因透出来', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async () => jsonResponse({ error: '储存天数需在 1~365 之间,收到 0' }, 400))
)
await expect(
saveBackupSchedule({ enabled: true, schedule_time: '03:30', retention_days: 0, retention_months: 3 })
).rejects.toThrow('储存天数需在 1~365 之间,收到 0')
})
it('立即备份返回 202 时识别为已启动', async () => {
vi.stubGlobal(
'fetch',
vi.fn(async () => jsonResponse({ data: { started: true, running: true } }, 202))
)
await expect(runBackupNow()).resolves.toEqual({ started: true, running: true })
})
})