'use strict'; const assert = require('node:assert/strict'); const WaveService = require('../../../../../app/service/systemMethods/order/wave'); /** * 创建 batchAllocateExpress 轻量测试上下文。 * * @param {Object} options 初始化选项。 * @param {Array} [options.orderIds=[]] 波次内订单 ID 集合。 * @return {{ service: WaveService, capture: Object }} 服务实例与调用记录。 */ function createHarness(options = {}) { const cacheStore = new Map(); const orderIds = Array.isArray(options.orderIds) ? options.orderIds : []; const capture = { orderFindAllAttributes: [], pipelineOrders: [], }; const ctx = { app: { config: {}, async setCacheAsync(key, value) { cacheStore.set(key, value); }, async getCacheAsync(key) { return cacheStore.get(key) || null; }, }, user: { store_id: 100000, }, logger: { info() {}, warn() {}, error() {}, }, model: {}, service: { systems: { express: { async createWaybillCodePipeline(orders) { capture.pipelineOrders = Array.isArray(orders) ? orders : []; return { code: 200, message: '分配完成', }; }, }, }, }, }; const service = new WaveService(ctx); service.getOrderModel = async () => ({ async findAll(query = {}) { const attributes = Array.isArray(query.attributes) ? query.attributes : []; capture.orderFindAllAttributes.push(attributes); if (attributes.includes('business_key')) { throw new Error("Unknown column 'business_key' in 'field list'"); } return orderIds.map(id => ({ id })); }, }); return { service, capture, }; } describe('test/app/service/systemMethods/order/wave.batchAllocateExpress.test.js', () => { it('should fallback to id-only query when business_key column is missing and keep task success', async () => { const { service, capture } = createHarness({ orderIds: [ 101, 102 ], }); await service.runBatchAllocateExpressTask({ taskId: 'task_missing_business_key_column', warehouseId: 100000, waveIds: [ 11 ], }); const progress = await service.getBatchAllocateExpressTaskProgress('task_missing_business_key_column'); assert.equal(progress.state, 'success'); assert.equal(progress.error, ''); assert.equal(progress.error_code, ''); assert.equal(progress.total_order_count, 2); assert.equal(progress.success_order_count, 2); assert.equal(capture.orderFindAllAttributes.length, 2); assert.deepEqual(capture.orderFindAllAttributes[0], [ 'id', 'business_key' ]); assert.deepEqual(capture.orderFindAllAttributes[1], [ 'id' ]); assert.deepEqual(capture.pipelineOrders, [ { order_id: 101, business_key: 1 }, { order_id: 102, business_key: 1 }, ]); }); });