Files
crawler-plugin/frontend-vue/tests/modules-shop-match.test.ts
T
huangzd1997 be02833b11 task-19: 拆分 shop-match / product-risk 模块 API 至 types/modules/shop-match.ts,java-modules.ts re-export
- product-risk 15 函数 + shop-match 17 函数 + 全套类型(含 ShopMatch 对 ProductRisk 的 type alias)
- 保留模块的 PatrolDelete/QueryAsin/Withdraw alias 通过 import type 兜底引用
- QueryAsinCountryAsins 内联定义避免跨模块循环依赖
- 10 个测试:双模块 CRUD/preference/match/task 系列/progressBatch 双端/dashboard/skip-asins/download/export compat
2026-08-31 19:42:18 +08:00

273 lines
12 KiB
TypeScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { http } from '../src/shared/api/http.ts'
import {
listProductRiskCandidates,
addProductRiskCandidate,
deleteProductRiskCandidate,
getProductRiskCountryPreference,
putProductRiskCountryPreference,
matchProductRiskShops,
getProductRiskDashboard,
getProductRiskHistory,
deleteProductRiskHistory,
createProductRiskTask,
deleteProductRiskTask,
deletePendingProductRiskShopResult,
getProductRiskTasksBatch,
getProductRiskResultDownloadUrl,
getProductRiskTaskProgressBatch,
listShopMatchCandidates,
addShopMatchCandidate,
deleteShopMatchCandidate,
getShopMatchCountryPreference,
putShopMatchCountryPreference,
matchShopMatchShops,
getShopMatchDashboard,
getShopMatchHistory,
deleteShopMatchHistory,
createShopMatchTask,
deleteShopMatchTask,
activateShopMatchTask,
completeShopMatchTaskStage,
getShopMatchTasksBatch,
getShopMatchResultDownloadUrl,
getShopMatchTaskSkipAsinsPaginated,
getShopMatchTaskProgressBatch,
} from '../src/shared/api/types/modules/shop-match.ts'
import {
type ShopMatchCreateTaskItem,
} from '../src/shared/api/types/modules/shop-match.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_product_risk_candidates_crud', async (t) => {
setupWindow()
const calls: { url?: string; method?: string; params?: Record<string, unknown>; data?: unknown }[] = []
mockRequest(t, (config) => {
calls.push(config)
return okResponse({ id: 1, shop_name: 's1' })
})
await listProductRiskCandidates()
await addProductRiskCandidate('店铺A')
await deleteProductRiskCandidate(3)
assert.equal(calls[0].url, '/newApi/api/product-risk-resolve/candidates')
assert.deepEqual(calls[0].params, { user_id: 42 })
assert.equal(calls[1].url, '/newApi/api/product-risk-resolve/candidates')
assert.equal(calls[1].method, 'POST')
assert.deepEqual(calls[1].data, { user_id: 42, shop_name: '店铺A' })
assert.equal(calls[2].url, '/newApi/api/product-risk-resolve/candidates/3')
assert.deepEqual(calls[2].params, { user_id: 42 })
assert.equal(calls[2].method, 'DELETE')
})
test('test_product_risk_preference_match', 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 getProductRiskCountryPreference()
await putProductRiskCountryPreference(['US'])
await matchProductRiskShops(['店铺A'])
assert.equal(calls[0].url, '/newApi/api/product-risk-resolve/country-preference')
assert.deepEqual(calls[0].params, { user_id: 42 })
assert.equal(calls[1].url, '/newApi/api/product-risk-resolve/country-preference')
assert.equal(calls[1].method, 'PUT')
assert.deepEqual(calls[1].data, { user_id: 42, country_codes: ['US'] })
assert.equal(calls[2].url, '/newApi/api/product-risk-resolve/match-shops')
assert.deepEqual(calls[2].data, { user_id: 42, shop_names: ['店铺A'] })
})
test('test_product_risk_task_apis', async (t) => {
setupWindow()
const calls: { url?: string; method?: string; params?: Record<string, unknown>; data?: unknown }[] = []
mockRequest(t, (config) => {
calls.push(config)
return okResponse({ taskId: 1, items: [] })
})
await createProductRiskTask([{ shopName: '店铺A', matched: true }])
await deleteProductRiskTask(7)
await deletePendingProductRiskShopResult('店铺A')
await getProductRiskTasksBatch([5, 3])
assert.equal(calls[0].url, '/newApi/api/product-risk-resolve/tasks')
assert.deepEqual(calls[0].data, { user_id: 42, items: [{ shopName: '店铺A', matched: true }] })
assert.equal(calls[1].url, '/newApi/api/product-risk-resolve/tasks/7')
assert.equal(calls[1].method, 'DELETE')
assert.deepEqual(calls[1].params, { user_id: 42 })
assert.equal(calls[2].url, '/newApi/api/product-risk-resolve/pending-shop-result')
assert.deepEqual(calls[2].params, { user_id: 42, shop_name: '店铺A' })
assert.equal(calls[3].url, '/newApi/api/product-risk-resolve/tasks/batch')
assert.deepEqual(calls[3].data, { taskIds: [5, 3] })
})
test('test_product_risk_dashboard_history_download', async (t) => {
setupWindow()
const calls: { url?: string; method?: string; params?: Record<string, unknown> }[] = []
mockRequest(t, (config) => {
calls.push(config)
return okResponse({ candidateCount: 0, processedTaskCount: 0, successTaskCount: 1, failedTaskCount: 0 })
})
await getProductRiskDashboard()
await getProductRiskHistory()
await deleteProductRiskHistory(9)
assert.equal(calls[0].url, '/newApi/api/product-risk-resolve/dashboard')
assert.deepEqual(calls[0].params, { user_id: 42 })
assert.equal(calls[1].url, '/newApi/api/product-risk-resolve/history')
assert.deepEqual(calls[1].params, { user_id: 42 })
assert.equal(calls[2].url, '/newApi/api/product-risk-resolve/history/9')
assert.equal(calls[2].method, 'DELETE')
assert.equal(
getProductRiskResultDownloadUrl(7),
'http://localhost/newApi/api/product-risk-resolve/results/7/download?user_id=42',
)
})
test('test_shop_match_candidates_crud', async (t) => {
setupWindow()
const calls: { url?: string; method?: string; params?: Record<string, unknown>; data?: unknown }[] = []
mockRequest(t, (config) => {
calls.push(config)
return okResponse({ id: 1, shop_name: 's1' })
})
await listShopMatchCandidates()
await addShopMatchCandidate('店铺A')
await deleteShopMatchCandidate(3)
assert.equal(calls[0].url, '/newApi/api/shop-match/candidates')
assert.deepEqual(calls[0].params, { user_id: 42 })
assert.equal(calls[1].url, '/newApi/api/shop-match/candidates')
assert.equal(calls[1].method, 'POST')
assert.deepEqual(calls[1].data, { user_id: 42, shop_name: '店铺A' })
assert.equal(calls[2].url, '/newApi/api/shop-match/candidates/3')
assert.equal(calls[2].method, 'DELETE')
})
test('test_shop_match_preference_match', 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 getShopMatchCountryPreference()
await putShopMatchCountryPreference(['US'])
await matchShopMatchShops(['店铺A'])
assert.equal(calls[0].url, '/newApi/api/shop-match/country-preference')
assert.deepEqual(calls[0].params, { user_id: 42 })
assert.equal(calls[1].url, '/newApi/api/shop-match/country-preference')
assert.equal(calls[1].method, 'PUT')
assert.deepEqual(calls[1].data, { user_id: 42, country_codes: ['US'] })
assert.equal(calls[2].url, '/newApi/api/shop-match/match-shops')
assert.deepEqual(calls[2].data, { user_id: 42, shop_names: ['店铺A'] })
})
test('test_shop_match_task_apis', async (t) => {
setupWindow()
const calls: { url?: string; method?: string; params?: Record<string, unknown>; data?: unknown }[] = []
mockRequest(t, (config) => {
calls.push(config)
return okResponse({ taskId: 1, items: [] })
})
const item: ShopMatchCreateTaskItem = { shopName: '店铺A', matched: true }
await createShopMatchTask([item], ['US'], ['2026-09-01 10:00'])
await deleteShopMatchTask(7)
await activateShopMatchTask(7, 1)
await completeShopMatchTaskStage(7, 1)
await getShopMatchTasksBatch([5, 3])
assert.equal(calls[0].url, '/newApi/api/shop-match/tasks')
assert.equal(calls[0].method, 'POST')
assert.deepEqual(calls[0].data, { user_id: 42, items: [item], country_codes: ['US'], schedule_times: ['2026-09-01 10:00'] })
assert.equal(calls[1].url, '/newApi/api/shop-match/tasks/7')
assert.equal(calls[1].method, 'DELETE')
assert.equal(calls[2].url, '/newApi/api/shop-match/tasks/7/activate?user_id=42&stage_index=1')
assert.equal(calls[2].method, 'POST')
assert.equal(calls[3].url, '/newApi/api/shop-match/tasks/7/stage-finished?user_id=42')
assert.deepEqual(calls[3].data, { stage_index: 1 })
assert.equal(calls[4].url, '/newApi/api/shop-match/tasks/batch')
assert.deepEqual(calls[4].data, { taskIds: [5, 3] })
})
test('test_shop_match_dashboard_history_skip_asins', async (t) => {
setupWindow()
const calls: { url?: string; method?: string; params?: Record<string, unknown> }[] = []
mockRequest(t, (config) => {
calls.push(config)
return okResponse({ candidateCount: 0, processedTaskCount: 0, successTaskCount: 1, failedTaskCount: 0 })
})
await getShopMatchDashboard()
await getShopMatchHistory()
await deleteShopMatchHistory(9)
await getShopMatchTaskSkipAsinsPaginated(7, 1, 1000, { shopName: '店铺A' })
assert.equal(calls[0].url, '/newApi/api/shop-match/dashboard')
assert.deepEqual(calls[0].params, { user_id: 42 })
assert.equal(calls[1].url, '/newApi/api/shop-match/history')
assert.equal(calls[2].url, '/newApi/api/shop-match/history/9')
assert.equal(calls[2].method, 'DELETE')
assert.equal(calls[3].url, '/newApi/api/shop-match/tasks/7/skip-asins/paginated')
assert.deepEqual(calls[3].params, { page: 1, page_size: 1000, shop_name: '店铺A' })
assert.equal(
getShopMatchResultDownloadUrl(7),
'http://localhost/newApi/api/shop-match/results/7/download?user_id=42',
)
})
test('test_shop_match_product_risk_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 shopMatch = await getShopMatchTaskProgressBatch([5, 3])
const productRisk = await getProductRiskTaskProgressBatch([5, 3])
assert.equal(calls[0].url, '/newApi/api/shop-match/tasks/progress/batch')
assert.deepEqual(calls[0].data, { taskIds: [3, 5] })
assert.equal(calls[0].timeout, 10000)
assert.equal(calls[1].url, '/newApi/api/product-risk-resolve/tasks/progress/batch')
assert.deepEqual(calls[1].data, { taskIds: [3, 5] })
assert.equal(calls[1].timeout, 10000)
assert.deepEqual(shopMatch, { items: [], missingTaskIds: [] })
assert.deepEqual(productRisk, { items: [], missingTaskIds: [] })
})
test('test_shop_match_product_risk_export_compat', async (t) => {
setupWindow()
const fromJavaModules = await import('../src/shared/api/java-modules.ts')
const fromShopMatch = await import('../src/shared/api/types/modules/shop-match.ts')
const productRiskNames = [
'listProductRiskCandidates', 'addProductRiskCandidate', 'deleteProductRiskCandidate',
'getProductRiskCountryPreference', 'putProductRiskCountryPreference', 'matchProductRiskShops',
'getProductRiskDashboard', 'getProductRiskHistory', 'deleteProductRiskHistory',
'createProductRiskTask', 'deleteProductRiskTask', 'deletePendingProductRiskShopResult',
'getProductRiskTasksBatch', 'getProductRiskResultDownloadUrl', 'getProductRiskTaskProgressBatch',
]
const shopMatchNames = [
'listShopMatchCandidates', 'addShopMatchCandidate', 'deleteShopMatchCandidate',
'getShopMatchCountryPreference', 'putShopMatchCountryPreference', 'matchShopMatchShops',
'getShopMatchDashboard', 'getShopMatchHistory', 'deleteShopMatchHistory',
'createShopMatchTask', 'deleteShopMatchTask', 'activateShopMatchTask',
'completeShopMatchTaskStage', 'getShopMatchTasksBatch', 'getShopMatchResultDownloadUrl',
'getShopMatchTaskSkipAsinsPaginated', 'getShopMatchTaskProgressBatch',
]
for (const name of [...productRiskNames, ...shopMatchNames]) {
assert.equal(fromJavaModules[name], fromShopMatch[name], `${name} 应为同一引用`)
}
const candidate = { id: 1, shop_name: 's1' }
assert.equal(candidate.shop_name, 's1')
})