// Printing set-up as plain data (plan step 6.10). import { test } from 'node:test' import assert from 'node:assert/strict' import { beepBytes, beepSettings, decodeBeep, decodeDots, decodeFont, encodeDots, encodeFont, move, orderedCategories, printerBody, printerProblems, setBypass, setMain, toggleSecond, zoneBody, zoneForm, zoneProblem, } from '../src/pages/printing/printingLogic.js' test('fonts and the dot leader round-trip; bad values fall back', () => { assert.deepEqual(decodeFont('48:1:0'), { size: '48', bold: true, caps: false }) assert.equal(encodeFont({ size: '16', bold: false, caps: true }), '16:0:1') assert.deepEqual(decodeFont('99:x'), { size: '0', bold: false, caps: false }) assert.equal(encodeDots(decodeDots('dash_space:32:1')), 'dash_space:32:1') assert.deepEqual(decodeDots(''), { style: 'dot_space', size: '0', bold: false }) }) test('beep: presets, custom, off, and what the test sends', () => { assert.deepEqual(decodeBeep('triple', 'true'), { preset: 'triple', n1: 1, n2: 1, n3: 3 }) assert.deepEqual(decodeBeep('custom:3:2:4', 'true'), { preset: 'custom', n1: 3, n2: 2, n3: 4 }) assert.equal(decodeBeep('double', 'false').preset, 'off') assert.deepEqual(beepSettings({ preset: 'off' }), [['print.beep_on_ticket', 'false']]) assert.deepEqual(beepSettings({ preset: 'custom', n1: 99, n2: 0, n3: 2.4 }), [['print.beep_on_ticket', 'true'], ['print.beep_pattern', 'custom:30:1:2']]) assert.deepEqual(beepBytes({ preset: 'long' }), { n1: 5, n2: 2, n3: 1 }) }) test('printer form: what is wrong, and the body', () => { assert.deepEqual(printerProblems({ name: ' ', ip_address: '10.0.0.300', port: 0, line_width: 48 }), ['name', 'ip_address', 'port']) assert.deepEqual(printerProblems({ name: 'Bar', ip_address: '192.168.1.20', port: '9100', line_width: '42' }), []) assert.deepEqual(printerBody({ name: ' Bar ', ip_address: '192.168.1.20 ', port: '9100', line_width: '42', codepage_n: '24' }), { name: 'Bar', ip_address: '192.168.1.20', port: 9100, protocol: 'escpos_tcp', line_width: 42, codepage_n: 24, is_active: true }) }) test('zones: main and second printers, the kitchen screen switches, what is missing, the body', () => { let f = zoneForm(null) assert.equal(zoneProblem(f), 'name') f = { ...f, name: ' Grill ' } assert.equal(zoneProblem(f), 'printer') f = setMain(f, 3) assert.deepEqual(f.printer_ids, [3]) f = toggleSecond(toggleSecond(f, 5), 3) // the main one can't be unticked assert.deepEqual(f.printer_ids, [3, 5]) assert.equal(zoneProblem(f), null) f = setBypass(f, 'bypass_kds', true) assert.equal(f.bypass_pending, true) f = setBypass(f, 'bypass_pending', false) assert.equal(f.bypass_kds, false) const body = zoneBody({ ...f, description: ' ', master_copies: 20 }) assert.equal(body.name, 'Grill') assert.equal(body.description, null) assert.equal(body.language, null) assert.equal(body.master_copies, 9) assert.equal(zoneForm({ name: 'Bar', group_by_category: 1, language: null }).group_by_category, true) }) test('category order: ranked first, then the rest; moving within bounds', () => { const cats = [{ id: 1 }, { id: 2 }, { id: 3 }] assert.deepEqual(orderedCategories(cats, [3, 9]).map(c => c.id), [3, 1, 2]) assert.deepEqual(move([1, 2, 3], 0, -1), [1, 2, 3]) assert.deepEqual(move([1, 2, 3], 2, -1), [1, 3, 2]) })