'use strict'; const assert = require('node:assert/strict'); const PendingOrderService = require('../../../../../app/service/systemMethods/order/pendingOrder'); /** * 创建 manualWaybill 操作日志回归测试上下文。 * * 仅覆盖 operationLog.createOperationLog 的 businessId 传入值。 */ function createManualWaybillLogHarness(options = {}) { const calls = { operationLogPayload: null, orderExpressCreatePayload: null, orderDeliveryUpdatePayload: null, syncOrderIdsPayload: null, }; const warehouseId = options.warehouseId || 100000; const testOrderId = options.orderId || 999001; const orderRow = { id: testOrderId, step: options.orderStep || 1, outbound_status: options.outboundStatus || 'none', warehouse_id: warehouseId, store_id: options.storeId || 1000000, }; const orderExpressMax = options.orderExpressMax !== undefined ? options.orderExpressMax : null; const ctx = { app: { Sequelize: { Op: { in: 'in', }, }, }, user: { store_id: warehouseId, employees_id: '9001', employees_name: '测试员', }, model: { OrderExpress: { async max(field, whereOptions) { return orderExpressMax; }, async create(values) { calls.orderExpressCreatePayload = values; return { id: 10001, ...values, }; }, }, OrderDeliveryFactory: { getCurrentModel: () => { throw new Error('应使用 getOrderModel,不是 OrderDeliveryFactory'); }, }, }, service: { systems: { operationLog: { async createOperationLog(payload) { calls.operationLogPayload = payload; return { id: 1 }; }, }, storeWarehouse: { async getContractConfig() { return null; }, }, }, }, logger: { info() {}, warn() {}, error() {}, }, }; const service = new PendingOrderService(ctx); // 注入 mock getOrderModel / buildOrderWhere / success / getWarehouseId service.getWarehouseId = () => warehouseId; service.getOrderLogOperator = () => ({ id: ctx.user && ctx.user.employees_id, name: ctx.user && ctx.user.employees_name, }); service.getOrderLogWarehouseId = () => warehouseId; service.getOrderModel = async () => ({ async findOne(query) { return orderRow; }, async update(values, whereOpts) { calls.orderDeliveryUpdatePayload = { values, where: whereOpts.where }; return [ 1 ]; }, }); service.buildOrderWhere = (orderIds) => ({ id: { [ctx.app.Sequelize.Op.in]: orderIds }, warehouse_id: warehouseId, }); service.success = (msg, data) => ({ code: 0, msg, data }); service.syncOrderIds = (payload) => { calls.syncOrderIdsPayload = payload; return Promise.resolve(); }; return { service, calls, ctx }; } describe('test/app/service/systems/express.manualWaybill.guard.test.js — manualWaybill 操作日志', () => { it('createOperationLog 的 businessId 应为 String(orderId),而非仓库ID', async () => { const testOrderId = 888001; const { service, calls } = createManualWaybillLogHarness({ orderId: testOrderId, warehouseId: 100000, }); await service.manualWaybill({ order_id: testOrderId, waybill_code: 'SF1234567890', }); assert.ok(calls.operationLogPayload, '应调用 createOperationLog'); assert.equal(calls.operationLogPayload.businessType, 'order'); assert.equal(calls.operationLogPayload.businessId, String(testOrderId), `businessId 应为订单ID "${testOrderId}",而非仓库ID`); assert.equal(calls.operationLogPayload.operationCode, 'ORDER_MANUAL_WAYBILL'); assert.equal(calls.operationLogPayload.extras.warehouseId, 100000); assert.equal(calls.operationLogPayload.extras.orderId, testOrderId); assert.equal(calls.operationLogPayload.extras.waybillCode, 'SF1234567890'); }); it('businessId 不应为 warehouseId(回归验证)', async () => { const testOrderId = 888002; const warehouseId = 100000; const { service, calls } = createManualWaybillLogHarness({ orderId: testOrderId, warehouseId, }); await service.manualWaybill({ order_id: testOrderId, waybill_code: 'YT9876543210', }); assert.ok(calls.operationLogPayload, '应调用 createOperationLog'); assert.notEqual(calls.operationLogPayload.businessId, String(warehouseId), `businessId 不应为仓库ID "${warehouseId}"`); assert.equal(calls.operationLogPayload.businessId, String(testOrderId), `businessId 应为订单ID "${testOrderId}"`); }); it('business_key 从已有最大值递增', async () => { const testOrderId = 888003; const { service, calls } = createManualWaybillLogHarness({ orderId: testOrderId, orderExpressMax: 3, }); await service.manualWaybill({ order_id: testOrderId, waybill_code: 'EMS0000000001', }); assert.ok(calls.orderExpressCreatePayload); assert.equal(calls.orderExpressCreatePayload.business_key, 4, '应从 max(3)+1=4 递增'); assert.equal(calls.orderExpressCreatePayload.status, 1, '手工录入 status 应为 1'); }); });