feat(waiter): sign-in v2 on the phones (platform-v2 step 4.2)

- Login follows auth.waiter_login_mode / auth.waiter_require_username from the
  bootstrap: name + PIN, name + password, typed username, or a "paired screens
  only" message for tap mode. POST /api/auth/login {client: "waiter"}; a miss
  shows the translated error without a reload.
- POST /api/auth/login-by-id removed; bootstrap login carries waiter_mode and
  waiter_require_username.
- Sign-out ends the session on the server (POST /api/auth/logout), so
  single_session_waiter = refuse_new no longer strands a waiter.
- Lock on the phone: menu "Lock", automatic lock / sign-out after inactivity
  (security.auto_lock*, auto_logout*), unlock with the PIN in the same session,
  lock kept across app restarts (restore() no longer clears it).
- e2e: new waiter.mjs (22 checks) in run-all; android.mjs signs in for real and
  locks/unlocks on the emulator; TLS live check waits for the socket; the
  rediscovery helper re-attaches to a stale WebView handle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 15:25:00 +02:00
co-authored by Claude Opus 5.5
parent 9f5498332a
commit 8299184e3a
14 changed files with 664 additions and 106 deletions
+186
View File
@@ -0,0 +1,186 @@
// Waiter app suite (plan phase 4): sign-in v2 on the phone (4.2): each login mode, wrong PIN,
// one session per waiter, lock / unlock / idle sign-out, a manager's approval on the phone.
// Starts its own backend (:8061) and the venue-mode dev build (:5181), phone-sized Edge.
// node waiter.mjs
import path from 'node:path'
import { chromium } from 'playwright'
import { BROWSER_CHANNEL, OUT, reporter, sleep } from './lib/env.mjs'
import { login, startBackend, startViteNative } from './lib/proc.mjs'
const backend = await startBackend({ port: 8061, siteId: 'test-site-1', env: { LICENSE_MODE: 'off' } })
const vite = await startViteNative({ port: 5181, backendUrl: backend.url })
const APP = 'http://localhost:5181'
const TOKEN_KEY = 'xenia:dev:token'
const browser = await chromium.launch(BROWSER_CHANNEL ? { channel: BROWSER_CHANNEL } : {})
const r = reporter('waiter: sign-in v2, lock, approvals (4.2)')
const errors = []
let failures = 0
async function api(method, url, { token, body, raw = false } = {}) {
const res = await fetch(backend.url + url, {
method, headers: { 'content-type': 'application/json', ...(token ? { authorization: `Bearer ${token}` } : {}) },
body: body === undefined ? undefined : JSON.stringify(body),
})
if (raw) return res
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 phone = () => browser.newContext({ viewport: { width: 393, height: 852 }, deviceScaleFactor: 2, isMobile: true, hasTouch: true })
async function open(ctx, url = `${APP}/login`) {
const page = await ctx.newPage()
page.on('pageerror', e => errors.push(e.message))
await page.goto(url)
return page
}
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)
async function pin(page, digits) {
for (const d of digits) await page.getByRole('button', { name: d, exact: true }).first().click()
}
const tokenOf = page => page.evaluate(k => localStorage.getItem(k), TOKEN_KEY)
const pathOf = page => page.evaluate(() => location.pathname)
const setting = (boss, key, value) => api('PUT', `/api/settings/${key}`, { token: boss, body: { value } })
try {
const boss = await login(backend.url)
await api('POST', '/api/business-day/open', { token: boss, body: {} }).catch(() => {})
const waiters = await api('GET', '/api/auth/waiters')
const maria = waiters.find(w => (w.full_name || '').includes('Κωνσταντίνου'))
// ── login-by-id is gone ────────────────────────────────────────────────────
const old = await api('POST', '/api/auth/login-by-id', { body: { waiter_id: maria?.id, pin: '3302' }, raw: true })
r.check('POST /api/auth/login-by-id is gone (404)', old.status === 404, String(old.status))
// ── PIN mode: pick your name, wrong PIN, right PIN ─────────────────────────
const ctxA = await phone()
const a = await open(ctxA)
r.check('PIN mode: the phone lists the waiters to pick from', await shown(a, 'waiter-card'))
await a.getByTestId('waiter-card').filter({ hasText: 'Κωνσταντίνου' }).click()
r.check('PIN mode: then the PIN pad', await shown(a, 'login-secret') && await a.getByTestId('login-secret').getAttribute('data-mode') === 'pin')
await a.evaluate(() => { window.__noReload = true })
await pin(a, '0000')
r.check('wrong PIN: a message, no reload, still on the PIN pad',
await shown(a, 'login-error', 5000) && await a.evaluate(() => window.__noReload === true) && await pathOf(a) === '/login',
await a.getByTestId('login-error').textContent().catch(() => ''))
await pin(a, '3302')
await a.waitForURL(u => u.pathname !== '/login', { timeout: 10_000 }).catch(() => {})
const tokenA = await tokenOf(a)
r.check('right PIN: signed in (a session token, off the login page)', !!tokenA && await pathOf(a) !== '/login', await pathOf(a))
const sessions = await api('GET', '/api/auth/sessions', { token: boss })
r.check('the session is a "waiter" session', sessions.some(s => s.username === 'demo_maria' && s.client === 'waiter'), JSON.stringify(sessions.map(s => [s.username, s.client])))
// start the shift on the phone (the gate after sign-in)
await a.getByRole('button', { name: /Έναρξη Βάρδιας/ }).click({ timeout: 10_000 }).catch(() => {})
await a.waitForURL(u => u.pathname.startsWith('/tables'), { timeout: 10_000 }).catch(() => {})
await sleep(1500)
// ── One session per waiter (refuse_new) ────────────────────────────────────
await setting(boss, 'auth.single_session_waiter', 'refuse_new')
const ctxB = await phone()
const b = await open(ctxB)
await b.getByTestId('waiter-card').filter({ hasText: 'Κωνσταντίνου' }).click({ timeout: 10_000 })
await pin(b, '3302')
r.check('second phone, same waiter (refuse_new): refused with a message, stays on sign-in',
await shown(b, 'login-error', 5000) && await pathOf(b) === '/login' && !(await tokenOf(b)),
await b.getByTestId('login-error').textContent().catch(() => ''))
// ── Lock from the menu, wrong PIN, right PIN (same session) ────────────────
await a.getByRole('button', { name: 'demo_maria' }).first().click({ timeout: 10_000 })
await a.getByTestId('menu-lock').click()
r.check('menu "Κλείδωμα" locks the phone', await shown(a, 'lock-screen', 5000))
await a.reload()
r.check('still locked after the app is reopened', await shown(a, 'lock-screen', 10_000))
await pin(a, '0000')
const lockErr = await a.getByTestId('unlock-error').textContent({ timeout: 5000 }).catch(() => '')
r.check('wrong PIN on the lock: tries left shown', /4/.test(lockErr), lockErr)
await a.screenshot({ path: path.join(OUT, 'waiter-lock.png') })
await pin(a, '3302')
r.check('right PIN unlocks: same session, same token', await gone(a, 'lock-screen', 5000) && await tokenOf(a) === tokenA)
// ── A manager's approval on the same phone (cancel without the key) ─────────
const tables = await api('GET', '/api/tables/', { token: boss })
const table = tables.find(t => !t.open_order_id && !t.current_order_id) || tables[0]
const order = await api('POST', '/api/orders/', { token: tokenA, body: { table_id: table.id } })
await a.evaluate(id => {
window.__res = window.__xeniaClient.delete(`/api/orders/${id}`)
.then(res => ({ ok: true, status: res.status }), e => ({ ok: false, code: e.xenia?.code }))
}, order.id)
const dialog = a.getByRole('dialog')
await dialog.waitFor({ timeout: 10_000 }).catch(() => {})
r.check('approval dialog opens on the phone for ordering.orders.cancel', await a.getByTestId('override-key').textContent().catch(() => '') === 'ordering.orders.cancel')
await a.screenshot({ path: path.join(OUT, 'waiter-approval.png') })
const inputs = dialog.locator('input')
await inputs.nth(0).fill('manager')
await inputs.nth(1).fill('1234')
await inputs.nth(1).press('Enter')
const result = await a.evaluate(() => window.__res)
r.check("the manager's PIN completes the cancel on the phone", result.ok === true, JSON.stringify(result))
// ── Signing out ends the session on the server: the second phone can sign in ─
await a.getByRole('button', { name: 'demo_maria' }).first().click({ timeout: 10_000 })
await a.getByRole('button', { name: /Αποσύνδεση/ }).last().click()
await a.waitForURL(u => u.pathname === '/login', { timeout: 10_000 }).catch(() => {})
await sleep(1000)
const after = await api('GET', '/api/auth/sessions', { token: boss })
r.check('sign-out ends the session on the server', !after.some(s => s.username === 'demo_maria' && s.client === 'waiter'))
await b.reload()
await b.getByTestId('waiter-card').filter({ hasText: 'Κωνσταντίνου' }).click({ timeout: 10_000 })
await pin(b, '3302')
await b.waitForURL(u => u.pathname !== '/login', { timeout: 10_000 }).catch(() => {})
r.check('…and then the second phone signs in', !!(await tokenOf(b)) && await pathOf(b) !== '/login', await pathOf(b))
// ── Auto-lock and idle sign-out follow the venue's settings ────────────────
await setting(boss, 'security.auto_lock', 'true')
await setting(boss, 'security.auto_lock_seconds', '3')
await setting(boss, 'security.auto_logout', 'true')
await setting(boss, 'security.auto_logout_seconds', '9')
await b.reload()
r.check('idle past auto_lock_seconds: the phone locks by itself', await shown(b, 'lock-screen', 10_000))
r.check('idle past auto_logout_seconds: signed out', await b.waitForURL(u => u.pathname === '/login', { timeout: 15_000 }).then(() => true).catch(() => false)
&& !(await tokenOf(b)))
await setting(boss, 'security.auto_lock', 'false')
await setting(boss, 'security.auto_logout', 'false')
await ctxA.close(); await ctxB.close()
// ── Username + password mode ───────────────────────────────────────────────
await setting(boss, 'auth.waiter_login_mode', 'password')
await setting(boss, 'auth.waiter_require_username', 'true')
const ctxC = await phone()
const c = await open(ctxC)
r.check('password mode with username: a username field and a password field',
await shown(c, 'login-username') && await shown(c, 'login-password') && await c.getByTestId('waiter-card').count() === 0)
await c.getByTestId('login-username').fill('manager')
await c.getByTestId('login-password').fill('wrong-one')
await c.getByTestId('login-submit').click()
r.check('wrong password: a message', await shown(c, 'login-error', 5000), await c.getByTestId('login-error').textContent().catch(() => ''))
await c.getByTestId('login-password').fill('password')
await c.getByTestId('login-submit').click()
await c.waitForURL(u => u.pathname !== '/login', { timeout: 10_000 }).catch(() => {})
r.check('right password: signed in', !!(await tokenOf(c)) && await pathOf(c) !== '/login', await pathOf(c))
await ctxC.close()
// ── Tap mode: phones say so instead of offering a sign-in ──────────────────
await setting(boss, 'auth.waiter_login_mode', 'tap')
await setting(boss, 'auth.waiter_require_username', 'false')
const ctxD = await phone()
const d = await open(ctxD)
r.check('tap mode: the phone explains that sign-in is on the paired screens', await shown(d, 'login-tap-only'))
await d.screenshot({ path: path.join(OUT, 'waiter-tap-only.png') })
await ctxD.close()
await setting(boss, 'auth.waiter_login_mode', 'pin')
r.check('no page errors', errors.length === 0, errors.join(' | '))
failures += r.done()
} catch (e) {
console.error(e)
failures += 1 + r.done()
} finally {
await browser.close()
vite.stop()
await backend.stop()
}
process.exit(failures ? 1 : 0)