WMS_MAIN/test/app/service/systemMethods/order/pendingOrder.allocateExpressCode.test.js

243 lines
7.5 KiB
JavaScript

'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: [],
orderDeliveryUpdatePayload: null,
orderDeliveryUpdateWhere: null,
markWaybillAsyncOrdersAsErrorArgs: null,
};
const orderDeliveryModel = {
async findAll() {
return orderDeliveryRows;
},
async update(payload = {}, query = {}) {
calls.orderDeliveryUpdatePayload = payload;
calls.orderDeliveryUpdateWhere = query.where || null;
const ids = query && query.where && query.where.id && query.where.id[Sequelize.Op.in];
const affectedRows = Array.isArray(ids) ? ids.length : 0;
return [ affectedRows ];
},
};
const ctx = {
app: {
Sequelize,
config: {},
},
user: {
store_id: 100000,
},
model: {
OrderDeliveryFactory: {
async getCurrentModel() {
return orderDeliveryModel;
},
},
},
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);
const originalMarkWaybillAsyncOrdersAsError = expressService.markWaybillAsyncOrdersAsError.bind(expressService);
expressService.markWaybillAsyncOrdersAsError = async (...args) => {
calls.markWaybillAsyncOrdersAsErrorArgs = args;
return await originalMarkWaybillAsyncOrdersAsError(...args);
};
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 rollback post_status for business_key=1 orders 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: 1 },
{ order_id: 202, business_key: 9902 },
{ order_id: 203, business_key: '1' },
{ order_id: 204, business_key: [ 2, '1' ] },
],
}),
/平台接口异常/
);
assert.equal(calls.operationLogs.length, 4);
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');
assert.equal(calls.operationLogs[2].businessId, '203');
assert.equal(calls.operationLogs[3].businessId, '204');
assert.ok(calls.markWaybillAsyncOrdersAsErrorArgs);
assert.deepEqual(calls.markWaybillAsyncOrdersAsErrorArgs[1], [ 201, 203, 204 ]);
assert.equal(calls.markWaybillAsyncOrdersAsErrorArgs[2], '平台接口异常');
assert.deepEqual(calls.orderDeliveryUpdatePayload, {
post_status: 'error',
step: 3,
order_flag: '平台接口异常',
updated_at: calls.orderDeliveryUpdatePayload.updated_at,
});
assert.deepEqual(calls.orderDeliveryUpdateWhere, {
id: {
[Sequelize.Op.in]: [ 201, 203, 204 ],
},
});
});
});