task-14: 拆分 publish 模块 API 至 types/modules/publish.ts,java-modules.ts re-export
- parsePublish/activatePublishTask/activatePublishFile/getPublishItemsPage/getPublishItemsPageUrl/getPublishTaskProgressBatch/submitPublishTaskResult/getPublishDashboard/getPublishHistory/deletePublishTask - Publish 全套类型随模块迁移;progressBatch 保留 task_ids 请求体并应用进度超时 - 10 个测试:parse URL/超时/activate/itemsPage 参数/itemsPageUrl/progressBatch 去重+超时/submitResult 请求体/dashboard/delete/export compat
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { http } from '../src/shared/api/http.ts'
|
||||
import {
|
||||
parsePublish,
|
||||
activatePublishTask,
|
||||
activatePublishFile,
|
||||
getPublishItemsPage,
|
||||
getPublishItemsPageUrl,
|
||||
getPublishTaskProgressBatch,
|
||||
submitPublishTaskResult,
|
||||
getPublishDashboard,
|
||||
getPublishHistory,
|
||||
deletePublishTask,
|
||||
type PublishParseRequest,
|
||||
type PublishTaskResultRequest,
|
||||
type PublishTaskBatchVo,
|
||||
} from '../src/shared/api/types/modules/publish.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 } })
|
||||
|
||||
const parseRequest: Omit<PublishParseRequest, 'user_id'> = {
|
||||
files: [{ fileKey: 'k1', originalFilename: 'a.xlsx' }],
|
||||
publish_country: 'US',
|
||||
sync_countries: ['CA'],
|
||||
}
|
||||
|
||||
test('test_publish_parse_url_payload', async (t) => {
|
||||
setupWindow()
|
||||
let captured: { url?: string; method?: string; data?: unknown; timeout?: number } = {}
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return okResponse({ taskId: 1, files: [] })
|
||||
})
|
||||
await parsePublish(parseRequest)
|
||||
assert.equal(captured.url, '/newApi/api/publish/parse')
|
||||
assert.equal(captured.method, 'POST')
|
||||
assert.deepEqual(captured.data, { ...parseRequest, user_id: 42 })
|
||||
assert.equal(captured.timeout, 180000)
|
||||
})
|
||||
|
||||
test('test_publish_activate_task', async (t) => {
|
||||
setupWindow()
|
||||
let captured: { url?: string; method?: string } = {}
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return okResponse(null)
|
||||
})
|
||||
await activatePublishTask(7)
|
||||
assert.equal(captured.url, '/newApi/api/publish/tasks/7/activate')
|
||||
assert.equal(captured.method, 'POST')
|
||||
})
|
||||
|
||||
test('test_publish_activate_file', async (t) => {
|
||||
setupWindow()
|
||||
let captured: { url?: string; method?: string } = {}
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return okResponse(null)
|
||||
})
|
||||
await activatePublishFile(7, 9)
|
||||
assert.equal(captured.url, '/newApi/api/publish/tasks/7/files/9/activate')
|
||||
assert.equal(captured.method, 'POST')
|
||||
})
|
||||
|
||||
test('test_publish_items_page_params', async (t) => {
|
||||
setupWindow()
|
||||
let captured: { url?: string; params?: Record<string, unknown> } = {}
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return okResponse({ taskId: 7, fileId: 9, page: 2, pageSize: 50, total: 0, totalPages: 0, items: [] })
|
||||
})
|
||||
await getPublishItemsPage(7, 9, 2, 50)
|
||||
assert.equal(captured.url, '/newApi/api/publish/tasks/7/items')
|
||||
assert.deepEqual(captured.params, { file_id: 9, page: 2, page_size: 50 })
|
||||
})
|
||||
|
||||
test('test_publish_items_page_url', async (t) => {
|
||||
setupWindow()
|
||||
const url = getPublishItemsPageUrl(7, 9, 100, 1)
|
||||
assert.equal(url, 'http://localhost/newApi/api/publish/tasks/7/items?file_id=9&page=1&page_size=100')
|
||||
})
|
||||
|
||||
test('test_publish_progress_batch_timeout', async (t) => {
|
||||
setupWindow()
|
||||
let captured: { url?: string; method?: string; data?: unknown; timeout?: number } = {}
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return okResponse({ items: [], missingTaskIds: [] })
|
||||
})
|
||||
const result = await getPublishTaskProgressBatch([5, 3, 5])
|
||||
assert.equal(captured.url, '/newApi/api/publish/tasks/progress/batch')
|
||||
assert.equal(captured.method, 'POST')
|
||||
assert.deepEqual(captured.data, { task_ids: [3, 5] })
|
||||
assert.equal(captured.timeout, 10000)
|
||||
assert.deepEqual(result, { items: [], missingTaskIds: [] })
|
||||
|
||||
const empty = await getPublishTaskProgressBatch([])
|
||||
assert.deepEqual(empty as PublishTaskBatchVo, { items: [], missingTaskIds: [] })
|
||||
})
|
||||
|
||||
test('test_publish_submit_result_body', async (t) => {
|
||||
setupWindow()
|
||||
let captured: { url?: string; method?: string; data?: unknown } = {}
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return okResponse(null)
|
||||
})
|
||||
const body: PublishTaskResultRequest = { files: [{ fileId: 9, rows: [{ a: 1 }] }] }
|
||||
await submitPublishTaskResult(7, body)
|
||||
assert.equal(captured.url, '/newApi/api/publish/tasks/7/result')
|
||||
assert.equal(captured.method, 'POST')
|
||||
assert.deepEqual(captured.data, body)
|
||||
})
|
||||
|
||||
test('test_publish_dashboard_history', async (t) => {
|
||||
setupWindow()
|
||||
const calls: { url?: string; params?: Record<string, unknown> }[] = []
|
||||
mockRequest(t, (config) => {
|
||||
calls.push(config)
|
||||
return okResponse({ pendingCount: 0, runningCount: 0, successCount: 1, failedCount: 0 })
|
||||
})
|
||||
await getPublishDashboard()
|
||||
await getPublishHistory()
|
||||
assert.equal(calls[0].url, '/newApi/api/publish/dashboard')
|
||||
assert.deepEqual(calls[0].params, { user_id: 42 })
|
||||
assert.equal(calls[1].url, '/newApi/api/publish/history')
|
||||
assert.deepEqual(calls[1].params, { user_id: 42 })
|
||||
})
|
||||
|
||||
test('test_publish_delete_task', async (t) => {
|
||||
setupWindow()
|
||||
let captured: { url?: string; method?: string; params?: Record<string, unknown> } = {}
|
||||
mockRequest(t, (config) => {
|
||||
captured = config
|
||||
return okResponse(null)
|
||||
})
|
||||
await deletePublishTask(7)
|
||||
assert.equal(captured.url, '/newApi/api/publish/tasks/7')
|
||||
assert.equal(captured.method, 'DELETE')
|
||||
assert.deepEqual(captured.params, { user_id: 42 })
|
||||
})
|
||||
|
||||
test('test_publish_export_compat', async (t) => {
|
||||
setupWindow()
|
||||
const fromJavaModules = await import('../src/shared/api/java-modules.ts')
|
||||
const fromPublish = await import('../src/shared/api/types/modules/publish.ts')
|
||||
const names = [
|
||||
'parsePublish', 'activatePublishTask', 'activatePublishFile', 'getPublishItemsPage',
|
||||
'getPublishItemsPageUrl', 'getPublishTaskProgressBatch', 'submitPublishTaskResult',
|
||||
'getPublishDashboard', 'getPublishHistory', 'deletePublishTask',
|
||||
]
|
||||
for (const name of names) {
|
||||
assert.equal(fromJavaModules[name], fromPublish[name], `${name} 应为同一引用`)
|
||||
}
|
||||
const summary = { id: 1, taskNo: 'T1', status: 'RUNNING' }
|
||||
assert.equal(summary.status, 'RUNNING')
|
||||
const itemsPage = { taskId: 1, fileId: 2, page: 1, pageSize: 100, total: 0, totalPages: 0, items: [] }
|
||||
assert.equal(itemsPage.totalPages, 0)
|
||||
})
|
||||
Reference in New Issue
Block a user