'use strict'; const assert = require('node:assert/strict'); const PrintService = require('../../../../app/service/systemMethods/print'); /** * 构建 print.send 测试上下文。 * @returns {{ctx: Object, calls: Object}} */ function createContext() { const calls = { printRecordCreatePayloads: [], printRecordUpdatePayloads: [], sendToComputerPayloads: [], }; const ctx = { app: { config: { jwt: {}, }, Sequelize: {}, }, user: { store_id: 100000, }, model: { PrintRecord: { async create(payload) { calls.printRecordCreatePayloads.push(payload); return { id: 9001, async update(updatePayload) { calls.printRecordUpdatePayloads.push(updatePayload); }, }; }, }, }, service: { systems: { print: { index: { async sendToComputer(payload) { calls.sendToComputerPayloads.push(payload); return { status: 'success', }; }, }, }, }, }, }; return { ctx, calls }; } describe('test/app/service/systemMethods/print.send.test.js', () => { it('type=express 不传 printer 时应发送成功并调用 sendToComputer', async () => { const { ctx, calls } = createContext(); const service = new PrintService(ctx); const result = await service.send({ computerId: 'PC-00', type: 'express', data: { order_no: 'SO10000', }, timestamp: '1709999999', count: 1, }); assert.equal(result.flag, 'success'); assert.equal(result.code, 200); assert.equal(calls.sendToComputerPayloads.length, 1); assert.equal(calls.printRecordUpdatePayloads.length, 1); assert.equal(calls.printRecordUpdatePayloads[0].status, 'sent'); }); it('type=express 不传 template 时应发送成功并调用 sendToComputer', async () => { const { ctx, calls } = createContext(); const service = new PrintService(ctx); const result = await service.send({ computerId: 'PC-01', printer: 'Printer-A', type: 'express', data: { order_no: 'SO10001', }, timestamp: '1710000000', count: 1, }); assert.equal(result.flag, 'success'); assert.equal(result.code, 200); assert.equal(calls.sendToComputerPayloads.length, 1); assert.equal(calls.printRecordUpdatePayloads.length, 1); assert.equal(calls.printRecordUpdatePayloads[0].status, 'sent'); }); 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); }); });