WMS_MAIN/test/app/service/systems/express.async_dispatch.test.js
2026-04-27 16:09:26 +08:00

265 lines
6.8 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,
cainiao: 0,
cainiaoInput: null,
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;
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;
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 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, []);
});
});