Files
xenia-pos-local/tests/e2e/signage.mjs
T
bonaminandClaude Opus 5.5 e90c2f4f8f feat(terminal): signage menu boards that page by themselves, and the widgets (platform-v2 step 5.5b)
- Menu board: the board's own order, grouped with headings or one list,
  photos, descriptions, badges (translated), sold out crossed out with a
  label, prices in the venue's money format, dotted leaders, a page dot row.
  Pages: rows that fit from the frame's shape (portrait fits more), text size,
  photos / descriptions, the title and widget bars (`page_size` overrides);
  they go round every `page_seconds` while the slide lasts.
- Widgets on text and menu slides: logo, name, date, clock, message of the
  day, QR code, weather (hidden without a forecast) in corners and bars, three
  sizes.
- logic.js: menuPages, rowsPerColumn, frameHeightVmin, boardPages, menuMs,
  pageAt, weatherIcon (+4 tests).
- e2e signage.mjs: a long board pages by itself, widgets in place, weather
  hidden with no place, the chosen product first with its badge, a price change
  and sold out show within seconds, a portrait TV needs fewer pages.

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

210 lines
15 KiB
JavaScript

// Signage suite (plan steps 5.5a, 5.5b): a TV paired as a signage screen plays its playlist (picture,
// text, video to its end, menu board) in order with transitions, turns for a TV mounted sideways,
// keeps playing with the server stopped (a quiet mark, no banner), and catches up when it is back.
// Starts its own backend (:8065) and the terminal (:5193).
// node signage.mjs
import { execFileSync } from 'node:child_process'
import path from 'node:path'
import { chromium } from 'playwright'
import { BROWSER_CHANNEL, OUT, PYTHON, reporter, sleep, waitFor } from './lib/env.mjs'
import { startBackend, startTerminalVite } from './lib/proc.mjs'
import { supportLogin } from './lib/support.mjs'
let backend = await startBackend({ port: 8065, siteId: 'test-site-1', env: { LICENSE_MODE: 'off' } })
const dataDir = backend.dataDir
const term = await startTerminalVite({ port: 5193, backendUrl: backend.url })
const browser = await chromium.launch(BROWSER_CHANNEL ? { channel: BROWSER_CHANNEL } : {})
const r = reporter('signage: the player on a TV (5.5a), menu boards and widgets (5.5b)')
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
}
async function upload(token, bytes, name, type) {
const form = new FormData()
form.append('file', new Blob([bytes], { type }), name)
const res = await fetch(`${backend.url}/api/media`, { method: 'POST', headers: { authorization: `Bearer ${token}` }, body: form })
return res.json()
}
const shown = (page, testId, timeout = 10_000) => page.getByTestId(testId).first().waitFor({ timeout }).then(() => true).catch(() => false)
const kindNow = page => page.getByTestId('signage-slide').getAttribute('data-kind', { timeout: 2000 }).catch(() => null)
/** The kinds the screen shows over `ms`, in order, without repeats next to each other. */
async function watch(page, ms) {
const seen = []
const end = Date.now() + ms
while (Date.now() < end) {
const k = await kindNow(page)
if (k && seen[seen.length - 1] !== k) seen.push(k)
await sleep(200)
}
return seen
}
try {
const boss = await supportLogin(backend.url)
await api('PUT', '/api/platform/modules/signage', { token: boss, body: { enabled: true } }).catch(() => {})
// a 2-second video recorded in the browser (WebM), a photo, a playlist of four slides
const rec = await browser.newPage()
const webm = Buffer.from(await rec.evaluate(async () => {
const c = document.createElement('canvas'); c.width = 320; c.height = 180
const g = c.getContext('2d')
const rec = new MediaRecorder(c.captureStream(25), { mimeType: 'video/webm' })
const parts = []
rec.ondataavailable = e => parts.push(e.data)
rec.start()
const t0 = performance.now()
await new Promise(done => {
const draw = () => {
const s = (performance.now() - t0) / 2000
g.fillStyle = `hsl(${Math.round(s * 360)},70%,50%)`; g.fillRect(0, 0, 320, 180)
if (s < 1) requestAnimationFrame(draw); else done()
}
draw()
})
rec.stop()
await new Promise(r => { rec.onstop = r })
const buf = await new Blob(parts, { type: 'video/webm' }).arrayBuffer()
return [...new Uint8Array(buf)]
}))
await rec.close()
const jpg = execFileSync(PYTHON, ['-c', "import io,sys;from PIL import Image;b=io.BytesIO();Image.new('RGB',(1920,1080),(200,40,40)).save(b,'JPEG');sys.stdout.buffer.write(b.getvalue())"])
const photo = await upload(boss, jpg, 'promo.jpg', 'image/jpeg')
const video = await upload(boss, webm, 'clip.webm', 'video/webm')
r.check('setup: a photo and a video in the library', photo.kind === 'image' && video.kind === 'video', `${photo.kind} ${video.kind} ${JSON.stringify(video).slice(0, 120)}`)
const pl = await api('POST', '/api/signage/playlists', { token: boss, body: { name: 'Window', orientation: 'landscape' } })
const add = body => api('POST', `/api/signage/playlists/${pl.id}/slides`, { token: boss, body })
await add({ kind: 'image', duration_s: 3, content: { media_id: photo.id } })
await add({ kind: 'text', duration_s: 3, transition: 'slide', content: { title: 'Happy hour', body: '17:00 - 19:00', background: { kind: 'colour', colour: '#1e3a8a' } } })
await add({ kind: 'video', content: { media_id: video.id } })
await add({ kind: 'menu', duration_s: 3, content: { title: 'Our menu', select: 'all', page_size: 12 } })
// ── the TV ────────────────────────────────────────────────────────────────
const tv = await (await browser.newContext({ viewport: { width: 1280, height: 720 } })).newPage()
tv.on('pageerror', e => errors.push(e.message))
await tv.goto(term.url)
await shown(tv, 'pairing-code', 20_000)
const code = await tv.getByTestId('pairing-code').getAttribute('data-code')
const dev = await api('POST', '/api/devices', { token: boss, body: { code, app_key: 'signage', name: 'Window TV' } })
r.check('a TV paired as a signage screen opens the player (nothing to play yet)', await shown(tv, 'app-signage', 20_000) && await shown(tv, 'signage-empty', 5000))
await api('PUT', `/api/signage/screens/${dev.id}`, { token: boss, body: { playlist_id: pl.id } })
r.check('…its playlist starts at once (signage.changed)', await waitFor(async () => (await tv.getByTestId('app-signage').getAttribute('data-playlist')) === String(pl.id), 15_000, 300))
const order = await watch(tv, 16_000)
const seq = order.join(' ')
r.check('the slides play in order: picture → text → video (to its end) → menu board → picture…',
/image text video menu/.test(seq) || /text video menu image/.test(seq) || /video menu image text/.test(seq) || /menu image text video/.test(seq), seq)
const img = await waitFor(async () => (await kindNow(tv)) === 'image' ? tv.getByTestId('signage-image').evaluate(i => i.naturalWidth) : null, 20_000, 200)
r.check('the picture shows (from the copy kept on the TV)', img > 0 && (await tv.getByTestId('signage-image').getAttribute('src')).startsWith('blob:'), String(img))
await tv.screenshot({ path: path.join(OUT, 'signage-image.png') })
await waitFor(async () => (await kindNow(tv)) === 'text', 20_000, 150)
r.check('the text slide comes in with its transition (slide)', (await tv.getByTestId('signage-slide').getAttribute('class')).includes('sg-in-slide')
&& (await tv.getByTestId('signage-title').textContent()) === 'Happy hour')
await sleep(900)
await tv.screenshot({ path: path.join(OUT, 'signage-text.png') })
await waitFor(async () => (await kindNow(tv)) === 'menu', 20_000, 150)
r.check('the menu board lists the products with prices', await tv.getByTestId('signage-menu-item').count() > 0 && await tv.getByTestId('signage-menu-price').count() > 0)
await tv.screenshot({ path: path.join(OUT, 'signage-menu.png') })
// ── a TV mounted sideways ─────────────────────────────────────────────────
await api('PUT', `/api/signage/screens/${dev.id}`, { token: boss, body: { rotation: 90 } })
r.check('turn the picture 90°: the frame turns', await waitFor(async () => (await tv.getByTestId('signage-frame').getAttribute('data-rotation')) === '90', 15_000, 300))
await tv.screenshot({ path: path.join(OUT, 'signage-rotated.png') })
await api('PUT', `/api/signage/screens/${dev.id}`, { token: boss, body: { rotation: 0 } })
await waitFor(async () => (await tv.getByTestId('signage-frame').getAttribute('data-rotation')) === '0', 15_000, 300)
// ── 5.5b: a menu board that pages by itself, live prices, sold out, widgets ──
await api('PUT', '/api/signage/message', { token: boss, body: { text: 'Fresh fish today' } })
const products = (await api('GET', '/api/products/?sold_at=in_store', { token: boss })).filter(p => !p.is_service_item && p.lifecycle_status === 'active')
const star = products[0]
const board = await api('POST', '/api/signage/playlists', { token: boss, body: { name: 'Menu board', orientation: 'landscape' } })
await api('POST', `/api/signage/playlists/${board.id}/slides`, { token: boss, body: { kind: 'menu', duration_s: 60, content: {
title: 'Our menu', select: 'all', show_descriptions: true, page_seconds: 4, product_order: [star.id], group_by_category: false, columns: 1,
badges: { [String(star.id)]: 'chef' }, background: { kind: 'colour', colour: '#14213d' }, accent_colour: '#fca311',
widgets: [{ kind: 'name', position: 'top-left' }, { kind: 'clock', position: 'top-right' }, { kind: 'message', position: 'bottom-bar' },
{ kind: 'date', position: 'bottom-bar' }, { kind: 'weather', position: 'top-center' }] } } })
await api('PUT', `/api/signage/screens/${dev.id}`, { token: boss, body: { playlist_id: board.id } })
await waitFor(async () => (await kindNow(tv)) === 'menu' && (await tv.getByTestId('app-signage').getAttribute('data-playlist')) === String(board.id), 20_000, 300)
const pagesWide = Number(await tv.getByTestId('signage-menu').getAttribute('data-pages'))
const firstPage = await tv.getByTestId('signage-menu').getAttribute('data-page')
const flipped = await waitFor(async () => (await tv.getByTestId('signage-menu').getAttribute('data-page')) !== firstPage, 12_000, 300)
r.check(`5.5b: a long menu pages by itself (${pagesWide} pages on a wide TV)`, pagesWide > 1 && !!flipped, `${pagesWide} pages`)
r.check('5.5b: widgets in their places: the message of the day and the date in the bottom bar, the clock, the name',
(await tv.getByTestId('signage-bottom-bar').textContent()).includes('Fresh fish today') && await tv.getByTestId('signage-w-clock').count() === 1
&& await tv.getByTestId('signage-w-date').count() === 1)
r.check('5.5b: no weather place set → the weather widget is hidden', await tv.getByTestId('signage-w-weather').count() === 0)
await waitFor(async () => (await tv.getByTestId('signage-menu').getAttribute('data-page')) === '0', 20_000, 200)
r.check('5.5b: the chosen product first, with its badge', await tv.getByTestId('signage-menu-item').first().getAttribute('data-id') === String(star.id)
&& (await tv.getByTestId('signage-menu-item').first().textContent()).includes("Chef's pick"))
await tv.screenshot({ path: path.join(OUT, 'signage-menu-board.png') })
// a price change and a product going sold out show without a reload
await api('PUT', `/api/products/${star.id}`, { token: boss, body: { base_price: 9.9 } })
const priced = await waitFor(async () => {
await waitFor(async () => (await tv.getByTestId('signage-menu').getAttribute('data-page')) === '0', 20_000, 200)
const t = await tv.locator(`[data-testid="signage-menu-item"][data-id="${star.id}"]`).textContent().catch(() => '')
return t.includes('9.90') ? t : null
}, 30_000, 500)
r.check('5.5b: a price change shows on the board within seconds', !!priced, priced || '')
await api('PUT', `/api/products/${star.id}`, { token: boss, body: { is_available: false } })
const out = await waitFor(async () => {
await waitFor(async () => (await tv.getByTestId('signage-menu').getAttribute('data-page')) === '0', 20_000, 200)
return (await tv.locator(`[data-testid="signage-menu-item"][data-id="${star.id}"]`).getAttribute('data-available').catch(() => null)) === 'false' ? true : null
}, 30_000, 500)
r.check('5.5b: …and a product going sold out is crossed out', !!out && await tv.getByTestId('signage-sold-out').count() > 0)
await api('PUT', `/api/products/${star.id}`, { token: boss, body: { is_available: true } })
// the same board on a portrait TV fits more on each page
const tall = await (await browser.newContext({ viewport: { width: 720, height: 1280 } })).newPage()
tall.on('pageerror', e => errors.push(e.message))
await tall.goto(term.url)
await shown(tall, 'pairing-code', 20_000)
const code2 = await tall.getByTestId('pairing-code').getAttribute('data-code')
const dev2 = await api('POST', '/api/devices', { token: boss, body: { code: code2, app_key: 'signage', name: 'Tall TV' } })
await api('PUT', `/api/signage/screens/${dev2.id}`, { token: boss, body: { playlist_id: board.id, orientation: 'portrait' } })
await waitFor(async () => (await tall.getByTestId('signage-slide').getAttribute('data-kind').catch(() => null)) === 'menu', 20_000, 300)
const pagesTall = Number(await tall.getByTestId('signage-menu').getAttribute('data-pages'))
r.check('5.5b: on a portrait TV the same board needs fewer pages', pagesTall < pagesWide, `${pagesTall} < ${pagesWide}`)
await tall.screenshot({ path: path.join(OUT, 'signage-menu-portrait.png') })
await tall.context().close()
await api('PUT', `/api/signage/screens/${dev.id}`, { token: boss, body: { playlist_id: pl.id } })
await waitFor(async () => (await tv.getByTestId('app-signage').getAttribute('data-playlist')) === String(pl.id), 15_000, 300)
// ── the server stops: the TV keeps playing ────────────────────────────────
await backend.stop()
const offlineOrder = await watch(tv, 14_000)
r.check('server stopped: the TV keeps playing its slides', new Set(offlineOrder).size >= 3, offlineOrder.join(' '))
r.check('…with a small mark, never the banner over the slides', await shown(tv, 'offline-mark', 15_000) && await tv.getByTestId('offline-banner').count() === 0)
const offImg = await waitFor(async () => (await kindNow(tv)) === 'image' ? tv.getByTestId('signage-image').evaluate(i => i.complete && i.naturalWidth) : null, 20_000, 200)
r.check('…and the picture still shows', offImg > 0, String(offImg))
// ── back: it catches up ──────────────────────────────────────────────────
backend = await startBackend({ port: 8065, siteId: 'test-site-1', dataDir, env: { LICENSE_MODE: 'off' } })
const boss2 = await supportLogin(backend.url)
await api('POST', `/api/signage/playlists/${pl.id}/slides`, { token: boss2, body: { kind: 'text', duration_s: 3, position: 0, content: { title: 'Back online' } } })
const caught = await waitFor(async () => {
const titles = await tv.getByTestId('signage-title').allTextContents().catch(() => [])
return titles.includes('Back online') ? true : null
}, 60_000, 300)
r.check('server back: the TV catches up (a slide added meanwhile shows)', !!caught)
r.check('…and the mark goes away', await tv.getByTestId('offline-mark').waitFor({ state: 'detached', timeout: 20_000 }).then(() => true).catch(() => false))
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()
term.stop()
await backend.stop()
}
process.exit(failures ? 1 : 0)