- object_Id/orderno 中 order.id 改为 16 进制编码,缩短订单唯一标识字符串 - 失败项/各平台成功回写分支按 parseInt(x,16) 解码回十进制,保持落库与映射兼容 - 修复本文件 31 处 ESLint(空格/引号/注释等),纯格式不涉及业务逻辑
390 lines
11 KiB
JavaScript
390 lines
11 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const ExpressPlatformService = require('../../../../app/service/systems/express_platform');
|
|
|
|
const DEFAULT_CAINIAO_TEMPLATE_URL = 'http://cloudprint.cainiao.com/cloudprint/template/getStandardTemplate.json?template_id=288948';
|
|
|
|
/**
|
|
* 构建菜鸟服务测试桩。
|
|
*
|
|
* @param {Object} [options={}] 测试选项。
|
|
* @return {{service: Object, calls: Object}} 服务实例与调用记录。
|
|
*/
|
|
function createCainiaoHarness(options = {}) {
|
|
const calls = {
|
|
signTopRequest: 0,
|
|
signTopRequestArgs: null,
|
|
curl: 0,
|
|
curlOptions: null,
|
|
transaction: 0,
|
|
orderExpressUpdate: 0,
|
|
orderExpressUpdateArgs: [],
|
|
orderDeliveryUpdate: 0,
|
|
storeWarehouseFindAll: 0,
|
|
storeWarehouseUpdate: 0,
|
|
storeWarehouseUpdateArgs: [],
|
|
printQuantityLogCreate: 0,
|
|
operationLogCreateOperationLogs: 0,
|
|
operationLogPayloads: [],
|
|
getContractConfigCalls: [],
|
|
syncOrderIdsCalls: [],
|
|
};
|
|
|
|
const relationRows = [
|
|
{
|
|
id: 501,
|
|
store_id: 5001,
|
|
warehouse_id: 100000,
|
|
print_quantity: 10,
|
|
print_quantity_locked: 2,
|
|
},
|
|
];
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {
|
|
qimen: {
|
|
appKey: 'tb-app-key',
|
|
security: 'tb-app-secret',
|
|
},
|
|
},
|
|
Sequelize: {
|
|
Op: {
|
|
or: 'or',
|
|
},
|
|
},
|
|
formatDate() {
|
|
return '2026-05-24 12:00:00';
|
|
},
|
|
async setCacheAsync() {
|
|
// no-op in tests
|
|
},
|
|
},
|
|
model: {
|
|
async transaction(callback) {
|
|
calls.transaction += 1;
|
|
const transaction = {
|
|
LOCK: {
|
|
UPDATE: 'UPDATE',
|
|
},
|
|
};
|
|
return callback(transaction);
|
|
},
|
|
OrderExpress: {
|
|
async update(values, query) {
|
|
calls.orderExpressUpdate += 1;
|
|
calls.orderExpressUpdateArgs.push({ values, query });
|
|
return [ 1 ];
|
|
},
|
|
},
|
|
StoreWarehouse: {
|
|
async findAll() {
|
|
calls.storeWarehouseFindAll += 1;
|
|
return relationRows;
|
|
},
|
|
async update(values, query) {
|
|
calls.storeWarehouseUpdate += 1;
|
|
calls.storeWarehouseUpdateArgs.push({ values, query });
|
|
return [ 1 ];
|
|
},
|
|
},
|
|
StoreWarehousePrintQuantityLog: {
|
|
async create() {
|
|
calls.printQuantityLogCreate += 1;
|
|
return {};
|
|
},
|
|
},
|
|
},
|
|
service: {
|
|
systems: {
|
|
sign: {
|
|
signTopRequest(params, body, secret, signMethod) {
|
|
calls.signTopRequest += 1;
|
|
calls.signTopRequestArgs = {
|
|
params,
|
|
body,
|
|
secret,
|
|
signMethod,
|
|
};
|
|
return 'TB_TEST_SIGN';
|
|
},
|
|
},
|
|
express: {
|
|
async loadExpressList() {
|
|
return [];
|
|
},
|
|
async getWaybillAsyncOrderDeliveryModel() {
|
|
return {
|
|
async update() {
|
|
calls.orderDeliveryUpdate += 1;
|
|
return [ 1 ];
|
|
},
|
|
};
|
|
},
|
|
},
|
|
operationLog: {
|
|
async createOperationLogs(payloads = []) {
|
|
calls.operationLogCreateOperationLogs += 1;
|
|
calls.operationLogPayloads.push(...payloads);
|
|
return payloads;
|
|
},
|
|
},
|
|
storeWarehouse: {
|
|
async getContractConfig(warehouseId, storeId) {
|
|
calls.getContractConfigCalls.push({ warehouseId, storeId });
|
|
if (options.contractConfig !== undefined) {
|
|
return options.contractConfig;
|
|
}
|
|
return { policy_config: { auto_sync_back: 1 } };
|
|
},
|
|
},
|
|
},
|
|
systemMethods: {
|
|
order: {
|
|
pendingOrder: {
|
|
async syncOrderIds(request) {
|
|
calls.syncOrderIdsCalls.push(request);
|
|
return { flag: 'success' };
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
state: {
|
|
user: {
|
|
employees_id: 9001,
|
|
employees_name: '测试员',
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
async curl(url, optionsArg) {
|
|
calls.curl += 1;
|
|
calls.curlOptions = {
|
|
url,
|
|
options: optionsArg,
|
|
};
|
|
|
|
if (options.responseData) {
|
|
return {
|
|
data: options.responseData,
|
|
};
|
|
}
|
|
|
|
return {
|
|
data: {
|
|
cainiao_waybill_ii_get_response: {
|
|
modules: {
|
|
waybill_cloud_print_response: [
|
|
{
|
|
object_id: '100000_2001_3001',
|
|
waybill_code: 'CNWB-001',
|
|
print_data: JSON.stringify({
|
|
data: {
|
|
extraInfo: {
|
|
encryptWaybillCode: 'ENC-WB-001',
|
|
},
|
|
},
|
|
signature: 'SIG-001',
|
|
}),
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
};
|
|
},
|
|
};
|
|
|
|
return {
|
|
service: new ExpressPlatformService(ctx),
|
|
calls,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 构造菜鸟接口入参。
|
|
*
|
|
* @return {Object} 菜鸟接口测试入参。
|
|
*/
|
|
function buildPayload() {
|
|
return {
|
|
matchedPlatform: {
|
|
template_url: 'https://example.com/cainiao-template',
|
|
platform_code: 'CAINIAO',
|
|
},
|
|
listByWarehousePlatformCode: {
|
|
id: 91,
|
|
token: 'tb-access-token',
|
|
ext_data: {
|
|
order_tag: 'normal',
|
|
},
|
|
city_name: '上海市',
|
|
province_name: '上海市',
|
|
district_name: '浦东新区',
|
|
town_name: '花木街道',
|
|
address: '世纪大道100号',
|
|
sender_tel: '13800000000',
|
|
sender_name: '测试发件人',
|
|
logistics_code: 'TB-EXP',
|
|
logistics_name: '淘宝快递',
|
|
name: '淘宝接口',
|
|
platform_code: 'CAINIAO',
|
|
customer_code: 'CUST-001',
|
|
site_code: 'SITE-001',
|
|
express: {
|
|
CAINIAO: 'YTO',
|
|
},
|
|
shop: {
|
|
owner_id: 'owner-1',
|
|
},
|
|
},
|
|
orders: [
|
|
{
|
|
business_key: 3001,
|
|
order_delivery: {
|
|
id: 2001,
|
|
warehouse_id: 100000,
|
|
store_id: 5001,
|
|
sys_platform_code: 'CAINIAO',
|
|
source_order_codes: 'SO-1,SO-2',
|
|
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.cainiao.test.js', () => {
|
|
it('should call taobao router/rest with signed top params', async () => {
|
|
const { service, calls } = createCainiaoHarness();
|
|
|
|
const result = await service.cainiao(buildPayload());
|
|
|
|
assert.equal(calls.curl, 1);
|
|
assert.equal(calls.curlOptions.url, 'https://eco.taobao.com/router/rest');
|
|
assert.equal(calls.curlOptions.options.method, 'POST');
|
|
assert.equal(calls.curlOptions.options.contentType, 'form');
|
|
assert.equal(calls.signTopRequest, 1);
|
|
assert.equal(calls.signTopRequestArgs.secret, 'tb-app-secret');
|
|
assert.equal(calls.signTopRequestArgs.signMethod, 'md5');
|
|
assert.equal(calls.signTopRequestArgs.body, '');
|
|
assert.equal(calls.signTopRequestArgs.params.method, 'cainiao.waybill.ii.get');
|
|
assert.equal(calls.signTopRequestArgs.params.app_key, 'tb-app-key');
|
|
assert.equal(calls.signTopRequestArgs.params.session, 'tb-access-token');
|
|
assert.equal(calls.signTopRequestArgs.params.format, 'json');
|
|
assert.equal(calls.signTopRequestArgs.params.v, '2.0');
|
|
assert.equal(calls.signTopRequestArgs.params.sign_method, 'md5');
|
|
|
|
const requestBody = calls.curlOptions.options.data;
|
|
const bizBody = JSON.parse(requestBody.param_waybill_cloud_print_apply_new_request);
|
|
assert.equal(bizBody.cp_code, 'YTO');
|
|
assert.equal(bizBody.customer_code, 'CUST-001');
|
|
assert.equal(bizBody.shipping_branch_code, 'SITE-001');
|
|
assert.equal(bizBody.sender.mobile, '13800000000');
|
|
assert.equal(bizBody.trade_order_info_dtos[0].object_id, '100000_2001_3001');
|
|
|
|
assert.equal(result.flag, true);
|
|
assert.equal(result.code, 0);
|
|
assert.equal(result.message, '菜鸟平台数据处理成功');
|
|
});
|
|
|
|
it('should persist success updates and write operation logs', async () => {
|
|
const { service, calls } = createCainiaoHarness();
|
|
|
|
await service.cainiao(buildPayload());
|
|
|
|
assert.equal(calls.transaction, 1);
|
|
assert.equal(calls.orderExpressUpdate, 1);
|
|
assert.equal(calls.orderDeliveryUpdate, 1);
|
|
assert.equal(calls.storeWarehouseFindAll, 1);
|
|
assert.equal(calls.storeWarehouseUpdate, 1);
|
|
assert.equal(calls.printQuantityLogCreate, 1);
|
|
assert.equal(calls.operationLogCreateOperationLogs, 1);
|
|
|
|
const updateValues = calls.orderExpressUpdateArgs[0].values;
|
|
assert.equal(updateValues.waybill_code, 'CNWB-001');
|
|
assert.equal(updateValues.signature, 'SIG-001');
|
|
assert.equal(updateValues.encryptprintinfo, 'ENC-WB-001');
|
|
assert.equal(typeof updateValues.printinfo, 'string');
|
|
|
|
assert.equal(calls.storeWarehouseUpdateArgs[0].values.print_quantity, 9);
|
|
assert.equal(calls.storeWarehouseUpdateArgs[0].values.print_quantity_locked, 1);
|
|
assert.equal(calls.operationLogPayloads[0].businessId, '2001');
|
|
});
|
|
|
|
it('should fallback to default template_url when matchedPlatform.template_url is invalid', async () => {
|
|
const { service, calls } = createCainiaoHarness();
|
|
const payload = buildPayload();
|
|
|
|
payload.matchedPlatform.template_url = 'invalid-template-url';
|
|
|
|
await service.cainiao(payload);
|
|
|
|
const requestBody = calls.curlOptions.options.data;
|
|
const bizBody = JSON.parse(requestBody.param_waybill_cloud_print_apply_new_request);
|
|
|
|
assert.equal(bizBody.trade_order_info_dtos[0].template_url, DEFAULT_CAINIAO_TEMPLATE_URL);
|
|
});
|
|
|
|
it('should throw when taobao returns error_response', async () => {
|
|
const { service } = createCainiaoHarness({
|
|
responseData: {
|
|
error_response: {
|
|
msg: 'isv.invalid-parameter',
|
|
sub_msg: '参数错误',
|
|
},
|
|
},
|
|
});
|
|
|
|
await assert.rejects(
|
|
async () => {
|
|
await service.cainiao(buildPayload());
|
|
},
|
|
/参数错误/
|
|
);
|
|
});
|
|
|
|
it('auto_sync_back 开启且无登录用户上下文时,应以显式 warehouseId 触发订单同步回传', async () => {
|
|
const { service, calls } = createCainiaoHarness();
|
|
|
|
await service.cainiao(buildPayload());
|
|
|
|
assert.equal(calls.getContractConfigCalls.length, 1);
|
|
assert.deepEqual(calls.getContractConfigCalls[0], { warehouseId: 100000, storeId: 5001 });
|
|
assert.equal(calls.syncOrderIdsCalls.length, 1);
|
|
assert.deepEqual(calls.syncOrderIdsCalls[0], {
|
|
orderIds: [ 2001 ],
|
|
warehouseId: 100000,
|
|
});
|
|
});
|
|
|
|
it('auto_sync_back 关闭时不应触发订单同步回传', async () => {
|
|
const { service, calls } = createCainiaoHarness({
|
|
contractConfig: { policy_config: { auto_sync_back: 0 } },
|
|
});
|
|
|
|
await service.cainiao(buildPayload());
|
|
|
|
assert.equal(calls.syncOrderIdsCalls.length, 0);
|
|
});
|
|
});
|