'use strict'; const assert = require('node:assert/strict'); const DeliveryOrderService = require('../../../../app/service/systems/delivery_order'); /** * 创建异步流程测试桩。 * * @return {{ ctx: Object, service: DeliveryOrderService, calls: Object }} 测试上下文。 */ function createHarness() { const calls = { sequence: [], warns: [], }; const ctx = { app: { config: {}, }, user: { store_id: 100000, warehouse_id: 100000, employees_name: '测试员', }, logger: { info() {}, error() {}, warn(message) { calls.warns.push(String(message || '')); }, }, service: { systemMethods: { order: { pendingOrder: { async allocateExpressCode() { calls.sequence.push('allocateExpress'); return { code: 1, message: 'ok', }; }, }, wave: { async getMinGroupOrderFetchQuantity() { calls.sequence.push('wave.getMinGroupOrderFetchQuantity'); return 2; }, }, }, }, systems: { orderStorageAllocation: { async allocateStorage() { calls.sequence.push('allocateStorage'); return { code: 200, message: 'ok', }; }, }, }, }, }; const service = new DeliveryOrderService(ctx); service.withPatchedCtxUser = async (_patchUser, callback) => { return await callback(); }; return { ctx, service, calls, }; } describe('test/app/service/systems/delivery_order.asyncProcessOrderCreated.test.js', () => { it('should execute async flow in order: express -> storage -> autoGrouping', async () => { const { service, calls, ctx } = createHarness(); service.buildAsyncProcessContext = async () => ({ orderId: 101, warehouseId: 100000, storeId: 200000, order: { id: 101, store_id: 200000, warehouse_id: 100000, logistics_code: 'STO', logistics_name: '申通', }, orderItemRows: [], }); service.getStoreWarehousePolicyConfig = async () => ({ auto_allocate_express: 1, auto_allocate_storage: 1, need_order_approval: 0, }); service.enqueueAutoWaybillAllocation = async () => { calls.sequence.push('allocateExpress'); }; service.buildExpressAllocateRequest = () => ({ orders: [{ order_id: 101 }] }); service.buildStorageAllocateRequest = () => ({ order_ids: [ 101 ], warehouse_id: 100000 }); service.runAutoGrouping = async () => { calls.sequence.push('autoGrouping'); }; ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null; await service.asyncProcessOrderCreated(101, { warehouseId: 100000, storeId: 200000, }, 'API'); assert.deepEqual(calls.sequence, [ 'allocateExpress', 'allocateStorage', 'autoGrouping' ]); }); it('should continue next steps when one step fails', async () => { const { service, calls, ctx } = createHarness(); service.buildAsyncProcessContext = async () => ({ orderId: 102, warehouseId: 100000, storeId: 200000, order: { id: 102, store_id: 200000, warehouse_id: 100000, }, orderItemRows: [], }); service.getStoreWarehousePolicyConfig = async () => ({ auto_allocate_express: 1, auto_allocate_storage: 1, need_order_approval: 0, }); service.enqueueAutoWaybillAllocation = async () => { calls.sequence.push('allocateExpress'); throw new Error('分配快递异常'); }; ctx.service.systems.orderStorageAllocation.allocateStorage = async () => { calls.sequence.push('allocateStorage'); return { code: 200 }; }; service.runAutoGrouping = async () => { calls.sequence.push('autoGrouping'); }; ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null; await service.asyncProcessOrderCreated(102, { warehouseId: 100000, storeId: 200000, }, 'API'); assert.deepEqual(calls.sequence, [ 'allocateExpress', 'allocateStorage', 'autoGrouping' ]); assert.equal(calls.warns.length >= 1, true); }); it('should skip auto express allocation when order remark matches exclude keywords', async () => { const { service, calls, ctx } = createHarness(); const skipLogs = []; service.buildAsyncProcessContext = async () => ({ orderId: 301, warehouseId: 100000, storeId: 200000, order: { id: 301, store_id: 200000, warehouse_id: 100000, remark: '买家要求:请勿自动分配快递,联系客服', }, orderItemRows: [], }); service.getStoreWarehousePolicyConfig = async () => ({ auto_allocate_express: 1, auto_allocate_storage: 1, need_order_approval: 0, }); service.getStoreSubjectConfig = async () => ({ auto_allocate_express_exclude_remarks: [ '请勿自动分配快递' ], }); service.enqueueAutoWaybillAllocation = async () => { calls.sequence.push('allocateExpress'); }; service.runAutoGrouping = async () => { calls.sequence.push('autoGrouping'); }; ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null; ctx.service.systems.operationLog = { createOperationLogs: async payloads => { skipLogs.push(...payloads); }, }; await service.asyncProcessOrderCreated(301, { warehouseId: 100000, storeId: 200000, }, 'API'); assert.deepEqual(calls.sequence, [ 'allocateStorage', 'autoGrouping' ]); assert.equal(skipLogs.length, 1); assert.equal(skipLogs[0].businessType, 'order'); assert.equal(skipLogs[0].businessId, '301'); assert.equal(skipLogs[0].operationCode, 'ORDER_AUTO_EXPRESS_SKIP'); assert.equal(skipLogs[0].operator.name, 'system'); assert.match(skipLogs[0].extras.message, /请勿自动分配快递/); }); it('should keep auto express allocation when order remark does not match exclude keywords', async () => { const { service, calls, ctx } = createHarness(); const skipLogs = []; service.buildAsyncProcessContext = async () => ({ orderId: 302, warehouseId: 100000, storeId: 200000, order: { id: 302, store_id: 200000, warehouse_id: 100000, remark: '正常订单,尽快发货', }, orderItemRows: [], }); service.getStoreWarehousePolicyConfig = async () => ({ auto_allocate_express: 1, auto_allocate_storage: 1, need_order_approval: 0, }); service.getStoreSubjectConfig = async () => ({ auto_allocate_express_exclude_remarks: [ '请勿自动分配快递' ], }); service.enqueueAutoWaybillAllocation = async () => { calls.sequence.push('allocateExpress'); }; service.runAutoGrouping = async () => { calls.sequence.push('autoGrouping'); }; ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null; ctx.service.systems.operationLog = { createOperationLogs: async payloads => { skipLogs.push(...payloads); }, }; await service.asyncProcessOrderCreated(302, { warehouseId: 100000, storeId: 200000, }, 'API'); assert.deepEqual(calls.sequence, [ 'allocateExpress', 'allocateStorage', 'autoGrouping' ]); assert.equal(skipLogs.length, 0); }); it('should treat buyer_message and seller_message as remark source for exclusion', async () => { const { service } = createHarness(); const config = { auto_allocate_express_exclude_remarks: [ '勿发圆通', '', '中通' ], }; assert.equal(service.findAutoExpressExcludedRemark({ remark: '正常备注' }, config), ''); assert.equal(service.findAutoExpressExcludedRemark({ remark: '勿发圆通' }, config), '勿发圆通'); assert.equal(service.findAutoExpressExcludedRemark({ buyer_message: '请用中通' }, config), '中通'); assert.equal(service.findAutoExpressExcludedRemark({ seller_message: '备注圆通' }, config), ''); assert.equal(service.findAutoExpressExcludedRemark({}, null), ''); assert.equal(service.findAutoExpressExcludedRemark({}, { auto_allocate_express_exclude_remarks: [] }), ''); }); it('should not pass allow_storage_location for auto storage allocation', () => { const { service } = createHarness(); const request = service.buildStorageAllocateRequest({ orderId: 99, warehouseId: 100000, }); assert.deepEqual(request, { order_ids: [ 99 ], warehouse_id: 100000, }); }); it('should skip auto grouping when switch is disabled', async () => { const { service } = createHarness(); let triggered = false; service.getStoreSubjectConfig = async () => ({ auto_grouping_enabled: false, max_wave_quantity: 10, }); service.buildAutoGroupingStructure = () => ({ itemPartType: 'items_hash', itemPart: 'hash_1', logisticsName: '申通', itemSnapshotKey: 'snapshot', itemDetailMachine: 'token', }); service.findAutoGroupingWaveId = async () => { triggered = true; return 0; }; await service.runAutoGrouping({ orderId: 103, warehouseId: 100000, storeId: 200000, order: {}, orderItemRows: [], }); assert.equal(triggered, false); }); it('should trigger auto grouping chain when enabled and conditions matched', async () => { const { service } = createHarness(); const called = []; service.getStoreSubjectConfig = async () => ({ auto_grouping_enabled: true, max_wave_quantity: 2, }); service.buildAutoGroupingStructure = () => ({ itemPartType: 'items_hash', itemPart: 'hash_2', logisticsName: '申通', itemSnapshotKey: 'snapshot_2', itemDetailMachine: 'token_2', }); service.findAutoGroupingWaveId = async () => 0; service.countCandidateGroupOrders = async () => 2; service.createGroupingWave = async () => { called.push('createGroupingWave'); return { wave_ids: [ 9001 ], }; }; service.markWaveAutoGrouping = async (_context, waveIds) => { called.push(`markWaveAutoGrouping:${waveIds.join(',')}`); }; service.finishAutoGroupingIfNeeded = async (_context, waveId, maxWaveQuantity) => { called.push(`finishAutoGroupingIfNeeded:${waveId}:${maxWaveQuantity}`); }; await service.runAutoGrouping({ orderId: 104, warehouseId: 100000, storeId: 200000, order: { id: 104, }, orderItemRows: [], }); assert.deepEqual(called, [ 'createGroupingWave', 'markWaveAutoGrouping:9001', 'finishAutoGroupingIfNeeded:9001:2', ]); }); it('should divert need_oms_waybill orders away from local auto waybill allocation', async () => { const { service, calls, ctx } = createHarness(); const waitLogs = []; service.buildAsyncProcessContext = async () => ({ orderId: 401, warehouseId: 100000, storeId: 200000, order: { id: 401, store_id: 200000, warehouse_id: 100000, need_oms_waybill: 1, }, orderItemRows: [], }); service.getStoreWarehousePolicyConfig = async () => ({ auto_allocate_express: 1, auto_allocate_storage: 0, need_order_approval: 0, }); service.enqueueAutoWaybillAllocation = async () => { calls.sequence.push('allocateExpress'); }; service.runAutoGrouping = async () => { calls.sequence.push('autoGrouping'); }; ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null; ctx.service.systems.operationLog = { createOperationLogs: async payloads => { waitLogs.push(...payloads); }, }; await service.asyncProcessOrderCreated(401, { warehouseId: 100000, storeId: 200000, }, 'API'); // 不进入本地取号队列 assert.equal(calls.sequence.includes('allocateExpress'), false); assert.equal(waitLogs.length, 1); assert.equal(waitLogs[0].businessType, 'order'); assert.equal(waitLogs[0].businessId, '401'); assert.equal(waitLogs[0].operationCode, 'ORDER_NEED_OMS_WAYBILL_WAIT'); assert.equal(waitLogs[0].operator.name, 'system'); assert.match(waitLogs[0].extras.message, /等待外部 OMS 取号/); }); it('should keep local auto waybill allocation when need_oms_waybill is 0', async () => { const { service, calls, ctx } = createHarness(); const waitLogs = []; service.buildAsyncProcessContext = async () => ({ orderId: 402, warehouseId: 100000, storeId: 200000, order: { id: 402, store_id: 200000, warehouse_id: 100000, need_oms_waybill: 0, }, orderItemRows: [], }); service.getStoreWarehousePolicyConfig = async () => ({ auto_allocate_express: 1, auto_allocate_storage: 0, need_order_approval: 0, }); service.enqueueAutoWaybillAllocation = async () => { calls.sequence.push('allocateExpress'); }; service.runAutoGrouping = async () => { calls.sequence.push('autoGrouping'); }; ctx.service.systemMethods.order.wave.submitWaveStrategyQuickScanForWarehouse = async () => null; ctx.service.systems.operationLog = { createOperationLogs: async payloads => { waitLogs.push(...payloads); }, }; await service.asyncProcessOrderCreated(402, { warehouseId: 100000, storeId: 200000, }, 'API'); assert.equal(calls.sequence.includes('allocateExpress'), true); assert.equal(waitLogs.length, 0); }); });