Files
xenia-pos-local/tests/e2e/menu_tablet.mjs
T
bonaminandClaude Opus 5.5 c1af7770cf feat: ordering from the menu tablet at a table (platform-v2 step 6.6, D-30)
- tablet_orders (0034) + services/tablet_orders.py: venue rules (menu_tablet.ordering) with
  per-tablet overrides: kitchen or waiter confirms first, no open order open/refuse, limits
  (items per order, of one dish, time between orders)
- /api/menu-tablet: quote, order, order/{id}, bill (see + ask), requests accept/decline, ordering
- terminal: Add to the order (POS ProductDialog), cart, note, send, live status, the bill
- waiter: TabletRequests on the table page, tablet_order message theme with Open the table
- manager: Settings -> Menu tablets
- tests: test_tablet_orders.py, menu-tablet unit, menu_tablet.mjs e2e (the done-when)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 14:42:37 +02:00

187 lines
14 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Menu tablet suite (plan step 5.7a): a tablet paired as a menu tablet shows the venue's menu with
// prices; search finds a product (also typed in Latin letters) and its price; a product opens big;
// the view options from the device config change it live; a price change shows without a reload;
// the guest's language; back to the start after a while without a touch; a tablet at a table calls
// a waiter (5.7b); guests at a table order (two of a dish with an extra) and it reaches the
// table's order and the kitchen; a waiter confirms first when set so; the bill (6.6).
// Starts its own backend (:8067) and the terminal (:5195).
// node menu_tablet.mjs
import path from 'node:path'
import { chromium } from 'playwright'
import { BROWSER_CHANNEL, OUT, reporter, sleep, waitFor } from './lib/env.mjs'
import { startBackend, startTerminalVite } from './lib/proc.mjs'
import { supportLogin } from './lib/support.mjs'
const backend = await startBackend({ port: 8067, siteId: 'test-site-1', env: { LICENSE_MODE: 'off' } })
const term = await startTerminalVite({ port: 5195, backendUrl: backend.url })
const browser = await chromium.launch(BROWSER_CHANNEL ? { channel: BROWSER_CHANNEL } : {})
const r = reporter('menu tablet: the menu, search, views, live prices, languages, idle reset (5.7a), call a waiter (5.7b), order and the bill (6.6)')
const errors = []
let failures = 0
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)
try {
const boss = await supportLogin(backend.url)
await api('PUT', '/api/platform/modules/menu_tablet', { token: boss, body: { enabled: true } }).catch(() => {})
const products = (await api('GET', '/api/products/?sold_at=in_store', { token: boss })).filter(p => !p.is_service_item && p.lifecycle_status === 'active')
const souvlaki = products.find(p => /Σουβλάκι/.test(p.name)) || products[0]
await api('PUT', `/api/products/${souvlaki.id}`, { token: boss, body: { allergens: { gluten: 'contains', mustard: 'may_contain' },
translations: { en: { name: 'Pork skewer (en)' } } } })
const tab = await (await browser.newContext({ viewport: { width: 1180, height: 820 }, hasTouch: true })).newPage()
tab.on('pageerror', e => errors.push(e.message))
await tab.goto(term.url)
await shown(tab, 'pairing-code', 20_000)
const code = await tab.getByTestId('pairing-code').getAttribute('data-code')
const dev = await api('POST', '/api/devices', { token: boss, body: { code, app_key: 'menu_tablet', name: 'Entrance' } })
r.check('a tablet paired as a menu tablet shows the menu by category, with prices', await shown(tab, 'menu-section', 20_000)
&& await tab.getByTestId('menu-section').count() > 1 && await tab.getByTestId('menu-price').count() > 5)
await tab.screenshot({ path: path.join(OUT, 'menu-tablet-expanded.png') })
// the staff's price check
await tab.getByTestId('menu-search').fill('souvl')
const hit = tab.getByTestId('menu-results').locator(`[data-testid="menu-item"][data-id="${souvlaki.id}"]`)
r.check(`search in Latin letters ("souvl") finds "${souvlaki.name}" with its price`, await hit.waitFor({ timeout: 5000 }).then(() => true).catch(() => false)
&& (await hit.textContent()).includes(souvlaki.base_price.toFixed(2)))
await hit.click()
r.check('…and opens it big with its price', await shown(tab, 'menu-details', 5000)
&& (await tab.getByTestId('menu-details').getByTestId('menu-price').textContent()).includes(souvlaki.base_price.toFixed(2)))
r.check('6.3: …with its allergens', await tab.getByTestId('menu-details').locator('[data-allergen="gluten"][data-level="contains"]').count() === 1
&& await tab.getByTestId('menu-details').locator('[data-allergen="mustard"][data-level="may_contain"]').count() === 1)
await tab.keyboard.press('Escape')
// 6.4: the language buttons switch the products' names too
await tab.getByTestId('menu-search').fill('')
const item = () => tab.locator(`[data-testid="menu-item"][data-id="${souvlaki.id}"]`).first()
const wasEnglish = await tab.getByTestId('menu-lang-en').getAttribute('aria-pressed') === 'true'
await tab.getByTestId('menu-lang-en').click()
const inEnglish = await tab.waitForFunction(id => document.querySelector(`[data-testid="menu-item"][data-id="${id}"]`)?.textContent.includes('Pork skewer (en)'), souvlaki.id, { timeout: 5000 }).then(() => true).catch(() => false)
await tab.getByTestId('menu-lang-el').click()
await sleep(300)
r.check('6.4: EN shows the English name, EL the name as typed', inEnglish && (await item().textContent()).includes(souvlaki.name))
if (wasEnglish) await tab.getByTestId('menu-lang-en').click() // as it was (prices below are read in its format)
await tab.getByTestId('menu-search').fill('zzzz')
r.check('nothing found: says so', await shown(tab, 'menu-no-results', 5000))
await tab.getByTestId('menu-search').fill('')
// the view options from the device config, live
await api('PATCH', `/api/devices/${dev.id}`, { token: boss, body: { config: { view: 'full', theme: 'dark' } } })
r.check('the view from the manager (full cards, dark) changes the tablet at once',
!!(await waitFor(async () => (await tab.getByTestId('app-menu_tablet').getAttribute('data-view')) === 'full', 15_000, 300))
&& await tab.evaluate(() => document.documentElement.dataset.theme) === 'dark')
await tab.screenshot({ path: path.join(OUT, 'menu-tablet-full.png') })
await api('PATCH', `/api/devices/${dev.id}`, { token: boss, body: { config: { view: 'compact', group: false, theme: 'light' } } })
r.check('compact, one A–Z list', !!(await waitFor(async () => (await tab.getByTestId('app-menu_tablet').getAttribute('data-view')) === 'compact'
&& (await tab.getByTestId('menu-section').count()) === 1, 15_000, 300)))
await tab.screenshot({ path: path.join(OUT, 'menu-tablet-compact.png') })
// a price change, live
await api('PUT', `/api/products/${souvlaki.id}`, { token: boss, body: { base_price: 7.7 } })
const live = await waitFor(async () => ((await tab.locator(`[data-testid="menu-item"][data-id="${souvlaki.id}"]`).textContent().catch(() => '')).includes('7.70') ? true : null), 15_000, 300)
r.check('a price change shows on the tablet without a reload', !!live)
// 5.7b: a tablet at a table calls a waiter; one without a table has no button
r.check('5.7b: a tablet not at a table has no "Call a waiter" button', await tab.getByTestId('menu-call-button').count() === 0)
const tables = await api('GET', '/api/tables/', { token: boss })
const t5 = tables.find(x => x.is_active !== false) || tables[0]
await api('PATCH', `/api/devices/${dev.id}`, { token: boss, body: { config: { table: t5.id } } })
r.check('5.7b: set at a table, it gets the button', await shown(tab, 'menu-call-button', 15_000))
await tab.getByTestId('menu-call-button').click()
r.check('5.7b: calling says a waiter is on the way, and waits before it can call again',
await shown(tab, 'menu-call-sent', 5000) && await tab.getByTestId('menu-call-button').isDisabled()
&& (await tab.getByTestId('menu-call-sent').textContent()).includes(t5.label || String(t5.number)))
await tab.screenshot({ path: path.join(OUT, 'menu-tablet-call.png') })
// 6.6: guests order from the tablet at the table (the done-when): two of a dish with an extra
await api('POST', '/api/business-day/open', { token: boss, body: {} }).catch(() => {})
await api('PUT', `/api/products/${souvlaki.id}`, { token: boss, body: { quick_options: [{ name: 'Extra pita', price: 0.5 }] } })
await api('PUT', '/api/menu-tablet/ordering', { token: boss, body: { confirm: 'kitchen', no_order: 'open', max_items: 20, max_of_one: 10, min_seconds: 0 } })
await api('PATCH', `/api/devices/${dev.id}`, { token: boss, body: { config: { table: t5.id, ordering: true } } }) // the device's whole config
await tab.getByTestId('menu-search').fill('souvl')
await tab.getByTestId('menu-results').locator(`[data-testid="menu-item"][data-id="${souvlaki.id}"]`).click()
r.check('6.6: with "Guests can order" on, a dish has "Add to the order"', await shown(tab, 'menu-order-add', 15_000))
await tab.getByTestId('menu-order-add').click()
await shown(tab, 'product-dialog', 5000)
await tab.getByTestId('product-dialog').locator('[data-quick]').first().click()
await tab.getByTestId('dialog-add').click()
await tab.getByTestId('menu-cart').click()
await tab.getByTestId('menu-order-line').first().getByRole('button').nth(1).click() // one more
const qty = await waitFor(async () => ((await tab.getByTestId('menu-order-qty').first().textContent()) === '2' ? true : null), 5000, 200)
const cartTotal = await waitFor(async () => { const x = await tab.getByTestId('menu-order-total').textContent(); return x.includes((2 * (7.7 + 0.5)).toFixed(2)) ? x : null }, 8000, 200) // re-priced after the change
r.check('6.6: the cart: two of it, priced by the server with the extra', !!qty && String(cartTotal).includes((2 * (7.7 + 0.5)).toFixed(2)), String(cartTotal))
await tab.screenshot({ path: path.join(OUT, 'menu-tablet-cart.png') })
await tab.getByTestId('menu-order-send').click()
await tab.getByTestId('menu-order-confirm').click()
r.check('6.6: sent: the tablet says it is in the kitchen', await tab.locator('[data-testid="menu-order-status"][data-status="sent"]').waitFor({ timeout: 10_000 }).then(() => true).catch(() => false))
const tableOrder = async () => (await api('GET', '/api/orders/', { token: boss })).find(o => o.table_id === t5.id)
const placed = await tableOrder()
const lines = (placed?.items || []).filter(i => i.product_id === souvlaki.id && i.status !== 'cancelled')
r.check("6.6: the table's order has them, from the tablet", lines.length > 0 && lines.reduce((n, i) => n + i.quantity, 0) === 2 && lines.every(i => i.source === 'tablet'),
JSON.stringify(lines.map(i => [i.quantity, i.source])))
// a waiter confirms first: it waits, then the waiter accepts and the tablet hears it
await api('PATCH', `/api/devices/${dev.id}`, { token: boss, body: { config: { table: t5.id, ordering: true, order_confirm: 'waiter' } } })
await sleep(500)
await tab.getByTestId('menu-search').fill('souvl')
await tab.getByTestId('menu-results').locator(`[data-testid="menu-item"][data-id="${souvlaki.id}"]`).click()
await tab.getByTestId('menu-order-add').click()
await tab.getByTestId('dialog-add').click()
await tab.getByTestId('menu-cart').click()
await tab.getByTestId('menu-order-send').click()
await tab.getByTestId('menu-order-confirm').click()
r.check('6.6: a waiter confirms first: the tablet says it waits', await tab.locator('[data-testid="menu-order-status"][data-status="waiting"]').waitFor({ timeout: 10_000 }).then(() => true).catch(() => false))
const waiting = await api('GET', `/api/menu-tablet/requests?table_id=${t5.id}`, { token: boss })
r.check("6.6: …and it is on the table's list for the waiters", waiting.length === 1)
if (waiting[0]) await api('POST', `/api/menu-tablet/requests/${waiting[0].id}/accept`, { token: boss, body: {} })
r.check('6.6: accepted: the tablet shows it in the kitchen, live', await tab.locator('[data-testid="menu-order-status"][data-status="sent"]').waitFor({ timeout: 10_000 }).then(() => true).catch(() => false))
// the bill: what the table ordered so far, and asking for it
await tab.getByTestId('menu-bill-button').click()
const due = 2 * (7.7 + 0.5) + 7.7 // two with the extra, then one without
r.check("6.6: the bill lists the table's order and what is due", await shown(tab, 'menu-bill', 5000)
&& await tab.getByTestId('menu-bill-line').count() >= 2 && (await tab.getByTestId('menu-bill-due').textContent()).includes(due.toFixed(2)),
`${due} / ${await tab.getByTestId('menu-bill-due').textContent().catch(() => '')}`)
await tab.screenshot({ path: path.join(OUT, 'menu-tablet-bill.png') })
await tab.getByTestId('menu-bill-ask').click()
r.check('6.6: asking for the bill says a waiter is bringing it', await shown(tab, 'menu-bill-asked', 5000))
await tab.keyboard.press('Escape')
await tab.getByTestId('menu-search').fill('')
// the guest's language, then back to the start after a while
await tab.getByTestId('menu-lang-en').click()
const enPlaceholder = await tab.getByTestId('menu-search').getAttribute('placeholder')
await tab.getByTestId('menu-lang-el').click()
const elPlaceholder = await tab.getByTestId('menu-search').getAttribute('placeholder')
r.check('the guest picks the language (the tablet\'s own texts)', enPlaceholder !== elPlaceholder, `${enPlaceholder} / ${elPlaceholder}`)
await api('PATCH', `/api/devices/${dev.id}`, { token: boss, body: { config: { idle_seconds: 30 } } })
await sleep(1500)
await tab.getByTestId('menu-search').fill('souvl')
await tab.getByTestId('menu-lang-en').click()
const reset = await waitFor(async () => ((await tab.getByTestId('menu-search').inputValue()) === '' ? true : null), 45_000, 1000)
r.check('after a while without a touch: back to the start (search cleared, the first language)', !!reset
&& await tab.getByTestId('menu-lang-el').getAttribute('aria-pressed') === 'true')
r.check('no page errors', errors.length === 0, errors.join(' | '))
failures += r.done()
} catch (e) {
console.error(e)
r.check('the suite ran to the end', false, String(e.message).split(/\r?\n/)[0])
failures += r.done()
} finally {
await browser.close()
term.stop()
await backend.stop()
}
process.exit(failures ? 1 : 0)