'use strict'; const assert = require('node:assert/strict'); const ExpressService = require('../../../../app/service/systems/express'); /** * 创建 createWaybillCodePipeline 跳过取消订单的测试上下文。 * 模拟数据库行为:current/dispatch 订单查询按 where(含 status != 'cancel')过滤。 * * @param {Object} [options={}] 测试覆盖配置。 * @return {{ ctx: Object, calls: Object }} 测试上下文与调用记录。 */ function createPipelineSkipCancelledHarness(options = {}) { const calls = { orderDeliveryFindAllQueries: [], dispatchFindAllQueries: [], asyncTaskPayloads: [], }; const orderDeliveryRows = Array.isArray(options.orderDeliveryRows) ? options.orderDeliveryRows : []; const dispatchRows = Array.isArray(options.dispatchRows) ? options.dispatchRows : []; const matchWhere = (row, where = {}) => { const idValues = Array.isArray(where.id) ? where.id : (where.id && Array.isArray(where.id.in) ? where.id.in : []); if (idValues.length > 0 && !idValues.includes(Number(row.id))) { return false; } const stepValues = Array.isArray(where.step) ? where.step : (where.step && Array.isArray(where.step.in) ? where.step.in : []); if (stepValues.length > 0 && !stepValues.includes(Number(row.step))) { return false; } const excludedStatus = where.status && where.status.ne ? String(where.status.ne).trim() : ''; if (excludedStatus && String(row.status || '').trim() === excludedStatus) { return false; } return true; }; const ctx = { app: { Sequelize: { Op: { in: 'in', ne: 'ne', }, }, }, user: { store_id: 100000, }, model: { OrderDeliveryFactory: { async getCurrentModel() { return { async findAll(query = {}) { calls.orderDeliveryFindAllQueries.push(query); return orderDeliveryRows.filter(row => matchWhere(row, query.where)).map(row => ({ ...row })); }, async update() { return [ 1 ]; }, }; }, async getDispatchModel() { return { async findAll(query = {}) { calls.dispatchFindAllQueries.push(query); return dispatchRows.filter(row => matchWhere(row, query.where)).map(row => ({ ...row })); }, }; }, }, PrintPlatform: { async findAll() { return [ { id: 901, platform_code: 'PDD_PRINT', request_platform_code: 'OPEN_API', }, ]; }, }, }, service: { systems: { platform: { async getPlatformList() { return [ { code: 'PDD', print_platform_id: 901, default_express_interface: 'POLYAPI', }, ]; }, }, setting: { expressInterface: { async listByWarehousePlatformCode() { return { data: [ { id: 22, sys_express_code: 'SF', express_code: 'SF', }, ], }; }, }, }, }, }, logger: { info() {}, warn() {}, error() {}, }, }; return { ctx, calls, }; } describe('test/app/service/systems/express.createWaybillCodePipeline.skipCancelled.test.js', () => { it('should exclude cancelled orders via status filter in current and dispatch queries', async () => { const { ctx, calls } = createPipelineSkipCancelledHarness({ orderDeliveryRows: [ { id: 1, warehouse_id: 100000, status: 'cancel', step: 4, sys_platform_code: 'PDD', logistics_code: 'SF', sys_platform_name: '拼多多', logistics_name: '顺丰', store_id: 100000, }, { id: 2, warehouse_id: 100000, status: 'created', step: 2, sys_platform_code: 'PDD', logistics_code: 'SF', sys_platform_name: '拼多多', logistics_name: '顺丰', store_id: 100000, }, ], }); const service = new ExpressService(ctx); service.asyncTask = (batch, options, matchedPlatform, expressInterfaceConfig) => { calls.asyncTaskPayloads.push({ batch, options, matchedPlatform, expressInterfaceConfig }); }; const result = await service.createWaybillCodePipeline([ { order_id: 1, business_key: 1 }, { order_id: 2, business_key: 1 }, ]); assert.deepEqual(result, { message: '分配快递任务已提交' }); // current 表查询必须携带 status != 'cancel' 过滤 const currentQuery = calls.orderDeliveryFindAllQueries[0]; assert.deepEqual(currentQuery.where.status, { ne: 'cancel' }); assert.deepEqual(currentQuery.where.step, { in: [ 1, 2, 3, 4 ] }); // dispatch 补查同样必须携带 status != 'cancel' 过滤 assert.equal(calls.dispatchFindAllQueries.length, 1); assert.deepEqual(calls.dispatchFindAllQueries[0].where.status, { ne: 'cancel' }); // 只有正常订单进入取号任务批次,取消订单被跳过 assert.equal(calls.asyncTaskPayloads.length, 1); assert.deepEqual( calls.asyncTaskPayloads[0].batch.map(order => Number(order.id)), [ 2 ] ); }); it('should not submit any waybill task when all orders are cancelled', async () => { const { ctx, calls } = createPipelineSkipCancelledHarness({ orderDeliveryRows: [ { id: 1, warehouse_id: 100000, status: 'cancel', step: 4, sys_platform_code: 'PDD', logistics_code: 'SF', sys_platform_name: '拼多多', logistics_name: '顺丰', store_id: 100000, }, ], }); const service = new ExpressService(ctx); service.asyncTask = (batch, options, matchedPlatform, expressInterfaceConfig) => { calls.asyncTaskPayloads.push({ batch, options, matchedPlatform, expressInterfaceConfig }); }; const result = await service.createWaybillCodePipeline([ { order_id: 1, business_key: 1 }, ]); assert.deepEqual(result, { message: '所有订单均已分配运单号,无需处理' }); assert.equal(calls.asyncTaskPayloads.length, 0); }); });