341 lines
9.4 KiB
JavaScript
341 lines
9.4 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const ExpressService = require('../../../../app/service/systems/express');
|
|
|
|
/**
|
|
* 创建 createWaybillCodePipeline 的最小测试上下文。
|
|
*
|
|
* @param {Object} [options={}] 测试覆盖配置。
|
|
* @return {{ ctx: Object, calls: Object }} 测试上下文与调用记录。
|
|
*/
|
|
function createPipelineHarness(options = {}) {
|
|
const calls = {
|
|
listByWarehousePlatformCodePayloads: [],
|
|
asyncTaskPayloads: [],
|
|
printPlatformFindAllQueries: [],
|
|
};
|
|
|
|
const orderDeliveryRows = Array.isArray(options.orderDeliveryRows)
|
|
? options.orderDeliveryRows
|
|
: [
|
|
{
|
|
id: 1,
|
|
sys_platform_code: 'PDD',
|
|
logistics_code: 'SF',
|
|
sys_platform_name: '拼多多',
|
|
logistics_name: '顺丰',
|
|
store_id: 100000,
|
|
},
|
|
];
|
|
|
|
const listData = Array.isArray(options.listData) ? options.listData : [];
|
|
const platformList = Array.isArray(options.platformList) ? options.platformList : [{
|
|
code: 'PDD',
|
|
print_platform_id: 901,
|
|
default_express_interface: 'POLYAPI',
|
|
}];
|
|
const printPlatformRows = Array.isArray(options.printPlatformRows) ? options.printPlatformRows : [
|
|
{
|
|
id: 901,
|
|
platform_code: 'PDD_PRINT',
|
|
request_platform_code: 'OPEN_API',
|
|
},
|
|
];
|
|
|
|
const ctx = {
|
|
app: {
|
|
Sequelize: {
|
|
Op: {
|
|
in: 'in',
|
|
},
|
|
},
|
|
},
|
|
user: {
|
|
store_id: 100000,
|
|
},
|
|
model: {
|
|
OrderDeliveryFactory: {
|
|
async getCurrentModel() {
|
|
return {
|
|
async findAll() {
|
|
return orderDeliveryRows;
|
|
},
|
|
async update() {
|
|
return [ 1 ];
|
|
},
|
|
};
|
|
},
|
|
},
|
|
PrintPlatform: {
|
|
async findAll(query) {
|
|
calls.printPlatformFindAllQueries.push(query);
|
|
return printPlatformRows;
|
|
},
|
|
},
|
|
},
|
|
service: {
|
|
systems: {
|
|
platform: {
|
|
async getPlatformList() {
|
|
return platformList;
|
|
},
|
|
},
|
|
setting: {
|
|
expressInterface: {
|
|
async listByWarehousePlatformCode(payload) {
|
|
calls.listByWarehousePlatformCodePayloads.push(payload);
|
|
return {
|
|
data: listData,
|
|
};
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
};
|
|
|
|
return {
|
|
ctx,
|
|
calls,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systems/express.createWaybillCodePipeline.matchExpressInterface.test.js', () => {
|
|
it('should choose matched SF interface instead of first YTO item', async () => {
|
|
const { ctx, calls } = createPipelineHarness({
|
|
orderDeliveryRows: [
|
|
{
|
|
id: 101,
|
|
sys_platform_code: 'PDD',
|
|
logistics_code: ' sf ',
|
|
sys_platform_name: '拼多多',
|
|
logistics_name: '顺丰',
|
|
store_id: 100000,
|
|
},
|
|
],
|
|
listData: [
|
|
{
|
|
id: 11,
|
|
sys_express_code: 'YTO',
|
|
express_code: 'YTO',
|
|
},
|
|
{
|
|
id: 22,
|
|
sys_express_code: ' SF ',
|
|
express_code: 'SF',
|
|
},
|
|
],
|
|
});
|
|
const service = new ExpressService(ctx);
|
|
service.asyncTask = (batch, options, matchedPlatform, expressInterfaceConfig) => {
|
|
calls.asyncTaskPayloads.push({ batch, options, matchedPlatform, expressInterfaceConfig });
|
|
};
|
|
|
|
await service.createWaybillCodePipeline([
|
|
{
|
|
order_id: 101,
|
|
business_key: 9001,
|
|
},
|
|
]);
|
|
|
|
assert.deepEqual(calls.listByWarehousePlatformCodePayloads[0], {
|
|
warehouseId: 100000,
|
|
platform_code: 'PDD_PRINT',
|
|
});
|
|
assert.deepEqual(calls.printPlatformFindAllQueries.map(item => item.attributes), [
|
|
[ 'id', 'platform_code' ],
|
|
[ 'id', 'request_platform_code' ],
|
|
]);
|
|
assert.equal(calls.asyncTaskPayloads.length, 1);
|
|
assert.equal(calls.asyncTaskPayloads[0].matchedPlatform.code, 'PDD');
|
|
assert.equal(calls.asyncTaskPayloads[0].matchedPlatform.request_platform_code, 'OPEN_API');
|
|
assert.equal(calls.asyncTaskPayloads[0].expressInterfaceConfig.id, 22);
|
|
assert.equal(calls.asyncTaskPayloads[0].expressInterfaceConfig.sys_express_code.trim(), 'SF');
|
|
});
|
|
|
|
it('should not fallback to first item when no interface matches logistics code', async () => {
|
|
const { ctx, calls } = createPipelineHarness({
|
|
orderDeliveryRows: [
|
|
{
|
|
id: 102,
|
|
sys_platform_code: 'PDD',
|
|
logistics_code: 'SF',
|
|
sys_platform_name: '拼多多',
|
|
logistics_name: '顺丰',
|
|
store_id: 100000,
|
|
},
|
|
],
|
|
listData: [
|
|
{
|
|
id: 31,
|
|
sys_express_code: 'YTO',
|
|
express_code: 'YTO',
|
|
},
|
|
],
|
|
});
|
|
const service = new ExpressService(ctx);
|
|
service.asyncTask = (batch, options, matchedPlatform, expressInterfaceConfig) => {
|
|
calls.asyncTaskPayloads.push({ batch, options, matchedPlatform, expressInterfaceConfig });
|
|
};
|
|
|
|
await service.createWaybillCodePipeline([
|
|
{
|
|
order_id: 102,
|
|
business_key: 9002,
|
|
},
|
|
]);
|
|
|
|
assert.deepEqual(calls.listByWarehousePlatformCodePayloads[0], {
|
|
warehouseId: 100000,
|
|
platform_code: 'PDD_PRINT',
|
|
});
|
|
assert.equal(calls.asyncTaskPayloads.length, 1);
|
|
assert.equal(calls.asyncTaskPayloads[0].matchedPlatform.code, 'PDD');
|
|
assert.equal(calls.asyncTaskPayloads[0].expressInterfaceConfig, null);
|
|
});
|
|
|
|
it('should fallback to sys platform code when print platform code is absent', async () => {
|
|
const { ctx, calls } = createPipelineHarness({
|
|
platformList: [
|
|
{
|
|
code: 'PDD',
|
|
print_platform_id: 901,
|
|
default_express_interface: '',
|
|
},
|
|
],
|
|
printPlatformRows: [
|
|
{
|
|
id: 901,
|
|
platform_code: 'PDD',
|
|
request_platform_code: '',
|
|
},
|
|
],
|
|
listData: [
|
|
{
|
|
id: 41,
|
|
sys_express_code: 'SF',
|
|
express_code: 'SF',
|
|
},
|
|
],
|
|
});
|
|
const service = new ExpressService(ctx);
|
|
service.asyncTask = (batch, options, matchedPlatform, expressInterfaceConfig) => {
|
|
calls.asyncTaskPayloads.push({ batch, options, matchedPlatform, expressInterfaceConfig });
|
|
};
|
|
|
|
await service.createWaybillCodePipeline([
|
|
{
|
|
order_id: 1,
|
|
business_key: 1,
|
|
},
|
|
]);
|
|
|
|
assert.deepEqual(calls.listByWarehousePlatformCodePayloads[0], {
|
|
warehouseId: 100000,
|
|
platform_code: 'PDD',
|
|
});
|
|
assert.equal(calls.asyncTaskPayloads[0].expressInterfaceConfig.id, 41);
|
|
});
|
|
|
|
it('should keep route platform code and default_express_interface with different values', async () => {
|
|
const { ctx, calls } = createPipelineHarness({
|
|
platformList: [
|
|
{
|
|
code: 'PDD',
|
|
print_platform_id: 901,
|
|
route_platform_code: 'PDD_ROUTE',
|
|
default_express_interface: 'POLYAPI',
|
|
},
|
|
],
|
|
listData: [
|
|
{
|
|
id: 51,
|
|
sys_express_code: 'SF',
|
|
express_code: 'SF',
|
|
},
|
|
],
|
|
});
|
|
const service = new ExpressService(ctx);
|
|
service.asyncTask = (batch, options, matchedPlatform, expressInterfaceConfig) => {
|
|
calls.asyncTaskPayloads.push({ batch, options, matchedPlatform, expressInterfaceConfig });
|
|
};
|
|
|
|
await service.createWaybillCodePipeline([
|
|
{
|
|
order_id: 1,
|
|
business_key: 1,
|
|
},
|
|
]);
|
|
|
|
assert.deepEqual(calls.listByWarehousePlatformCodePayloads[0], {
|
|
warehouseId: 100000,
|
|
platform_code: 'PDD_PRINT',
|
|
});
|
|
assert.equal(calls.asyncTaskPayloads[0].matchedPlatform.request_platform_code, 'OPEN_API');
|
|
assert.equal(calls.asyncTaskPayloads[0].matchedPlatform.route_platform_code, 'PDD_ROUTE');
|
|
assert.equal(calls.asyncTaskPayloads[0].matchedPlatform.default_express_interface, 'POLYAPI');
|
|
});
|
|
|
|
it('should group and match by mapped print platform code for same sys platform code', async () => {
|
|
const { ctx, calls } = createPipelineHarness({
|
|
orderDeliveryRows: [
|
|
{
|
|
id: 201,
|
|
sys_platform_code: 'PDD',
|
|
logistics_code: 'SF',
|
|
sys_platform_name: '拼多多',
|
|
logistics_name: '顺丰',
|
|
store_id: 100000,
|
|
},
|
|
{
|
|
id: 202,
|
|
sys_platform_code: 'PDD',
|
|
logistics_code: 'SF',
|
|
sys_platform_name: '拼多多',
|
|
logistics_name: '顺丰',
|
|
store_id: 100000,
|
|
},
|
|
],
|
|
listData: [
|
|
{
|
|
id: 61,
|
|
express: {
|
|
pdd_print: 'SF',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
const service = new ExpressService(ctx);
|
|
service.asyncTask = (batch, options, matchedPlatform, expressInterfaceConfig) => {
|
|
calls.asyncTaskPayloads.push({ batch, options, matchedPlatform, expressInterfaceConfig });
|
|
};
|
|
|
|
await service.createWaybillCodePipeline([
|
|
{
|
|
order_id: 201,
|
|
business_key: 9201,
|
|
},
|
|
{
|
|
order_id: 202,
|
|
business_key: 9202,
|
|
},
|
|
]);
|
|
|
|
assert.equal(calls.asyncTaskPayloads.length, 1);
|
|
assert.equal(calls.asyncTaskPayloads[0].batch.length, 2);
|
|
assert.equal(calls.asyncTaskPayloads[0].options.sys_platform_code, 'PDD');
|
|
assert.equal(calls.asyncTaskPayloads[0].options.print_platform_code, 'PDD_PRINT');
|
|
assert.deepEqual(calls.listByWarehousePlatformCodePayloads[0], {
|
|
warehouseId: 100000,
|
|
platform_code: 'PDD_PRINT',
|
|
});
|
|
assert.equal(calls.asyncTaskPayloads[0].expressInterfaceConfig.id, 61);
|
|
});
|
|
});
|