feat(terminal): older TVs get a legacy build with a compatibility layer; modern output unchanged
- @vitejs/plugin-legacy: browsers without import.meta.resolve (older than Chrome 105 / Firefox 106 / Safari 16.4) load a second copy built for Chrome 79 / Firefox 78 / Safari 13 with polyfills. The modern targets and the stylesheet stay on Vite's own defaults, so modern chunks are byte-identical to before (only plugin-legacy's 230-byte guard in the entry). - terminal/src/compat.js (legacy copy only): swaps in <name>.compat.css (lightningcss for the older targets), sets container-size variables for signage slides, emulates flex gap. - @xenia/signage cq()/CQ_BOX: container units as before where supported, calc(var()) fallback otherwise; EmbeddedSlide no longer uses inline `inset`. - Shared Notice (@xenia/ui); "Devices that work" in Pair a device and Signage -> Screens (EN/EL). - tests/e2e/compat.mjs: modern browser loads nothing extra; a simulated older TV runs the legacy copy and the layer, pairs, and plays a slide at the same sizes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
// Older TVs (2026-11-21): the terminal's production build has a second copy for older browsers (a TV's
|
||||
// built-in browser from 2021–2023), and modern browsers never load it. This suite builds the terminal
|
||||
// and serves the build. A modern browser must get the same files as always: no "-legacy" chunk, no
|
||||
// compat layer, no .compat.css. Then it plays an "older TV": the same page with the modern scripts
|
||||
// taken out, container units reported missing and the flex-gap fix forced. That run must load the legacy
|
||||
// copy and the compat layer, show the pairing screen, play the signage slide at the same sizes as
|
||||
// the modern browser, and space flex items itself.
|
||||
// A real Chrome 79 isn't driven here: syntax and missing APIs are the legacy build's job (Babel +
|
||||
// core-js for LEGACY_TARGETS in terminal/vite.config.js); what's checked is our layer.
|
||||
// node compat.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, reporter, waitFor } from './lib/env.mjs'
|
||||
import { killTree, startBackend, waitHttp } from './lib/proc.mjs'
|
||||
import { supportLogin } from './lib/support.mjs'
|
||||
|
||||
const PORT = 5196
|
||||
const DIST = path.join(OUT, 'compat-dist')
|
||||
const r = reporter('older TVs: the legacy build and the compat layer (terminal)')
|
||||
const errors = []
|
||||
|
||||
execFileSync('npx', ['vite', 'build', '--outDir', DIST, '--emptyOutDir'], { cwd: path.join(CS, 'terminal'), shell: process.platform === 'win32', stdio: 'ignore' })
|
||||
const backend = await startBackend({ port: 8066, siteId: 'test-site-1', env: { LICENSE_MODE: 'off' } })
|
||||
const log = openSync(path.join(OUT, `vite-${PORT}.log`), 'w')
|
||||
const preview = spawn('npx', ['vite', 'preview', '--outDir', DIST, '--port', String(PORT), '--strictPort'], {
|
||||
cwd: path.join(CS, 'terminal'), shell: process.platform === 'win32', detached: process.platform !== 'win32',
|
||||
stdio: ['ignore', log, log], env: { ...process.env, XENIA_BACKEND: backend.url },
|
||||
})
|
||||
const URL = `http://localhost:${PORT}`
|
||||
await waitHttp(URL + '/')
|
||||
const browser = await chromium.launch(BROWSER_CHANNEL ? { channel: BROWSER_CHANNEL } : {})
|
||||
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()
|
||||
if (!res.ok) throw new Error(`${method} ${url} → ${res.status} ${text.slice(0, 200)}`)
|
||||
return JSON.parse(text)
|
||||
}
|
||||
const shown = (page, testId, timeout = 15_000) => page.getByTestId(testId).first().waitFor({ timeout }).then(() => true).catch(() => false)
|
||||
|
||||
/** The page as an older browser runs it: no module scripts, the nomodule ones run (polyfills + SystemJS). */
|
||||
function asOldBrowser(html) {
|
||||
return html
|
||||
.replace(/<script type="module"[^>]*>[\s\S]*?<\/script>/g, '')
|
||||
.replace(/<link rel="modulepreload"[^>]*>/g, '')
|
||||
.replace(/<script nomodule/g, '<script')
|
||||
}
|
||||
|
||||
async function tv({ old }) {
|
||||
const ctx = await browser.newContext({ viewport: { width: 1280, height: 720 } })
|
||||
const page = await ctx.newPage()
|
||||
page.on('pageerror', e => errors.push(`${old ? 'old' : 'modern'}: ${e.message}`))
|
||||
const fetched = []
|
||||
page.on('request', q => fetched.push(q.url()))
|
||||
if (old) {
|
||||
await page.addInitScript(() => {
|
||||
const real = CSS.supports.bind(CSS)
|
||||
CSS.supports = (...a) => (a.join(':').includes('cq') ? false : real(...a))
|
||||
window.__XENIA_COMPAT_FORCE = ['gap']
|
||||
})
|
||||
await page.route(u => new globalThis.URL(u).pathname === '/', async route => {
|
||||
const res = await route.fetch()
|
||||
await route.fulfill({ response: res, body: asOldBrowser(await res.text()) })
|
||||
})
|
||||
}
|
||||
await page.goto(URL + '/')
|
||||
return { page, fetched }
|
||||
}
|
||||
|
||||
try {
|
||||
const boss = await supportLogin(backend.url)
|
||||
await api('PUT', '/api/platform/modules/signage', { token: boss, body: { enabled: true } }).catch(() => {})
|
||||
const pl = await api('POST', '/api/signage/playlists', { token: boss, body: { name: 'Window', orientation: 'landscape' } })
|
||||
await api('POST', `/api/signage/playlists/${pl.id}/slides`, { token: boss, body: { kind: 'text', duration_s: 60, content: {
|
||||
title: 'Happy hour', body: '17:00 - 19:00', background: { kind: 'colour', colour: '#1e3a8a' },
|
||||
widgets: [
|
||||
{ kind: 'clock', position: 'top-right' },
|
||||
{ kind: 'stack', direction: 'row', position: 'custom', box: { x: 5, y: 60, w: 40, h: 30 }, cells: [
|
||||
{ share: 50, widgets: [{ kind: 'clock' }] }, { share: 50, widgets: [{ kind: 'date' }] }] },
|
||||
] } } })
|
||||
|
||||
const modern = await tv({ old: false })
|
||||
r.check('modern: the pairing screen', await shown(modern.page, 'pairing-code'))
|
||||
const flags = await modern.page.evaluate(() => ({ modern: window.__vite_is_modern_browser === true, compat: document.documentElement.getAttribute('data-compat') }))
|
||||
r.check('modern: runs the modern build, without the compat layer', flags.modern && flags.compat === null, JSON.stringify(flags))
|
||||
const extra = modern.fetched.filter(u => /-legacy|compat|polyfills/.test(u))
|
||||
r.check('modern: downloads nothing of the older-browser copy', extra.length === 0, extra.join(' '))
|
||||
|
||||
const old = await tv({ old: true })
|
||||
r.check('older TV: the pairing screen', await shown(old.page, 'pairing-code'))
|
||||
r.check('older TV: runs the legacy copy with the compat layer (stylesheet, container sizes, flex gaps)',
|
||||
await waitFor(async () => (await old.page.getAttribute('html', 'data-compat')) === 'legacy css cq gap', 10_000, 300),
|
||||
String(await old.page.getAttribute('html', 'data-compat')))
|
||||
r.check('older TV: the legacy chunks and the rewritten stylesheet were loaded',
|
||||
old.fetched.some(u => /index-legacy-.*\.js$/.test(u)) && old.fetched.some(u => u.endsWith('.compat.css')) && !old.fetched.some(u => /\/assets\/index-[\w-]+\.js$/.test(u) && !u.includes('-legacy')))
|
||||
|
||||
for (const [t, label] of [[modern, 'modern'], [old, 'older TV']]) {
|
||||
const code = await t.page.getByTestId('pairing-code').getAttribute('data-code')
|
||||
await api('POST', '/api/devices', { token: boss, body: { code, app_key: 'signage', name: `TV ${label}` } })
|
||||
.then(dev => api('PUT', `/api/signage/screens/${dev.id}`, { token: boss, body: { playlist_id: pl.id } }))
|
||||
r.check(`${label}: plays the slide`, await shown(t.page, 'signage-title', 30_000))
|
||||
}
|
||||
const sizes = page => page.evaluate(() => {
|
||||
const px = el => (el ? parseFloat(getComputedStyle(el).fontSize) : null)
|
||||
const box = el => { const b = el?.getBoundingClientRect(); return b ? [Math.round(b.width), Math.round(b.height)] : null }
|
||||
const clocks = [...document.querySelectorAll('[data-testid="signage-w-clock"]')]
|
||||
return { title: px(document.querySelector('[data-testid="signage-title"]')), body: px(document.querySelector('[data-testid="signage-body"]')),
|
||||
clocks: clocks.map(px), cell: box(document.querySelector('[data-testid="signage-stack-cell"]')),
|
||||
vars: document.querySelector('[data-testid="signage-stack-place"]')?.style.getPropertyValue('--xcqmin') || '',
|
||||
unit: document.querySelector('[data-testid="signage-title"]')?.getAttribute('style') || '',
|
||||
gap: !!document.querySelector('[data-testid="signage-body"][data-xgap]') }
|
||||
})
|
||||
await waitFor(async () => (await sizes(old.page)).clocks.length === 2, 10_000, 300)
|
||||
const a = await sizes(modern.page)
|
||||
const b = await sizes(old.page)
|
||||
const near = (x, y) => x != null && y != null && Math.abs(x - y) <= 0.6
|
||||
r.check('older TV: title and text the same size as on a modern browser', near(a.title, b.title) && near(a.body, b.body), `${JSON.stringify(a)} | ${JSON.stringify(b)}`)
|
||||
r.check('older TV: corner clock and clock inside a stack the same size (nested containers)',
|
||||
a.clocks.length === 2 && b.clocks.length === 2 && near(a.clocks[0], b.clocks[0]) && near(a.clocks[1], b.clocks[1]), `${a.clocks} | ${b.clocks}`)
|
||||
r.check('older TV: the sizes come from the layer (container variables, calc() instead of cqmin)',
|
||||
b.vars.endsWith('px') && a.vars === '' && b.unit.includes('var(--xcqmin') && a.unit.includes('cqmin') && !a.unit.includes('var('), `${b.vars} | ${a.unit} | ${b.unit}`)
|
||||
r.check('older TV: flex items spaced by the layer (title and text)', b.gap && !a.gap)
|
||||
await old.page.screenshot({ path: path.join(OUT, 'compat-old-tv.png') })
|
||||
await modern.page.screenshot({ path: path.join(OUT, 'compat-modern.png') })
|
||||
} catch (e) {
|
||||
failures++
|
||||
console.log('FAIL (crashed)', e.stack || e)
|
||||
} finally {
|
||||
const real = errors.filter(e => !/import\.meta\.resolve not supported/.test(e))
|
||||
r.check('no page errors', real.length === 0, real.join(' | '))
|
||||
await browser.close()
|
||||
killTree(preview)
|
||||
await backend.stop?.()
|
||||
}
|
||||
failures += r.done()
|
||||
process.exit(failures ? 1 : 0)
|
||||
Reference in New Issue
Block a user