'use strict'; const assert = require('node:assert/strict'); const { Op } = require('sequelize'); 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 missingBusinessKeyColumn = Boolean(options.missingBusinessKeyColumn); const configuredOrderRows = Array.isArray(options.orderRows) ? options.orderRows : []; const orderRows = configuredOrderRows.length > 0 ? configuredOrderRows : orderIds.map(id => ({ id, business_key: 1 })); const pipelineError = options.pipelineError || null; const capture = { orderFindAllAttributes: [], pipelineOrders: [], orderUpdatePayload: null, orderUpdateWhere: null, markWaybillAsyncOrdersAsErrorArgs: null, }; 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 : []; if (pipelineError) { throw pipelineError; } return { code: 200, message: '分配完成', }; }, async markWaybillAsyncOrdersAsError(orderModel, orderIds, errorMessage, extraWhere = {}) { capture.markWaybillAsyncOrdersAsErrorArgs = { orderIds, errorMessage, extraWhere, }; await orderModel.update({ post_status: 'error', }, { where: { ...extraWhere, id: { [Op.in]: orderIds, }, }, }); }, }, }, }, }; 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') && missingBusinessKeyColumn) { throw new Error("Unknown column 'business_key' in 'field list'"); } if (attributes.includes('business_key')) { return orderRows.map(row => ({ id: Number(row && row.id || 0), business_key: row && row.business_key, })); } return orderRows.map(row => ({ id: Number(row && row.id || 0) })); }, async update(payload = {}, query = {}) { capture.orderUpdatePayload = payload; capture.orderUpdateWhere = query.where || null; const rollbackOrderIds = query && query.where && query.where.id && query.where.id[Op.in]; return [ Array.isArray(rollbackOrderIds) ? rollbackOrderIds.length : 0 ]; }, }); return { service, capture, }; } describe('test/app/service/systemMethods/order/wave.batchAllocateExpress.test.js', () => { it('should skip post_status rollback when business_key column is missing and pipeline fails', async () => { const { service, capture } = createHarness({ orderIds: [ 101, 102 ], missingBusinessKeyColumn: true, pipelineError: new Error('connect ECONNREFUSED 127.0.0.1:9100'), }); 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, 'failed'); assert.equal(progress.error_code, 'WAVE_BATCH_ALLOCATE_EXPRESS_FAILED'); assert.equal(progress.total_order_count, 2); assert.equal(progress.success_order_count, 0); assert.equal(progress.failed_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: 0 }, { order_id: 102, business_key: 0 }, ]); assert.equal(capture.markWaybillAsyncOrdersAsErrorArgs, null); assert.equal(capture.orderUpdatePayload, null); assert.equal(capture.orderUpdateWhere, null); }); it('should only rollback business_key=1 orders when pipeline throws', async () => { const { service, capture } = createHarness({ orderRows: [ { id: 301, business_key: 1 }, { id: 302, business_key: 2 }, { id: 303, business_key: '1' }, ], pipelineError: new Error('connect ECONNREFUSED 127.0.0.1:9100'), }); await service.runBatchAllocateExpressTask({ taskId: 'task_pipeline_error', warehouseId: 100000, waveIds: [ 99 ], }); const progress = await service.getBatchAllocateExpressTaskProgress('task_pipeline_error'); assert.equal(progress.state, 'failed'); assert.equal(progress.total_order_count, 3); assert.equal(progress.processed_order_count, 3); assert.equal(progress.success_order_count, 0); assert.equal(progress.failed_order_count, 3); assert.equal(progress.progress_percent, 100); assert.equal(progress.error_code, 'WAVE_BATCH_ALLOCATE_EXPRESS_FAILED'); assert.ok(capture.markWaybillAsyncOrdersAsErrorArgs); assert.deepEqual(capture.markWaybillAsyncOrdersAsErrorArgs.orderIds, [ 301, 303 ]); assert.equal(capture.markWaybillAsyncOrdersAsErrorArgs.errorMessage, 'connect ECONNREFUSED 127.0.0.1:9100'); assert.deepEqual(capture.markWaybillAsyncOrdersAsErrorArgs.extraWhere, { warehouse_id: 100000, wave_id: { [Op.in]: [ 99 ], }, }); assert.deepEqual(capture.orderUpdatePayload, { post_status: 'error', }); assert.ok(capture.orderUpdateWhere); assert.equal(capture.orderUpdateWhere.warehouse_id, 100000); assert.deepEqual(capture.orderUpdateWhere.wave_id, { [Op.in]: [ 99 ], }); assert.deepEqual(capture.orderUpdateWhere.id, { [Op.in]: [ 301, 303 ], }); }); });