'use strict'; const assert = require('node:assert/strict'); const ExpressService = require('../../../../app/service/systems/express'); /** * 创建 asyncTask 日志回归测试上下文。 * * @return {{ service: ExpressService, calls: Object }} 测试服务与调用记录。 */ function createAsyncTaskHarness() { const calls = { pendingExceptionPayloads: [], operationLogs: [], hardDeletePayloads: [], }; const ctx = { app: { Sequelize: { Op: { in: 'in', }, }, }, user: { store_id: 100000, employees_id: '9001', employees_name: '测试员A', }, logger: { info() {}, warn() {}, error() {}, }, service: { systems: { orderPendingException: { markPendingException(payload) { calls.pendingExceptionPayloads.push(payload); }, }, operationLog: { async createOperationLogs(payloads = []) { calls.operationLogs.push(...payloads); return payloads; }, async createOperationLog(payload) { calls.operationLogs.push(payload); return payload; }, }, }, }, }; const service = new ExpressService(ctx); service.hardDeleteWaybillAsyncOrderExpressRows = async (orders, options) => { calls.hardDeletePayloads.push({ orders, options }); }; return { service, calls, }; } describe('test/app/service/systems/express.asyncTask.logs.test.js', () => { it('should write failure order logs when async task fails before createWaybillCode', async () => { const { service, calls } = createAsyncTaskHarness(); await service.asyncTask( [{ id: 301, business_key: 7101 }], { sys_platform_code: 'taobao', sys_platform_name: '淘宝', logistics_name: '中通', warehouse_id: 100000, }, { code: 'polyapi' }, null ); assert.equal(calls.pendingExceptionPayloads.length, 1); assert.equal(calls.pendingExceptionPayloads[0].ids[0], 301); assert.match(calls.pendingExceptionPayloads[0].exception_text, /未配置有效的快递接口/); assert.equal(calls.operationLogs.length, 1); assert.equal(calls.operationLogs[0].operationCode, 'ORDER_EXPRESS_ALLOCATE_FAILED'); assert.equal(calls.operationLogs[0].businessId, '301'); assert.equal(calls.operationLogs[0].operator.id, '9001'); assert.equal(calls.operationLogs[0].operator.name, '测试员A'); assert.match(calls.operationLogs[0].extras.message, /分配快递失败:/); assert.equal(calls.hardDeletePayloads.length, 1); assert.equal(calls.hardDeletePayloads[0].options.platform_code, 'taobao'); }); it('should support batch rows with order_id when writing failure logs', async () => { const { service, calls } = createAsyncTaskHarness(); await service.asyncTask( [{ order_id: 302, business_key: 7102 }], { sys_platform_code: 'taobao', sys_platform_name: '淘宝', logistics_name: '圆通', warehouse_id: 100000, }, null, { id: 1 } ); assert.equal(calls.operationLogs.length, 1); assert.equal(calls.operationLogs[0].businessId, '302'); assert.match(calls.operationLogs[0].extras.message, /未找到订单平台/); }); });