WMS_MAIN/test/print_shopify_pdf.unit.test.js
liufumingren 60a2027741 fix: route PRINT_COMPONENT through PDF printing
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-25 02:27:10 +08:00

125 lines
4.4 KiB
JavaScript

'use strict';
const assert = require('assert');
const PrintService = require('../app/service/systemMethods/print');
const PrintClientService = require('../app/service/systems/print');
const OrderPrintSystemService = require('../app/service/systems/order_print');
const PrinterDataBuilder = require('../app/lib/printer_data_builder');
describe('PDF print payload', () => {
const service = Object.create(PrintService.prototype);
it('recognizes PDF print component codes without case sensitivity', () => {
assert.strictEqual(service.isPdfPrintPlatform('PRINT_COMPONENT'), true);
assert.strictEqual(service.isPdfPrintPlatform('SHOPIFY'), true);
assert.strictEqual(service.isPdfPrintPlatform('shopify'), true);
assert.strictEqual(service.isPdfPrintPlatform('amazon'), false);
});
it('maps PRINT_COMPONENT to Shopify PDF metadata and handling', () => {
const normalized = PrinterDataBuilder.normalizePlatformAlias('PRINT_COMPONENT');
const orderPrintService = Object.create(OrderPrintSystemService.prototype);
assert.strictEqual(normalized.platform, 'PRINT_COMPONENT');
assert.strictEqual(normalized.meta.name, '本机组件');
assert.strictEqual(orderPrintService.isPdfPrintPlatformCode('PRINT_COMPONENT'), true);
assert.strictEqual(orderPrintService.isPdfPrintPlatformCode('SHOPIFY'), true);
});
it('returns PDF contents when the stored platform code is PRINT_COMPONENT', async () => {
const orderPrintService = Object.create(OrderPrintSystemService.prototype);
orderPrintService.ctx = {
app: { config: {} },
model: { Store: { findByPk: async () => null } },
};
const result = await orderPrintService.buildOrderPlatformPrintItem({
orderRow: {
id: 1924092,
warehouse_id: 1000028,
sys_logistics_code: 'KJKYSD',
sys_logistics_name: '跨源速递',
},
orderExpress: {
platform_code: 'PRINT_COMPONENT',
waybill_code: 'WB-1924092',
pdf_base64: 'JVBERi0xLjQ=',
},
platformCode: 'PRINT_COMPONENT',
templateSample: {},
});
assert.strictEqual(result.platform, 'PRINT_COMPONENT');
assert.strictEqual(result.platform_name, '本机组件');
assert.deepStrictEqual(result.print_data.contents, [{ pdf_base64: 'JVBERi0xLjQ=' }]);
});
it('converts contents pdf_base64 into the printPdf payload shape', () => {
const result = service.buildPdfPrintPayload({
waveGroup: { wave_id: 12, wave_no: 'W-12' },
platformGroup: { platform: 'SHOPIFY', platform_name: 'Shopify' },
orders: [{
documentID: 'WAYBILL-1',
contents: [{ pdf_base64: 'JVBERi0xLjQ=' }],
}],
orderIds: [ 321 ],
printerName: 'Label Printer',
printId: 'task-12',
});
assert.deepStrictEqual(result.payload[0].platforms[0].orders, [{
order_id: '321',
pdf_base64: 'JVBERi0xLjQ=',
}]);
assert.strictEqual(result.printer, 'Label Printer');
assert.strictEqual(result.printId, 'task-12');
});
it('rejects Shopify orders without PDF data', () => {
assert.throws(
() => service.buildPdfPrintPayload({
orders: [{ documentID: 'WAYBILL-1', contents: [] }],
}),
/缺少 pdf_base64/
);
});
it('sends PDF jobs through the printPdf method', async () => {
const calls = [];
const redisValues = {};
const printerRedis = {
get: async key => redisValues[key] || null,
set: async (key, value) => {
redisValues[key] = value;
},
};
const client = Object.create(PrintClientService.prototype);
client.ctx = {
app: {
config: { printService: { url: 'http://print-service' } },
redis: { get: () => printerRedis },
},
curl: async (url, options) => {
calls.push(options.data.method);
if (options.data.method === 'printer.getWindowsPrinters') {
await printerRedis.set('printer:2:printerList', JSON.stringify({
updatedAt: Date.now() + 1,
data: [{ name: 'Label Printer', workOffline: 'FALSE' }],
}));
}
return { data: { status: 'success' } };
},
};
client.app = client.ctx.app;
await client.sendToComputer({
warehouse: '1',
computerId: '2',
data: { printer: 'Label Printer' },
printMethod: 'printer.printPdf',
});
assert.deepStrictEqual(calls, [ 'printer.getWindowsPrinters', 'printer.printPdf' ]);
});
});