oms/backend/test/app/lib/printPlatformDgj.test.js
liufumingren 2b8fcc2286 外部打单平台增加「是否自带运单」
- external_print_platforms 新增 has_own_waybill(默认 0),同步接口契约与前端类型
- 自带运单的平台取号时复用平台已确认运单号(WaybillCodes),只重新拉取打印数据,不分配新面单
- 平台属性查询补齐 has_own_waybill,更新店管家取号文档并补测试

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-23 21:13:48 +08:00

323 lines
12 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('egg-mock/bootstrap');
const DgjService = require('../../../app/lib/printPlatform/dgj');
describe('printPlatform/dgj isOrderFinished', () => {
const base = { ErpState: 'waitsellersend', PrintState: 0, LogisticStatus: 0, WaybillCodes: [] };
it('ErpState 为 sended / success / close 时判定为已处理', () => {
for (const state of [ 'sended', 'success', 'close', 'SENDED', 'Closed' ]) {
assert.strictEqual(DgjService.isOrderFinished({ ...base, ErpState: state }), true, state);
}
});
it('待发货且没有任何发货证据时保留(需要处理)', () => {
assert.strictEqual(DgjService.isOrderFinished(base), false);
});
it('有 OnlineSendTime 时判定为已发货', () => {
assert.strictEqual(DgjService.isOrderFinished({ ...base, OnlineSendTime: '2026-11-02 10:00:00' }), true);
});
it('面单包裹 IsSended=true 时判定为已发货', () => {
assert.strictEqual(
DgjService.isOrderFinished({ ...base, WaybillCodes: [{ WaybillCode: '770', IsSended: false }, { WaybillCode: '771', IsSended: true }] }),
true
);
});
it('LogisticStatus > 0 时判定为已发货', () => {
assert.strictEqual(DgjService.isOrderFinished({ ...base, LogisticStatus: 2 }), true);
});
it('只打印了面单、未发货(PrintState=1 且有 ExpressPrintTime)时仍需要处理', () => {
assert.strictEqual(
DgjService.isOrderFinished({ ...base, PrintState: 1, ExpressPrintTime: '2026-11-02 10:00:00' }),
false
);
});
it('状态字段缺失时不误判', () => {
assert.strictEqual(DgjService.isOrderFinished({}), false);
assert.strictEqual(DgjService.isOrderFinished(null), false);
});
});
describe('printPlatform/dgj extractPrintHistoryIds', () => {
it('解析真实 ExpressPrint 响应的 PrintHistoryIds(打印记录ID)', () => {
const printed = {
Data: {
PrintHistoryIds: [
{ Id: 10000078656002, Pid: '3316997317085033076', WaybillCode: '9823617121121', WaybillCodeChild: null, BatchIndex: 1 },
],
WaybillCode: [{ WaybillCode: '9823617121121', WaybillCodeId: 10000298258339 }],
},
};
const list = DgjService.extractPrintHistoryIds(printed);
console.log('打印记录:', JSON.stringify(list));
assert.deepStrictEqual(list, [
{ id: '10000078656002', pid: '3316997317085033076', waybill_code: '9823617121121', batch_index: 1 },
]);
});
it('多包裹按顺序返回,缺失数据不炸', () => {
const printed = { Data: { PrintHistoryIds: [{ Id: 1, Pid: 'a', WaybillCode: 'W1', BatchIndex: 1 }, { Id: 2, WaybillCode: 'W2' }, null, {}] } };
const list = DgjService.extractPrintHistoryIds(printed);
assert.deepStrictEqual(list, [
{ id: '1', pid: 'a', waybill_code: 'W1', batch_index: 1 },
{ id: '2', pid: '', waybill_code: 'W2', batch_index: null },
]);
});
it('没有该字段时返回空数组', () => {
assert.deepStrictEqual(DgjService.extractPrintHistoryIds({ Data: {} }), []);
assert.deepStrictEqual(DgjService.extractPrintHistoryIds(null), []);
});
});
describe('printPlatform/dgj extractDocumentContents', () => {
it('快手单据要取出逐单密钥 key 与密文 encryptedData', () => {
const document = {
contents: [{
encryptedData: 'A1B2C3D4'.repeat(400),
key: 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQ==',
signature: 'MEQCIB',
ver: '1',
templateURL: 'https://example.com/t.json',
}],
};
const contents = DgjService.extractDocumentContents(document);
assert.strictEqual(contents.encryptprintinfo, 'A1B2C3D4'.repeat(400));
assert.strictEqual(contents.key, 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQ==');
assert.strictEqual(contents.signature, 'MEQCIB');
assert.strictEqual(contents.ver, '1');
assert.strictEqual(contents.template_url, 'https://example.com/t.json');
});
it('没有 contents 时各字段为空串', () => {
assert.deepStrictEqual(DgjService.extractDocumentContents(null), {
signature: '', encryptprintinfo: '', key: '', template_url: '', ver: '',
});
assert.deepStrictEqual(DgjService.extractDocumentContents({ printInfo: 'x' }), {
signature: '', encryptprintinfo: '', key: '', template_url: '', ver: '',
});
});
});
describe('printPlatform/dgj buildWaybillPrintFields', () => {
const contents = {
signature: 'MD:xNFhrlrQ7keXD+RciHKOWA==',
encryptprintinfo: 'AES:rU904rj6UH2oqfSUb43+Z199',
key: 'PER_ORDER_KEY',
template_url: 'http://cloudprint.cainiao.com/template/standard/304351',
ver: 'waybill_print_secret_version_1',
};
it('快手:printinfo 用 encryptedData,encryptprintinfo/key 都用逐单密钥', () => {
const fields = DgjService.buildWaybillPrintFields({
platformCode: 'KS',
contents,
documentPrintInfo: '',
});
assert.deepStrictEqual(fields, {
printinfo: 'AES:rU904rj6UH2oqfSUb43+Z199',
encryptprintinfo: 'PER_ORDER_KEY',
key: 'PER_ORDER_KEY',
});
});
it('快手:没有 encryptedData 时回退平台下发的 printInfo', () => {
const fields = DgjService.buildWaybillPrintFields({
platformCode: 'KS',
contents: { ...contents, encryptprintinfo: '' },
documentPrintInfo: 'PLAIN_PRINT_DATA',
});
assert.strictEqual(fields.printinfo, 'PLAIN_PRINT_DATA');
assert.strictEqual(fields.key, 'PER_ORDER_KEY');
});
it('淘宝:printinfo 回拼菜鸟整包 JSON(含模板地址),encryptprintinfo 仍是密文', () => {
const fields = DgjService.buildWaybillPrintFields({
platformCode: 'TB',
contents,
documentPrintInfo: '',
});
assert.deepStrictEqual(JSON.parse(fields.printinfo), {
encryptedData: 'AES:rU904rj6UH2oqfSUb43+Z199',
signature: 'MD:xNFhrlrQ7keXD+RciHKOWA==',
ver: 'waybill_print_secret_version_1',
templateURL: 'http://cloudprint.cainiao.com/template/standard/304351',
});
assert.strictEqual(fields.encryptprintinfo, 'AES:rU904rj6UH2oqfSUb43+Z199');
});
it('淘宝:平台已下发 printInfo 时保持原值,不覆盖', () => {
const fields = DgjService.buildWaybillPrintFields({
platformCode: 'TB',
contents,
documentPrintInfo: 'PLAIN_PRINT_DATA',
});
assert.strictEqual(fields.printinfo, 'PLAIN_PRINT_DATA');
});
it('其它平台:printinfo 原样,encryptprintinfo 仍是密文', () => {
const fields = DgjService.buildWaybillPrintFields({
platformCode: 'PDD',
contents,
documentPrintInfo: 'PLAIN_PRINT_DATA',
});
assert.deepStrictEqual(fields, {
printinfo: 'PLAIN_PRINT_DATA',
encryptprintinfo: 'AES:rU904rj6UH2oqfSUb43+Z199',
key: 'PER_ORDER_KEY',
});
});
});
describe('printPlatform/dgj fetchWaybillNo 失败标记', () => {
const platform = { code: 'dgj', name: '店管家', url: 'dgjapp.com' };
const account = { id: 1, account: 'acc', password: 'pwd', token: 'tk', cookie: 'ck' };
const order = { order_no: 'dgj-40000834686950', place_order_time: '2026-11-20 10:00:00' };
const template = { template_id: 'tpl-1', platform_type: 'taobao' };
function createService() {
const service = new DgjService({ platform, account: { ...account } });
service.resolveTraceId = async () => 'trace-1';
return service;
}
it('店管家查不到订单行时标记「未消耗面单」,允许后续重试', async () => {
const service = createService();
service.findPlatformOrderRow = async () => null;
service.listProductAreas = async () => [];
await assert.rejects(
service.fetchWaybillNo(order, { template }),
err => {
assert.match(err.message, /店管家未找到订单/);
assert.strictEqual(err.waybillNotConsumed, true);
return true;
}
);
});
it('模板校验(只读接口)失败同样标记「未消耗面单」', async () => {
const service = createService();
service.findPlatformOrderRow = async () => ({ ErpState: 'waitsellersend' });
service.checkTemplateAvailable = async () => {
throw new Error('店管家模板校验失败:HTTP 500');
};
await assert.rejects(
service.fetchWaybillNo(order, { template }),
err => {
assert.strictEqual(err.waybillNotConsumed, true);
return true;
}
);
});
it('取号接口网络异常不带标记:结果可能已出号,必须按待核实处理', async () => {
const service = createService();
service.findPlatformOrderRow = async () => ({ ErpState: 'waitsellersend' });
service.checkTemplateAvailable = async () => ({ Data: { Balance: 100, PrintCount: 1 } });
service.expressPrint = async () => {
throw new Error('fetch failed');
};
await assert.rejects(
service.fetchWaybillNo(order, { template }),
err => {
assert.strictEqual(err.waybillNotConsumed, undefined);
return true;
}
);
});
});
describe('printPlatform/dgj fetchWaybillNo 自带运单', () => {
const account = { id: 1, account: 'acc', password: 'pwd', token: 'tk', cookie: 'ck' };
const order = { order_no: 'dgj-40000834686950', place_order_time: '2026-11-20 10:00:00' };
const template = { template_id: 'tpl-1', platform_type: 'taobao' };
function createService() {
const platform = { code: 'dgj', name: '店管家', url: 'dgjapp.com', fetch_waybill_no: 1, has_own_waybill: 1 };
const service = new DgjService({ platform, account: { ...account } });
service.resolveTraceId = async () => 'trace-1';
service.checkTemplateAvailable = async () => ({ Data: { Balance: 1, PrintCount: 1 } });
return service;
}
it('订单已有出号时复用平台运单号,不分配新面单', async () => {
const service = createService();
service.findPlatformOrderRow = async () => ({
ErpState: 'waitsellersend',
LastWaybillCode: '770000000001',
WaybillCodes: [{ WaybillCode: '770000000002' }],
});
let printedWith = null;
service.expressPrint = async (host, options) => {
printedWith = options;
return {
Data: {
WaybillCode: [{ WaybillCode: '770000000002', IsError: false }],
PrintHistoryIds: [],
PrintDataList: { task: { documents: [{ documentID: '770000000002', printInfo: 'PLAIN_PRINT_DATA' }] } },
},
};
};
const result = await service.fetchWaybillNo(order, { template });
assert.strictEqual(printedWith.reuseWaybill, true);
assert.strictEqual(printedWith.packageCount, 1);
assert.strictEqual(result.waybill_code, '770000000002');
assert.deepStrictEqual(result.waybill_codes, [ '770000000002' ]);
});
it('订单还没有出号时直接报错,且标记「未消耗面单」', async () => {
const service = createService();
service.findPlatformOrderRow = async () => ({ ErpState: 'waitsellersend', WaybillCodes: [] });
service.expressPrint = async () => {
throw new Error('不应该调用 ExpressPrint');
};
await assert.rejects(
service.fetchWaybillNo(order, { template }),
err => {
assert.match(err.message, /还没有运单号/);
assert.strictEqual(err.waybillNotConsumed, true);
return true;
}
);
});
});
describe('printPlatform/dgj resolveTraceId', () => {
const platform = { code: 'dgj', name: '店管家', url: 'dgjapp.com' };
const account = { id: 90123, token: 'tk-cache-test', cookie: 'ck' };
it('按账号+token 进程级缓存 traceId,跨实例只打一次 GetUserInfo', async () => {
const calls = [];
const first = new DgjService({ platform, account });
first.getUserInfo = async () => {
calls.push('first');
return { data: { TraceId: 'trace-1' } };
};
const second = new DgjService({ platform, account });
second.getUserInfo = async () => {
calls.push('second');
return { data: { TraceId: 'trace-2' } };
};
assert.strictEqual(await first.resolveTraceId(), 'trace-1');
assert.strictEqual(await second.resolveTraceId(), 'trace-1');
assert.deepStrictEqual(calls, [ 'first' ]);
// token 变化(重新登录)后必须重新取,避免沿用旧 token 的 traceId
second.account = { ...account, token: 'tk-cache-test-2' };
assert.strictEqual(await second.resolveTraceId(), 'trace-2');
});
});