Management → Signage: playlists (name, landscape / portrait, default transition, duplicate, delete with the screens it plays on); the timeline (drag or arrows to reorder, duplicate, remove, on / off, time and when it shows at a glance, a round's total time); a slide's settings (name, seconds or a video to its end, transition, days / hours / dates, the picture or video, fit, text, alignment, font, colour, size, background colour or photo with darkening; a menu board's title, columns and colours) with a live preview drawn by @xenia/signage at 1920 × 1080 and scaled down; new slides empty or from templates (promotion, announcement, happy hour 17-19, welcome, menu board, video); a warning on the slide and its card when a picture or video doesn't fit the playlist's shape. Unit tests (4), e2e signage.mjs builds a playlist in the manager and the TV plays it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
272 lines
20 KiB
JavaScript
272 lines
20 KiB
JavaScript
// Signage suite (plan steps 5.5a, 5.5b, 5.6a): 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, spawn } from 'node:child_process'
|
|
import { openSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { chromium } from 'playwright'
|
|
import { BROWSER_CHANNEL, CS, OUT, PYTHON, reporter, sleep, waitFor } from './lib/env.mjs'
|
|
import { killTree, startBackend, startTerminalVite, waitHttp } 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 MPORT = 5194
|
|
const MANAGER = `http://localhost:${MPORT}`
|
|
const mlog = openSync(path.join(OUT, `vite-${MPORT}.log`), 'w')
|
|
const managerVite = spawn('npx', ['vite', '--port', String(MPORT), '--strictPort'], {
|
|
cwd: path.join(CS, 'manager_dashboard'), shell: process.platform === 'win32', detached: process.platform !== 'win32',
|
|
stdio: ['ignore', mlog, mlog], env: { ...process.env, XENIA_BACKEND: 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), the editor (5.6a)')
|
|
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)
|
|
|
|
// ── 5.6a: the editor in the manager ──────────────────────────────────────
|
|
await waitHttp(`${MANAGER}/`)
|
|
const tallJpg = execFileSync(PYTHON, ['-c', "import io,sys;from PIL import Image;b=io.BytesIO();Image.new('RGB',(1080,1920),(30,140,90)).save(b,'JPEG');sys.stdout.buffer.write(b.getvalue())"])
|
|
const tallPhoto = await upload(boss, tallJpg, 'tall poster.jpg', 'image/jpeg')
|
|
const mgr = await (await browser.newContext({ viewport: { width: 1440, height: 900 } })).newPage()
|
|
mgr.on('pageerror', e => errors.push(e.message))
|
|
await mgr.goto(`${MANAGER}/login`)
|
|
await mgr.getByTestId('login-form').waitFor({ timeout: 60_000 })
|
|
if (!(await mgr.getByTestId('login-account').count())) await mgr.getByLabel('Username').fill('manager')
|
|
await mgr.getByLabel('Password').fill('password')
|
|
await mgr.getByRole('button', { name: 'Sign in', exact: true }).click()
|
|
await mgr.waitForURL(u => u.pathname !== '/login', { timeout: 20_000 })
|
|
await mgr.goto(`${MANAGER}/management/signage`)
|
|
r.check('5.6a: Management → Signage lists the playlists', await shown(mgr, 'signage-playlist-item', 20_000)
|
|
&& await mgr.getByTestId('signage-playlist-item').count() >= 2)
|
|
await mgr.getByTestId('signage-new-playlist').click()
|
|
await mgr.getByTestId('signage-new-name').fill('Opening')
|
|
await mgr.getByTestId('signage-create-playlist').click()
|
|
await waitFor(async () => (await mgr.getByTestId('signage-playlist-name').inputValue().catch(() => '')) === 'Opening', 15_000, 200)
|
|
await mgr.getByRole('dialog').waitFor({ state: 'detached', timeout: 5000 }).catch(() => {})
|
|
await mgr.getByTestId('signage-add-slide').click()
|
|
r.check('5.6a: "Add a slide" offers empty slides and ready-made ones with a preview',
|
|
await mgr.getByTestId('signage-template-preview').count() >= 5)
|
|
await mgr.getByTestId('signage-template-announcement').click()
|
|
await mgr.getByTestId('signage-slide-editor').waitFor({ timeout: 10_000 })
|
|
await mgr.getByTestId('signage-text-title').fill('Opening soon')
|
|
r.check('5.6a: the preview follows the editor as you type (as the TV draws it)',
|
|
await waitFor(async () => (await mgr.getByTestId('signage-preview').getByTestId('signage-title').textContent().catch(() => '')) === 'Opening soon', 5000, 200))
|
|
await mgr.getByTestId('signage-slide-save').click()
|
|
await sleep(800)
|
|
await mgr.getByTestId('signage-add-slide').click()
|
|
await mgr.getByTestId('signage-template-promo').click()
|
|
await mgr.getByTestId('signage-picture-pick').click({ timeout: 10_000 })
|
|
await mgr.getByTestId('media-picker').locator(`[data-testid="media-item"][data-id="${tallPhoto.id}"]`).click()
|
|
r.check('5.6a: a tall picture on a landscape playlist is flagged', await shown(mgr, 'signage-picture-misfit', 5000))
|
|
await mgr.getByTestId('signage-slide-save').click()
|
|
await sleep(800)
|
|
r.check('5.6a: …and the timeline card shows the warning', await mgr.getByTestId('signage-card-misfit').count() === 1)
|
|
await mgr.screenshot({ path: path.join(OUT, 'signage-editor.png'), fullPage: true })
|
|
// the picture first: the arrow on its card
|
|
await mgr.getByTestId('signage-slide-card').nth(1).getByTestId('signage-up').click()
|
|
await sleep(800)
|
|
const opening = (await api('GET', '/api/signage/playlists', { token: boss })).find(p => p.name === 'Opening')
|
|
const full = await api('GET', `/api/signage/playlists/${opening.id}`, { token: boss })
|
|
r.check('5.6a: built in the manager: the picture, then the text "Opening soon"', full.slides.map(x => x.kind).join(',') === 'image,text'
|
|
&& full.slides[1].content.title === 'Opening soon' && full.slides[0].content.media_id === tallPhoto.id, JSON.stringify(full.slides.map(x => [x.kind, x.content.title])))
|
|
await api('PUT', `/api/signage/screens/${dev.id}`, { token: boss, body: { playlist_id: opening.id } })
|
|
const onTv = await waitFor(async () => ((await tv.getByTestId('signage-title').allTextContents().catch(() => [])).includes('Opening soon') ? true : null), 30_000, 300)
|
|
r.check('5.6a: …and the TV plays it', !!onTv)
|
|
await mgr.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()
|
|
killTree(managerVite)
|
|
await backend.stop()
|
|
}
|
|
process.exit(failures ? 1 : 0)
|