- backend: menu tablet device config `table` (ref, validated against active
tables) and `call_waiter`; GET /api/menu-tablet/menu gives the table and
whether it can call; POST /api/menu-tablet/call sends a `table_call` staff
message ("Table 5: the guests call a waiter.", venue language) to the table's
waiters (its open order's, else the zone's), at most once a minute per tablet
(menu_tablet.call_too_soon with the seconds left; menu_tablet.no_table).
- terminal: "Call a waiter" only on a tablet at a table; "A waiter is on the
way to table 5", then a countdown before it can call again; a failed call
says to ask at the counter.
- waiter: the call shows with its own look and "Open the table".
- manager: tables as the choices of a device setting.
- tests: backend +2 (535 → 533 passed total), e2e menu_tablet.mjs +3.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
117 lines
7.4 KiB
JavaScript
117 lines
7.4 KiB
JavaScript
// 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).
|
||
// 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)')
|
||
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]
|
||
|
||
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)))
|
||
await tab.keyboard.press('Escape')
|
||
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') })
|
||
|
||
// 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)
|