599 lines
17 KiB
JavaScript
599 lines
17 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const PrintService = require('../../../../app/service/systemMethods/print');
|
|
|
|
/**
|
|
* 构建 print.send 测试上下文。
|
|
* @param {Object} [options] 可选项。
|
|
* @param {Object} [options.printStation] 打印台数据。
|
|
* @param {Function} [options.sendToComputer] 发送函数桩。
|
|
* @return {{ctx: Object, calls: Object}} 包含模拟上下文与调用记录。
|
|
*/
|
|
function createContext(options = {}) {
|
|
const requestOptions = options && typeof options === 'object' ? options : {};
|
|
const calls = {
|
|
printRecordCreatePayloads: [],
|
|
printRecordUpdatePayloads: [],
|
|
sendToComputerPayloads: [],
|
|
sendOrderMarks: [],
|
|
redisGetKeys: [],
|
|
platformFindAllWheres: [],
|
|
};
|
|
|
|
const platformComponents = requestOptions.platformComponents || {
|
|
cainiao: '菜鸟',
|
|
pdd: '拼多多',
|
|
douyin: '抖音',
|
|
};
|
|
|
|
const printStation = requestOptions.printStation || {
|
|
id: 101,
|
|
warehouse_id: 100000,
|
|
deleted: 0,
|
|
computer_id: 'PC-STATION-001',
|
|
default_printer_name: 'PRN-DEFAULT',
|
|
subject_config: {
|
|
express_bind_printer: [
|
|
{
|
|
express_code: 'STO',
|
|
printer_name: 'PRN-STO',
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
const sendToComputer = requestOptions.sendToComputer || (async payload => {
|
|
calls.sendToComputerPayloads.push(payload);
|
|
const marker = payload
|
|
&& payload.data
|
|
&& payload.data.data
|
|
&& payload.data.data.task
|
|
&& Array.isArray(payload.data.data.task.documents)
|
|
&& payload.data.data.task.documents[0]
|
|
&& Array.isArray(payload.data.data.task.documents[0].contents)
|
|
&& payload.data.data.task.documents[0].contents[0]
|
|
? payload.data.data.task.documents[0].contents[0].marker
|
|
: '';
|
|
calls.sendOrderMarks.push(marker);
|
|
return {
|
|
status: 'success',
|
|
};
|
|
});
|
|
|
|
const defaultComponentStatusMap = {
|
|
'printer:PC-STATION-001:name:菜鸟:status': 'connected',
|
|
'printer:PC-STATION-001:name:拼多多:status': 'connected',
|
|
'printer:PC-STATION-001:name:抖音:status': 'connected',
|
|
};
|
|
const componentStatusMap = {
|
|
...defaultComponentStatusMap,
|
|
...(requestOptions.componentStatuses || {}),
|
|
};
|
|
|
|
const ctx = {
|
|
app: {
|
|
baseDir: process.cwd(),
|
|
config: {
|
|
jwt: {},
|
|
},
|
|
Sequelize: {},
|
|
redis: {
|
|
get() {
|
|
return {
|
|
async get(key) {
|
|
calls.redisGetKeys.push(key);
|
|
return Object.prototype.hasOwnProperty.call(componentStatusMap, key)
|
|
? componentStatusMap[key]
|
|
: null;
|
|
},
|
|
};
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
user: {
|
|
store_id: 100000,
|
|
},
|
|
model: {
|
|
PrintRecord: {
|
|
async create(payload) {
|
|
calls.printRecordCreatePayloads.push(payload);
|
|
return {
|
|
id: 9001,
|
|
async update(updatePayload) {
|
|
calls.printRecordUpdatePayloads.push(updatePayload);
|
|
},
|
|
};
|
|
},
|
|
},
|
|
PrintStations: {
|
|
async findOne() {
|
|
return printStation;
|
|
},
|
|
},
|
|
Platform: {
|
|
async findAll(query) {
|
|
calls.platformFindAllWheres.push(query && query.where ? query.where : {});
|
|
const codeList = query && query.where && Array.isArray(query.where.code)
|
|
? query.where.code
|
|
: [];
|
|
return codeList.map(code => ({
|
|
code,
|
|
component: Object.prototype.hasOwnProperty.call(platformComponents, code)
|
|
? platformComponents[code]
|
|
: '',
|
|
}));
|
|
},
|
|
},
|
|
},
|
|
service: {
|
|
systems: {
|
|
print: {
|
|
index: {
|
|
sendToComputer,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
return { ctx, calls };
|
|
}
|
|
|
|
/**
|
|
* 构建 express 场景的 waveGroups。
|
|
* @return {Array<Object>} waveGroups。
|
|
*/
|
|
function createExpressWaveGroups() {
|
|
return [
|
|
{
|
|
wave_id: 1,
|
|
wave_no: 'WAVE-1',
|
|
platforms: [
|
|
{
|
|
platform: 'cainiao',
|
|
sys_logistics_code: 'STO',
|
|
orders: [
|
|
{
|
|
print_data: {
|
|
documentID: 'DOC-OLD-1',
|
|
contents: [
|
|
{
|
|
marker: 'W1-P1',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
{
|
|
print_data: {
|
|
documentID: 'DOC-OLD-2',
|
|
contents: [
|
|
{
|
|
marker: 'W1-P1-2',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
platform: 'pdd',
|
|
sys_logistics_code: 'YTO',
|
|
orders: [
|
|
{
|
|
print_data: {
|
|
documentID: 'DOC-OLD-3',
|
|
contents: [
|
|
{
|
|
marker: 'W1-P2',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
wave_id: 2,
|
|
wave_no: 'WAVE-2',
|
|
platforms: [
|
|
{
|
|
platform: 'douyin',
|
|
sys_logistics_code: 'STO',
|
|
orders: [
|
|
{
|
|
print_data: {
|
|
documentID: 'DOC-OLD-4',
|
|
contents: [
|
|
{
|
|
marker: 'W2-P1',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 构建快手平台的 express 场景波次数据。
|
|
* @param {number} orderCount 订单数量。
|
|
* @return {Array<Object>} waveGroups。
|
|
*/
|
|
function createKuaishouWaveGroups(orderCount) {
|
|
const normalizedOrderCount = Number(orderCount) || 0;
|
|
const orders = [];
|
|
for (let index = 0; index < normalizedOrderCount; index++) {
|
|
const current = index + 1;
|
|
orders.push({
|
|
print_data: {
|
|
documentID: `DOC-KS-${current}`,
|
|
contents: [
|
|
{
|
|
marker: `KS-${current}`,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
}
|
|
return [
|
|
{
|
|
wave_id: 1,
|
|
wave_no: 'WAVE-KS-1',
|
|
platforms: [
|
|
{
|
|
platform: 'kuaishou',
|
|
sys_logistics_code: 'STO',
|
|
orders,
|
|
},
|
|
],
|
|
},
|
|
];
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/print.send.test.js', () => {
|
|
it('type=express 缺失 printStationId 时应返回失败', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
requestCode: 'REQ-MISSING-STATION',
|
|
data: {
|
|
waveGroups: createExpressWaveGroups(),
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'failure');
|
|
assert.equal(result.code, 400);
|
|
assert.match(result.message, /printStationId/);
|
|
assert.equal(calls.sendToComputerPayloads.length, 0);
|
|
});
|
|
|
|
it('type=express 使用 waveGroups 串行发送并保持 success 返回风格', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
printStationId: 101,
|
|
requestCode: 'REQ-SEQ-001',
|
|
data: {
|
|
waveGroups: createExpressWaveGroups(),
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(result.code, 200);
|
|
assert.equal(calls.printRecordCreatePayloads.length, 1);
|
|
assert.equal(calls.printRecordCreatePayloads[0].computer_id, 'PC-STATION-001');
|
|
assert.equal(calls.printRecordCreatePayloads[0].printer, 'PRN-DEFAULT');
|
|
assert.equal(calls.sendToComputerPayloads.length, 3);
|
|
assert.deepEqual(calls.sendOrderMarks, [ 'W1-P1', 'W1-P2', 'W2-P1' ]);
|
|
assert.equal(calls.printRecordUpdatePayloads.length, 1);
|
|
assert.equal(calls.printRecordUpdatePayloads[0].status, 'sent');
|
|
});
|
|
|
|
it('type=express 默认打印机为空时创建记录应写入稳定占位值', async () => {
|
|
const { ctx, calls } = createContext({
|
|
printStation: {
|
|
id: 101,
|
|
warehouse_id: 100000,
|
|
deleted: 0,
|
|
computer_id: 'PC-STATION-001',
|
|
default_printer_name: '',
|
|
subject_config: {
|
|
express_bind_printer: [
|
|
{
|
|
express_code: 'STO',
|
|
printer_name: 'PRN-STO',
|
|
},
|
|
{
|
|
express_code: 'YTO',
|
|
printer_name: 'PRN-YTO',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
});
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
printStationId: 101,
|
|
requestCode: 'REQ-RECORD-PLACEHOLDER-001',
|
|
data: {
|
|
waveGroups: createExpressWaveGroups(),
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(calls.printRecordCreatePayloads.length, 1);
|
|
assert.equal(calls.printRecordCreatePayloads[0].computer_id, 'PC-STATION-001');
|
|
assert.equal(calls.printRecordCreatePayloads[0].printer, 'EXPRESS_DYNAMIC_PRINTER');
|
|
});
|
|
|
|
it('type=express 应按快递匹配打印机并在未命中时回退默认打印机', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
printStationId: 101,
|
|
requestCode: 'REQ-PRINTER-001',
|
|
data: {
|
|
waveGroups: createExpressWaveGroups(),
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(calls.sendToComputerPayloads.length, 3);
|
|
const printerNames = calls.sendToComputerPayloads.map(item => item.data.printer);
|
|
assert.deepEqual(printerNames, [ 'PRN-STO', 'PRN-DEFAULT', 'PRN-STO' ]);
|
|
});
|
|
|
|
it('type=express 发送成功时 payload 应带 component 字段', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
printStationId: 101,
|
|
requestCode: 'REQ-COMPONENT-OK-001',
|
|
data: {
|
|
waveGroups: createExpressWaveGroups(),
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
const components = calls.sendToComputerPayloads.map(item => item.data.component);
|
|
assert.deepEqual(components, [ '菜鸟', '拼多多', '抖音' ]);
|
|
});
|
|
|
|
it('type=express 任意组件离线时应整批失败且不发送任务', async () => {
|
|
const { ctx, calls } = createContext({
|
|
componentStatuses: {
|
|
'printer:PC-STATION-001:name:拼多多:status': 'disconnected',
|
|
},
|
|
});
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
printStationId: 101,
|
|
requestCode: 'REQ-COMPONENT-OFFLINE-001',
|
|
data: {
|
|
waveGroups: createExpressWaveGroups(),
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'failure');
|
|
assert.equal(result.code, 400);
|
|
assert.match(result.message, /未在线/);
|
|
assert.equal(calls.printRecordCreatePayloads.length, 0);
|
|
assert.equal(calls.sendToComputerPayloads.length, 0);
|
|
});
|
|
|
|
it('type=express 某平台 orders 为空且组件离线时不应影响整批发送', async () => {
|
|
const { ctx, calls } = createContext({
|
|
componentStatuses: {
|
|
'printer:PC-STATION-001:name:拼多多:status': 'disconnected',
|
|
},
|
|
});
|
|
const service = new PrintService(ctx);
|
|
const waveGroups = [
|
|
{
|
|
wave_id: 1,
|
|
wave_no: 'WAVE-EMPTY-PLATFORM-1',
|
|
platforms: [
|
|
{
|
|
platform: 'cainiao',
|
|
sys_logistics_code: 'STO',
|
|
orders: [
|
|
{
|
|
print_data: {
|
|
documentID: 'DOC-ONLY-1',
|
|
contents: [
|
|
{
|
|
marker: 'ONLY-CAINIAO',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
platform: 'pdd',
|
|
sys_logistics_code: 'YTO',
|
|
orders: [],
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
printStationId: 101,
|
|
requestCode: 'REQ-EMPTY-PLATFORM-OFFLINE-001',
|
|
data: {
|
|
waveGroups,
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(calls.sendToComputerPayloads.length, 1);
|
|
assert.deepEqual(calls.sendOrderMarks, [ 'ONLY-CAINIAO' ]);
|
|
assert.equal(calls.platformFindAllWheres.length, 1);
|
|
assert.deepEqual(calls.platformFindAllWheres[0].code, [ 'cainiao' ]);
|
|
assert.deepEqual(calls.redisGetKeys, [ 'printer:PC-STATION-001:name:菜鸟:status' ]);
|
|
});
|
|
|
|
it('type=express 平台 component 为空时应失败且不发送任务', async () => {
|
|
const { ctx, calls } = createContext({
|
|
platformComponents: {
|
|
cainiao: '菜鸟',
|
|
pdd: '',
|
|
douyin: '抖音',
|
|
},
|
|
});
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
printStationId: 101,
|
|
requestCode: 'REQ-COMPONENT-EMPTY-001',
|
|
data: {
|
|
waveGroups: createExpressWaveGroups(),
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'failure');
|
|
assert.equal(result.code, 400);
|
|
assert.match(result.message, /未配置打印组件/);
|
|
assert.equal(calls.printRecordCreatePayloads.length, 0);
|
|
assert.equal(calls.sendToComputerPayloads.length, 0);
|
|
});
|
|
|
|
it('type=express 快手平台应按10单分批发送并注入文档位置信息', async () => {
|
|
const { ctx, calls } = createContext({
|
|
platformComponents: {
|
|
kuaishou: '快手',
|
|
},
|
|
componentStatuses: {
|
|
'printer:PC-STATION-001:name:快手:status': 'connected',
|
|
},
|
|
});
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
printStationId: 101,
|
|
requestCode: 'REQ-KS-BATCH-001',
|
|
data: {
|
|
waveGroups: createKuaishouWaveGroups(21),
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
assert.equal(calls.sendToComputerPayloads.length, 3);
|
|
|
|
const requestPayloads = calls.sendToComputerPayloads.map(item => item.data.data);
|
|
const firstDocumentNumbers = requestPayloads.map(item => item.task.firstDocumentNumber);
|
|
const totalDocumentCounts = requestPayloads.map(item => item.task.totalDocumentCount);
|
|
const documentCounts = requestPayloads.map(item => item.task.documents.length);
|
|
|
|
assert.deepEqual(firstDocumentNumbers, [ 1, 11, 21 ]);
|
|
assert.deepEqual(totalDocumentCounts, [ 21, 21, 21 ]);
|
|
assert.deepEqual(documentCounts, [ 10, 10, 1 ]);
|
|
});
|
|
|
|
it('type=express 同一次请求内 requestId/taskID/documentID 应唯一', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
type: 'express',
|
|
printStationId: 101,
|
|
requestCode: 'REQ-UNIQ-001',
|
|
data: {
|
|
waveGroups: createExpressWaveGroups(),
|
|
},
|
|
});
|
|
|
|
assert.equal(result.flag, 'success');
|
|
const requestIdSet = new Set();
|
|
const taskIdSet = new Set();
|
|
const documentIdSet = new Set();
|
|
|
|
for (const callPayload of calls.sendToComputerPayloads) {
|
|
const requestPayload = callPayload.data.data;
|
|
const requestId = requestPayload.requestID || requestPayload.requetID;
|
|
requestIdSet.add(requestId);
|
|
taskIdSet.add(requestPayload.task.taskID);
|
|
const documents = Array.isArray(requestPayload.task.documents) ? requestPayload.task.documents : [];
|
|
for (const document of documents) {
|
|
documentIdSet.add(document.documentID);
|
|
}
|
|
}
|
|
|
|
assert.equal(requestIdSet.size, calls.sendToComputerPayloads.length);
|
|
assert.equal(taskIdSet.size, calls.sendToComputerPayloads.length);
|
|
assert.equal(documentIdSet.size, 4);
|
|
});
|
|
|
|
it('type=customize 不传 printer 时应返回失败且提示 printer', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
computerId: 'PC-03',
|
|
type: 'customize',
|
|
template: {
|
|
page_width: 100,
|
|
page_height: 60,
|
|
template_content: [],
|
|
},
|
|
data: {
|
|
order_no: 'SO10003',
|
|
},
|
|
timestamp: '1710000002',
|
|
count: 1,
|
|
});
|
|
|
|
assert.equal(result.flag, 'failure');
|
|
assert.equal(result.code, 400);
|
|
assert.match(result.message, /printer/);
|
|
assert.equal(calls.sendToComputerPayloads.length, 0);
|
|
});
|
|
|
|
it('type=customize 不传 template 时应返回失败且提示 template', async () => {
|
|
const { ctx, calls } = createContext();
|
|
const service = new PrintService(ctx);
|
|
|
|
const result = await service.send({
|
|
computerId: 'PC-02',
|
|
printer: 'Printer-B',
|
|
type: 'customize',
|
|
data: {
|
|
order_no: 'SO10002',
|
|
},
|
|
timestamp: '1710000001',
|
|
count: 1,
|
|
});
|
|
|
|
assert.equal(result.flag, 'failure');
|
|
assert.equal(result.code, 400);
|
|
assert.match(result.message, /template/);
|
|
assert.equal(calls.sendToComputerPayloads.length, 0);
|
|
});
|
|
});
|