'use strict'; const assert = require('assert'); const PrintService = require('../app/service/systemMethods/print'); const PrintClientService = require('../app/service/systems/print'); describe('Shopify PDF print payload', () => { const service = Object.create(PrintService.prototype); it('recognizes Shopify platform codes without case sensitivity', () => { assert.strictEqual(service.isShopifyPdfPlatform('SHOPIFY'), true); assert.strictEqual(service.isShopifyPdfPlatform('shopify'), true); assert.strictEqual(service.isShopifyPdfPlatform('amazon'), false); }); it('converts contents pdf_base64 into the printPdf payload shape', () => { const result = service.buildShopifyPdfPayload({ 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.buildShopifyPdfPayload({ 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' ]); }); });