liufumingren abf2c4d94a 1
2026-05-29 16:47:38 +08:00

2248 lines
69 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const assert = require('node:assert/strict');
const IndexMethodService = require('../../../../app/service/systemMethods/index');
/**
* 创建 systemMethods.index 的最小测试桩环境。
*
* @return {{service: Object, ctx: Object, calls: Object}} 测试桩对象
*/
function createHarness() {
const calls = {
curl: 0,
getCacheAsync: 0,
syncPddSfTimedDeliveryExpressProductTypes: 0,
setCacheAsync: 0,
getExpressProductTypeList: 0,
getPrintPlatformList: 0,
printPlatformFindOne: 0,
printPlatformFindOneCalls: [],
expressFindAll: 0,
platformFindOne: 0,
shopCreate: 0,
};
const ctx = {
user: {
store_id: 100000,
},
app: {
formatDate() {
return '2026-05-24 12:00:00';
},
Sequelize: {
Op: {
notLike: Symbol('notLike'),
},
},
async getCacheAsync(key) {
calls.getCacheAsync += 1;
calls.getCacheAsyncPayload = key;
return undefined;
},
async setCacheAsync(key, value, ttl) {
calls.setCacheAsync += 1;
calls.setCacheAsyncPayload = {
key,
value,
ttl,
};
},
config: {
pdd: {
client_id: '1',
client_secret: 'testSecret',
},
qimen: {
appKey: 'tbAppKey',
security: 'tbAppSecret',
},
poloApi: {
appKey: 'polyAppKey',
appSecret: 'polyAppSecret',
},
},
},
model: {
PrintPlatform: {
async findOne(options) {
calls.printPlatformFindOne += 1;
calls.printPlatformFindOnePayload = options;
calls.printPlatformFindOneCalls.push(options);
const platformCode = options && options.where && options.where.platform_code;
if (!platformCode) {
return null;
}
return {
platform_code: platformCode,
auth_base_url: 'https://oauth.example.com/auth?state=',
};
},
},
Platform: {
async findOne(options) {
calls.platformFindOne += 1;
calls.platformFindOnePayload = options;
const code = options && options.where && options.where.code;
if (code === 'TB') {
return {
code: 'TB',
auth_base_url: 'https://oauth.example.com/auth?state=',
};
}
return null;
},
},
Express: {
rawAttributes: {
code: { field: 'code' },
CAINIAO: { field: 'CAINIAO' },
PDD: { field: 'PDD' },
DY: { field: 'DY' },
KS: { field: 'KS' },
},
async findAll(options) {
calls.expressFindAll += 1;
calls.expressFindAllPayload = options;
return [];
},
},
Shop: {
async findOne() {
return null;
},
async findAll() {
return [];
},
async create(payload) {
calls.shopCreate += 1;
calls.shopCreatePayload = payload;
return payload;
},
},
},
logger: {
error() {},
info() {},
warn() {},
},
async curl(url, options) {
calls.curl += 1;
calls.request = { url, options };
return {
data: {
success: true,
code: 0,
},
};
},
};
const service = Object.create(IndexMethodService.prototype);
service.ctx = ctx;
service.app = ctx.app;
service.service = {
systems: {
express: {
async getExpressList() {
return [];
},
async getExpressProductTypeList(expressCode) {
calls.getExpressProductTypeList += 1;
calls.getExpressProductTypeListPayload = expressCode;
return [];
},
async syncPddSfTimedDeliveryExpressProductTypes(payload) {
calls.syncPddSfTimedDeliveryExpressProductTypes += 1;
calls.syncPayload = payload;
return {
createdCount: 0,
updatedCount: 0,
parsedCount: 0,
};
},
},
setting: {
expressInterface: {
async getPrintPlatformList() {
calls.getPrintPlatformList += 1;
return [
{
id: 1,
code: 'CAINIAO',
name: '菜鸟电子面单',
value: 'CAINIAO',
label: '菜鸟电子面单',
platform_code: 'CAINIAO',
platform_name: '菜鸟电子面单',
platid: '1001',
need_callback_url: 'https://callback.example.com/cainiao',
},
];
},
},
},
sign: {
signTopRequest(params) {
calls.tbSignParams = { ...params };
return 'TB_SIGN';
},
},
poloApi: {
async request() {
return {};
},
},
},
};
return {
service,
ctx,
calls,
};
}
describe('test/app/service/systemMethods/index.test.js', () => {
it('应按拼多多官方示例参数生成签名并发起请求', async () => {
const { service, calls } = createHarness();
const result = await service.test({
type: 'pdd.order.number.list.get',
timestamp: '1480411125',
dataType: 'XML',
accessToken: 'asd78172s8ds9a921j9qqwda12312w1w21211',
order_status: '1',
page: '1',
page_size: '10',
});
assert.equal(calls.curl, 1);
assert.equal(calls.request.url, 'https://gw-api.pinduoduo.com/api/router');
assert.equal(calls.request.options.method, 'POST');
assert.equal(calls.request.options.headers['Content-Type'], 'application/json');
assert.equal(calls.request.options.data.sign, 'E4DE3ED21002510DED352819E7AE6775');
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.requestBody.sign, 'E4DE3ED21002510DED352819E7AE6775');
assert.deepEqual(result.data.response, { success: true, code: 0 });
});
it('缺少 type 时应直接返回参数错误', async () => {
const { service, calls } = createHarness();
const result = await service.test({
page: '1',
});
assert.equal(calls.curl, 0);
assert.equal(result.flag, 'failure');
assert.equal(result.code, 400);
assert.equal(result.message, 'type 不能为空');
});
it('应兼容 access_token 等下划线参数命名', async () => {
const { service, calls } = createHarness();
const result = await service.test({
type: 'pdd.order.number.list.get',
timestamp: '1480411125',
data_type: 'XML',
access_token: 'asd78172s8ds9a921j9qqwda12312w1w21211',
order_status: '1',
page: '1',
page_size: '10',
});
assert.equal(calls.curl, 1);
assert.equal(calls.request.options.data.access_token, 'asd78172s8ds9a921j9qqwda12312w1w21211');
assert.equal(calls.request.options.data.sign, 'E4DE3ED21002510DED352819E7AE6775');
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
});
it('getShopExpressList 在 PDD 下 SF 且无 TIMED-DELIVERY 时应补齐时效服务', async () => {
const { service, ctx, calls } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 12,
warehouse_id: 100000,
platform_code: 'PDD',
access_token: 'ACCESS_TOKEN',
};
},
});
service.service.systems.express.getExpressList = async isEnabled => {
assert.equal(isEnabled, true);
return [
{
code: 'SF',
name: '顺丰',
PDD: 'SF-PDD',
},
];
};
service.service.systems.express.getExpressProductTypeList = async expressCode => {
calls.getExpressProductTypeList += 1;
calls.getExpressProductTypeListPayload = expressCode;
return [
{ code: 'T6', name: '特快' },
{ code: 'T7', name: '标快' },
];
};
service.requestPddWaybillSearch = async accessToken => {
assert.equal(accessToken, 'ACCESS_TOKEN');
return {
pdd_waybill_search_response: {
request_id: 'REQ-001',
waybill_apply_subscription_cols: [
{
wp_code: 'SF-PDD',
wp_type: '1',
branch_account_cols: [
{
branch_code: 'PDD-BRANCH-001',
branch_name: '拼多多直营网点',
},
],
},
{
wp_code: 'UNKNOWN-PDD',
wp_type: '2',
branch_account_cols: [],
},
],
},
};
};
const result = await service.getShopExpressList({
shop_id: 12,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.shop_id, 12);
assert.equal(result.data.platform_code, 'PDD');
assert.equal(result.data.list[0].wp_name, '顺丰');
assert.equal(result.data.list[0].sys_express_code, 'SF');
assert.deepEqual(result.data.list[0].accounts[0].services, []);
assert.equal(result.data.list[1].wp_name, '');
assert.equal(result.data.list[1].sys_express_code, '');
assert.equal(calls.syncPddSfTimedDeliveryExpressProductTypes, 0);
assert.equal(calls.getExpressProductTypeList, 0);
});
it('getShopExpressList 在 TB 下 SF 且无 TIMED-DELIVERY 时也应补齐时效服务', async () => {
const { service, ctx, calls } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 23,
warehouse_id: 100000,
platform_code: 'CAINIAO',
access_token: 'TB_ACCESS_TOKEN',
printPlatform: {
platform_code: 'CAINIAO',
platform_name: '菜鸟电子面单',
},
};
},
});
service.service.systems.express.getExpressList = async isEnabled => {
assert.equal(isEnabled, true);
return [
{
code: 'SF',
name: '顺丰',
CAINIAO: 'SF',
},
];
};
service.service.systems.express.getExpressProductTypeList = async expressCode => {
calls.getExpressProductTypeList += 1;
calls.getExpressProductTypeListPayload = expressCode;
return [
{ code: 'T6', name: '特快' },
];
};
service.requestTbWaybillSearch = async payload => {
assert.deepEqual(payload, {
accessToken: 'TB_ACCESS_TOKEN',
cpCode: 'SF',
});
return {
cainiao_waybill_ii_search_response: {
request_id: 'TB-REQ-023',
modules: {
waybill_apply_subscription_cols: {
waybill_apply_subscription_info: {
cp_code: 'SF',
cp_name: '顺丰',
waybill_type: 'STANDARD',
branch_account_cols: {
waybill_branch_account: {
branch_name: '顺丰直营网点',
customer_code_list: {
string: 'SF-TB-001',
},
service_info_cols: {
service_info_dto: [
{
service_code: 'SPECIAL',
service_name: '普通服务',
service_desc: '非定时服务',
required: false,
service_attributes: {
service_attribute_dto: {
attribute_code: 'SPECIAL_CODE',
attribute_name: '服务编码',
attribute_type: 'INPUT',
type_desc: '文本',
},
},
},
],
},
},
},
},
},
},
},
};
};
const result = await service.getShopExpressList({
shop_id: 23,
cp_code: 'SF',
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.platform_code, 'CAINIAO');
assert.deepEqual(result.data.list[0].accounts[0].services, [
{
service_code: 'SPECIAL',
service_name: '普通服务',
service_desc: '非定时服务',
required: false,
attributes: [
{
attribute_code: 'SPECIAL_CODE',
attribute_name: '服务编码',
attribute_type: 'INPUT',
type_desc: '文本',
},
],
},
]);
assert.equal(calls.getExpressProductTypeList, 0);
assert.equal(calls.syncPddSfTimedDeliveryExpressProductTypes, 0);
});
it('getShopExpressList 在 SF 且已有 TIMED-DELIVERY 时不应重复追加', async () => {
const { service, ctx, calls } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 21,
warehouse_id: 100000,
platform_code: 'PDD',
access_token: 'ACCESS_TOKEN',
};
},
});
service.service.systems.express.getExpressList = async () => ([
{
code: 'SF',
name: '顺丰',
PDD: 'SF-PDD',
},
]);
service.requestPddWaybillSearch = async () => ({
pdd_waybill_search_response: {
request_id: 'REQ-021',
waybill_apply_subscription_cols: [
{
wp_code: 'SF-PDD',
wp_type: '1',
branch_account_cols: [
{
branch_code: 'PDD-BRANCH-021',
branch_name: '顺丰网点',
service_info_cols: [
{
service_code: 'TIMED-DELIVERY',
service_name: '定时派送',
service_desc: '顺丰定时派送',
required: false,
service_attributes: [
{
attribute_code: 'PRODUCT_TYPE',
attribute_name: '产品类型',
attribute_type: 'SELECT',
type_desc: '{"T6":"特快"}',
},
],
},
],
},
],
},
],
},
});
const result = await service.getShopExpressList({
shop_id: 21,
});
assert.equal(result.flag, 'success');
assert.equal(result.data.list[0].accounts[0].services.length, 1);
assert.equal(result.data.list[0].accounts[0].services[0].service_code, 'TIMED-DELIVERY');
assert.equal(calls.getExpressProductTypeList, 0);
});
it('getShopExpressList 在 DY 下应编排为与 PDD 一致的统一结构', async () => {
const { service, ctx } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 24,
warehouse_id: 100000,
platform_code: 'DY',
access_token: 'DY_ACCESS_TOKEN',
};
},
});
service.service.systems.express.getExpressList = async isEnabled => {
assert.equal(isEnabled, true);
return [
{
code: 'YTO_SYS',
name: '圆通速递',
DY: 'YTO-DY',
},
];
};
service.service.systems.poloApi.request = async (method, bizcontent, config) => {
assert.equal(method, 'Differ.JH.Logistics.GetWayBillCondition');
assert.deepEqual(bizcontent, {});
assert.equal(config.interface_url, 'https://api.polyapi.com/OpenAPI/do');
assert.equal(config.appkey, 'polyAppKey');
assert.equal(config.app_secret, 'polyAppSecret');
assert.equal(config.version, '1.5');
assert.equal(config.token, 'DY_ACCESS_TOKEN');
return {
code: '10000',
data: {
request_id: 'DY-REQ-001',
waybill_apply_subscription_cols: [
{
wp_code: 'YTO-DY',
wp_type: 'STANDARD',
branch_account_cols: [
{
branch_code: 'DY-BRANCH-001',
branch_name: '抖音网点',
quantity: 12,
cancel_quantity: 1,
recycled_quantity: 0,
allocated_quantity: 3,
customer_code_list: {
string: [ 'CUST-001', 'CUST-002' ],
},
shipp_address_cols: [
{
country: '中国',
province: '浙江省',
city: '杭州市',
district: '余杭区',
detail: '文一西路1号',
full_address: '浙江省杭州市余杭区文一西路1号',
},
],
service_info_cols: [
{
service_code: 'SEND_TIME',
service_name: '派送时效',
service_desc: '预约派送',
required: true,
service_attributes: [
{
attribute_code: 'TIME_RANGE',
attribute_name: '时段',
attribute_type: 'SELECT',
type_desc: '枚举',
},
],
},
],
},
],
},
],
},
};
};
const result = await service.getShopExpressList({
shop_id: 24,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.shop_id, 24);
assert.equal(result.data.platform_code, 'DY');
assert.deepEqual(result.data.list[0], {
wp_code: 'YTO-DY',
wp_name: '圆通速递',
sys_express_code: 'YTO_SYS',
wp_type: 'STANDARD',
accounts: [
{
branch_code: 'DY-BRANCH-001',
branch_name: '抖音网点',
quantity: 12,
cancel_quantity: 1,
recycled_quantity: 0,
allocated_quantity: 3,
customer_code_list: [ 'CUST-001', 'CUST-002' ],
ship_addresses: [
{
country: '中国',
province: '浙江省',
city: '杭州市',
district: '余杭区',
detail: '文一西路1号',
full_address: '浙江省杭州市余杭区文一西路1号',
},
],
services: [
{
service_code: 'SEND_TIME',
service_name: '派送时效',
service_desc: '预约派送',
required: true,
attributes: [
{
attribute_code: 'TIME_RANGE',
attribute_name: '时段',
attribute_type: 'SELECT',
type_desc: '枚举',
},
],
},
],
},
],
});
});
it('getShopExpressList 在 DY cpcode 缺失映射时应回退 sys_express_code 命中 wms_express.DY 名称', async () => {
const { service, ctx } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 25,
warehouse_id: 100000,
platform_code: 'DY',
access_token: 'DY_ACCESS_TOKEN_2',
};
},
});
service.service.systems.express.getExpressList = async () => ([
{
code: 'YTO_SYS',
name: '圆通速递',
DY: 'YTO-DY',
},
]);
service.service.systems.poloApi.request = async () => ({
code: '10000',
data: {
request_id: 'DY-REQ-002',
results: [
{
cpcode: 'UNKNOWN-DY',
cpname: '圆通快递',
sys_express_code: 'YTO_SYS',
waybill_type: 'STANDARD',
waybill_branch_account: [
{
branch_code: 'DY-BRANCH-002',
branch_name: '抖音次级网点',
customer_code_list: {
string: [ 'DY-CUST-001' ],
},
service_info_dto: [
{
service_code: 'DELIVER_TYPE',
service_name: '配送类型',
service_attribute_dto: [
{
attribute_code: 'TYPE',
attribute_name: '类型',
attribute_type: 'SELECT',
attribute_type_name: '枚举',
},
],
},
],
},
],
},
],
},
});
const result = await service.getShopExpressList({
shop_id: 25,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.platform_code, 'DY');
assert.equal(result.data.list.length, 1);
assert.equal(result.data.list[0].wp_code, 'UNKNOWN-DY');
assert.equal(result.data.list[0].wp_name, '圆通速递');
assert.equal(result.data.list[0].sys_express_code, 'YTO_SYS');
});
it('getShopExpressList 在 DY 扁平 results 存在重复 cpcode 时应按 wp_code 聚合并去重地址', async () => {
const { service, ctx } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 26,
warehouse_id: 100000,
platform_code: 'DY',
access_token: 'DY_ACCESS_TOKEN_3',
};
},
});
service.service.systems.express.getExpressList = async () => ([
{
code: 'YTO_SYS',
name: '圆通速递',
DY: 'YTO-DY',
},
]);
service.service.systems.poloApi.request = async () => ({
code: '10000',
data: {
request_id: 'DY-REQ-003',
results: [
{
cpcode: 'YTO-DY',
cpname: '圆通快递',
waybill_type: 'STANDARD',
waybill_branch_account: [
{
branch_code: 'DY-BRANCH-A',
branch_name: '抖音网点A',
shipp_address_cols: [
{
country: '中国',
province: '浙江省',
city: '杭州市',
district: '余杭区',
detail: '文一西路1号',
full_address: '浙江省杭州市余杭区文一西路1号',
},
],
},
],
},
{
cpcode: 'YTO-DY',
cpname: '圆通快递',
waybill_type: 'STANDARD',
waybill_branch_account: [
{
branch_code: 'DY-BRANCH-B',
branch_name: '抖音网点B',
shipp_address_cols: [
{
country: '中国',
province: '浙江省',
city: '杭州市',
district: '余杭区',
detail: '文一西路1号',
full_address: '浙江省杭州市余杭区文一西路1号',
},
{
country: '中国',
province: '江苏省',
city: '苏州市',
district: '工业园区',
detail: '星湖街88号',
full_address: '江苏省苏州市工业园区星湖街88号',
},
],
},
],
},
],
},
});
const result = await service.getShopExpressList({
shop_id: 26,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.platform_code, 'DY');
assert.equal(result.data.list.length, 1);
assert.equal(result.data.list[0].wp_code, 'YTO-DY');
assert.equal(result.data.list[0].accounts.length, 2);
assert.equal(result.data.list[0].accounts[0].ship_addresses.length, 1);
assert.equal(result.data.list[0].accounts[1].ship_addresses.length, 1);
assert.equal(result.data.list[0].accounts[1].ship_addresses[0].province, '江苏省');
});
it('getShopExpressList 在 DY cpcode 节点使用 results 时应正确映射 accounts', async () => {
const { service, ctx } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 27,
warehouse_id: 100000,
platform_code: 'DY',
access_token: 'DY_ACCESS_TOKEN_4',
};
},
});
service.service.systems.express.getExpressList = async () => ([
{
code: 'YTO_SYS',
name: '圆通速递',
DY: 'YTO-DY',
},
]);
service.service.systems.poloApi.request = async () => ({
code: '10000',
data: {
request_id: 'DY-REQ-004',
results: [
{
cpcode: 'YTO-DY',
cpname: '圆通快递',
waybill_type: 'STANDARD',
results: [
{
branch_code: 'DY-BRANCH-C',
branch_name: '抖音网点C',
customer_code_list: {
string: [ 'DY-CUST-002' ],
},
shipp_address_cols: [
{
country: '中国',
province: '上海市',
city: '上海市',
district: '闵行区',
detail: '申虹路99号',
full_address: '上海市闵行区申虹路99号',
},
],
service_info_dto: [
{
service_code: 'SEND_TIME',
service_name: '派送时效',
},
],
},
],
},
],
},
});
const result = await service.getShopExpressList({
shop_id: 27,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.platform_code, 'DY');
assert.equal(result.data.list.length, 1);
assert.equal(result.data.list[0].wp_code, 'YTO-DY');
assert.equal(result.data.list[0].accounts.length, 1);
assert.equal(result.data.list[0].accounts[0].branch_code, 'DY-BRANCH-C');
assert.deepEqual(result.data.list[0].accounts[0].customer_code_list, [ 'DY-CUST-002' ]);
assert.equal(result.data.list[0].accounts[0].ship_addresses[0].province, '上海市');
assert.equal(result.data.list[0].accounts[0].services[0].service_code, 'SEND_TIME');
});
it('getShopExpressList 在 DY sitecode 场景应严格映射 branch 与 shippers 地址', async () => {
const { service, ctx } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 28,
warehouse_id: 100000,
platform_code: 'DY',
access_token: 'DY_ACCESS_TOKEN_5',
};
},
});
service.service.systems.express.getExpressList = async () => ([
{
code: 'STO_SYS',
name: '申通快递',
DY: 'shentong',
},
]);
service.service.systems.poloApi.request = async () => ({
code: '10000',
data: {
request_id: 'DY-REQ-005',
results: [
{
cpcode: 'shentong',
cpname: '申通快递',
waybill_type: 'STANDARD',
results: [
{
sitecode: 'ST-A',
sitename: '申通网点A',
quantity: 11,
shippers: [
{
country: '中国',
province: '浙江省',
city: '杭州市',
area: '西湖区',
address: '文三路100号',
addressid: 'ADDR-001',
},
],
},
],
},
{
cpcode: 'shentong',
cpname: '申通快递',
waybill_type: 'STANDARD',
results: [
{
sitecode: 'ST-B',
sitename: '申通网点B',
quantity: 22,
shippers: [
{
country: '中国',
province: '浙江省',
town: '宁波市',
area: '鄞州区',
address: '天童南路66号',
addressid: 'ADDR-002',
},
],
},
],
},
],
},
});
const result = await service.getShopExpressList({
shop_id: 28,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.platform_code, 'DY');
assert.equal(result.data.list.length, 1);
assert.equal(result.data.list[0].wp_code, 'shentong');
assert.equal(result.data.list[0].accounts.length, 2);
assert.equal(result.data.list[0].accounts[0].branch_code, 'ST-A');
assert.equal(result.data.list[0].accounts[0].branch_name, '申通网点A');
assert.equal(result.data.list[0].accounts[1].branch_code, 'ST-B');
assert.equal(result.data.list[0].accounts[1].branch_name, '申通网点B');
assert.equal(result.data.list[0].accounts[0].ship_addresses.length > 0, true);
assert.equal(result.data.list[0].accounts[0].ship_addresses[0].province, '浙江省');
assert.equal(result.data.list[0].accounts[0].ship_addresses[0].city, '杭州市');
assert.equal(result.data.list[0].accounts[0].ship_addresses[0].district, '西湖区');
assert.equal(result.data.list[0].accounts[0].ship_addresses[0].detail, '文三路100号');
assert.equal(result.data.list[0].accounts[1].ship_addresses.length > 0, true);
assert.equal(result.data.list[0].accounts[1].ship_addresses[0].province, '浙江省');
assert.equal(result.data.list[0].accounts[1].ship_addresses[0].city, '宁波市');
assert.equal(result.data.list[0].accounts[1].ship_addresses[0].district, '鄞州区');
assert.equal(result.data.list[0].accounts[1].ship_addresses[0].detail, '天童南路66号');
});
it('getShopExpressList 在非 SF 快递时不应补齐 TIMED-DELIVERY', async () => {
const { service, ctx, calls } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 22,
warehouse_id: 100000,
platform_code: 'PDD',
access_token: 'ACCESS_TOKEN',
};
},
});
service.service.systems.express.getExpressList = async () => ([
{
code: 'YTO',
name: '圆通',
PDD: 'YTO-PDD',
},
]);
service.requestPddWaybillSearch = async () => ({
pdd_waybill_search_response: {
request_id: 'REQ-022',
waybill_apply_subscription_cols: [
{
wp_code: 'YTO-PDD',
wp_type: '1',
branch_account_cols: [
{
branch_code: 'PDD-BRANCH-022',
branch_name: '圆通网点',
service_info_cols: [],
},
],
},
],
},
});
const result = await service.getShopExpressList({
shop_id: 22,
});
assert.equal(result.flag, 'success');
assert.deepEqual(result.data.list[0].accounts[0].services, []);
assert.equal(calls.getExpressProductTypeList, 0);
});
it('getShopExpressList 应仅在拼多多顺丰定时派送场景触发产品类型同步', async () => {
const { service, ctx, calls } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 13,
warehouse_id: 100000,
platform_code: 'PDD',
access_token: 'ACCESS_TOKEN',
};
},
});
service.service.systems.express.getExpressList = async () => ([
{
code: 'SF',
name: '顺丰',
PDD: 'SF-PDD',
},
]);
service.requestPddWaybillSearch = async () => ({
pdd_waybill_search_response: {
request_id: 'REQ-002',
waybill_apply_subscription_cols: [
{
wp_code: 'SF-PDD',
wp_type: '1',
branch_account_cols: [
{
branch_code: 'PDD-BRANCH-002',
branch_name: '顺丰网点',
service_info_cols: [
{
service_code: 'TIMED-DELIVERY',
service_name: '定时派送',
service_desc: '顺丰定时派送',
required: false,
service_attributes: [
{
attribute_code: 'PRODUCT_TYPE',
attribute_name: '产品类型',
attribute_type: 'SELECT',
type_desc: '{"T6":"特快","T7":"标快"}',
},
],
},
],
},
],
},
],
},
});
const result = await service.getShopExpressList({
shop_id: 13,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(calls.syncPddSfTimedDeliveryExpressProductTypes, 1);
assert.deepEqual(calls.syncPayload, {
shopExpressList: result.data.list,
});
});
it('getShopExpressList 应将淘宝菜鸟响应转换为统一结构', async () => {
const { service, ctx } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 18,
warehouse_id: 100000,
platform_code: 'CAINIAO',
access_token: 'TB_ACCESS_TOKEN',
printPlatform: {
platform_code: 'CAINIAO',
platform_name: '菜鸟电子面单',
},
};
},
});
service.service.systems.express.getExpressList = async isEnabled => {
assert.equal(isEnabled, true);
return [
{
code: 'YTO_SYS',
name: '圆通速递',
CAINIAO: 'YTO',
},
];
};
service.requestTbWaybillSearch = async payload => {
assert.deepEqual(payload, {
accessToken: 'TB_ACCESS_TOKEN',
cpCode: 'YTO',
});
return {
cainiao_waybill_ii_search_response: {
request_id: 'TB-REQ-001',
modules: {
waybill_apply_subscription_cols: {
waybill_apply_subscription_info: {
cp_code: 'YTO',
cp_name: '圆通',
waybill_type: 'STANDARD',
branch_account_cols: {
waybill_branch_account: {
branch_name: '杭州转运中心',
quantity: 20,
cancel_quantity: 2,
allocated_quantity: 6,
customer_code_list: {
string: 'CUST-001',
},
address_dto: {
province: '浙江省',
city: '杭州市',
district: '余杭区',
detail: '文一西路1号',
},
service_info_dto: {
service_code: 'SEND_TIME',
service_name: '派送时效',
service_desc: '预约派送',
required: true,
service_attribute_dto: {
attribute_code: 'TIME_RANGE',
attribute_name: '时段',
attribute_type: 'SELECT',
type_desc: '枚举',
},
},
},
},
},
},
},
},
};
};
const result = await service.getShopExpressList({
shop_id: 18,
cp_code: 'YTO',
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.shop_id, 18);
assert.equal(result.data.platform_code, 'CAINIAO');
assert.deepEqual(result.data.list[0], {
wp_code: 'YTO',
wp_name: '圆通速递',
sys_express_code: 'YTO_SYS',
wp_type: 'STANDARD',
accounts: [
{
branch_code: 'CUST-001',
branch_name: '杭州转运中心',
quantity: 20,
cancel_quantity: 2,
recycled_quantity: 0,
allocated_quantity: 6,
customer_code_list: [ 'CUST-001' ],
ship_addresses: [
{
country: '中国',
province: '浙江省',
city: '杭州市',
district: '余杭区',
detail: '文一西路1号',
full_address: '',
},
],
services: [
{
service_code: 'SEND_TIME',
service_name: '派送时效',
service_desc: '预约派送',
required: true,
attributes: [
{
attribute_code: 'TIME_RANGE',
attribute_name: '时段',
attribute_type: 'SELECT',
type_desc: '枚举',
},
],
},
],
},
],
});
});
it('getShopExpressList 应过滤淘宝代收货款、定时派送、空属性服务并保留普通服务', async () => {
const { service, ctx } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 19,
warehouse_id: 100000,
platform_code: 'CAINIAO',
access_token: 'TB_ACCESS_TOKEN',
printPlatform: {
platform_code: 'CAINIAO',
platform_name: '菜鸟电子面单',
},
};
},
});
service.service.systems.express.getExpressList = async () => ([
{
code: 'ZTO_SYS',
name: '中通快递',
CAINIAO: 'ZTO',
},
]);
service.requestTbWaybillSearch = async () => ({
cainiao_waybill_ii_search_response: {
request_id: 'TB-REQ-002',
modules: {
waybill_apply_subscription_cols: {
waybill_apply_subscription_info: {
cp_code: 'ZTO',
cp_name: '中通',
waybill_type: 'STANDARD',
branch_account_cols: {
waybill_branch_account: {
branch_name: '上海分拨中心',
customer_code_list: {
string: 'CUST-002',
},
shipp_address_cols: {
address_dto: {
province: '上海市',
city: '上海市',
district: '青浦区',
detail: '华徐公路99号',
},
},
service_info_cols: {
service_info_dto: [
{
service_code: 'COD',
service_name: '货到付款',
service_desc: '签收收款',
required: false,
service_attributes: {
service_attribute_dto: {
attribute_code: 'AMOUNT_LIMIT',
attribute_name: '金额限制',
attribute_type: 'INPUT',
type_desc: '文本',
},
},
},
{
service_code: 'INTERCEPT_NOTE',
service_name: '拦截件',
service_desc: '异常件拦截服务',
required: false,
service_attributes: {
service_attribute_dto: {
attribute_code: 'INTERCEPT_REASON',
attribute_name: '拦截原因',
attribute_type: 'INPUT',
type_desc: '文本',
},
},
},
{
service_code: 'APPOINTMENT_DELIVERY',
service_name: '定时派送',
service_desc: '预约时间段配送',
required: false,
service_attributes: {
service_attribute_dto: {
attribute_code: 'DELIVERY_TIME',
attribute_name: '派送时间',
attribute_type: 'SELECT',
type_desc: '枚举',
},
},
},
{
service_code: 'TRADE_IN',
service_name: '上门取旧换新',
service_desc: '支持上门取旧换新服务',
required: false,
service_attributes: {
service_attribute_dto: {
attribute_code: 'TRADE_IN_NOTICE',
attribute_name: '换新说明',
attribute_type: 'INPUT',
type_desc: '文本',
},
},
},
{
service_code: 'EMPTY_ATTR_SERVICE',
service_name: '短信通知',
service_desc: '普通服务但无属性',
required: false,
service_attributes: {
service_attribute_dto: [],
},
},
{
service_code: 'INSURE',
service_name: '运费险',
service_desc: '普通增值服务',
required: true,
service_attributes: {
service_attribute_dto: {
attribute_code: 'INSURE_TYPE',
attribute_name: '投保类型',
attribute_type: 'SELECT',
type_desc: '枚举',
},
},
},
],
},
},
},
},
},
},
},
});
const result = await service.getShopExpressList({
shop_id: 19,
cp_code: 'ZTO',
});
assert.equal(result.flag, 'success');
assert.equal(result.data.list[0].accounts[0].ship_addresses[0].province, '上海市');
assert.deepEqual(result.data.list[0].accounts[0].services, [
{
service_code: 'INSURE',
service_name: '运费险',
service_desc: '普通增值服务',
required: true,
attributes: [
{
attribute_code: 'INSURE_TYPE',
attribute_name: '投保类型',
attribute_type: 'SELECT',
type_desc: '枚举',
},
],
},
]);
});
it('getShopExpressList 应提取淘宝 type_desc.desc 并保留扁平 JSON 字符串', async () => {
const { service, ctx } = createHarness();
ctx.model.Shop.findOne = async () => ({
get() {
return {
id: 20,
warehouse_id: 100000,
platform_code: 'CAINIAO',
access_token: 'TB_ACCESS_TOKEN',
printPlatform: {
platform_code: 'CAINIAO',
platform_name: '菜鸟电子面单',
},
};
},
});
service.service.systems.express.getExpressList = async () => ([
{
code: 'SF_SYS',
name: '顺丰速运',
CAINIAO: 'SF',
},
]);
service.requestTbWaybillSearch = async () => ({
cainiao_waybill_ii_search_response: {
request_id: 'TB-REQ-003',
modules: {
waybill_apply_subscription_cols: {
waybill_apply_subscription_info: {
cp_code: 'SF',
cp_name: '顺丰',
waybill_type: 'STANDARD',
branch_account_cols: {
waybill_branch_account: {
branch_name: '顺丰直营网点',
customer_code_list: {
string: 'CUST-003',
},
service_info_dto: {
service_code: 'SPECIAL',
service_name: '特殊服务',
service_desc: '测试归一化',
required: true,
service_attribute_dto: [
{
attribute_code: 'EXPRESS_TYPE',
attribute_name: '快递类型',
attribute_type: 'SELECT',
type_desc: '{"desc":{"1":"标准快递","2":"特惠专配"},"required":true,"type":"enum"}',
},
{
attribute_code: 'PRODUCT_TYPE',
attribute_name: '产品类型',
attribute_type: 'SELECT',
type_desc: '{"T6":"特快","T7":"标快"}',
},
],
},
},
},
},
},
},
},
});
const result = await service.getShopExpressList({
shop_id: 20,
cp_code: 'SF',
});
assert.equal(result.flag, 'success');
assert.deepEqual(result.data.list[0].accounts[0].services[0].attributes, [
{
attribute_code: 'EXPRESS_TYPE',
attribute_name: '快递类型',
attribute_type: 'SELECT',
type_desc: '{"1":"标准快递","2":"特惠专配"}',
},
{
attribute_code: 'PRODUCT_TYPE',
attribute_name: '产品类型',
attribute_type: 'SELECT',
type_desc: '{"T6":"特快","T7":"标快"}',
},
]);
});
it('getCainiaoStfTemplates 应直接查询公共模板列表且不依赖店铺上下文', async () => {
const { service, ctx, calls } = createHarness();
let shopQueryCount = 0;
let stdTemplateRequestCount = 0;
let waybillSearchCallCount = 0;
ctx.model.Shop.findAll = async () => {
shopQueryCount += 1;
return [];
};
service.requestTbWaybillSearch = async () => {
waybillSearchCallCount += 1;
throw new Error('不应调用 cainiao.waybill.ii.search');
};
service.requestTbCainiaoStdTemplates = async () => {
stdTemplateRequestCount += 1;
return {
cainiao_cloudprint_stdtemplates_get_response: {
result: {
datas: [
{
cp_code: 'YTO',
brand_code: 'YTO',
success: true,
standard_templates: [
{
standard_template_id: 'TPL-001',
standard_template_name: '圆通标准模板',
standard_template_url: 'https://example.com/tpl-001',
},
{
standard_template_id: 'TPL-002',
standard_template_name: '圆通扩展模板',
standard_template_url: 'https://example.com/tpl-002',
},
],
},
{
brand_code: 'ZTO',
success: false,
error_code: 'NO_TEMPLATE_PERMISSION',
error_message: '该快递公司暂未返回模板',
standard_templates: [],
},
],
},
},
};
};
ctx.model.Express.findAll = async options => {
calls.expressFindAll += 1;
calls.expressFindAllPayload = options;
return [
{
code: 'YTO',
CAINIAO: 'YTO',
},
];
};
const result = await service.getCainiaoStfTemplates({
express_code: 'yto',
});
assert.equal(shopQueryCount, 0);
assert.equal(stdTemplateRequestCount, 1);
assert.equal(waybillSearchCallCount, 0);
assert.equal(calls.expressFindAll, 1);
assert.deepEqual(calls.expressFindAllPayload.where, {
CAINIAO: 'YTO',
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.deepEqual(result.data, {
platform_code: 'CAINIAO',
express_code: 'YTO',
responseData: {
cainiao_cloudprint_stdtemplates_get_response: {
result: {
datas: [
{
cp_code: 'YTO',
brand_code: 'YTO',
success: true,
standard_templates: [
{
standard_template_id: 'TPL-001',
standard_template_name: '圆通标准模板',
standard_template_url: 'https://example.com/tpl-001',
},
{
standard_template_id: 'TPL-002',
standard_template_name: '圆通扩展模板',
standard_template_url: 'https://example.com/tpl-002',
},
],
},
],
},
},
},
});
});
it('getCainiaoStfTemplates 在缓存命中时不应请求淘宝公共模板接口', async () => {
const { service, calls } = createHarness();
const cachedResponseData = {
cainiao_cloudprint_stdtemplates_get_response: {
result: {
datas: [
{
cp_code: 'YTO',
standard_templates: [],
},
{
cp_code: 'ZTO',
standard_templates: [],
},
],
},
},
};
let stdTemplateRequestCount = 0;
service.app.getCacheAsync = async key => {
calls.getCacheAsync += 1;
calls.getCacheAsyncPayload = key;
return cachedResponseData;
};
service.requestTbCainiaoStdTemplates = async () => {
stdTemplateRequestCount += 1;
throw new Error('命中缓存时不应发起淘宝请求');
};
service.ctx.model.Express.findAll = async options => {
calls.expressFindAll += 1;
calls.expressFindAllPayload = options;
return [
{
code: 'YTO',
CAINIAO: 'YTO',
},
];
};
const result = await service.getCainiaoStfTemplates({
express_code: 'YTO',
});
assert.equal(calls.getCacheAsync, 1);
assert.equal(calls.getCacheAsyncPayload, 'index:cainiao:stfTemplates:responseData');
assert.equal(stdTemplateRequestCount, 0);
assert.equal(calls.expressFindAll, 1);
assert.equal(calls.setCacheAsync, 0);
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.deepEqual(result.data, {
platform_code: 'CAINIAO',
express_code: 'YTO',
responseData: {
cainiao_cloudprint_stdtemplates_get_response: {
result: {
datas: [
{
cp_code: 'YTO',
standard_templates: [],
},
],
},
},
},
});
});
it('getCainiaoStfTemplates 在缓存未命中时应请求淘宝并写入 1 小时缓存', async () => {
const { service, calls } = createHarness();
const responseData = {
cainiao_cloudprint_stdtemplates_get_response: {
result: {
datas: [
{
cp_code: 'STO',
standard_templates: [
{
standard_template_id: 'TPL-100',
},
],
},
],
},
},
};
let stdTemplateRequestCount = 0;
service.requestTbCainiaoStdTemplates = async () => {
stdTemplateRequestCount += 1;
return responseData;
};
service.ctx.model.Express.findAll = async options => {
calls.expressFindAll += 1;
calls.expressFindAllPayload = options;
return [
{
code: 'STO',
CAINIAO: 'STO',
},
];
};
const result = await service.getCainiaoStfTemplates({
express_code: 'STO',
});
assert.equal(calls.getCacheAsync, 1);
assert.equal(calls.getCacheAsyncPayload, 'index:cainiao:stfTemplates:responseData');
assert.equal(stdTemplateRequestCount, 1);
assert.equal(calls.expressFindAll, 1);
assert.equal(calls.setCacheAsync, 1);
assert.deepEqual(calls.setCacheAsyncPayload, {
key: 'index:cainiao:stfTemplates:responseData',
value: responseData,
ttl: 3600,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.deepEqual(result.data, {
platform_code: 'CAINIAO',
express_code: 'STO',
responseData,
});
});
it('getPrintPlatformTemplateList 应按入参平台编码与 express_code 过滤模板并写入对应缓存键', async () => {
const { service, calls } = createHarness();
const responseData = {
cainiao_cloudprint_stdtemplates_get_response: {
result: {
datas: [
{
cp_code: 'YTO',
standard_templates: [
{
standard_template_id: 'TPL-YTO-1',
},
],
},
{
cp_code: 'ZTO',
standard_templates: [
{
standard_template_id: 'TPL-ZTO-1',
},
],
},
],
},
},
};
service.requestTbCainiaoStdTemplates = async () => responseData;
service.ctx.model.Express.findAll = async options => {
calls.expressFindAll += 1;
calls.expressFindAllPayload = options;
return [
{
code: 'YTO',
CAINIAO: 'YTO',
},
];
};
const result = await service.getPrintPlatformTemplateList({
platform_code: 'cainiao',
express_code: 'yto',
});
assert.equal(calls.getCacheAsync, 1);
assert.equal(calls.getCacheAsyncPayload, 'index:cainiao:stfTemplates:responseData');
assert.equal(calls.expressFindAll, 1);
assert.equal(calls.setCacheAsync, 1);
assert.deepEqual(calls.setCacheAsyncPayload, {
key: 'index:cainiao:stfTemplates:responseData',
value: responseData,
ttl: 3600,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.deepEqual(result.data, {
platform_code: 'CAINIAO',
express_code: 'YTO',
responseData: {
cainiao_cloudprint_stdtemplates_get_response: {
result: {
datas: [
{
cp_code: 'YTO',
standard_templates: [
{
standard_template_id: 'TPL-YTO-1',
},
],
},
],
},
},
},
});
});
it('getPrintPlatformTemplateList 应识别 standard_templates 子项中的 brand_code 并仅保留命中模板', async () => {
const { service, calls } = createHarness();
const responseData = {
cainiao_cloudprint_stdtemplates_get_response: {
result: {
datas: [
{
cp_code: 'SF',
standard_templates: [
{
brand_code: 'SF',
standard_template_id: 'TPL-SF-TOP-1',
},
{
brand_code: 'YTO',
standard_template_id: 'TPL-SF-TOP-2',
},
],
},
{
cp_code: 'CAINIAO_COMMON',
standard_templates: {
standard_template_do: [
{
brand_code: 'SF',
standard_template_id: 'TPL-SF-NESTED-1',
},
{
brand_code: 'YTO',
standard_template_id: 'TPL-YTO-NESTED-1',
},
],
},
},
{
cp_code: 'YTO',
standard_templates: {
standard_template_do: [
{
brand_code: 'YTO',
standard_template_id: 'TPL-YTO-ONLY-1',
},
],
},
},
],
},
},
};
service.requestTbCainiaoStdTemplates = async () => responseData;
service.ctx.model.Express.findAll = async options => {
calls.expressFindAll += 1;
calls.expressFindAllPayload = options;
return [
{
code: 'SF',
CAINIAO: 'SF',
},
];
};
const result = await service.getPrintPlatformTemplateList({
platform_code: 'CAINIAO',
express_code: 'sf',
});
assert.equal(calls.expressFindAll, 1);
assert.deepEqual(result.data, {
platform_code: 'CAINIAO',
express_code: 'SF',
responseData: {
cainiao_cloudprint_stdtemplates_get_response: {
result: {
datas: [
{
cp_code: 'SF',
standard_templates: [
{
brand_code: 'SF',
standard_template_id: 'TPL-SF-TOP-1',
},
{
brand_code: 'YTO',
standard_template_id: 'TPL-SF-TOP-2',
},
],
},
{
cp_code: 'CAINIAO_COMMON',
standard_templates: {
standard_template_do: [
{
brand_code: 'SF',
standard_template_id: 'TPL-SF-NESTED-1',
},
],
},
},
],
},
},
},
});
});
it('getPrintPlatformTemplateList 对未实现平台应返回空对象并按平台维度缓存', async () => {
const { service, calls } = createHarness();
const result = await service.getPrintPlatformTemplateList({
platformCode: 'pdd',
express_code: 'SF',
});
assert.equal(calls.getCacheAsync, 1);
assert.equal(calls.getCacheAsyncPayload, 'index:printPlatform:PDD:templates:responseData');
assert.equal(calls.setCacheAsync, 1);
assert.deepEqual(calls.setCacheAsyncPayload, {
key: 'index:printPlatform:PDD:templates:responseData',
value: {},
ttl: 3600,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.deepEqual(result.data, {
platform_code: 'PDD',
express_code: 'SF',
responseData: {},
});
});
it('getPrintPlatformTemplateList 缺少 express_code 时应返回参数错误', async () => {
const { service, calls } = createHarness();
const result = await service.getPrintPlatformTemplateList({
platform_code: 'cainiao',
});
assert.equal(result.flag, 'failure');
assert.equal(result.code, 400);
assert.equal(result.message, 'express_code 不能为空');
assert.equal(calls.getCacheAsync, 0);
assert.equal(calls.setCacheAsync, 0);
});
it('getPlatformOauthLink 应以打印平台校验店铺平台码,并使用打印平台 OAuth 配置生成 CAINIAO 状态', async () => {
const { service, calls } = createHarness();
const result = await service.getPlatformOauthLink({
platform_code: 'cainiao',
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(calls.printPlatformFindOne, 3);
assert.equal(calls.platformFindOne, 0);
assert.equal(
calls.printPlatformFindOneCalls.some(call => (
Array.isArray(call && call.attributes)
&& call.attributes.includes('auth_base_url')
)),
true
);
assert.equal(calls.shopCreate, 1);
assert.equal(calls.shopCreatePayload.platform_code, 'CAINIAO');
assert.equal(result.data.platform_code, 'CAINIAO');
assert.match(String(result.data.oauth_link || ''), /CAINIAO_/);
});
it('getPlatformOauthLink 应从打印平台表读取 auth_base_url', async () => {
const { service, ctx, calls } = createHarness();
ctx.model.PrintPlatform.findOne = async options => {
calls.printPlatformFindOne += 1;
calls.printPlatformFindOnePayload = options;
calls.printPlatformFindOneCalls.push(options);
if (!options || !options.attributes) {
return {
platform_code: 'PDD',
};
}
return {
platform_code: 'PDD',
auth_base_url: 'https://pdd.example.com/oauth?state=',
};
};
const result = await service.getPlatformOauthLink({
platform_code: 'PDD',
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(calls.platformFindOne, 0);
assert.match(String(result.data.oauth_link || ''), /^https:\/\/pdd\.example\.com\/oauth\?state=/);
});
it('getShopList 应携带打印平台名称并按打印平台编码过滤', async () => {
const { service, ctx } = createHarness();
ctx.model.Shop.findAll = async options => {
assert.equal(options.where.platform_code, 'CAINIAO');
assert.equal(Array.isArray(options.include), true);
return [{
get() {
return {
id: 101,
warehouse_id: 100000,
platform_code: 'CAINIAO',
shop_name: '菜鸟店铺A',
custom_name: '打印店铺A',
status: 1,
owner_id: 'owner-12345',
access_token: 'access-token',
refresh_token: 'refresh-token',
auth_random_code: 'auth-code',
printPlatform: {
platform_code: 'CAINIAO',
platform_name: '菜鸟电子面单',
},
};
},
}];
};
const result = await service.getShopList({ platform_code: 'cainiao' });
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.list[0].platform_code, 'CAINIAO');
assert.equal(result.data.list[0].platform_name, '菜鸟电子面单');
});
it('requestTbCainiaoStdTemplates 应使用公共模板接口且不发送 session', async () => {
const { service, calls } = createHarness();
const result = await service.requestTbCainiaoStdTemplates();
assert.equal(calls.curl, 1);
assert.equal(calls.request.url, 'https://eco.taobao.com/router/rest');
assert.equal(calls.request.options.method, 'POST');
assert.equal(calls.request.options.contentType, 'form');
assert.equal(calls.request.options.data.method, 'cainiao.cloudprint.stdtemplates.get');
assert.equal(calls.request.options.data.sign, 'TB_SIGN');
assert.equal(calls.tbSignParams.method, 'cainiao.cloudprint.stdtemplates.get');
assert.equal(Object.prototype.hasOwnProperty.call(calls.tbSignParams, 'session'), false);
assert.equal(Object.prototype.hasOwnProperty.call(calls.request.options.data, 'session'), false);
assert.deepEqual(result, {
success: true,
code: 0,
});
});
it('requestTbWaybillSearch 在 accessToken 缺失时不应发送 session 参数', async () => {
const { service, calls } = createHarness();
const result = await service.requestTbWaybillSearch({
cpCode: '',
});
assert.equal(calls.curl, 1);
assert.equal(calls.request.url, 'https://eco.taobao.com/router/rest');
assert.equal(calls.request.options.method, 'POST');
assert.equal(calls.request.options.contentType, 'form');
assert.equal(calls.request.options.data.sign, 'TB_SIGN');
assert.equal(Object.prototype.hasOwnProperty.call(calls.tbSignParams, 'session'), false);
assert.equal(Object.prototype.hasOwnProperty.call(calls.request.options.data, 'session'), false);
assert.deepEqual(result, {
success: true,
code: 0,
});
});
it('listenPlatformOauthBind 应委托平台 service 并返回监听结果', async () => {
const { service } = createHarness();
let capturedData = null;
service.service.systems.platform = {
async listenPlatformOauthBind(data) {
capturedData = data;
return {
success: true,
code: 200,
data: {
code: 'BIND001',
status: 'success',
access_token: 'ACCESS_TOKEN',
source: 'event',
},
message: '授权完成',
};
},
};
const result = await service.listenPlatformOauthBind({
business_code: 'BIND001',
long_poll: true,
});
assert.deepEqual(capturedData, {
code: 'BIND001',
longPoll: true,
timeoutMs: undefined,
});
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(result.data.status, 'success');
assert.equal(result.data.source, 'event');
});
it('getPrintPlatformList 应返回打印平台全量列表', async () => {
const { service, calls } = createHarness();
const result = await service.getPrintPlatformList();
assert.equal(calls.getPrintPlatformList, 1);
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
assert.equal(Array.isArray(result.data), true);
assert.equal(result.data.length, 1);
assert.deepEqual(result.data[0], {
id: 1,
code: 'CAINIAO',
name: '菜鸟电子面单',
value: 'CAINIAO',
label: '菜鸟电子面单',
platform_code: 'CAINIAO',
platform_name: '菜鸟电子面单',
platid: '1001',
need_callback_url: 'https://callback.example.com/cainiao',
});
});
describe('getShopExpressList', () => {
it('在 DY 平台下应能处理复杂和不规范的真实场景数据', async () => {
const { service, ctx } = createHarness();
// 1. 构造严格的输入模拟
const mockDyResponse = {
success: true,
result_code: '200',
result_msg: 'success',
results: [
{
cp_code: 'YTO',
cp_name: '圆通速递',
waybill_type: '2',
accounts: [
{ branch_code: 'YTO001', branch_name: '圆通上海网点', quantity: 100 },
{ branch_code: 'YTO002', branch_name: '圆通北京网点', quantity: -1 }, // quantity 为 -1
],
},
{
cp_code: 'SF',
cp_name: '顺丰速运',
waybill_type: '1',
accounts: [
{ branch_code: 'SF001', branch_name: '顺丰深圳网点', quantity: null }, // quantity 为 null
],
},
{
cp_code: 'YTO', // 重复的 cp_code
cp_name: '圆通', // 名称不一致
waybill_type: '2',
accounts: [
{ branch_code: 'YTO003', branch_name: '圆通广州网点', quantity: undefined }, // quantity 为 undefined
{ branch_code: 'YTO004', branch_name: '圆通杭州网点', quantity: 50 },
],
},
{
cp_code: 'ZTO',
cp_name: '中通快递',
waybill_type: '1',
accounts: [], // accounts 为空数组
},
],
};
service.service.systems.poloApi.request = async () => mockDyResponse;
service.service.systems.express.getExpressList = async () => ([
{ code: 'YTO', name: '圆通速递', DY: 'YTO' },
{ code: 'SF', name: '顺丰速运', DY: 'SF' },
{ code: 'ZTO', name: '中通快递', DY: 'ZTO' },
]);
ctx.model.Shop.findOne = async () => ({
get: () => ({
id: 123,
warehouse_id: 100000,
platform_code: 'DY',
access_token: 'fake_token',
}),
});
// 2. 执行被测方法
const result = await service.getShopExpressList({ shop_id: 123 });
// 3. 构造严格的输出断言
assert.equal(result.flag, 'success');
assert.equal(result.code, 200);
const { data } = result;
assert.strictEqual(data.shop_id, 123, '返回的 shop_id 应正确');
assert.strictEqual(data.platform_code, 'DY', '返回的 platform_code 应为 DY');
const list = data.list;
assert.strictEqual(list.length, 3, '应返回3个快递公司(YTO应被合并)');
// 验证圆通 (YTO)
const yto = list.find(item => item.wp_code === 'YTO');
assert(yto, '结果中应包含圆通');
assert.strictEqual(yto.wp_name, '圆通速递', '圆通的名称应正确');
assert.strictEqual(yto.sys_express_code, 'YTO', '圆通的系统快递编码应正确');
assert.strictEqual(yto.accounts.length, 4, '圆通应有4个网点账户');
const ytoAccount1 = yto.accounts.find(a => a.branch_code === 'YTO001');
assert.strictEqual(ytoAccount1.quantity, 100, 'YTO001 的 quantity 应为 100');
const ytoAccount2 = yto.accounts.find(a => a.branch_code === 'YTO002');
assert.strictEqual(ytoAccount2.quantity, -1, 'YTO002 的 quantity 应为 -1');
const ytoAccount3 = yto.accounts.find(a => a.branch_code === 'YTO003');
assert.strictEqual(ytoAccount3.quantity, 0, 'YTO003 的 quantity (undefined) 应被规范化为 0');
const ytoAccount4 = yto.accounts.find(a => a.branch_code === 'YTO004');
assert.strictEqual(ytoAccount4.quantity, 50, 'YTO004 的 quantity 应为 50');
// 验证顺丰 (SF)
const sf = list.find(item => item.wp_code === 'SF');
assert(sf, '结果中应包含顺丰');
assert.strictEqual(sf.accounts.length, 1, '顺丰应有1个网点账户');
assert.strictEqual(sf.accounts[0].quantity, 0, '顺丰的 quantity (null) 应被规范化为 0');
// 验证中通 (ZTO)
const zto = list.find(item => item.wp_code === 'ZTO');
assert(zto, '结果中应包含中通');
assert.strictEqual(zto.accounts.length, 0, '中通应有0个网点账户');
});
});
});