'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 } = 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, }); service.buildExpressAllocateRequest = () => ({ orders: [{ order_id: 101 }] }); service.buildStorageAllocateRequest = () => ({ order_ids: [ 101 ], allow_storage_location: true }); service.runAutoGrouping = async () => { calls.sequence.push('autoGrouping'); }; 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, }); ctx.service.systemMethods.order.pendingOrder.allocateExpressCode = 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'); }; 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 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', ]); }); });