// Printing problems (plan step 6.10), the step's "done when": with the kitchen printer stopped, // a waiter's order shows "not printed" on the phone and in the manager, is sent to the bar // printer from there, and the warning clears everywhere. Then a second one is marked "Handled" // from the manager. The kitchen printer is a port nobody listens on; the bar printer is a small // TCP listener that keeps what it gets. // Starts its own backend (:8063), the waiter app (:5183) and the manager (:5184). // node print_problems.mjs import net from 'node:net' import path from 'node:path' import { openSync } from 'node:fs' import { spawn } from 'node:child_process' import { chromium } from 'playwright' import { BROWSER_CHANNEL, CS, OUT, reporter, sleep } from './lib/env.mjs' import { killTree, login, startBackend, startViteNative, waitHttp } from './lib/proc.mjs' const backend = await startBackend({ port: 8063, siteId: 'test-site-1', env: { LICENSE_MODE: 'off' } }) const waiterVite = await startViteNative({ port: 5183, backendUrl: backend.url }) const MANAGER = 'http://localhost:5184' const mlog = openSync(path.join(OUT, 'vite-5184.log'), 'w') const managerVite = spawn('npx', ['vite', '--port', '5184', '--strictPort'], { cwd: path.join(CS, 'manager_dashboard'), shell: process.platform === 'win32', detached: process.platform !== 'win32', stdio: ['ignore', mlog, mlog], env: { ...process.env, XENIA_BACKEND: backend.url }, }) const browser = await chromium.launch(BROWSER_CHANNEL ? { channel: BROWSER_CHANNEL } : {}) const r = reporter('printing problems (6.10): shown on the phone and in the manager, sent elsewhere, handled') // the bar printer: accepts and keeps every ticket const barTickets = [] const bar = net.createServer(sock => { let buf = Buffer.alloc(0); sock.on('data', d => { buf = Buffer.concat([buf, d]) }); sock.on('error', () => {}); sock.on('close', () => { if (buf.length) barTickets.push(buf) }) }) await new Promise(res => bar.listen(0, '127.0.0.1', res)) // the kitchen printer: a port that was free a moment ago (connection refused, like a printer off) const deadPort = await new Promise(res => { const s = net.createServer(); s.listen(0, '127.0.0.1', () => { const p = s.address().port; s.close(() => res(p)) }) }) async function api(method, url, { token, body } = {}) { const res = await fetch(backend.url + url, { method, headers: { 'content-type': 'application/json', ...(token ? { authorization: `Bearer ${token}` } : {}) }, body: body === undefined ? undefined : JSON.stringify(body), }) const text = await res.text() let data = null try { data = JSON.parse(text) } catch { data = text } if (!res.ok) throw new Error(`${method} ${url} → ${res.status} ${text.slice(0, 200)}`) return data } const shown = (page, testId, timeout = 10_000) => page.getByTestId(testId).first().waitFor({ timeout }).then(() => true).catch(() => false) const gone = (page, testId, timeout = 10_000) => page.getByTestId(testId).first().waitFor({ state: 'detached', timeout }).then(() => true).catch(() => false) const errors = [] try { const boss = await login(backend.url) await api('POST', '/api/business-day/open', { token: boss, body: {} }).catch(() => {}) // printers and a kitchen zone the dishes print in const kitchen = await api('POST', '/api/system/printers', { token: boss, body: { name: 'Kitchen E2E', ip_address: '127.0.0.1', port: deadPort, is_active: true } }) const barP = await api('POST', '/api/system/printers', { token: boss, body: { name: 'Bar E2E', ip_address: '127.0.0.1', port: bar.address().port, is_active: true } }) const zone = await api('POST', '/api/prep-zones', { token: boss, body: { name: 'Kitchen E2E', printer_ids: [kitchen.id], master_printer_id: kitchen.id, auto_print: 'master' } }) const [dish, dish2] = (await api('GET', '/api/orders/menu', { token: boss })).products.filter(p => p.available && !p.service && !p.prefs.length) await api('POST', '/api/products/bulk-prep-zones', { token: boss, body: { product_ids: [dish.id, dish2.id], prep_zone_ids: [zone.id] } }) // ── the phone: Maria signs in and starts her shift ───────────────────────── const ctxP = await browser.newContext({ viewport: { width: 393, height: 852 }, deviceScaleFactor: 2, isMobile: true, hasTouch: true }) const ph = await ctxP.newPage() ph.on('pageerror', e => errors.push(`phone: ${e.message}`)) await ph.goto(`http://localhost:5183/login`) await ph.getByTestId('waiter-card').filter({ hasText: 'Κωνσταντίνου' }).click({ timeout: 30_000 }) for (const d of '3302') await ph.getByRole('button', { name: d, exact: true }).first().click() await ph.waitForURL(u => u.pathname !== '/login', { timeout: 15_000 }).catch(() => {}) await ph.getByRole('button', { name: /Start shift/ }).click({ timeout: 10_000 }).catch(() => {}) await ph.waitForURL(u => u.pathname.startsWith('/tables'), { timeout: 10_000 }).catch(() => {}) const maria = await ph.evaluate(() => localStorage.getItem('xenia:dev:token')) // ── the manager: signed in on the overview ───────────────────────────────── await waitHttp(`${MANAGER}/`) const ctxM = await browser.newContext({ viewport: { width: 1400, height: 900 } }) const mg = await ctxM.newPage() mg.on('pageerror', e => errors.push(`manager: ${e.message}`)) await mg.goto(`${MANAGER}/login`) await mg.getByTestId('login-form').waitFor({ timeout: 60_000 }) if (!(await mg.getByTestId('login-account').count())) await mg.getByLabel('Username').fill('manager') await mg.getByLabel('Password').fill('password') await mg.getByRole('button', { name: 'Sign in', exact: true }).click() await mg.waitForURL(u => u.pathname.startsWith('/dashboard'), { timeout: 20_000 }).catch(() => {}) // ── Maria's order: the kitchen printer is off ────────────────────────────── const busy = new Set((await api('GET', '/api/orders/', { token: boss })).map(o => o.table_id)) const table = (await api('GET', '/api/tables/', { token: boss })).find(t => t.is_active !== false && !busy.has(t.id)) const order = await api('POST', '/api/orders/', { token: maria, body: { table_id: table.id } }) const sent = await api('POST', `/api/orders/${order.id}/items`, { token: maria, body: { lines: [{ product_id: dish.id, quantity: 2 }] } }) r.check('the kitchen printer is off: the ticket fails', sent.print_results?.length === 1 && sent.print_results[0].success === false, JSON.stringify(sent.print_results)) r.check('the phone shows it at once: the red printer button', await shown(ph, 'print-problems-button', 15_000)) r.check('…and in the manager: the card on the overview names the table, the printer and the dish', await mg.getByTestId('print-problem').first().waitFor({ timeout: 15_000 }).then(() => true).catch(() => false) && (await mg.getByTestId('print-problems-card').textContent()).includes('Kitchen E2E') && (await mg.getByTestId('print-problems-card').textContent()).includes(dish.name)) await mg.screenshot({ path: path.join(OUT, 'print-problems-manager.png') }) // the table's page on the phone warns too; from there: send it to the bar await ph.goto(`http://localhost:5183/tables/${table.id}`) r.check("the table's page on the phone: \"Not printed on Kitchen E2E\"", await shown(ph, 'table-print-warning') && (await ph.getByTestId('table-print-warning').textContent()).includes('Kitchen E2E')) await ph.getByTestId('table-print-warning').click() await shown(ph, 'print-problems-sheet') await ph.screenshot({ path: path.join(OUT, 'print-problems-phone.png') }) await ph.getByTestId('problem-printer').first().selectOption(String(barP.id)) await ph.getByTestId('problem-redirect').first().click() for (let i = 0; i < 40 && !barTickets.length; i++) await sleep(100) r.check('sent to the bar printer from the phone: it printed there', barTickets.length === 1 && barTickets[0].length > 50, String(barTickets.map(b => b.length))) r.check('the warning clears on the phone (button and table)', await gone(ph, 'print-problems-button', 10_000) && await gone(ph, 'table-print-warning', 10_000)) r.check('…and in the manager, by itself (live)', await gone(mg, 'print-problem', 10_000)) const left = await api('GET', '/api/print-problems', { token: boss }) r.check('nothing left waiting', left.problems.length === 0, JSON.stringify(left.problems)) // ── a second one, handled from the manager's order page ──────────────────── await api('POST', `/api/orders/${order.id}/items`, { token: maria, body: { lines: [{ product_id: dish2.id, quantity: 1 }] } }) await mg.goto(`${MANAGER}/orders/${order.id}`) r.check("the manager's order page lists the order's ticket", await mg.getByTestId('print-problem').first().waitFor({ timeout: 10_000 }).then(() => true).catch(() => false)) await mg.getByTestId('problem-handled').first().click() r.check('"Handled" clears it in the manager and on the phone', await gone(mg, 'print-problem', 10_000) && (await api('GET', '/api/print-problems', { token: maria })).problems.length === 0) r.check('no page errors', errors.length === 0, errors.slice(0, 3).join(' | ')) } catch (e) { console.error(e) r.check('ran to the end', false, String(e)) } finally { await browser.close() waiterVite.stop() killTree(managerVite) bar.close() await backend.stop() } process.exit(r.done() ? 1 : 0)