Files
xenia-pos-local/tests/e2e/ui.mjs
T
bonaminandClaude Opus 5.5 dbb8d5f0f4 feat(i18n): @xenia/i18n translations, language resolution and venue-time formatting
Platform-v2 step 2.4 (APPS_PLATFORM D-11, D-12, §7.2, §8):
- i18next 26.7.2 + react-i18next 17.1.13 (exact); en/el locale files
  (common, dev); createI18n, I18nProvider, useLanguage, setUserLanguage
  (screen first, then PATCH /api/auth/me through an app-provided save)
- resolveLanguage: user -> device -> venue -> en
- useFormat(): dates/times always in the venue zone from bootstrap (24 h,
  DST-safe, business dates never shifted), relative times, money and
  numbers per language; parseNumber accepts a comma or a dot
- scripts/check-i18n.mjs guard: keys missing in en, placeholder mismatches,
  dates formatted without the venue zone in new code (fails); missing el (warns)
- @xenia/ui components read their texts from UiLabelsProvider; useUiLabels()
  supplies the translated set
- dev-only /i18n-sample in the manager; ui.mjs e2e checks it with the browser
  in New York; unit tests in run-all; lockfile keeps both apps' versions
- new pack rule CS-17

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:51:11 +02:00

179 lines
11 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.
// Shared frontend packages (Playwright + Edge), on their dev-only pages served by the
// manager's Vite dev server (:5185). No backend needed.
// - @xenia/ui components on /ui-gallery (plan step 2.3)
// - @xenia/i18n on /i18n-sample (2.4): live language switch, venue time on a device
// in another zone (the browser runs in America/New_York), money/number formats,
// comma-or-dot price input, translated component labels
// node ui.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, waitHttp } from './lib/proc.mjs'
const PORT = 5185
const URL = `http://localhost:${PORT}/ui-gallery`
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],
})
const browser = await chromium.launch(BROWSER_CHANNEL ? { channel: BROWSER_CHANNEL } : {})
let r = reporter('ui: @xenia/ui components (gallery)')
let failures = 0
try {
await waitHttp(`http://localhost:${PORT}/`)
const p = await browser.newPage({ viewport: { width: 1280, height: 900 } })
const errs = []
p.on('pageerror', e => errs.push('pageerror: ' + e.message))
// The manager shell may call the API (no backend here): ignore failed requests only
p.on('console', m => { if (m.type() === 'error' && !/Failed to load resource/i.test(m.text())) errs.push(m.text().slice(0, 200)) })
const active = () => p.evaluate(() => { const a = document.activeElement; return a ? (a.getAttribute('aria-label') || a.textContent || a.tagName).trim().slice(0, 40) : null })
const btn = name => p.getByRole('button', { name, exact: true })
await p.goto(URL)
await p.waitForSelector('text=@xenia/ui gallery', { timeout: 60_000 })
// Dialog
await btn('Open dialog').click()
const dlg = p.getByRole('dialog', { name: 'Delete product?' })
r.check('dialog: opens with role + accessible name', await dlg.isVisible())
r.check('dialog: focuses the first field, not the ×', await p.evaluate(() => document.activeElement?.tagName) === 'INPUT')
for (let i = 0; i < 6; i++) await p.keyboard.press('Tab')
r.check('dialog: Tab stays inside', await p.evaluate(() => !!document.activeElement?.closest('[role="dialog"]')))
r.check('dialog: page scroll locked', await p.evaluate(() => document.body.style.overflow) === 'hidden')
await p.keyboard.press('Escape')
r.check('dialog: Escape closes, focus back on the opener', !(await dlg.isVisible()) && (await active()) === 'Open dialog')
r.check('dialog: scroll unlocked', await p.evaluate(() => document.body.style.overflow) === '')
await btn('Open dialog').click()
await p.mouse.click(10, 10)
r.check('dialog: backdrop click closes', !(await dlg.isVisible()))
await btn('Non-dismissible dialog').click()
await p.keyboard.press('Escape'); await p.mouse.click(10, 10)
const locked = p.getByRole('dialog', { name: 'Finish the close of day' })
r.check('dialog: non-dismissible ignores Escape/backdrop, no ×', await locked.isVisible() && (await locked.getByRole('button', { name: 'Close' }).count()) === 0)
await locked.getByRole('button', { name: 'Done' }).click()
// Sheet
await btn('Bottom sheet').click()
const sheet = p.getByRole('dialog', { name: 'Move order' })
await sheet.getByRole('button', { name: 'Close' }).click()
r.check('sheet: × closes', !(await sheet.isVisible()))
// Toast
await btn('Toast: success').click()
await btn('Toast: danger').click()
r.check('toast: success is a polite status, danger an alert',
await p.locator('.x-toast--success[role="status"]').count() === 1 && await p.locator('.x-toast--danger[role="alert"]').count() === 1)
await btn('Toast: action').click()
await p.getByRole('button', { name: 'Undo' }).click()
r.check('toast: action runs and dismisses', await p.locator('.x-toast', { hasText: 'Undone' }).count() === 1 && await p.locator('.x-toast', { hasText: 'Order moved' }).count() === 0)
await sleep(4500)
r.check('toast: auto-dismisses', await p.locator('.x-toast', { hasText: 'Saved' }).count() === 0)
// Field
const price = p.getByLabel('Price')
r.check('field: error wired (aria-invalid + aria-describedby)', await price.getAttribute('aria-invalid') === 'true' &&
await p.evaluate(id => document.getElementById(id)?.textContent, await price.getAttribute('aria-describedby')) === 'Enter a price, e.g. 3,50')
const name = p.getByLabel('Product name *')
r.check('field: required + hint wired', await name.getAttribute('required') !== null &&
(await p.evaluate(id => document.getElementById(id)?.textContent, await name.getAttribute('aria-describedby'))).startsWith('Shown on'))
// Switch
const sw = p.getByRole('switch', { name: 'Alert after print failures' })
await sw.click()
r.check('switch: toggles aria-checked', await sw.getAttribute('aria-checked') === 'true')
await p.getByText('Print kitchen tickets').click()
r.check('switch: the label toggles it', await p.getByRole('switch', { name: 'Print kitchen tickets' }).getAttribute('aria-checked') === 'false')
// InfoTip
const tip = p.getByRole('button', { name: 'More info' }).first()
await tip.click()
r.check('infotip: opens', await p.getByRole('tooltip').isVisible())
await p.keyboard.press('Escape')
r.check('infotip: Escape closes, focus back', !(await p.getByRole('tooltip').isVisible()) && (await active()) === 'More info')
await tip.click(); await p.mouse.click(5, 300)
r.check('infotip: click outside closes', !(await p.getByRole('tooltip').isVisible()))
// PinPad
const pad = p.locator('.x-pinpad')
await pad.scrollIntoViewIfNeeded()
for (const d of '9999') await pad.getByRole('button', { name: d, exact: true }).click()
await sleep(900)
r.check('pinpad: wrong PIN → error, cleared', await pad.getByRole('alert').textContent() === 'Wrong PIN' && await pad.locator('.x-pinpad__dot--filled').count() === 0)
await pad.getByRole('button', { name: '1', exact: true }).click()
await pad.getByRole('button', { name: 'Delete' }).click()
r.check('pinpad: backspace', await pad.locator('.x-pinpad__dot--filled').count() === 0)
await p.locator('body').click({ position: { x: 5, y: 5 } })
await p.keyboard.type('1234')
await sleep(900)
r.check('pinpad: right PIN from the keyboard', (await p.getByTestId('pin-result').textContent()).startsWith('Unlocked'))
await name.fill('')
await name.type('12')
r.check('pinpad: typing in a field does not feed it', await pad.locator('.x-pinpad__dot--filled').count() === 0)
// Themes
await p.getByLabel('Theme', { exact: true }).selectOption('dark')
await p.getByLabel('Venue theme').selectOption('amber')
await sleep(500) // buttons fade between colours
const bg = await p.evaluate(() => getComputedStyle(document.querySelector('.x-btn--primary')).backgroundColor)
r.check('theme: dark + venue amber change the accent', bg === 'rgb(245, 158, 11)', bg)
await p.screenshot({ path: path.join(OUT, 'ui-gallery-dark-amber.png'), fullPage: true })
await p.evaluate(() => { history.pushState({}, '', '/login'); dispatchEvent(new PopStateEvent('popstate')) })
await sleep(500)
r.check('theme: leaving the gallery restores <html>', await p.evaluate(() => !document.documentElement.hasAttribute('data-theme') && !document.documentElement.hasAttribute('data-venue-theme')))
r.check('no page or console errors', errs.length === 0, errs.slice(0, 3).join(' | '))
failures += r.done()
// ── @xenia/i18n ──────────────────────────────────────────────────────────
r = reporter('ui: @xenia/i18n (sample, device in New York)')
const ctx = await browser.newContext({ timezoneId: 'America/New_York', locale: 'en-US', viewport: { width: 1100, height: 1000 } })
const q = await ctx.newPage()
const qerrs = []
q.on('pageerror', e => qerrs.push('pageerror: ' + e.message))
q.on('console', m => { if (m.type() !== 'error' && m.type() !== 'warning') return; if (!/Failed to load resource/i.test(m.text())) qerrs.push(m.text().slice(0, 200)) })
const text = id => q.getByTestId(id).textContent()
await q.goto(`http://localhost:${PORT}/i18n-sample`)
await q.waitForSelector('[data-testid="title"]', { timeout: 60_000 })
r.check('device really is in New York', (await text('device-zone')) === 'America/New_York', await text('device-zone'))
r.check('venue zone = Athens (bootstrap default)', (await text('venue-zone')).startsWith('Europe/Athens'))
r.check('10:00 UTC in July shows 13:00 venue time (NY would be 06:00)', (await text('fixed-time')) === '01/07/2026, 13:00', await text('fixed-time'))
r.check('business date is not shifted', /Friday.*13.*November.*2026/.test(await text('business-date')), await text('business-date'))
r.check('en: money €1,234.50', (await text('money')) === '€1,234.50', await text('money'))
r.check('en: plural', (await text('plural')) === '3 items in the order')
r.check('en: relative time', (await text('relative')) === '5 minutes ago', await text('relative'))
await q.evaluate(() => { window.__noReload = true })
await q.getByLabel('Language').selectOption('el')
await q.waitForFunction(() => document.querySelector('[data-testid="title"]').textContent === 'Γλώσσες και μορφές', null, { timeout: 5000 }).catch(() => {})
r.check('switch to Ελληνικά: texts change live, no reload',
(await text('title')) === 'Γλώσσες και μορφές' && await q.evaluate(() => window.__noReload === true))
r.check('<html lang> follows', await q.evaluate(() => document.documentElement.lang) === 'el')
r.check('el: interpolation', (await text('greeting')) === 'Γεια σου, Maria!')
r.check('el: money 1.234,50 €', (await text('money')).replace(/[\u00a0\u202f]/g, ' ') === '1.234,50 €', await text('money'))
r.check('el: number 1.234,57', (await text('number')) === '1.234,57')
r.check('el: relative time', (await text('relative')) === 'πριν από 5 λεπτά', await text('relative'))
r.check('el: same venue time', (await text('fixed-time')) === '01/07/2026, 13:00')
const input = q.getByTestId('price-input')
for (const [typed, want] of [['3,50', '3,50 €'], ['3.50', '3,50 €'], ['1.234,5', '1.234,50 €'], ['abc', 'δεν είναι αριθμός']]) {
await input.fill(typed)
const hint = (await q.locator('.x-field__hint').textContent()).replace(/[\u00a0\u202f]/g, ' ')
r.check(`price input "${typed}" → ${want}`, hint.endsWith(want), hint)
}
await q.getByRole('button', { name: 'Άνοιγμα παραθύρου' }).click()
r.check('ui labels translated (dialog ×, (i), spinner)',
await q.getByRole('dialog').getByRole('button', { name: 'Κλείσιμο' }).count() === 1
&& await q.getByRole('button', { name: 'Περισσότερες πληροφορίες' }).count() === 1
&& (await q.locator('.x-spinner .x-visually-hidden').textContent()) === 'Φόρτωση')
await q.screenshot({ path: path.join(OUT, 'i18n-sample-el.png'), fullPage: true })
r.check('no page errors or missing-key warnings', qerrs.length === 0, qerrs.slice(0, 3).join(' | '))
failures += r.done()
await ctx.close()
} finally {
await browser.close()
killTree(vite)
}
process.exit(failures ? 1 : 0)