WMS_MAIN/test/app/service/systems/express.async_dispatch.test.js
liufumingren dc4894cb6d 1
2026-05-25 17:08:56 +08:00

308 lines
8.2 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const ExpressService = require('../../../../app/service/systems/express');
function createAsyncDispatchHarness(options = {}) {
const calls = {
sequence: [],
polyapi: 0,
polyapiInput: null,
polyapiInputs: [],
cainiao: 0,
cainiaoInput: null,
cainiaoInputs: [],
orderExpressDestroy: 0,
orderExpressDestroyOptions: null,
orderDeliveryUpdate: 0,
orderDeliveryUpdatePayload: null,
orderDeliveryUpdateOptions: null,
loggerErrors: [],
};
const serviceMethods = Object.assign({}, options.serviceMethods || {});
const ctx = {
app: {
Sequelize: {
Op: {
in: 'in',
},
},
},
model: {
OrderExpress: {
async destroy(queryOptions) {
calls.sequence.push('destroy');
calls.orderExpressDestroy += 1;
calls.orderExpressDestroyOptions = queryOptions;
if (typeof options.destroy === 'function') {
return await options.destroy(queryOptions);
}
return 1;
},
},
OrderDelivery: {
async update(payload, queryOptions) {
calls.sequence.push('update');
calls.orderDeliveryUpdate += 1;
calls.orderDeliveryUpdatePayload = payload;
calls.orderDeliveryUpdateOptions = queryOptions;
if (typeof options.update === 'function') {
return await options.update(payload, queryOptions);
}
return [ 1 ];
},
},
},
service: {
systems: {
expressPlatform: serviceMethods,
},
},
logger: {
info() {},
warn() {},
error(...args) {
calls.loggerErrors.push(args);
},
},
};
if (!serviceMethods.polyapi && options.enableDefaultPolyapi !== false) {
ctx.service.systems.expressPlatform.polyapi = async data => {
calls.polyapi += 1;
calls.polyapiInput = data;
calls.polyapiInputs.push(data);
return {
flag: 'success',
message: 'ok',
data: {
platform: 'polyapi',
},
};
};
}
if (!serviceMethods.cainiao && options.enableDefaultCainiao === true) {
ctx.service.systems.expressPlatform.cainiao = async data => {
calls.cainiao += 1;
calls.cainiaoInput = data;
calls.cainiaoInputs.push(data);
return {
flag: 'success',
message: 'ok',
data: {
platform: 'cainiao',
},
};
};
}
return {
ctx,
calls,
};
}
describe('test/app/service/systems/express.async_dispatch.test.js', () => {
it('should dispatch to express platform service method directly', async () => {
const { ctx, calls } = createAsyncDispatchHarness();
const service = new ExpressService(ctx);
const bizcontent = {
platform_code: 'polyapi',
order_list: [{ order_id: 101 }],
};
await service.dispatchWaybillAsyncPlatform('polyapi', bizcontent);
assert.equal(calls.polyapi, 1);
assert.strictEqual(calls.polyapiInput, bizcontent);
});
it('should dispatch cainiao orders one by one but keep polyapi batched', async () => {
const { ctx, calls } = createAsyncDispatchHarness({
enableDefaultCainiao: true,
});
const service = new ExpressService(ctx);
const cainiaoBizcontent = {
matchedPlatform: {
default_express_interface: 'cainiao',
},
orders: [
{ order_id: 101 },
{ order_id: 102 },
],
trace_id: 'trace_async_split',
};
const polyapiBizcontent = {
matchedPlatform: {
default_express_interface: 'polyapi',
},
orders: [
{ order_id: 201 },
{ order_id: 202 },
],
trace_id: 'trace_async_batch',
};
await service.dispatchWaybillAsyncPlatform('cainiao', cainiaoBizcontent);
await service.dispatchWaybillAsyncPlatform('polyapi', polyapiBizcontent);
assert.equal(calls.cainiao, 2);
assert.equal(calls.cainiaoInputs.length, 2);
assert.deepEqual(calls.cainiaoInputs.map(item => item.orders.length), [ 1, 1 ]);
assert.deepEqual(calls.cainiaoInputs.map(item => item.orders[0].order_id), [ 101, 102 ]);
assert.equal(calls.polyapi, 1);
assert.strictEqual(calls.polyapiInput, polyapiBizcontent);
assert.equal(calls.polyapiInputs.length, 1);
assert.equal(calls.polyapiInputs[0].orders.length, 2);
});
it('should throw interface not implemented when platform method is missing', async () => {
const { ctx } = createAsyncDispatchHarness({
serviceMethods: {
polyapi: async () => ({
flag: 'success',
message: 'ok',
}),
},
enableDefaultPolyapi: false,
enableDefaultCainiao: false,
});
const service = new ExpressService(ctx);
await assert.rejects(
() => service.dispatchWaybillAsyncPlatform('sto', { platform_code: 'sto' }),
/接口未实现/
);
});
it('should write back error status for failed groups and continue successful groups', async () => {
const { ctx, calls } = createAsyncDispatchHarness({
serviceMethods: {
polyapi: async data => {
calls.polyapi += 1;
calls.polyapiInput = data;
return {
flag: 'failure',
message: '平台失败',
};
},
cainiao: async data => {
calls.cainiao += 1;
calls.cainiaoInput = data;
return {
flag: 'success',
message: 'ok',
data: {
platform: 'cainiao',
},
};
},
},
enableDefaultPolyapi: false,
enableDefaultCainiao: false,
});
const service = new ExpressService(ctx);
await service.runWaybillAsyncPostProcess([
{
order_id: 101,
business_key: 9101,
wms_express_platform: {
platform_code: 'polyapi',
},
},
{
order_id: 102,
business_key: 9102,
wms_express_platform: {
platform_code: 'cainiao',
},
},
], {
warehouse_id: 100000,
store_id: 100000,
});
assert.equal(calls.polyapi, 1);
assert.equal(calls.cainiao, 1);
assert.equal(calls.orderDeliveryUpdate, 1);
assert.equal(calls.orderExpressDestroy, 1);
assert.deepEqual(calls.sequence, [ 'destroy', 'update' ]);
assert.deepEqual(
calls.orderExpressDestroyOptions.where[ctx.app.Sequelize.Op.or],
[
{
order_id: 101,
business_key: 9101,
},
]
);
assert.deepEqual(calls.orderDeliveryUpdatePayload, {
post_status: 'error',
step: 3,
order_flag: '平台失败',
updated_at: calls.orderDeliveryUpdatePayload.updated_at,
});
assert.ok(calls.orderDeliveryUpdatePayload.updated_at instanceof Date);
assert.deepEqual(calls.orderDeliveryUpdateOptions.where.id.in, [ 101 ]);
});
it('should skip hard delete and write back when failed orders miss business_key', async () => {
const { ctx, calls } = createAsyncDispatchHarness({
serviceMethods: {
polyapi: async data => {
calls.polyapi += 1;
calls.polyapiInput = data;
return {
flag: 'failure',
message: '平台失败',
};
},
cainiao: async data => {
calls.cainiao += 1;
calls.cainiaoInput = data;
return {
flag: 'success',
message: 'ok',
data: {
platform: 'cainiao',
},
};
},
},
enableDefaultPolyapi: false,
enableDefaultCainiao: false,
});
const service = new ExpressService(ctx);
await service.runWaybillAsyncPostProcess([
{
order_id: 201,
wms_express_platform: {
platform_code: 'polyapi',
},
},
{
order_id: 202,
business_key: 9202,
wms_express_platform: {
platform_code: 'cainiao',
},
},
], {
warehouse_id: 100000,
store_id: 100000,
});
assert.equal(calls.polyapi, 1);
assert.equal(calls.cainiao, 1);
assert.equal(calls.orderExpressDestroy, 0);
assert.equal(calls.orderDeliveryUpdate, 0);
assert.equal(calls.loggerErrors.length > 0, true);
assert.deepEqual(calls.sequence, []);
});
});