Files
crawler-plugin/admin-frontend-vue/tests/task-123.test.ts
T
huangzd1997 df8255c41e task-123(记录与版本中心): 实现历史记录分页加载
新增 history-model.ts(历史行 snake 解析) 与 history-api.ts(GET /api/admin/history)。

TDD: task-123.test.ts 8 用例 RED→GREEN。
2026-09-05 17:31:29 +08:00

79 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test'
import assert from 'node:assert/strict'
import { readSource } from './helpers.ts'
import { parseHistoryPage, toHistoryRecordItem } from '../src/pages/records/history-model.ts'
test('test_task_123_history_load_normal_primary_path', () => {
// 正常主路径:历史行(snake)分页负载解析。
const page = parseHistoryPage({
success: true,
data: {
items: [
{ id: 3, user_id: 2, username: '张', created_at: '2026-01-01 10:00', panel_type: 'deletebrand', original_urls: ['https://a'], result_urls: ['https://b'], long_image_url: 'https://l', params: { x: 1 } },
],
total: 1,
page: 1,
page_size: 15,
},
})
assert.equal(page.items.length, 1)
const item = page.items[0]
assert.equal(item.id, 3)
assert.equal(item.userId, 2)
assert.equal(item.panelType, 'deletebrand')
assert.deepEqual(item.resultUrls, ['https://b'])
assert.equal(item.longImageUrl, 'https://l')
assert.deepEqual(item.params, { x: 1 })
assert.equal(page.pageSize, 15)
})
test('test_task_123_history_load_normal_variant_input', () => {
// 正常变体:缺 userId/url 字段回默认。
const item = toHistoryRecordItem({ id: 7, username: 'A', created_at: 't', panel_type: 'queryasin' })
assert.ok(item)
assert.equal(item.userId, null)
assert.deepEqual(item.originalUrls, [])
assert.equal(item.longImageUrl, '')
})
test('test_task_123_history_load_repeated_is_idempotent', () => {
// 正常重复:解析稳定、不改输入。
const payload = { data: { items: [{ id: 1, username: 'A', panel_type: 'x' }], total: 1, page: 1, page_size: 15 } }
assert.deepEqual(parseHistoryPage(payload), parseHistoryPage(payload))
})
test('test_task_123_history_load_boundary_empty_input', () => {
// 边界空值:空负载回默认空结果。
const page = parseHistoryPage({})
assert.deepEqual(page.items, [])
assert.equal(page.total, 0)
})
test('test_task_123_history_load_boundary_single_item', () => {
// 边界单元素:单记录;缺 id 行过滤。
const page = parseHistoryPage({ data: { items: [{ id: 9, panel_type: 'a' }, { panel_type: 'no-id' }], total: 2, page: 1, page_size: 15 } })
assert.equal(page.items.length, 1)
})
test('test_task_123_history_load_boundary_limit_or_missing_field', () => {
// 边界上限/缺字段:缺省字段回空数组/空串。
const item = toHistoryRecordItem({ id: 2 })
assert.deepEqual(item.resultUrls, [])
assert.equal(item.params, undefined)
})
test('test_task_123_history_load_invalid_input_rejected', () => {
// 异常输入:success=false 抛后端 message;非对象行过滤。
assert.throws(() => parseHistoryPage({ success: false, message: '无权访问生成记录模块' }), /无权访问/)
assert.equal(toHistoryRecordItem('garbage'), null)
})
test('test_task_123_history_load_dependency_failure_returns_actionable_message', () => {
// 依赖失败/加载走 adapterGET /api/admin/history + 解析。
const api = readSource('src/pages/records/history-api.ts')
assert.match(api, /\/api\/admin\/history/)
assert.match(api, /fetchHistory/)
const model = readSource('src/pages/records/history-model.ts')
assert.equal(/axios|http\./.test(model), false, '历史解析保持纯逻辑')
})