feat(terminal): the signage player: playlists on a TV, offline, transitions, rotation (platform-v2 step 5.5a)

- terminal/src/apps/signage: plays the screen's playlist (or the one its
  schedule picks now, switching by itself) full screen: slides in order with
  their time, a video to its end (a 15 min safety limit), only in their days /
  hours / dates; transitions fade / slide / zoom / none (CSS, reduced motion
  respected); the frame turned 90/180/270 for a TV mounted sideways. Picture,
  video and text slides drawn; a first menu board (5.5b lays it out fully).
- Offline: the last GET /api/signage/screen kept in localStorage, every
  picture and video (up to 120 MB) as an in-memory copy; reads again on
  signage.changed / device.updated / reconnect / every 5 min.
- logic.js: the server's schedule pick ported (same test cases), slide windows,
  next slide, frame; tests/signage.test.mjs (6).
- Signage screens get no offline banner over the slides, a small mark instead
  (registry QUIET_OFFLINE).
- e2e signage.mjs (in run-all): a paired TV plays picture → text → video →
  menu in order, the text's slide transition, rotation, keeps playing with the
  server stopped (mark, no banner), catches up when it is back. terminal.mjs
  passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 19:40:35 +02:00
co-authored by Claude Opus 5.5
parent 0d766d3c86
commit e612e2436c
13 changed files with 666 additions and 6 deletions
+1
View File
@@ -27,6 +27,7 @@ const suites = [
['manager (login, bootstrap, approvals)', 'node', ['manager.mjs'], E2E],
['media (Media page, product photos, waiter offline photos)', 'node', ['media.mjs'], E2E],
['terminal (pairing, sign-in, states)', 'node', ['terminal.mjs'], E2E],
['signage (the player on a TV)', 'node', ['signage.mjs'], E2E],
['order app (cloud: takeaway + delivery + bookings)', 'node', ['order_app.mjs'], E2E],
['QR menu at the table (cloud + venue)', 'node', ['qr_table.mjs'], E2E],
['stack (compose: terminal :8082, LAN only)', 'node', ['stack.mjs'], E2E],
+154
View File
@@ -0,0 +1,154 @@
// 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)