// Signage suite (plan step 5.5a): 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)') 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) // ── 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)