'use strict'; const assert = require('node:assert/strict'); const Sequelize = require('sequelize'); const PendingOrderService = require('../../../../../app/service/systemMethods/order/pendingOrder'); const ExpressService = require('../../../../../app/service/systems/express'); /** * 创建待处理订单分配快递号的测试上下文。 * * @param {Object} [options={}] 测试桩配置。 * @return {{ ctx: Object, pendingService: PendingOrderService, expressService: ExpressService }} 测试上下文。 */ function createAllocateExpressHarness(options = {}) { const orderDeliveryRows = Array.isArray(options.orderDeliveryRows) ? options.orderDeliveryRows : []; const calls = { operationLogs: [], }; const ctx = { app: { Sequelize, config: {}, }, user: { store_id: 100000, }, model: { OrderDeliveryFactory: { async getCurrentModel() { return { async findAll() { return orderDeliveryRows; }, }; }, }, }, logger: { info() {}, warn() {}, error() {}, }, }; ctx.service = { systems: { express: null, operationLog: { async createOperationLogs(payloads = []) { calls.operationLogs.push(...payloads); return payloads; }, async createOperationLog(payload) { calls.operationLogs.push(payload); return payload; }, }, }, }; const pendingService = new PendingOrderService(ctx); const expressService = new ExpressService(ctx); ctx.service.systems.express = expressService; return { ctx, calls, pendingService, expressService, }; } describe('test/app/service/systemMethods/order/pendingOrder.allocateExpressCode.test.js', () => { it('should pass the order array directly into createWaybillCodePipeline and keep array business_key intact', async () => { const { pendingService, expressService, calls } = createAllocateExpressHarness(); const receivedOrders = []; expressService.createWaybillCodePipeline = async orders => { receivedOrders.push(orders); return { orders: orders.map(order => ({ ...order, waybill_code: 'WB-001', })), waybill_codes: [ 'WB-001' ], skippedOrders: [], }; }; const orders = [ { order_id: 101, business_key: [ 9901, '9902' ], sys_platform_code: 'TAOBAO', logistics_code: 'STO', express_platform_code: 'polyapi', platform_code: 'polyapi', wms_express_platform: { platform_code: 'polyapi', }, }, ]; const result = await pendingService.allocateExpressCode({ orders }); assert.equal(receivedOrders.length, 1); assert.deepEqual(receivedOrders[0], orders); assert.deepEqual(receivedOrders[0][0].business_key, [ 9901, '9902' ]); assert.equal(result.flag, 'success'); assert.equal(result.message, 'ok'); assert.equal(result.code, 1); assert.equal(calls.operationLogs.length, 0); assert.deepEqual(result.waybill_codes, [ 'WB-001' ]); assert.deepEqual(result.orders, [ { order_id: 101, business_key: [ 9901, '9902' ], sys_platform_code: 'TAOBAO', logistics_code: 'STO', express_platform_code: 'polyapi', platform_code: 'polyapi', wms_express_platform: { platform_code: 'polyapi', }, waybill_code: 'WB-001', }, ]); }); it('should skip invalid order objects through the existing normalizeWaybillOrders flow', async () => { const { pendingService, expressService } = createAllocateExpressHarness({ orderDeliveryRows: [ { id: 102, sys_platform_code: 'TAOBAO', logistics_code: 'STO', }, ], }); const createWaybillCodePipelineCalls = []; expressService.createWaybillCodePipeline = async orders => { createWaybillCodePipelineCalls.push(orders); return { orders: orders.filter(order => order && Number(order.order_id) > 0), waybill_codes: [], skippedOrders: [ { reason: '订单数据无效', }, ], }; }; const result = await pendingService.allocateExpressCode({ orders: [ null, { order_id: 102, business_key: 9902, sys_platform_code: 'TAOBAO', logistics_code: 'STO', }, ], }); assert.equal(createWaybillCodePipelineCalls.length, 1); assert.equal(result.skippedOrders.length, 1); assert.equal(result.skippedOrders[0].reason, '订单数据无效'); assert.equal(result.orders.length, 1); assert.equal(result.orders[0].order_id, 102); }); it('should reject non-array payloads', async () => { const { pendingService } = createAllocateExpressHarness(); await assert.rejects( async () => await pendingService.allocateExpressCode({ orders: {} }), /仅支持订单对象数组/ ); }); it('should only write failure logs when allocate pipeline throws', async () => { const { pendingService, expressService, calls } = createAllocateExpressHarness(); expressService.createWaybillCodePipeline = async () => { throw new Error('平台接口异常'); }; await assert.rejects( async () => await pendingService.allocateExpressCode({ orders: [ { order_id: 201, business_key: 9901 }, { order_id: 202, business_key: 9902 }, ], }), /平台接口异常/ ); assert.equal(calls.operationLogs.length, 2); assert.equal(calls.operationLogs[0].operationCode, 'ORDER_EXPRESS_ALLOCATE_FAILED'); assert.equal(calls.operationLogs[0].extras.message, '分配快递失败:平台接口异常'); assert.equal(calls.operationLogs[0].operator.id, null); assert.equal(calls.operationLogs[0].operator.name, 'system'); assert.equal(calls.operationLogs[1].businessId, '202'); }); });