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).window = { localStorage: { getItem: () => '42' }, location: { origin: 'http://localhost' }, } } function mockRequest( t: Parameters[1] extends (t: infer T) => unknown ? T : never, impl: (config: { url?: string; method?: string; params?: Record; data?: unknown; timeout?: number }) => Promise, ) { 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; 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; 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; 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], userId: 42 }) }) test('test_product_risk_dashboard_history_download', async (t) => { setupWindow() const calls: { url?: string; method?: string; params?: Record }[] = [] 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; 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; 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; 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], userId: 42 }) }) test('test_shop_match_dashboard_history_skip_asins', async (t) => { setupWindow() const calls: { url?: string; method?: string; params?: Record }[] = [] 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], userId: 42 }) 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], userId: 42 }) 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') })