task-123(记录与版本中心): 实现历史记录分页加载
新增 history-model.ts(历史行 snake 解析) 与 history-api.ts(GET /api/admin/history)。 TDD: task-123.test.ts 8 用例 RED→GREEN。
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
/** 历史记录列表加载适配(任务 123):GET /api/admin/history + 归一与解析。 */
|
||||
import { http } from '@/api/http'
|
||||
import { parseHistoryPage } from './history-model.ts'
|
||||
import { normalizeHistoryParams, toHistoryListQuery, type HistoryListParams, type HistoryPageResult } from './history-dto.ts'
|
||||
|
||||
export const HISTORY_ENDPOINT = '/api/admin/history'
|
||||
|
||||
export async function fetchHistoryList(params: Partial<HistoryListParams> = {}): Promise<HistoryPageResult> {
|
||||
const normalized = normalizeHistoryParams(params)
|
||||
const { data } = await http.get<unknown>(HISTORY_ENDPOINT, { params: toHistoryListQuery(normalized) })
|
||||
return parseHistoryPage(data)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/** 历史生成记录列表加载模型(任务 123):解析 ImageHistoryListVo(snake) 记录行;纯逻辑。 */
|
||||
import { unwrap } from '../../api/envelope.ts'
|
||||
import { emptyHistoryPage, type HistoryPageResult, type HistoryRecordItem } from './history-dto.ts'
|
||||
|
||||
function text(value: unknown): string {
|
||||
return typeof value === 'string' ? value.trim() : ''
|
||||
}
|
||||
|
||||
function numberOrNull(value: unknown): number | null {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
|
||||
}
|
||||
|
||||
function strings(value: unknown): string[] {
|
||||
return Array.isArray(value) ? value.map((url) => text(url)).filter(Boolean) : []
|
||||
}
|
||||
|
||||
/** 解析单条历史记录;缺 id 视为无效。 */
|
||||
export function toHistoryRecordItem(raw: unknown): HistoryRecordItem | null {
|
||||
if (!raw || typeof raw !== 'object') return null
|
||||
const r = raw as Record<string, unknown>
|
||||
const id = numberOrNull(r.id)
|
||||
if (id === null) return null
|
||||
const item: HistoryRecordItem = {
|
||||
id,
|
||||
userId: numberOrNull(r.user_id ?? r.userId),
|
||||
username: text(r.username),
|
||||
createdAt: text(r.created_at ?? r.createdAt),
|
||||
panelType: text(r.panel_type ?? r.panelType),
|
||||
originalUrls: strings(r.original_urls ?? r.originalUrls),
|
||||
resultUrls: strings(r.result_urls ?? r.resultUrls),
|
||||
longImageUrl: text(r.long_image_url ?? r.longImageUrl),
|
||||
}
|
||||
if (r.params && typeof r.params === 'object') item.params = r.params as Record<string, unknown>
|
||||
return item
|
||||
}
|
||||
|
||||
/** 归一化历史分页负载为前端结果;缺省字段回默认。 */
|
||||
export function parseHistoryPage(payload: unknown): HistoryPageResult {
|
||||
const out = emptyHistoryPage()
|
||||
const core = unwrap<unknown>(payload)
|
||||
if (!core || typeof core !== 'object') return out
|
||||
const record = core as Record<string, unknown>
|
||||
if (Array.isArray(record.items)) {
|
||||
out.items = record.items
|
||||
.map((raw) => toHistoryRecordItem(raw))
|
||||
.filter((item): item is HistoryRecordItem => item !== null)
|
||||
}
|
||||
if (typeof record.total === 'number') out.total = Math.floor(record.total)
|
||||
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
|
||||
const rawSize = record.page_size ?? record.pageSize
|
||||
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
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', () => {
|
||||
// 依赖失败/加载走 adapter:GET /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, '历史解析保持纯逻辑')
|
||||
})
|
||||
Reference in New Issue
Block a user