8a20f85ca6
原来「按商品状态(全量)」写死在售。现在用户端可勾选 在售/缺少的报价/定价问题 (默认只勾在售),勾几个跑几个,状态清单随循环任务落库并在每次派发子任务时下发。 - 载荷的 mode 与状态清单都取派发上下文(childTaskRequest),不再读界面当前勾选: 循环跑到第 N 轮时用户可能已经改过勾选,用界面值会把没勾的状态跑出去。 - listingStatuses 白名单 Active/PricingIssue/MissingOffer 三值,校验放在 try 之外—— try 里的 catch 会把「不支持的商品状态」改写成「循环任务序列化失败」,那是句假话。 - V159 给 biz_price_track_loop_run 加 listing_statuses_json(可空=本特性之前的行=在售)。 - 状态标识用我们自己的键 MissingOffer,页面真实取值是 OfferMissing(2026-10-17 黄建新 英国站真机实锤),两者不同形是有意的:改键名会断载荷兼容。
278 lines
11 KiB
TypeScript
278 lines
11 KiB
TypeScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { http } from '../src/shared/api/http.ts'
|
|
import {
|
|
listPriceTrackCandidates,
|
|
addPriceTrackCandidate,
|
|
deletePriceTrackCandidate,
|
|
getPriceTrackCountryPreference,
|
|
putPriceTrackCountryPreference,
|
|
matchPriceTrackShops,
|
|
createPriceTrackTask,
|
|
deletePriceTrackTask,
|
|
getPriceTrackTasksBatch,
|
|
createPriceTrackLoopRun,
|
|
getPriceTrackLoopRun,
|
|
dispatchNextPriceTrackLoopRun,
|
|
completePriceTrackLoopChild,
|
|
stopPriceTrackLoopRun,
|
|
getPriceTrackTaskProgressBatch,
|
|
getPriceTrackResultDownloadUrl,
|
|
getTaskSkipPriceAsinsPaginated,
|
|
markPriceTrackDispatchFailed,
|
|
checkTaskSkipPriceAsin,
|
|
deletePendingPriceTrackShopResult,
|
|
getPriceTrackDashboard,
|
|
getPriceTrackHistory,
|
|
deletePriceTrackHistory,
|
|
type PriceTrackCreateTaskRequest,
|
|
type PriceTrackLoopRunCreateRequest,
|
|
} from '../src/shared/api/types/modules/price-track.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_price_track_candidates_crud', async (t) => {
|
|
setupWindow()
|
|
const calls: { url?: string; method?: string; data?: unknown }[] = []
|
|
mockRequest(t, (config) => {
|
|
calls.push(config)
|
|
return okResponse({ id: 1, shopName: 's1' })
|
|
})
|
|
await listPriceTrackCandidates()
|
|
await addPriceTrackCandidate('店铺A')
|
|
await deletePriceTrackCandidate(3)
|
|
assert.equal(calls[0].url, '/newApi/api/price-track/candidates?user_id=42')
|
|
assert.equal(calls[0].method, 'GET')
|
|
assert.equal(calls[1].url, '/newApi/api/price-track/candidates')
|
|
assert.equal(calls[1].method, 'POST')
|
|
assert.deepEqual(calls[1].data, { userId: 42, shopName: '店铺A' })
|
|
assert.equal(calls[2].url, '/newApi/api/price-track/candidates/3?user_id=42')
|
|
assert.equal(calls[2].method, 'DELETE')
|
|
})
|
|
|
|
test('test_price_track_country_preference', async (t) => {
|
|
setupWindow()
|
|
const calls: { url?: string; method?: string; data?: unknown }[] = []
|
|
mockRequest(t, (config) => {
|
|
calls.push(config)
|
|
return okResponse({ userId: 42, countryCodes: ['US'] })
|
|
})
|
|
const result = await getPriceTrackCountryPreference()
|
|
await putPriceTrackCountryPreference(['US', 'CA'])
|
|
assert.equal(calls[0].url, '/newApi/api/price-track/country-preference?user_id=42')
|
|
assert.equal(result.countryCodes[0], 'US')
|
|
assert.equal(calls[1].url, '/newApi/api/price-track/country-preference')
|
|
assert.equal(calls[1].method, 'PUT')
|
|
assert.deepEqual(calls[1].data, { userId: 42, countryCodes: ['US', 'CA'] })
|
|
})
|
|
|
|
test('test_price_track_match_shops', async (t) => {
|
|
setupWindow()
|
|
let captured: { url?: string; method?: string; data?: unknown } = {}
|
|
mockRequest(t, (config) => {
|
|
captured = config
|
|
return okResponse({ items: [] })
|
|
})
|
|
await matchPriceTrackShops(['店铺A'], { asinFiles: ['k1'], countryCodes: ['US'] })
|
|
assert.equal(captured.url, '/newApi/api/price-track/match-shops')
|
|
assert.equal(captured.method, 'POST')
|
|
assert.deepEqual(captured.data, { userId: 42, shopNames: ['店铺A'], asinFiles: ['k1'], countryCodes: ['US'] })
|
|
})
|
|
|
|
test('test_price_track_create_task', async (t) => {
|
|
setupWindow()
|
|
let captured: { url?: string; method?: string; data?: unknown } = {}
|
|
mockRequest(t, (config) => {
|
|
captured = config
|
|
return okResponse({ taskId: 7, items: [] })
|
|
})
|
|
const request: Omit<PriceTrackCreateTaskRequest, 'userId'> = {
|
|
statusMode: true,
|
|
asinMode: false,
|
|
items: [{ shopName: '店铺A' }],
|
|
asinFiles: [],
|
|
countryCodes: ['US'],
|
|
}
|
|
await createPriceTrackTask(request)
|
|
assert.equal(captured.url, '/newApi/api/price-track/tasks')
|
|
assert.equal(captured.method, 'POST')
|
|
assert.deepEqual(captured.data, { ...request, userId: 42 })
|
|
})
|
|
|
|
test('test_price_track_tasks_batch_delete', async (t) => {
|
|
setupWindow()
|
|
const calls: { url?: string; method?: string; data?: unknown }[] = []
|
|
mockRequest(t, (config) => {
|
|
calls.push(config)
|
|
return okResponse({ items: [], missingTaskIds: [] })
|
|
})
|
|
await getPriceTrackTasksBatch([5, 3])
|
|
await deletePriceTrackTask(7)
|
|
assert.equal(calls[0].url, '/newApi/api/price-track/tasks/batch')
|
|
assert.equal(calls[0].method, 'POST')
|
|
assert.deepEqual(calls[0].data, { taskIds: [5, 3], userId: 42 })
|
|
assert.equal(calls[1].url, '/newApi/api/price-track/tasks/7?user_id=42')
|
|
assert.equal(calls[1].method, 'DELETE')
|
|
})
|
|
|
|
test('test_price_track_loop_run', async (t) => {
|
|
setupWindow()
|
|
const calls: { url?: string; method?: string; data?: unknown }[] = []
|
|
mockRequest(t, (config) => {
|
|
calls.push(config)
|
|
return okResponse({ id: 1, status: 'RUNNING' })
|
|
})
|
|
const request: Omit<PriceTrackLoopRunCreateRequest, 'userId'> = {
|
|
statusMode: true,
|
|
asinMode: false,
|
|
items: [],
|
|
asinFiles: [],
|
|
countryCodes: ['US'],
|
|
executionMode: 'FINITE',
|
|
targetRounds: 3,
|
|
}
|
|
await createPriceTrackLoopRun(request)
|
|
await getPriceTrackLoopRun(1)
|
|
await dispatchNextPriceTrackLoopRun(1)
|
|
await completePriceTrackLoopChild(1, 7)
|
|
await stopPriceTrackLoopRun(1)
|
|
assert.equal(calls[0].url, '/newApi/api/price-track/loop-runs')
|
|
assert.equal(calls[0].method, 'POST')
|
|
assert.deepEqual(calls[0].data, { ...request, userId: 42 })
|
|
assert.equal(calls[1].url, '/newApi/api/price-track/loop-runs/1?user_id=42')
|
|
assert.equal(calls[1].method, 'GET')
|
|
assert.equal(calls[2].url, '/newApi/api/price-track/loop-runs/1/dispatch-next?user_id=42')
|
|
assert.equal(calls[2].method, 'POST')
|
|
assert.equal(calls[3].url, '/newApi/api/price-track/loop-runs/1/child-finished?user_id=42')
|
|
assert.deepEqual(calls[3].data, { childTaskId: 7 })
|
|
assert.equal(calls[4].url, '/newApi/api/price-track/loop-runs/1/stop?user_id=42')
|
|
assert.equal(calls[4].method, 'POST')
|
|
})
|
|
|
|
test('test_price_track_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 getPriceTrackTaskProgressBatch([5, 3])
|
|
assert.equal(captured.url, '/newApi/api/price-track/tasks/progress/batch')
|
|
assert.equal(captured.method, 'POST')
|
|
assert.deepEqual(captured.data, { taskIds: [3, 5], userId: 42 })
|
|
assert.equal(captured.timeout, 10000)
|
|
assert.deepEqual(result, { items: [], missingTaskIds: [] })
|
|
})
|
|
|
|
test('test_price_track_skip_asin_apis', async (t) => {
|
|
setupWindow()
|
|
const calls: { url?: string; method?: string; params?: Record<string, unknown>; data?: unknown }[] = []
|
|
mockRequest(t, (config) => {
|
|
calls.push(config)
|
|
return okResponse({ page: 1, pageSize: 1000, total: 0, totalPages: 0, skipAsinsByCountry: {} })
|
|
})
|
|
await getTaskSkipPriceAsinsPaginated(7, 1, 1000, { shopName: '店铺A', countryCode: 'US' })
|
|
await markPriceTrackDispatchFailed(7, '超时')
|
|
await checkTaskSkipPriceAsin(7, 'US', 'B001')
|
|
await deletePendingPriceTrackShopResult('店铺A')
|
|
assert.equal(calls[0].url, '/newApi/api/price-track/tasks/7/skip-asins/paginated')
|
|
assert.deepEqual(calls[0].params, { page: 1, page_size: 1000, shop_name: '店铺A', country_code: 'US' })
|
|
assert.equal(calls[1].url, '/newApi/api/price-track/tasks/7/dispatch-failed?user_id=42')
|
|
assert.deepEqual(calls[1].data, { errorMessage: '超时' })
|
|
assert.equal(calls[2].url, '/newApi/api/price-track/tasks/7/skip-asin/check')
|
|
assert.deepEqual(calls[2].params, { country: 'US', asin: 'B001' })
|
|
assert.equal(calls[3].url, '/newApi/api/price-track/pending-shop-result?user_id=42&shop_name=%E5%BA%97%E9%93%BAA')
|
|
})
|
|
|
|
test('test_price_track_dashboard_history', async (t) => {
|
|
setupWindow()
|
|
const calls: { url?: string; method?: string }[] = []
|
|
mockRequest(t, (config) => {
|
|
calls.push(config)
|
|
return okResponse({ candidateCount: 0, processedTaskCount: 0, successTaskCount: 1, failedTaskCount: 0 })
|
|
})
|
|
await getPriceTrackDashboard()
|
|
await getPriceTrackHistory()
|
|
await deletePriceTrackHistory(9)
|
|
assert.equal(calls[0].url, '/newApi/api/price-track/dashboard?user_id=42')
|
|
assert.equal(calls[1].url, '/newApi/api/price-track/history?user_id=42')
|
|
assert.equal(calls[2].url, '/newApi/api/price-track/history/9?user_id=42')
|
|
assert.equal(calls[2].method, 'DELETE')
|
|
})
|
|
|
|
test('test_price_track_download_url', async (t) => {
|
|
setupWindow()
|
|
const url = getPriceTrackResultDownloadUrl(7)
|
|
assert.equal(url, 'http://localhost/newApi/api/price-track/results/7/download?user_id=42')
|
|
})
|
|
|
|
test('test_price_track_export_compat', async (t) => {
|
|
setupWindow()
|
|
const fromJavaModules = await import('../src/shared/api/java-modules.ts')
|
|
const fromPriceTrack = await import('../src/shared/api/types/modules/price-track.ts')
|
|
const names = [
|
|
'listPriceTrackCandidates', 'addPriceTrackCandidate', 'deletePriceTrackCandidate',
|
|
'getPriceTrackCountryPreference', 'putPriceTrackCountryPreference', 'matchPriceTrackShops',
|
|
'createPriceTrackTask', 'deletePriceTrackTask', 'getPriceTrackTasksBatch',
|
|
'createPriceTrackLoopRun', 'getPriceTrackLoopRun', 'dispatchNextPriceTrackLoopRun',
|
|
'completePriceTrackLoopChild', 'stopPriceTrackLoopRun', 'getPriceTrackTaskProgressBatch',
|
|
'getPriceTrackResultDownloadUrl', 'getTaskSkipPriceAsinsPaginated', 'markPriceTrackDispatchFailed',
|
|
'checkTaskSkipPriceAsin', 'deletePendingPriceTrackShopResult', 'getPriceTrackDashboard',
|
|
'getPriceTrackHistory', 'deletePriceTrackHistory',
|
|
]
|
|
for (const name of names) {
|
|
assert.equal(fromJavaModules[name], fromPriceTrack[name], `${name} 应为同一引用`)
|
|
}
|
|
const item = { id: 1, shopName: 's1', matched: true }
|
|
assert.equal(item.matched, true)
|
|
})
|
|
|
|
test('test_price_track_unwrap_and_error', async (t) => {
|
|
setupWindow()
|
|
mockRequest(t, () => okResponse({ taskId: 1, items: [] }))
|
|
const result = await createPriceTrackTask({ statusMode: true, asinMode: false, items: [], asinFiles: [], countryCodes: [] })
|
|
assert.equal(result.taskId, 1)
|
|
|
|
mockRequest(t, () => Promise.resolve({ data: { success: false, message: '店铺不存在' } }))
|
|
await assert.rejects(addPriceTrackCandidate('x'), /店铺不存在/)
|
|
})
|
|
|
|
test('test_price_track_loop_run_listing_statuses_passthrough', async (t) => {
|
|
setupWindow()
|
|
let captured: { url?: string; data?: unknown } = {}
|
|
mockRequest(t, (config) => {
|
|
captured = config
|
|
return okResponse({ id: 1, status: 'RUNNING' })
|
|
})
|
|
const request: Omit<PriceTrackLoopRunCreateRequest, 'userId'> = {
|
|
statusMode: true,
|
|
listingStatuses: ['Active', 'MissingOffer', 'PricingIssue'],
|
|
asinMode: false,
|
|
items: [],
|
|
asinFiles: [],
|
|
countryCodes: ['US'],
|
|
executionMode: 'FINITE',
|
|
targetRounds: 1,
|
|
}
|
|
await createPriceTrackLoopRun(request)
|
|
assert.deepEqual(
|
|
(captured.data as Record<string, unknown>).listingStatuses,
|
|
['Active', 'MissingOffer', 'PricingIssue'],
|
|
'勾选的状态清单必须原样提交给后端——循环每一轮子任务的载荷都从后端记录回读,不吃界面当下勾选',
|
|
)
|
|
})
|