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

208 lines
5.0 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const ExpressPlatformService = require('../../../../app/service/systems/express_platform');
/**
* 构建拼多多服务最小测试桩。
*
* @return {{ service: Object, calls: Object }} 服务实例与调用记录。
*/
function createPddHarness() {
const calls = {
curl: 0,
curlOptions: null,
transaction: 0,
orderExpressUpdate: 0,
orderDeliveryUpdate: 0,
storeWarehouseFindAll: 0,
storeWarehouseUpdate: 0,
printQuantityLogCreate: 0,
operationLogCreateOperationLogs: 0,
};
const ctx = {
app: {
config: {
pdd: {
client_id: 'test-client-id',
client_secret: 'test-client-secret',
},
},
Sequelize: {
Op: {
or: 'or',
},
},
},
model: {
async transaction(callback) {
calls.transaction += 1;
const transaction = {
LOCK: {
UPDATE: 'UPDATE',
},
};
return callback(transaction);
},
OrderExpress: {
async update() {
calls.orderExpressUpdate += 1;
return [ 1 ];
},
},
StoreWarehouse: {
async findAll() {
calls.storeWarehouseFindAll += 1;
return [];
},
async update() {
calls.storeWarehouseUpdate += 1;
return [ 1 ];
},
},
StoreWarehousePrintQuantityLog: {
async create() {
calls.printQuantityLogCreate += 1;
return {};
},
},
},
service: {
systems: {
express: {
async getWaybillAsyncOrderDeliveryModel() {
return {
async update() {
calls.orderDeliveryUpdate += 1;
return [ 1 ];
},
};
},
},
operationLog: {
async createOperationLogs() {
calls.operationLogCreateOperationLogs += 1;
return [];
},
},
},
},
logger: {
info() {},
warn() {},
error() {},
},
async curl(url, options) {
calls.curl += 1;
calls.curlOptions = {
url,
options,
};
return {
data: {
pdd_waybill_get_response: {
modules: [
{
object_id: '100000_2001_3001',
waybill_code: 'WB-2001',
},
],
},
},
};
},
};
return {
service: new ExpressPlatformService(ctx),
calls,
};
}
/**
* 构造拼多多接口入参。
*
* @return {Object} 拼多多接口测试入参。
*/
function buildPayload() {
return {
matchedPlatform: {
template_url: 'https://example.com/template',
},
listByWarehousePlatformCode: {
id: 91,
token: 'test-token',
ext_data: {
category: 'fragile',
tags: [ 'cold', 'urgent' ],
},
city_name: '上海市',
province_name: '上海市',
district_name: '浦东新区',
town_name: '花木街道',
address: '世纪大道100号',
sender_tel: '13800000000',
sender_name: '测试发件人',
logistics_code: 'PDD-EXP',
logistics_name: '拼多多快递',
name: '拼多多接口',
platform_code: 'PDD',
express: {
PDD: 'YTO',
},
},
orders: [
{
business_key: 3001,
order_delivery: {
id: 2001,
warehouse_id: 100000,
store_id: 5001,
source_order_codes: 'SO-1',
delivery_order_code: 'DO-1',
orderItems: [
{
item_name: '商品A',
plan_qty: 2,
},
],
city: '上海市',
province: '上海市',
area: '浦东新区',
town: '花木街道',
detail_address: '测试地址1号',
mobile: '13900000000',
tel: '021-00000000',
name: '收件人A',
},
},
],
};
}
describe('test/app/service/systems/express_platform.pdd.ext_data.test.js', () => {
it('should stringify object ext_data for logistics_services and notify_extend_info', async () => {
const { service, calls } = createPddHarness();
const payload = buildPayload();
const result = await service.pdd(payload);
assert.equal(calls.curl, 1);
assert.ok(calls.curlOptions);
const requestBody = calls.curlOptions.options.data;
const waybillRequest = JSON.parse(requestBody.param_waybill_cloud_print_apply_new_request);
const orderItem = waybillRequest.trade_order_info_dtos[0];
assert.equal(typeof orderItem.logistics_services, 'string');
assert.equal(typeof orderItem.notify_extend_info, 'string');
assert.equal(orderItem.logistics_services, JSON.stringify(payload.listByWarehousePlatformCode.ext_data));
assert.equal(orderItem.notify_extend_info, JSON.stringify(payload.listByWarehousePlatformCode.ext_data));
assert.equal(result.flag, true);
assert.equal(result.code, 0);
});
});