cb1490ccc2
新增 shop-data-model.ts(按店铺分组任务列表 + 结果文件行解析) 与 shop-data-api.ts(GET /api/admin/shop-data-crawl-tasks 加载适配)。 TDD: task-110.test.ts 8 用例先 RED 后 GREEN。
114 lines
4.4 KiB
TypeScript
114 lines
4.4 KiB
TypeScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readSource } from './helpers.ts'
|
||
import { parseShopDataTaskPage, toShopDataResultRow } from '../src/pages/tasks/shop-data-model.ts'
|
||
|
||
test('test_task_110_shop_data_list_load_normal_primary_path', () => {
|
||
// 正常主路径:按店铺分组的 snake 负载解析为任务组+结果行。
|
||
const page = parseShopDataTaskPage({
|
||
success: true,
|
||
data: {
|
||
items: [
|
||
{
|
||
shop_name: 'BlueWave',
|
||
shop_id: 's1',
|
||
group_name: '华东组',
|
||
latest_created_at: '2026-01-02 10:00:00',
|
||
results: [
|
||
{
|
||
task_id: 11,
|
||
task_no: 'T-1',
|
||
result_id: 501,
|
||
username: '张伟',
|
||
shop_name: 'BlueWave',
|
||
group_name: '华东组',
|
||
status: 'SUCCESS',
|
||
success: true,
|
||
country_codes: ['DE'],
|
||
output_filename: 'shop_data_DE.xlsx',
|
||
result_file_url: 'https://oss/a.xlsx',
|
||
file_ready: true,
|
||
file_status: 'SUCCESS',
|
||
file_size: 2048,
|
||
row_count: 120,
|
||
finished_at: '2026-01-02 10:00:00',
|
||
},
|
||
],
|
||
},
|
||
],
|
||
total: 1,
|
||
page: 1,
|
||
page_size: 20,
|
||
},
|
||
})
|
||
assert.equal(page.items.length, 1)
|
||
const group = page.items[0]
|
||
assert.equal(group.shopName, 'BlueWave')
|
||
assert.equal(group.groupName, '华东组')
|
||
assert.equal(group.results.length, 1)
|
||
const row = group.results[0]
|
||
assert.equal(row.resultId, '501')
|
||
assert.equal(row.status, 'SUCCESS')
|
||
assert.equal(row.success, true)
|
||
assert.deepEqual(row.countryCodes, ['DE'])
|
||
assert.equal(row.filename, 'shop_data_DE.xlsx')
|
||
assert.equal(row.fileUrl, 'https://oss/a.xlsx')
|
||
assert.equal(row.fileSize, 2048)
|
||
assert.equal(row.rowCount, 120)
|
||
assert.equal(page.total, 1)
|
||
assert.equal(page.pageSize, 20)
|
||
})
|
||
|
||
test('test_task_110_shop_data_list_load_normal_variant_input', () => {
|
||
// 正常变体:组内无结果/单结果排序,缺 file_ready 默认 false。
|
||
const row = toShopDataResultRow({ task_id: 1, result_id: 9, shop_name: 'S', status: 'FAILED', error: 'x' })
|
||
assert.ok(row)
|
||
assert.equal(row.resultId, '9')
|
||
assert.equal(row.fileReady, false)
|
||
assert.equal(row.fileStatus, '')
|
||
})
|
||
|
||
test('test_task_110_shop_data_list_load_repeated_is_idempotent', () => {
|
||
// 正常重复:解析稳定、不改输入。
|
||
const payload = { data: { items: [{ shop_name: 'A', results: [{ task_id: 1, result_id: 2, status: 'SUCCESS' }] }], total: 1, page: 1, page_size: 20 } }
|
||
assert.deepEqual(parseShopDataTaskPage(payload), parseShopDataTaskPage(payload))
|
||
})
|
||
|
||
test('test_task_110_shop_data_list_load_boundary_empty_input', () => {
|
||
// 边界空值:空负载回默认分页空结果。
|
||
const page = parseShopDataTaskPage({})
|
||
assert.deepEqual(page.items, [])
|
||
assert.equal(page.total, 0)
|
||
assert.equal(page.pageSize, 20)
|
||
})
|
||
|
||
test('test_task_110_shop_data_list_load_boundary_single_item', () => {
|
||
// 边界单元素:单组单行;缺 result_id 行被过滤。
|
||
const row = toShopDataResultRow({ task_id: 1, status: 'SUCCESS' })
|
||
assert.equal(row, null)
|
||
})
|
||
|
||
test('test_task_110_shop_data_list_load_boundary_limit_or_missing_field', () => {
|
||
// 边界上限/缺字段:可选字段缺省回默认;success 为 null 保留 null。
|
||
const row = toShopDataResultRow({ task_id: 1, result_id: 4, success: null })
|
||
assert.ok(row)
|
||
assert.equal(row.success, null)
|
||
assert.equal(row.finishedAt, undefined)
|
||
})
|
||
|
||
test('test_task_110_shop_data_list_load_invalid_input_rejected', () => {
|
||
// 异常输入:success=false 抛后端 message;缺 result_id/非对象行过滤。
|
||
assert.throws(() => parseShopDataTaskPage({ success: false, message: '无权访问店铺数据任务' }), /无权访问/)
|
||
assert.equal(toShopDataResultRow('garbage'), null)
|
||
})
|
||
|
||
test('test_task_110_shop_data_list_load_dependency_failure_returns_actionable_message', () => {
|
||
// 依赖失败/加载走 adapter:GET /api/admin/shop-data-crawl-tasks + http.get + 解析。
|
||
const api = readSource('src/pages/tasks/shop-data-api.ts')
|
||
assert.match(api, /\/api\/admin\/shop-data-crawl-tasks/)
|
||
assert.match(api, /fetchShopDataTaskList/)
|
||
assert.match(api, /http\.get/)
|
||
const model = readSource('src/pages/tasks/shop-data-model.ts')
|
||
assert.equal(/axios|http\./.test(model), false, '店铺数据列表解析保持纯逻辑')
|
||
})
|