task-22: 拆分 collect-data 模块 API 至 types/modules/collect-data.ts,java-modules.ts re-export
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { http } from '../src/shared/api/http.ts'
|
||||
import {
|
||||
parseCollectData,
|
||||
activateCollectDataTask,
|
||||
getCollectDataItemsPage,
|
||||
getCollectDataDashboard,
|
||||
getCollectDataHistory,
|
||||
getCollectDataTaskProgressBatch,
|
||||
deleteCollectDataTask,
|
||||
deleteCollectDataHistory,
|
||||
getCollectDataCountryPreference,
|
||||
putCollectDataCountryPreference,
|
||||
failCollectDataTask,
|
||||
} from '../src/shared/api/types/modules/collect-data.ts'
|
||||
|
||||
function setupWindow() {
|
||||
;(globalThis as Record<string, unknown>).window = {
|
||||
localStorage: { getItem: () => '42' },
|
||||
location: { origin: 'http://localhost' },
|
||||
}
|
||||
}
|
||||
|
||||
function mockRequest(
|
||||
t: Parameters<typeof test>[1] extends (t: infer T) => unknown ? T : never,
|
||||
impl: (config: { url?: string; method?: string; params?: Record<string, unknown>; data?: unknown; timeout?: number }) => Promise<unknown>,
|
||||
) {
|
||||
t.mock.method(http, 'request', impl as never)
|
||||
}
|
||||
|
||||
const okResponse = (data: unknown) => Promise.resolve({ data: { success: true, message: 'ok', data } })
|
||||
|
||||
test('test_collect_data_parse', async (t) => {
|
||||
setupWindow()
|
||||
const calls: { url?: string; method?: string; data?: unknown }[] = []
|
||||
mockRequest(t, (config) => {
|
||||
calls.push(config)
|
||||
return okResponse({ taskId: 1 })
|
||||
})
|
||||
await parseCollectData({ files: [{ fileKey: 'k1' }], task_type: 'typeA' })
|
||||
assert.equal(calls[0].url, '/newApi/api/collect-data/parse')
|
||||
assert.equal(calls[0].method, 'POST')
|
||||
assert.deepEqual(calls[0].data, { user_id: 42, files: [{ fileKey: 'k1' }], task_type: 'typeA' })
|
||||
})
|
||||
|
||||
test('test_collect_data_items_page', async (t) => {
|
||||
setupWindow()
|
||||
const calls: { url?: string; params?: Record<string, unknown> }[] = []
|
||||
mockRequest(t, (config) => {
|
||||
calls.push(config)
|
||||
return okResponse({ items: [] })
|
||||
})
|
||||
await getCollectDataItemsPage(7, 2, 100)
|
||||
assert.equal(calls[0].url, '/newApi/api/collect-data/tasks/7/items')
|
||||
assert.deepEqual(calls[0].params, { user_id: 42, page: 2, page_size: 100 })
|
||||
})
|
||||
|
||||
test('test_collect_data_progress_batch', async (t) => {
|
||||
setupWindow()
|
||||
const calls: { url?: string; method?: string; data?: unknown; timeout?: number }[] = []
|
||||
mockRequest(t, (config) => {
|
||||
calls.push(config)
|
||||
return okResponse({ items: [], missingTaskIds: [] })
|
||||
})
|
||||
const result = await getCollectDataTaskProgressBatch([5, 3], { force: true })
|
||||
assert.equal(calls[0].url, '/newApi/api/collect-data/tasks/progress/batch')
|
||||
assert.equal(calls[0].method, 'POST')
|
||||
assert.deepEqual(calls[0].data, { taskIds: [3, 5] })
|
||||
assert.equal(calls[0].timeout, 10000)
|
||||
assert.deepEqual(result, { items: [], missingTaskIds: [] })
|
||||
})
|
||||
|
||||
test('test_collect_data_fail', async (t) => {
|
||||
setupWindow()
|
||||
const calls: { url?: string; method?: string; data?: unknown }[] = []
|
||||
mockRequest(t, (config) => {
|
||||
calls.push(config)
|
||||
return okResponse(null)
|
||||
})
|
||||
await failCollectDataTask(7)
|
||||
await failCollectDataTask(7, '网络错误')
|
||||
assert.equal(calls[0].url, '/newApi/api/collect-data/tasks/7/fail?user_id=42')
|
||||
assert.equal(
|
||||
calls[1].url,
|
||||
`/newApi/api/collect-data/tasks/7/fail?user_id=42&error=${encodeURIComponent('网络错误')}`,
|
||||
)
|
||||
assert.equal(calls[0].method, 'POST')
|
||||
})
|
||||
|
||||
test('test_collect_data_activate', async (t) => {
|
||||
setupWindow()
|
||||
const calls: { url?: string; method?: string }[] = []
|
||||
mockRequest(t, (config) => {
|
||||
calls.push(config)
|
||||
return okResponse(null)
|
||||
})
|
||||
await activateCollectDataTask(7)
|
||||
assert.equal(calls[0].url, '/newApi/api/collect-data/tasks/7/activate?user_id=42')
|
||||
assert.equal(calls[0].method, 'POST')
|
||||
})
|
||||
|
||||
test('test_collect_data_preference', async (t) => {
|
||||
setupWindow()
|
||||
const calls: { url?: string; method?: string; params?: Record<string, unknown>; data?: unknown }[] = []
|
||||
mockRequest(t, (config) => {
|
||||
calls.push(config)
|
||||
return okResponse({ country_codes: ['US'] })
|
||||
})
|
||||
await getCollectDataCountryPreference()
|
||||
await putCollectDataCountryPreference(['US'])
|
||||
assert.equal(calls[0].url, '/newApi/api/collect-data/country-preference')
|
||||
assert.deepEqual(calls[0].params, { user_id: 42 })
|
||||
assert.equal(calls[1].url, '/newApi/api/collect-data/country-preference')
|
||||
assert.equal(calls[1].method, 'PUT')
|
||||
assert.deepEqual(calls[1].data, { user_id: 42, country_codes: ['US'] })
|
||||
})
|
||||
|
||||
test('test_collect_data_dashboard_history', async (t) => {
|
||||
setupWindow()
|
||||
const calls: { url?: string; params?: Record<string, unknown> }[] = []
|
||||
mockRequest(t, (config) => {
|
||||
calls.push(config)
|
||||
return okResponse({ items: [] })
|
||||
})
|
||||
await getCollectDataDashboard()
|
||||
await getCollectDataHistory()
|
||||
await getCollectDataHistory(10)
|
||||
assert.equal(calls[0].url, '/newApi/api/collect-data/dashboard')
|
||||
assert.deepEqual(calls[0].params, { user_id: 42 })
|
||||
assert.equal(calls[1].url, '/newApi/api/collect-data/history')
|
||||
assert.deepEqual(calls[1].params, { user_id: 42, limit: 50 })
|
||||
assert.deepEqual(calls[2].params, { user_id: 42, limit: 10 })
|
||||
})
|
||||
|
||||
test('test_collect_data_delete', async (t) => {
|
||||
setupWindow()
|
||||
const calls: { url?: string; method?: string; params?: Record<string, unknown> }[] = []
|
||||
mockRequest(t, (config) => {
|
||||
calls.push(config)
|
||||
return okResponse(null)
|
||||
})
|
||||
await deleteCollectDataTask(7)
|
||||
await deleteCollectDataHistory(9)
|
||||
assert.equal(calls[0].url, '/newApi/api/collect-data/tasks/7')
|
||||
assert.equal(calls[0].method, 'DELETE')
|
||||
assert.deepEqual(calls[0].params, { user_id: 42 })
|
||||
assert.equal(calls[1].url, '/newApi/api/collect-data/history/9')
|
||||
assert.equal(calls[1].method, 'DELETE')
|
||||
})
|
||||
|
||||
test('test_collect_data_export_compat', async (t) => {
|
||||
setupWindow()
|
||||
const fromJavaModules = await import('../src/shared/api/java-modules.ts')
|
||||
const fromCollectData = await import('../src/shared/api/types/modules/collect-data.ts')
|
||||
const names = [
|
||||
'parseCollectData', 'activateCollectDataTask', 'getCollectDataItemsPage',
|
||||
'getCollectDataDashboard', 'getCollectDataHistory', 'getCollectDataTaskProgressBatch',
|
||||
'deleteCollectDataTask', 'deleteCollectDataHistory', 'getCollectDataCountryPreference',
|
||||
'putCollectDataCountryPreference', 'failCollectDataTask',
|
||||
]
|
||||
for (const name of names) {
|
||||
assert.equal(fromJavaModules[name], fromCollectData[name], `${name} 应为同一引用`)
|
||||
}
|
||||
const filters = { minAmount: 10, max_amount: 100, fba: true }
|
||||
assert.equal(filters.fba, true)
|
||||
})
|
||||
|
||||
test('test_collect_data_unwrap_and_error', async (t) => {
|
||||
setupWindow()
|
||||
mockRequest(t, () => okResponse({ taskId: 1 }))
|
||||
const result = await parseCollectData({ files: [] })
|
||||
assert.equal(result.taskId, 1)
|
||||
|
||||
mockRequest(t, () => Promise.resolve({ data: { success: false, message: '文件不存在' } }))
|
||||
await assert.rejects(parseCollectData({ files: [] }), /文件不存在/)
|
||||
})
|
||||
Reference in New Issue
Block a user