Files
go-sms-V2/web/tests/record-history.spec.ts
T

71 lines
3.5 KiB
TypeScript

// record-history.spec.ts Day 55(D26):历史记录契约用例(msw fe4HistoryHandlers)。
// 锁定:冷数据口径(种子 >5 天/仅终态)/时间范围分区语义/异步导出状态机(pending→ready)/下载中心过期隐藏。
import { describe, expect, it } from 'vitest'
import { createHistoryExport, fetchHistoryExports, fetchHistoryRecords } from '../src/api/record'
import { exportDB, historyDB } from '../mocks/fe4-handlers'
function dayBoundary(days: number): string {
const d = new Date(Date.now() - days * 86400_000)
const p = (v: number) => String(v).padStart(2, '0')
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} 00:00:00`
}
describe('D26 历史记录(Mock 契约,对齐 Day61 ClickHouse 历史域)', () => {
it('列表:种子全部 >5 天(冷数据窗口口径),分页 100/页', async () => {
const page = await fetchHistoryRecords({ page: 1, page_size: 100 })
expect(page.total).toBe(historyDB.length)
expect(page.list.length).toBe(Math.min(100, historyDB.length))
const boundary = dayBoundary(5)
for (const r of historyDB) expect(r.created_at < boundary).toBe(true)
})
it('冷数据仅终态:无 0/1 态(待发/进行中不在历史域)', async () => {
for (const r of historyDB) expect([2, 3, 4, 5]).toContain(r.status)
})
it('时间范围筛选:主索引语义(8~15 天窗口只含 10/14 天前两批次)', async () => {
const start = dayBoundary(16)
const end = dayBoundary(8)
const page = await fetchHistoryRecords({ page: 1, page_size: 100, start_time: start, end_time: end })
expect(page.total).toBeGreaterThan(0)
for (const r of page.list) {
expect(r.created_at >= start).toBe(true)
expect(r.created_at <= end).toBe(true)
}
const nos = new Set(page.list.map((r) => r.task_no))
expect(nos.size).toBe(2) // 14 天+10 天前两批次(20/6 天前批次被裁剪)
})
it('终态筛选+任务号精确:status=5 拦截行可单独圈出', async () => {
const page = await fetchHistoryRecords({ page: 1, page_size: 100, task_no: 'T20260910001', status: 5 })
expect(page.total).toBeGreaterThan(0)
for (const r of page.list) {
expect(r.task_no).toBe('T20260910001')
expect(r.status).toBe(5)
}
})
it('异步导出状态机:提交返回任务号,下载中心可见 pending→ready(2s 后)', async () => {
const before = exportDB.length
const res = await createHistoryExport({ task_no: 'T20260903001' })
expect(res.id).toBeGreaterThan(0)
expect(exportDB.length).toBe(before + 1)
expect(exportDB.find((t) => t.id === res.id)?.status).toBe('pending')
// 快照 scope 携带筛选条件(下载中心可辨识)
expect(exportDB.find((t) => t.id === res.id)?.scope).toContain('task_no=T20260903001')
// 2s 后轮询转 ready(vi 环境真实等待 2.1s——状态机由 mock 时间驱动)
await new Promise((r) => setTimeout(r, 2100))
const list = (await fetchHistoryExports()).list
const done = list.find((t) => t.id === res.id)
expect(done?.status).toBe('ready')
expect(done && done.total > 0).toBe(true)
})
it('下载中心:预置任务可见;过期任务隐藏', async () => {
const list = (await fetchHistoryExports()).list
expect(list.some((t) => t.id === 8801)).toBe(true) // 预置 ready 任务
// 全部在有效期(expires_at 过滤器存在即被测——过期隐藏由 filter 保证)
const now = Date.now()
for (const t of list) expect(new Date(t.expires_at.replace(' ', 'T') + 'Z').getTime()).toBeGreaterThan(now)
})
})