// End-to-end check of the built deck: serves it the way Kody mounts it, blocks every // off-origin request, walks every fragment with the keyboard at 1920x1080 and on a phone // (portrait and landscape, reduced motion), checks nothing spills off the 16:9 frame, and // saves one screenshot per slide per viewport. import { spawn } from 'node:child_process' import { existsSync, mkdirSync } from 'node:fs' import { join } from 'node:path' import { chromium, devices } from 'playwright' const root = new URL('..', import.meta.url).pathname const port = Number(process.env.VERIFY_PORT ?? 4391) const mount = '/packages/cloudflare-connect-pse-hyperframes/' const base = `http://127.0.0.1:${port}${mount}` const outDir = process.env.SHOTS_DIR ?? join(root, 'verify-shots') const chrome = process.env.CHROME_PATH ?? (existsSync('/usr/local/bin/google-chrome') ? '/usr/local/bin/google-chrome' : undefined) const settle = Number(process.env.SETTLE_MS ?? 900) const server = spawn(process.execPath, [join(root, 'scripts/serve.mjs')], { env: { ...process.env, PORT: String(port), HOST: '127.0.0.1' }, stdio: ['ignore', 'pipe', 'inherit'] }) await new Promise((resolve, reject) => { server.stdout.on('data', (d) => String(d).includes('Deck at') && resolve()) server.on('exit', (code) => reject(new Error(`server exited with ${code}`))) }) const failures = [] const fail = (msg) => { failures.push(msg) console.error(' FAIL ' + msg) } const browser = await chromium.launch({ executablePath: chrome }) const deckState = (page) => page.evaluate(() => { const player = document.querySelector('hyperframes-player') const deck = player?.iframeElement?.contentWindow?.__deck if (!deck || !deck.scene) return null return { scene: deck.scene, step: deck.step, hash: location.hash, scenes: deck.scenes, soft: deck.soft } }) async function waitFor(page, pred, ms = 6000) { const end = Date.now() + ms let st = null while (Date.now() < end) { st = await deckState(page) if (st && pred(st)) return st await page.waitForTimeout(80) } return st } // Elements in the active scene that spill outside the 1920x1080 frame. const spills = (page) => page.evaluate(() => { const doc = document.querySelector('hyperframes-player').iframeElement.contentDocument const scene = [...doc.querySelectorAll('.scene-frame')].find((s) => s.style.visibility === 'visible') const out = [] for (const el of scene.querySelectorAll('[data-shown]')) { const opacity = Number(getComputedStyle(el).opacity) const shown = el.getAttribute('data-shown') === '1' if (shown ? opacity < 0.95 : opacity > 0.05) out.push(`"${el.textContent.trim().slice(0, 30)}" should be ${shown ? 'shown' : 'hidden'} (opacity ${opacity})`) } for (const el of scene.querySelectorAll('*')) { if (el.closest('.notes, svg') || !el.checkVisibility({ opacityProperty: true, visibilityProperty: true })) continue const r = el.getBoundingClientRect() if (!r.width || !r.height) continue if (r.left < -2 || r.top < -2 || r.right > 1922 || r.bottom > 1082) out.push(`${el.tagName.toLowerCase()}.${el.className} [${Math.round(r.left)},${Math.round(r.top)},${Math.round(r.right)},${Math.round(r.bottom)}]`) if (el.matches('.node, .code, h2, p, li') && el.scrollWidth > el.clientWidth + 4 && getComputedStyle(el).overflowX === 'visible' && el.clientWidth) out.push(`${el.tagName.toLowerCase()}.${el.className} text overflows its box`) } return out.slice(0, 5) }) async function walk(name, contextOptions, query, input = 'keys') { console.log(`\n${name}`) const dir = join(outDir, name) mkdirSync(dir, { recursive: true }) const context = await browser.newContext(contextOptions) const page = await context.newPage() const offsite = [] await context.route('**/*', (route) => { const url = route.request().url() if (url.startsWith(`http://127.0.0.1:${port}/`) || url.startsWith('data:') || url.startsWith('blob:')) return route.continue() offsite.push(url) return route.abort() }) page.on('pageerror', (e) => fail(`${name}: page error ${e.message}`)) page.on('console', (m) => m.type() === 'error' && fail(`${name}: console error ${m.text()}`)) await page.goto(base + query) let st = await waitFor(page, (s) => s.scene === 'title', 15000) if (!st) { fail(`${name}: deck never loaded`) await context.close() return } const scenes = st.scenes const order = scenes.flatMap((s) => Array.from({ length: Math.max(1, s.fragments.length) }, (_, step) => ({ scene: s.id, step, last: step === Math.max(1, s.fragments.length) - 1 }))) await page.focus('hyperframes-slideshow').catch(() => {}) await page.waitForTimeout(settle) const touch = input === 'touch' ? await toucher(context, page) : null for (let i = 0; i < order.length; i++) { const want = order[i] st = await waitFor(page, (s) => s.scene === want.scene && s.step === want.step) if (!st || st.scene !== want.scene || st.step !== want.step) { fail(`${name}: expected ${want.scene} step ${want.step}, got ${st?.scene} step ${st?.step}`) break } if (want.last) { await page.waitForTimeout(st.soft ? settle * 2 : settle) const n = scenes.findIndex((s) => s.id === want.scene) + 1 const hash = want.step > 0 ? `#${n}.${want.step}` : `#${n}` if (st.hash !== hash) fail(`${name}: ${want.scene} hash is ${st.hash}, expected ${hash}`) const bad = await spills(page) if (bad.length) fail(`${name}: ${want.scene} spills: ${bad.join('; ')}`) await page.screenshot({ path: join(dir, `${String(n).padStart(2, '0')}-${want.scene}.png`) }) console.log(` ok ${String(n).padStart(2, '0')} ${want.scene}`) } if (i < order.length - 1) { if (touch) await (i % 3 === 2 ? touch.swipe('next') : touch.tap('next')) else await page.keyboard.press(i % 3 === 2 ? 'Space' : 'ArrowRight') } } if (touch) { await phoneChecks(page, name, touch, scenes) if (offsite.length) fail(`${name}: off-origin requests ${[...new Set(offsite)].join(', ')}`) await context.close() return } const lastId = scenes[scenes.length - 1].id await page.keyboard.press('ArrowRight') st = await waitFor(page, (s) => s.scene === lastId, 1500) if (st?.scene !== lastId) fail(`${name}: ArrowRight past the end left the last slide`) await page.keyboard.press('ArrowLeft') st = await waitFor(page, (s) => s.scene !== lastId) if (st?.scene !== scenes[scenes.length - 2].id) fail(`${name}: ArrowLeft from the end went to ${st?.scene}`) await page.keyboard.press('Home') st = await waitFor(page, (s) => s.scene === 'title') if (st?.scene !== 'title' || st.hash !== '#1') fail(`${name}: Home went to ${st?.scene} ${st?.hash}`) await page.keyboard.press('End') st = await waitFor(page, (s) => s.scene === lastId) if (st?.scene !== lastId) fail(`${name}: End went to ${st?.scene}`) await page.evaluate(() => (location.hash = '#5')) st = await waitFor(page, (s) => s.scene === scenes[4].id) if (st?.scene !== scenes[4].id) fail(`${name}: changing the hash to #5 opened ${st?.scene}`) await page.goto('about:blank') await page.goto(base + query + '#13') st = await waitFor(page, (s) => s.scene === scenes[12].id, 15000) if (st?.scene !== scenes[12].id) fail(`${name}: deep link #13 opened ${st?.scene}`) const stepped = scenes.findIndex((s) => s.fragments.length > 3) if (stepped >= 0) { await page.goto('about:blank') await page.goto(base + query + `#${stepped + 1}.3`) st = await waitFor(page, (s) => s.scene === scenes[stepped].id && s.step === 3, 15000) if (st?.scene !== scenes[stepped].id || st?.step !== 3) fail(`${name}: deep link #${stepped + 1}.3 opened ${st?.scene} step ${st?.step}`) // Back steps through the slide's steps before leaving it: #N.3 goes to #N.2, then #N.1. await page.focus('hyperframes-slideshow').catch(() => {}) for (const want of [2, 1]) { await page.keyboard.press('ArrowLeft') st = await waitFor(page, (s) => s.scene === scenes[stepped].id && s.step === want && s.hash === `#${stepped + 1}.${want}`) if (st?.scene !== scenes[stepped].id || st?.step !== want || st?.hash !== `#${stepped + 1}.${want}`) fail(`${name}: ArrowLeft toward #${stepped + 1}.${want} went to ${st?.scene} step ${st?.step} (${st?.hash})`) } } if (contextOptions.reducedMotion === 'reduce' && !st?.soft) fail(`${name}: reduced motion did not soften`) if (offsite.length) fail(`${name}: off-origin requests ${[...new Set(offsite)].join(', ')}`) console.log(` keyboard: ArrowRight, Space, ArrowLeft, Home, End, #5, #13 and step-back within a slide checked`) await context.close() } // Touch input through CDP, the way Android Chrome delivers it. In the rotated portrait fit // the slide's x axis runs down the screen. async function toucher(context, page) { const cdp = await context.newCDPSession(page) const size = () => page.evaluate(() => [innerWidth, innerHeight, document.documentElement.hasAttribute('data-rotated') && matchMedia('(orientation: portrait)').matches]) const point = (w, h, rot, fx) => (rot ? { x: w / 2, y: h * fx } : { x: w * fx, y: h / 2 }) async function drag(from, to) { await cdp.send('Input.dispatchTouchEvent', { type: 'touchStart', touchPoints: [from] }) for (let i = 1; i <= 6; i++) await cdp.send('Input.dispatchTouchEvent', { type: 'touchMove', touchPoints: [{ x: from.x + ((to.x - from.x) * i) / 6, y: from.y + ((to.y - from.y) * i) / 6 }] }) await cdp.send('Input.dispatchTouchEvent', { type: 'touchEnd', touchPoints: [] }) } return { async tap(dir) { const [w, h, rot] = await size() const p = point(w, h, rot, dir === 'next' ? 0.8 : 0.15) await cdp.send('Input.dispatchTouchEvent', { type: 'touchStart', touchPoints: [p] }) await cdp.send('Input.dispatchTouchEvent', { type: 'touchEnd', touchPoints: [] }) }, async swipe(dir) { const [w, h, rot] = await size() const a = point(w, h, rot, dir === 'next' ? 0.75 : 0.25) const b = point(w, h, rot, dir === 'next' ? 0.25 : 0.75) await drag(a, b) }, } } const stage = (page) => page.evaluate(() => { const r = document.querySelector('hyperframes-player').iframeElement.getBoundingClientRect() return { w: r.width, h: r.height, l: r.left, t: r.top, r: r.right, b: r.bottom, vw: innerWidth, vh: innerHeight, href: location.href } }) async function phoneChecks(page, name, touch, scenes) { const lastId = scenes[scenes.length - 1].id let st await touch.tap('prev') st = await waitFor(page, (s) => s.scene !== lastId) if (st?.scene !== scenes[scenes.length - 2].id) fail(`${name}: tapping the left side went to ${st?.scene}`) await page.waitForTimeout(400) await touch.swipe('prev') st = await waitFor(page, (s) => s.scene === scenes[scenes.length - 3].id) if (st?.scene !== scenes[scenes.length - 3].id) fail(`${name}: swiping right went to ${st?.scene}`) for (let i = 0; i < 3; i++) { await touch.swipe('prev') await page.waitForTimeout(300) } if (!(await page.url()).startsWith(base)) fail(`${name}: a swipe navigated the browser away to ${page.url()}`) // Flows animate on the phone: a packet on How the import works moves between samples. await page.evaluate(() => window.postMessage({ type: 'goto', slideIndex: 7 }, '*')) st = await waitFor(page, (s) => s.scene === 'how-import') await page.waitForTimeout(1600) const sample = () => page.evaluate(() => { const w = document.querySelector('hyperframes-player').iframeElement.contentWindow return { active: w.__deck.flowActive, packets: [...w.document.querySelectorAll('#slide-how-import .packet')].map((p) => p.style.transform + p.style.opacity).join('|') } }) const a = await sample() await page.waitForTimeout(500) const b = await sample() if (!a.active || a.packets === b.packets) fail(`${name}: the how-import flow is not animating (active ${a.active})`) // The stage fits the viewport, and refits when the browser toolbar changes its height. const vp = page.viewportSize() for (const h of [vp.height, vp.height - 56, vp.height]) { await page.setViewportSize({ width: vp.width, height: h }) await page.waitForTimeout(500) const s = await stage(page) const fits = s.l >= -1 && s.t >= -1 && s.r <= s.vw + 1 && s.b <= s.vh + 1 const fills = Math.abs(s.w - s.vw) < 2 || Math.abs(s.h - s.vh) < 2 if (!fits || !fills) fail(`${name}: at ${vp.width}x${h} the stage is ${Math.round(s.w)}x${Math.round(s.h)} at ${Math.round(s.l)},${Math.round(s.t)}`) } // Portrait: "Rotate slides" turns the stage sideways to fill the screen, and gestures follow. if (vp.height > vp.width) { await touch.tap('next') await page.waitForTimeout(300) await page.tap('[data-fit-toggle]') await page.waitForTimeout(900) const s = await stage(page) if (!(s.h > s.w * 1.5)) fail(`${name}: Rotate slides left the stage at ${Math.round(s.w)}x${Math.round(s.h)}`) const before = await deckState(page) await touch.swipe('next') st = await waitFor(page, (x) => x.scene !== before.scene || x.step !== before.step) if (st.scene === before.scene && st.step === before.step) fail(`${name}: swiping in the rotated fit did nothing`) await page.screenshot({ path: join(outDir, name, 'rotated-fit.png') }) await page.tap('[data-fit-toggle]').catch(() => {}) } console.log(` touch: taps, swipes, no back-swipe escape, live animation, toolbar refit${vp.height > vp.width ? ', rotated fit' : ''} checked`) } async function softCheck(contextOptions) { const context = await browser.newContext(contextOptions) const page = await context.newPage() await page.goto(base + '#8') const st = await waitFor(page, (s) => s.scene === 'how-import', 15000) if (!st?.soft) fail('reduced motion did not soften the deck') await page.waitForTimeout(1500) const active = await page.evaluate(() => document.querySelector('hyperframes-player').iframeElement.contentWindow.__deck.flowActive) if (!active) fail('reduced motion stopped the flow instead of softening it') console.log('\nreduced motion: softened, flows still run') await context.close() } try { await walk('desktop-1920x1080', { viewport: { width: 1920, height: 1080 } }, '?still') // Phones: Android Chrome on a Pixel 7 (touch, DPR 2.625, mobile UA), motion on, driven // only by taps and swipes. const { viewport, ...pixel } = devices['Pixel 7'] await walk('mobile-portrait', { ...pixel, viewport, reducedMotion: 'no-preference' }, '', 'touch') await walk('mobile-landscape', { ...pixel, viewport: { width: viewport.height, height: viewport.width }, reducedMotion: 'no-preference' }, '', 'touch') await softCheck({ ...pixel, viewport, reducedMotion: 'reduce' }) } finally { await browser.close() server.kill() } if (failures.length) { console.error(`\n${failures.length} problem(s).`) process.exit(1) } console.log(`\nAll slides verified. Screenshots in ${outDir}`)