'use strict'; const assert = require('node:assert/strict'); const { Op } = require('sequelize'); const WaveService = require('../../../../../app/service/systemMethods/order/wave'); const runWaveStrategyQuickScanRunner = require('../../../../../app/lib/wave_strategy_quick_scan_runner'); /** * 构建快速扫描测试上下文。 * * @return {{ service: WaveService, cache: Map }} 服务实例与缓存。 */ function createHarness() { const cache = new Map(); const ctx = { app: { config: {}, async setCacheAsync(key, payload) { cache.set(key, payload); }, async getCacheAsync(key) { return cache.get(key) || null; }, async delCacheAsync(key) { cache.delete(key); }, }, user: { store_id: 100000, }, logger: { info() {}, warn() {}, error() {}, }, model: { wms_wave_strategy: { async findAll() { return []; }, }, }, }; return { service: new WaveService(ctx), cache, }; } describe('test/app/service/systemMethods/order/wave.waveStrategyQuickScan.test.js', () => { it('传入 warehouse_id 参数时应抛错', async () => { const { service } = createHarness(); await assert.rejects( async () => { await service.waveStrategyQuickScan({ warehouse_id: 100000 }); }, error => { assert.match(String(error && error.message || ''), /不支持传入 warehouse_id/u); return true; } ); }); it('同仓存在运行任务时应返回已有 task_id 且不重复启动', async () => { const { service } = createHarness(); const runCalls = []; service.runWaveStrategyQuickScanTask = async params => { runCalls.push(params); await service.setWaveStrategyQuickScanTaskProgress(params.taskId, 'running', { warehouse_id: 100000, }); }; const firstResult = await service.waveStrategyQuickScan({}); await new Promise(resolve => setImmediate(resolve)); const secondResult = await service.waveStrategyQuickScan({}); assert.equal(firstResult.flag, 'success'); assert.equal(secondResult.flag, 'success'); assert.equal(secondResult.task_id, firstResult.task_id); assert.equal(runCalls.length, 1); }); it('执行任务时应按策略命中写入扫描结果和策略版本', async () => { const { service } = createHarness(); const updateCalls = []; const warehouseId = 100000; const taskId = 'wave_quick_scan_test_task_1'; service.getWarehouseSubjectConfig = async () => ({ subject_config: { wave_strategy_version: 9, }, }); service.ctx.model.wms_wave_strategy = { async findAll() { return [ { status: 1, order_min_sku: 1, order_max_sku: 1, order_min_qty: 1, order_max_qty: 2, min_same_structure: 1, max_same_structure: 10, allowed_owners: [ 11 ], allowed_expresses: [ 'YD' ], allowed_platforms: [ 'PDD' ], allow_cross_owner: true, require_same_express: false, }, ]; }, }; service.getOrderModel = async () => ({ async findAll() { return [ { id: 1, store_id: 11, items_count: 1, items_total: 1, items_hash: 'hash_a', items: '', logistics_code: 'YD', sys_logistics_code: 'YD', logistics_name: '韵达', province: 'zj', sys_platform_code: 'PDD', source_platform_code: '', }, { id: 2, store_id: 12, items_count: 3, items_total: 6, items_hash: 'hash_b', items: '', logistics_code: 'SF', sys_logistics_code: 'SF', logistics_name: '顺丰', province: 'js', sys_platform_code: 'TB', source_platform_code: '', }, ]; }, async update(values, options) { updateCalls.push({ values, options }); return [ Array.isArray(options.where.id[Op.in]) ? options.where.id[Op.in].length : 0 ]; }, }); await service.setWaveStrategyQuickScanWarehouseLock(warehouseId, taskId, { state: 'submitted', }); await service.runWaveStrategyQuickScanTask({ taskId, warehouseId, }); const matchedCall = updateCalls.find(item => Number(item.values.wave_scan_result) === 1); const unmatchedCall = updateCalls.find(item => Number(item.values.wave_scan_result) === 2); assert.ok(matchedCall); assert.ok(unmatchedCall); assert.equal(matchedCall.values.wave_strategy_version, 9); assert.equal(unmatchedCall.values.wave_strategy_version, 9); assert.deepEqual(matchedCall.options.where.id[Op.in], [ 1 ]); assert.deepEqual(unmatchedCall.options.where.id[Op.in], [ 2 ]); const progress = await service.getWaveStrategyQuickScanTaskProgress(taskId); assert.equal(progress.state, 'success'); assert.equal(progress.total_order_count, 2); assert.equal(progress.processed_order_count, 2); assert.equal(progress.matched_order_count, 1); assert.equal(progress.unmatched_order_count, 1); const lockPayload = await service.getWaveStrategyQuickScanWarehouseLock(warehouseId); assert.equal(lockPayload, null); }); it('进度查询应返回 running 和 success 的基础字段', async () => { const { service } = createHarness(); const taskId = 'wave_quick_scan_test_task_2'; await service.setWaveStrategyQuickScanTaskProgress(taskId, 'running', { warehouse_id: 100000, total_order_count: 10, processed_order_count: 3, matched_order_count: 1, unmatched_order_count: 2, progress_percent: 30, submitted_at: 1000, start_time: 2000, end_time: null, }); const runningResult = await service.waveStrategyQuickScanTaskProgress({ task_id: taskId, }); assert.equal(runningResult.flag, 'success'); assert.equal(runningResult.state, 'running'); assert.equal(runningResult.total_order_count, 10); assert.equal(runningResult.processed_order_count, 3); assert.equal(runningResult.progress_percent, 30); await service.setWaveStrategyQuickScanTaskProgress(taskId, 'success', { processed_order_count: 10, matched_order_count: 4, unmatched_order_count: 6, progress_percent: 100, end_time: 3000, }); const successResult = await service.waveStrategyQuickScanTaskProgress({ taskId, }); assert.equal(successResult.flag, 'success'); assert.equal(successResult.state, 'success'); assert.equal(successResult.total_order_count, 10); assert.equal(successResult.processed_order_count, 10); assert.equal(successResult.matched_order_count, 4); assert.equal(successResult.unmatched_order_count, 6); assert.equal(successResult.progress_percent, 100); assert.equal(successResult.end_time, 3000); }); it('allow_cross_owner=false 时应按同结构主货主优先命中', async () => { const { service } = createHarness(); const orders = [ { id: 101, store_id: 11, items_count: 1, items_total: 1, items_hash: 'hash_owner_same_structure', logistics_code: 'YD', sys_logistics_code: 'YD', logistics_name: '韵达', province: 'zj', sys_platform_code: 'PDD', source_platform_code: '', }, { id: 102, store_id: 11, items_count: 1, items_total: 1, items_hash: 'hash_owner_same_structure', logistics_code: 'YD', sys_logistics_code: 'YD', logistics_name: '韵达', province: 'zj', sys_platform_code: 'PDD', source_platform_code: '', }, { id: 103, store_id: 12, items_count: 1, items_total: 1, items_hash: 'hash_owner_same_structure', logistics_code: 'YD', sys_logistics_code: 'YD', logistics_name: '韵达', province: 'zj', sys_platform_code: 'PDD', source_platform_code: '', }, ]; const strategyList = [ { strategy_id: 1001, order_min_sku: 1, order_max_sku: 999, order_min_qty: 1, order_max_qty: 999, min_same_structure: 1, max_same_structure: 999, allowed_owners: [], allowed_expresses: [], allowed_provinces: [], allowed_platforms: [], allow_cross_owner: false, require_same_express: false, }, ]; const result = await runWaveStrategyQuickScanRunner({ orders, strategyList, getStructureKey: order => service.getWaveStrategyQuickScanStructureKey(order), getExpressKey: order => service.getWaveStrategyQuickScanExpressKey(order), getMatchedStrategyIds: (order, normalizedStrategyList, context) => service.getWaveStrategyQuickScanMatchedStrategyIds( order, normalizedStrategyList, context ), }); assert.deepEqual(result.matchedOrderIds, [ 101, 102 ]); }); it('require_same_express=true 时应按同结构主快递优先命中', async () => { const { service } = createHarness(); const orders = [ { id: 201, store_id: 11, items_count: 1, items_total: 1, items_hash: 'hash_express_same_structure', logistics_code: 'YD', sys_logistics_code: 'YD', logistics_name: '韵达', province: 'zj', sys_platform_code: 'PDD', source_platform_code: '', }, { id: 202, store_id: 11, items_count: 1, items_total: 1, items_hash: 'hash_express_same_structure', logistics_code: 'YD', sys_logistics_code: 'YD', logistics_name: '韵达', province: 'zj', sys_platform_code: 'PDD', source_platform_code: '', }, { id: 203, store_id: 11, items_count: 1, items_total: 1, items_hash: 'hash_express_same_structure', logistics_code: 'SF', sys_logistics_code: 'SF', logistics_name: '顺丰', province: 'zj', sys_platform_code: 'PDD', source_platform_code: '', }, ]; const strategyList = [ { strategy_id: 1002, order_min_sku: 1, order_max_sku: 999, order_min_qty: 1, order_max_qty: 999, min_same_structure: 1, max_same_structure: 999, allowed_owners: [], allowed_expresses: [], allowed_provinces: [], allowed_platforms: [], allow_cross_owner: true, require_same_express: true, }, ]; const result = await runWaveStrategyQuickScanRunner({ orders, strategyList, getStructureKey: order => service.getWaveStrategyQuickScanStructureKey(order), getExpressKey: order => service.getWaveStrategyQuickScanExpressKey(order), getMatchedStrategyIds: (order, normalizedStrategyList, context) => service.getWaveStrategyQuickScanMatchedStrategyIds( order, normalizedStrategyList, context ), }); assert.deepEqual(result.matchedOrderIds, [ 201, 202 ]); }); });