Platform-v2 step 2.6d: - /devices (core.devices.manage, sidebar): devices from /api/devices with status (online / last seen / waiting for the device / app turned off / removed), app, sign-in mode (own or venue default), linked device; usage per device app against the license limit (devices) - pair dialog: code as typed (normalized), app (only apps that are on, "all in use" when the limit is reached), name, sign-in mode for apps with a choice, optional linked device; license.limit_reached explained with the app's limit - edit (name, sign-in mode, link), change app (signs out whoever is on it), remove with confirm (the device goes back to pairing), show removed - manager_dashboard gets node --test (tests/platform.test.mjs: module states, dependents, device status/usage/room/links), added to run-all - e2e manager.mjs: 54 checks (+ wrong code, normalized code, pair, the device picks up its token, online, rename, change app, remove → device.revoked) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
349 lines
25 KiB
JavaScript
349 lines
25 KiB
JavaScript
// Manager suite (Playwright + Edge): the manager on the shared packages (plan steps 2.5, 2.6).
|
||
// The demo venue has no venue.language set, so the shell starts in English.
|
||
// Starts a backend on a demo-seeded database (:8057, licensing off) and the manager's Vite
|
||
// dev server (:5186) proxied to it. Checks login and the platform bootstrap through
|
||
// @xenia/api + @xenia/access, the 401 rules (wrong password / wrong PIN stay on screen, an
|
||
// ended session logs out), a module turned off, and the manager-approval dialog.
|
||
// node manager.mjs
|
||
import { spawn } from 'node:child_process'
|
||
import { openSync } from 'node:fs'
|
||
import path from 'node:path'
|
||
import { chromium } from 'playwright'
|
||
import { BROWSER_CHANNEL, CS, OUT, reporter, sleep } from './lib/env.mjs'
|
||
import { killTree, startBackend, waitHttp } from './lib/proc.mjs'
|
||
import { SUPPORT_MASTER, codeFor, siteSecret } from './lib/support.mjs'
|
||
|
||
const PORT = 5186
|
||
const APP = `http://localhost:${PORT}`
|
||
const backend = await startBackend({ port: 8057, siteId: 'test-site-1' })
|
||
const log = openSync(path.join(OUT, `vite-${PORT}.log`), 'w')
|
||
const vite = spawn('npx', ['vite', '--port', String(PORT), '--strictPort'], {
|
||
cwd: path.join(CS, 'manager_dashboard'), shell: process.platform === 'win32', detached: process.platform !== 'win32',
|
||
stdio: ['ignore', log, log], env: { ...process.env, XENIA_BACKEND: backend.url },
|
||
})
|
||
const browser = await chromium.launch(BROWSER_CHANNEL ? { channel: BROWSER_CHANNEL } : {})
|
||
const r = reporter('manager: login + bootstrap on @xenia/api / @xenia/access')
|
||
|
||
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
|
||
}
|
||
// A device's side of pairing (what the terminal app will do in phase 3)
|
||
async function deviceRequestsPairing() {
|
||
return api('POST', '/api/devices/pair-request')
|
||
}
|
||
async function devicePicksUpToken(req) {
|
||
const res = await fetch(`${backend.url}/api/devices/pair-request/${req.code}`, { headers: { 'X-Xenia-Pair-Secret': req.secret } })
|
||
return (await res.json()).device_token
|
||
}
|
||
const asDevice = (token, url) => fetch(`${backend.url}${url}`, { headers: { 'X-Xenia-Device': token } })
|
||
const loginApi = (username, secret) => api('POST', '/api/auth/login', { body: { username, ...secret } }).then(d => d.access_token)
|
||
|
||
async function uiLogin(page, password) {
|
||
await page.goto(`${APP}/login`)
|
||
await page.getByTestId('login-form').waitFor({ timeout: 60_000 })
|
||
if (!(await page.getByTestId('login-account').count())) await page.getByLabel('Username').fill('manager')
|
||
await page.getByLabel('Password').fill(password)
|
||
await page.getByRole('button', { name: 'Sign in', exact: true }).click()
|
||
}
|
||
const typePin = async (page, pin) => { for (const d of pin) { await page.keyboard.type(d); await sleep(80) } await page.keyboard.press('Enter') }
|
||
const setSetting = (token, key, value) => api('PUT', `/api/settings/${key}`, { token, body: { value } })
|
||
|
||
try {
|
||
await waitHttp(`${APP}/`)
|
||
const ctx = await browser.newContext({ viewport: { width: 1400, height: 900 } })
|
||
const page = await ctx.newPage()
|
||
const errors = []
|
||
page.on('pageerror', e => errors.push(e.message))
|
||
const bootstraps = []
|
||
page.on('request', q => { if (q.url().includes('/api/platform/bootstrap')) bootstraps.push(q.headers().authorization || 'anonymous') })
|
||
|
||
// ── Wrong password: the message shows, no reload ───────────────────────────
|
||
await uiLogin(page, 'wrong-password')
|
||
await page.evaluate(() => { window.__noReload = true })
|
||
await sleep(1500)
|
||
r.check('bootstrap loaded before login (anonymous)', bootstraps[0] === 'anonymous', bootstraps.join(','))
|
||
await page.screenshot({ path: path.join(OUT, 'manager-login-wrong-password.png') })
|
||
r.check('wrong password: error shown, page not reloaded',
|
||
await page.getByText('Wrong username or password').count() > 0 && await page.evaluate(() => window.__noReload === true))
|
||
|
||
// ── Right password: dashboard, bootstrap again with the session ────────────
|
||
await page.getByLabel('Password').fill('password')
|
||
await page.getByRole('button', { name: 'Sign in', exact: true }).click()
|
||
await page.waitForURL(u => u.pathname === '/dashboard', { timeout: 15_000 }).catch(() => {})
|
||
await sleep(1500)
|
||
r.check('login → /dashboard', new URL(page.url()).pathname === '/dashboard', page.url())
|
||
r.check('bootstrap reloaded with the session token', bootstraps.some(b => b.startsWith('Bearer ')), bootstraps.join(','))
|
||
const sidebar = page.getByTestId('sidebar')
|
||
r.check('sidebar built from modules + keys (Management, Reports)',
|
||
await sidebar.getByText('Management').count() > 0 && await sidebar.getByText('Reports').count() > 0)
|
||
await page.goto(`${APP}/management/products`)
|
||
await sleep(1500)
|
||
r.check('a guarded page renders for the manager', await page.getByTestId('route-guard').count() === 0 && (await page.evaluate(() => document.body.innerText.length)) > 200)
|
||
r.check('page title from the nav (Management › Products)', (await page.locator('header nav').first().innerText()).replace(/\s+/g, ' ').includes('Management Products'))
|
||
|
||
// ── 2.6a: a module turned off disappears from the sidebar and its route ────
|
||
const mgrNotes = await page.evaluate(() => localStorage.getItem('manager_token'))
|
||
await api('PUT', '/api/platform/modules/notes', { token: mgrNotes, body: { enabled: false } })
|
||
await page.goto(`${APP}/notes`)
|
||
await sleep(2000)
|
||
await sidebar.getByText('Management').click().catch(() => {})
|
||
r.check('notes off: no "Notes" in the sidebar', await sidebar.getByText('Notes', { exact: true }).count() === 0)
|
||
r.check('notes off: /notes shows "turned off"', await page.getByTestId('route-guard').getByText('This feature is turned off').count() === 1)
|
||
await api('PUT', '/api/platform/modules/notes', { token: mgrNotes, body: { enabled: true } })
|
||
await page.goto(`${APP}/notes`)
|
||
await sleep(2000)
|
||
r.check('notes on again: the page and the item are back', await page.getByTestId('route-guard').count() === 0 && await sidebar.getByText('Notes', { exact: true }).count() === 1)
|
||
|
||
// ── 2.6a: the profile menu language switch (live, then saved) ──────────────
|
||
await page.goto(`${APP}/dashboard`)
|
||
await sleep(1500)
|
||
await page.getByTestId('user-menu').click()
|
||
await page.getByRole('menuitemradio', { name: 'Ελληνικά' }).click()
|
||
await page.waitForFunction(() => document.querySelector('[data-testid="sidebar"]')?.innerText.includes('Διαχείριση'), null, { timeout: 5000 }).catch(() => {})
|
||
r.check('language → Ελληνικά: the shell changes live', await sidebar.getByText('Διαχείριση').count() > 0 && await page.evaluate(() => document.documentElement.lang) === 'el')
|
||
await page.reload()
|
||
await sleep(2500)
|
||
r.check('…and it was saved (still Greek after a reload)', await sidebar.getByText('Διαχείριση').count() > 0)
|
||
await page.getByTestId('user-menu').click()
|
||
await page.getByRole('menuitemradio', { name: 'English' }).click()
|
||
await page.waitForFunction(() => document.querySelector('[data-testid="sidebar"]')?.innerText.includes('Management'), null, { timeout: 5000 }).catch(() => {})
|
||
r.check('language → English again', await sidebar.getByText('Management').count() > 0)
|
||
|
||
// ── 2.6c: Apps page: module cards, toggles, dependency warning ─────────────
|
||
await page.goto(`${APP}/apps`)
|
||
await page.getByTestId('module-notes').waitFor({ timeout: 15_000 })
|
||
const card = key => page.getByTestId(`module-${key}`)
|
||
r.check('apps: cards for device apps, features and core', await card('kds').count() === 1 && await card('notes').count() === 1 && await card('core').count() === 1)
|
||
r.check('apps: core is "Always on" with no switch', await card('core').getAttribute('data-state') === 'always' && await card('core').getByRole('switch').count() === 0)
|
||
await card('notes').getByRole('switch').click()
|
||
await page.waitForFunction(() => document.querySelector('[data-testid="module-notes"]')?.dataset.state === 'off', null, { timeout: 8000 }).catch(() => {})
|
||
await sidebar.getByText('Management').click().catch(() => {})
|
||
r.check('apps: notes switched off from its card → state off, gone from the sidebar',
|
||
await card('notes').getAttribute('data-state') === 'off' && await sidebar.getByText('Notes', { exact: true }).count() === 0)
|
||
await card('notes').getByRole('switch').click()
|
||
await page.waitForFunction(() => document.querySelector('[data-testid="module-notes"]')?.dataset.state === 'on', null, { timeout: 8000 }).catch(() => {})
|
||
r.check('apps: notes back on', await card('notes').getAttribute('data-state') === 'on' && await sidebar.getByText('Notes', { exact: true }).count() === 1)
|
||
await card('ordering').getByRole('switch').click()
|
||
const stopped = await page.getByTestId('confirm-stopped').textContent({ timeout: 5000 }).catch(() => '')
|
||
r.check('apps: turning off Ordering first lists what stops with it', ['Kitchen display', 'Waiter app', 'Pricing'].every(n => stopped.includes(n)), stopped)
|
||
await page.getByRole('dialog').getByRole('button', { name: 'Cancel' }).click()
|
||
r.check('apps: Cancel leaves Ordering on', await card('ordering').getAttribute('data-state') === 'on')
|
||
r.check('apps: a card with a page has "Open" (KDS)', await card('kds').getByRole('button', { name: 'Open' }).count() === 1)
|
||
|
||
// ── 2.6c: Integrations and License ─────────────────────────────────────────
|
||
await page.goto(`${APP}/integrations`)
|
||
await card('fiscal').waitFor({ timeout: 15_000 })
|
||
r.check('integrations: only integrations (fiscal, caller ID)', await card('caller_id').count() === 1 && await card('kds').count() === 0)
|
||
await page.goto(`${APP}/license`)
|
||
await page.getByTestId('license-status').waitFor({ timeout: 20_000 })
|
||
r.check('license: development mode, site id, a row per module',
|
||
(await page.getByTestId('license-mode').textContent()).startsWith('Development mode')
|
||
&& (await page.getByTestId('license-site').textContent()) === 'test-site-1'
|
||
&& await page.locator('[data-testid^="license-row-"]').count() >= 20)
|
||
r.check('license: limits are shown (kitchen screens: unlimited in dev)', (await page.getByTestId('license-row-kds').textContent()).includes('Kitchen screens'))
|
||
await page.screenshot({ path: path.join(OUT, 'manager-license.png'), fullPage: true })
|
||
await page.goto(`${APP}/apps`)
|
||
await card('notes').waitFor()
|
||
await page.screenshot({ path: path.join(OUT, 'manager-apps.png'), fullPage: true })
|
||
|
||
// ── 2.6d: Devices: pair by code, edit, change app, remove ──────────────────
|
||
await page.goto(`${APP}/devices`)
|
||
await page.getByTestId('device-usage').waitFor({ timeout: 15_000 })
|
||
r.check('devices: empty state before any pairing', await page.getByText('No devices yet').count() === 1)
|
||
const dlg = page.getByRole('dialog')
|
||
await page.getByRole('button', { name: 'Pair a device' }).first().click()
|
||
await dlg.getByLabel('Code shown on the device').fill('zz-zz-zz')
|
||
await dlg.getByLabel('App to run').selectOption('kds')
|
||
await dlg.getByLabel('Name').fill('Kitchen')
|
||
await dlg.getByRole('button', { name: 'Pair a device' }).click()
|
||
await page.getByTestId('pair-error').waitFor({ timeout: 5000 }).catch(() => {})
|
||
r.check('devices: a wrong code is refused with a message', (await page.getByTestId('pair-error').textContent().catch(() => '')).includes('pairing code is wrong'))
|
||
const req = await deviceRequestsPairing()
|
||
const typed = `${req.code.slice(0, 3).toLowerCase()}-${req.code.slice(3).toLowerCase()}`
|
||
await dlg.getByLabel('Code shown on the device').fill(typed)
|
||
r.check('devices: the code is normalized as typed (lower case, dash)', await dlg.getByLabel('Code shown on the device').inputValue() === req.code)
|
||
await dlg.getByLabel('How staff sign in').selectOption('pin')
|
||
await dlg.getByRole('button', { name: 'Pair a device' }).click()
|
||
await page.waitForFunction(() => document.querySelectorAll('[data-testid^="device-"][data-status]').length === 1, null, { timeout: 8000 }).catch(() => {})
|
||
const row = page.locator('[data-testid^="device-"][data-status]').first()
|
||
r.check('devices: paired → listed as "waiting for the device"', await row.getAttribute('data-status') === 'waiting' && (await row.textContent()).includes('Kitchen'))
|
||
const deviceToken = await devicePicksUpToken(req)
|
||
const me = await asDevice(deviceToken, '/api/devices/me').then(x => x.json())
|
||
r.check('devices: the device got its token and runs the kitchen display', me.app_key === 'kds' && me.name === 'Kitchen' && me.login_mode === 'pin', JSON.stringify(me))
|
||
await page.reload()
|
||
await row.waitFor({ timeout: 10_000 })
|
||
r.check('devices: after it connected → "Online", sign-in PIN', await row.getAttribute('data-status') === 'online' && (await row.textContent()).includes('PIN'))
|
||
r.check('devices: usage shows kitchen screens in use', (await page.getByTestId('device-usage').textContent()).includes('Kitchen screens: 1'))
|
||
|
||
await row.getByRole('button', { name: 'Edit' }).click()
|
||
await dlg.getByLabel('Name').fill('Kitchen 1')
|
||
await dlg.getByRole('button', { name: 'Save' }).click()
|
||
await page.waitForFunction(() => document.querySelector('[data-testid^="device-"][data-status]')?.textContent.includes('Kitchen 1'), null, { timeout: 8000 }).catch(() => {})
|
||
r.check('devices: renamed', (await row.textContent()).includes('Kitchen 1') && (await asDevice(deviceToken, '/api/devices/me').then(x => x.json())).name === 'Kitchen 1')
|
||
|
||
await row.getByRole('button', { name: 'Change app' }).click()
|
||
await dlg.getByLabel('App to run').selectOption('pos_counter')
|
||
await dlg.getByRole('button', { name: 'Save' }).click()
|
||
await page.waitForFunction(() => document.querySelector('[data-testid^="device-"][data-status]')?.textContent.includes('Counter POS'), null, { timeout: 8000 }).catch(() => {})
|
||
r.check('devices: app changed to Counter POS (the device follows)', (await row.textContent()).includes('Counter POS') && (await asDevice(deviceToken, '/api/devices/me').then(x => x.json())).app_key === 'pos_counter')
|
||
await page.screenshot({ path: path.join(OUT, 'manager-devices.png'), fullPage: true })
|
||
|
||
await row.getByRole('button', { name: 'Remove' }).click()
|
||
await dlg.getByRole('button', { name: 'Remove' }).click()
|
||
await page.getByText('No devices yet').waitFor({ timeout: 8000 }).catch(() => {})
|
||
const after = await asDevice(deviceToken, '/api/devices/me')
|
||
r.check('devices: removed → gone from the list, the device is refused (device.revoked)',
|
||
await page.getByText('No devices yet').count() === 1 && after.status === 401 && (await after.json()).error.code === 'device.revoked')
|
||
await page.getByLabel('Show removed devices').check()
|
||
await page.waitForFunction(() => document.querySelector('[data-testid^="device-"][data-status="removed"]'), null, { timeout: 8000 }).catch(() => {})
|
||
r.check('devices: "show removed" lists it as removed', await page.locator('[data-status="removed"]').count() === 1)
|
||
|
||
// ── Lock screen: a wrong PIN stays locked (no logout), the right one unlocks ─
|
||
await page.goto(`${APP}/dashboard`)
|
||
await sleep(1500)
|
||
await page.locator('header button').filter({ has: page.locator('svg.lucide-lock') }).first().click()
|
||
await sleep(600)
|
||
const tokenBeforeLock = await page.evaluate(() => localStorage.getItem('manager_token'))
|
||
await typePin(page, '0000')
|
||
await sleep(1500)
|
||
await page.screenshot({ path: path.join(OUT, 'manager-lock-wrong-pin.png') })
|
||
r.check('lock: wrong PIN → "Wrong PIN. 4 tries left…", still signed in',
|
||
await page.getByTestId('lock-screen').getByText('Wrong PIN. 4 tries left').count() === 1 && !!(await page.evaluate(() => localStorage.getItem('manager_token'))) && new URL(page.url()).pathname === '/dashboard')
|
||
await typePin(page, '1234')
|
||
await sleep(2000)
|
||
r.check('lock: right PIN unlocks the SAME session (token unchanged)',
|
||
await page.getByTestId('lock-screen').count() === 0 && new URL(page.url()).pathname === '/dashboard'
|
||
&& await page.evaluate(() => localStorage.getItem('manager_token')) === tokenBeforeLock)
|
||
|
||
// ── A module turned off: /kds shows the "turned off" screen ────────────────
|
||
const mgr = await page.evaluate(() => localStorage.getItem('manager_token'))
|
||
await api('PUT', '/api/platform/modules/kds', { token: mgr, body: { enabled: false } })
|
||
await page.goto(`${APP}/kds`)
|
||
await sleep(2000)
|
||
r.check('kds off → "feature turned off" screen', await page.locator('.x-empty__title').count() === 1, await page.locator('body').innerText().then(t => t.slice(0, 80)))
|
||
await api('PUT', '/api/platform/modules/kds', { token: mgr, body: { enabled: true } })
|
||
await page.goto(`${APP}/kds`)
|
||
await sleep(2000)
|
||
r.check('kds on again → the KDS renders', await page.locator('.x-empty__title').count() === 0)
|
||
|
||
// ── 2.6b: five wrong PINs on the lock screen end the session ───────────────
|
||
await page.goto(`${APP}/dashboard`)
|
||
await sleep(1500)
|
||
await page.locator('header button').filter({ has: page.locator('svg.lucide-lock') }).first().click()
|
||
await sleep(500)
|
||
for (let i = 0; i < 5; i++) { await typePin(page, '0000'); await sleep(900) }
|
||
await page.waitForURL(u => u.pathname === '/login', { timeout: 10_000 }).catch(() => {})
|
||
const endedToken = mgr
|
||
r.check('lock: 5 wrong PINs → signed out', new URL(page.url()).pathname === '/login' && !(await page.evaluate(() => localStorage.getItem('manager_token'))), page.url())
|
||
r.check('…and that session is really over on the server', await api('GET', '/api/auth/me', { token: endedToken }).then(() => false, e => e.message.includes('401')))
|
||
|
||
// ── 2.6b: PIN login mode ───────────────────────────────────────────────────
|
||
const owner = await loginApi('manager', { password: 'password', client: 'manager' })
|
||
await setSetting(owner, 'auth.manager_login_mode', 'pin')
|
||
await page.goto(`${APP}/login`)
|
||
await page.getByTestId('login-form').waitFor()
|
||
if (!(await page.getByTestId('login-account').count())) await page.getByLabel('Username').fill('manager')
|
||
r.check('pin mode: the login shows a PIN pad, no password field', await page.locator('.x-pinpad').count() === 1 && await page.getByLabel('Password').count() === 0)
|
||
await page.locator('.x-pinpad').getByRole('button', { name: '1', exact: true }).click()
|
||
for (const d of '234') await page.locator('.x-pinpad').getByRole('button', { name: d, exact: true }).click()
|
||
await page.locator('.x-pinpad').getByRole('button', { name: 'OK' }).click()
|
||
await page.waitForURL(u => u.pathname === '/dashboard', { timeout: 10_000 }).catch(() => {})
|
||
r.check('pin mode: PIN → /dashboard', new URL(page.url()).pathname === '/dashboard', page.url())
|
||
await setSetting(owner, 'auth.manager_login_mode', 'password')
|
||
const mgr2 = await page.evaluate(() => localStorage.getItem('manager_token'))
|
||
await page.evaluate(() => { localStorage.removeItem('manager_token') })
|
||
await page.goto('about:blank') // leave first: the open page would redirect itself on the 401
|
||
await api('POST', '/api/auth/logout', { token: mgr2 })
|
||
|
||
// ── 2.6b: support access from the login screen ─────────────────────────────
|
||
await page.goto(`${APP}/login`)
|
||
await page.getByTestId('login-title').waitFor()
|
||
await page.getByRole('button', { name: 'Support access' }).click()
|
||
const challenge = (await page.getByTestId('support-challenge').textContent({ timeout: 10_000 })).trim()
|
||
await page.screenshot({ path: path.join(OUT, 'manager-support-access.png') })
|
||
r.check('support: a challenge code is shown', /^[A-Z0-9]{4}-[A-Z0-9]{4}$/.test(challenge), challenge)
|
||
await page.getByLabel("Support's code").fill('WRONGCODE1')
|
||
await page.getByRole('button', { name: 'Open support session' }).click()
|
||
await sleep(1000)
|
||
r.check('support: a wrong code is refused with a message', await page.getByText('That support code is wrong').count() === 1)
|
||
await page.getByLabel("Support's code").fill(codeFor(siteSecret(SUPPORT_MASTER, 'test-site-1'), 'test-site-1', challenge))
|
||
await page.getByRole('button', { name: 'Open support session' }).click()
|
||
await page.waitForURL(u => u.pathname === '/dashboard', { timeout: 10_000 }).catch(() => {})
|
||
r.check('support: the right code opens a session', new URL(page.url()).pathname === '/dashboard', page.url())
|
||
await page.evaluate(() => { localStorage.removeItem('manager_token') })
|
||
await uiLogin(page, 'password')
|
||
await page.waitForURL(u => u.pathname === '/dashboard', { timeout: 10_000 }).catch(() => {})
|
||
const mgr3 = await page.evaluate(() => localStorage.getItem('manager_token'))
|
||
|
||
// ── Session ended elsewhere: the next request logs out ─────────────────────
|
||
await page.goto(`${APP}/dashboard`)
|
||
await sleep(1500)
|
||
await api('POST', '/api/auth/logout', { token: mgr3 })
|
||
await sidebar.getByText('Reports').first().click()
|
||
await sidebar.getByText('Today').first().click()
|
||
await page.waitForURL(u => u.pathname === '/login', { timeout: 10_000 }).catch(() => {})
|
||
r.check('session ended → back to /login, token cleared',
|
||
new URL(page.url()).pathname === '/login' && !(await page.evaluate(() => localStorage.getItem('manager_token'))), page.url())
|
||
|
||
// ── Manager approval (D-10): a waiter cancels without the key ──────────────
|
||
const boss = await loginApi('manager', { password: 'password' })
|
||
await api('POST', '/api/business-day/open', { token: boss, body: {} }).catch(() => {})
|
||
const waiter = await loginApi('demo_maria', { pin: '3302', client: 'waiter' })
|
||
await api('POST', '/api/shifts/start', { token: waiter, body: {} }).catch(() => {})
|
||
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: waiter, body: { table_id: table.id } })
|
||
const w = await (await browser.newContext({ viewport: { width: 1200, height: 900 } })).newPage()
|
||
w.on('pageerror', e => errors.push(e.message))
|
||
await w.goto(`${APP}/login`)
|
||
await w.evaluate(t => { localStorage.setItem('manager_token', t); localStorage.setItem('manager_username', 'demo_maria') }, waiter)
|
||
await w.goto(`${APP}/dashboard`)
|
||
await sleep(2500)
|
||
// ── 2.6a: a user without the keys: no item, and "no access" on the URL ─────
|
||
const wSidebar = w.getByTestId('sidebar')
|
||
r.check('waiter in the manager: no Reports / Management items', await wSidebar.getByText('Reports').count() === 0 && await wSidebar.getByText('Management').count() === 0)
|
||
await w.goto(`${APP}/reports/today`)
|
||
await sleep(2000)
|
||
r.check('waiter opens /reports/today by URL → "no access"', await w.getByTestId('route-guard').getByText("You don't have access to this page").count() === 1)
|
||
await w.goto(`${APP}/dashboard`)
|
||
await sleep(2000)
|
||
await w.evaluate(id => {
|
||
window.__res = window.__xeniaClient.delete(`/api/orders/${id}`) // cancel the whole order
|
||
.then(res => ({ ok: true, status: res.status }), e => ({ ok: false, code: e.xenia?.code, cancelled: !!e.xenia?.overrideCancelled }))
|
||
}, order.id)
|
||
const dialog = w.getByRole('dialog')
|
||
await dialog.waitFor({ timeout: 10_000 }).catch(() => {})
|
||
r.check('approval dialog opens for ordering.orders.cancel', await w.getByTestId('override-key').textContent().catch(() => '') === 'ordering.orders.cancel')
|
||
const inputs = dialog.locator('input')
|
||
await inputs.nth(0).fill('manager')
|
||
await inputs.nth(1).fill('0000')
|
||
await inputs.nth(1).press('Enter')
|
||
await sleep(1500)
|
||
r.check('wrong manager PIN: error in the dialog, waiter still signed in',
|
||
await dialog.locator('.x-field__error').count() === 1 && !!(await w.evaluate(() => localStorage.getItem('manager_token'))))
|
||
await w.screenshot({ path: path.join(OUT, 'manager-approval-wrong-pin.png') })
|
||
await inputs.nth(1).fill('1234')
|
||
await inputs.nth(1).press('Enter')
|
||
const result = await w.evaluate(() => window.__res)
|
||
r.check('right PIN: the cancel is retried and succeeds', result.ok === true && result.status === 204, JSON.stringify(result))
|
||
r.check('dialog closed', await w.getByRole('dialog').count() === 0)
|
||
const audit = await api('GET', '/api/access/overrides', { token: boss })
|
||
r.check('the approval is audited', audit[0]?.key === 'ordering.orders.cancel' && audit[0]?.target === `order:${order.id}` && !!audit[0]?.used_at, JSON.stringify(audit[0]))
|
||
await w.screenshot({ path: path.join(OUT, 'manager-after-approval.png') })
|
||
|
||
r.check('no page errors', errors.length === 0, errors.slice(0, 3).join(' | '))
|
||
} finally {
|
||
await browser.close()
|
||
killTree(vite)
|
||
await backend.stop()
|
||
}
|
||
process.exit(r.done() ? 1 : 0)
|