Skip to content
← Public packages

@kentcdodds/the-last-software-engineer

The Last Software Engineer: Kent C. Dodds's React Summit US 2026 talk deck. Judgment and product engineering in an AI era.

FollowForkOpen app

Files

verify.mjs

496 lines · 28.4 KB · JavaScript
// Walks every slide and every click step of dist/index.html with network blocked, in both themes,
// at 1280x720 and 1920x1080, and fails on: console errors, any network request, missing fonts,
// content outside the dome-safe box, clipped text, overlapping text, missing notes, em/en dashes.
// Also checks the T toggle, ?theme=, the speaker view, and saves screenshots:
//   <out>/<theme>/NN-<id>.png   final state of each slide at 1920x1080
//   <out>/steps/<theme>/NN-<id>-<step>.png   every step at 1280x720 (with --steps)
// Usage: node scripts/verify.mjs dist/index.html verify-shots [--steps]
import { chromium } from 'playwright'
import { mkdirSync, rmSync } from 'node:fs'
import { resolve, join } from 'node:path'
import { pathToFileURL } from 'node:url'

const [file = 'dist/index.html', out = 'verify-shots'] = process.argv.slice(2).filter((a) => !a.startsWith('--'))
const allSteps = process.argv.includes('--steps')
const url = pathToFileURL(resolve(file)).href
const SAFE = { left: 200, right: 1720, top: 100, bottom: 990 } // canvas px (1920x1080)
const MIN_FONT = 26

const browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH || undefined })
const problems = []
const fail = (msg) => problems.push(msg)

async function open(viewport, query, colorScheme = 'dark') {
	const context = await browser.newContext({ viewport, deviceScaleFactor: 1, colorScheme })
	await context.route('**/*', (route) => {
		const u = route.request().url()
		if (u.startsWith('file:') || u.startsWith('data:')) return route.continue()
		fail(`network request: ${u}`)
		return route.abort()
	})
	const page = await context.newPage()
	page.on('console', (m) => { if (m.type() === 'error') fail(`console error (${query}): ${m.text()}`) })
	page.on('pageerror', (e) => fail(`page error (${query}): ${e.message}`))
	await page.goto(`${url}?${query}`)
	await page.waitForFunction(() => window.__deck?.isReady())
	await page.evaluate(() => document.fonts.ready)
	return { context, page }
}

// Measures the current state of the current slide.
const measure = () => {
	const section = window.__deck.getCurrentSlide()
	const box = section.getBoundingClientRect()
	const scale = box.width / 1920
	const toCanvas = (r) => ({ l: (r.left - box.left) / scale, t: (r.top - box.top) / scale, r: (r.right - box.left) / scale, b: (r.bottom - box.top) / scale })
	const shown = (el) => {
		for (let e = el; e && e !== section.parentElement; e = e.parentElement) {
			const cs = getComputedStyle(e)
			if (cs.display === 'none' || cs.visibility === 'hidden' || Number(cs.opacity) < 0.05) return false
		}
		return true
	}
	const issues = []
	const texts = []
	// WCAG contrast: resolve any CSS color (oklch, color-mix) to sRGB through a canvas.
	const cv = document.createElement('canvas').getContext('2d', { willReadFrequently: true })
	const rgba = (c) => { cv.clearRect(0, 0, 1, 1); cv.fillStyle = '#000'; cv.fillStyle = c; cv.fillRect(0, 0, 1, 1); return [...cv.getImageData(0, 0, 1, 1).data] }
	const lum = ([r, g, b]) => [r, g, b].map((v) => { v /= 255; return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4 }).reduce((s, v, i) => s + v * [0.2126, 0.7152, 0.0722][i], 0)
	const page = rgba(getComputedStyle(document.documentElement).getPropertyValue('--page'))
	const bgOf = (el) => {
		for (let e = el; e && e !== document.documentElement; e = e.parentElement) {
			const c = rgba(getComputedStyle(e).backgroundColor)
			if (c[3] > 200) return c
		}
		return page
	}
	window.__minContrast ??= 99
	for (const el of section.querySelectorAll('*')) {
		if (el.closest('aside.notes') || el.matches('style, script, .step-marker')) continue
		const hasText = [...el.childNodes].some((n) => n.nodeType === 3 && n.textContent.trim())
		const isMedia = el.matches('img, svg, canvas')
		if (!hasText && !isMedia) continue
		// Texture canvases (the buzzword wall) may bleed to the edges by design; they carry no content.
		if (el.matches('canvas.texture')) continue
		if (el.closest('svg') && el.tagName.toLowerCase() !== 'svg' && el.tagName.toLowerCase() !== 'text') continue
		if (!shown(el)) continue
		const rect = el.getBoundingClientRect()
		if (rect.width < 2 || rect.height < 2) continue
		const c = toCanvas(rect)
		const label = (el.textContent.trim() || el.getAttribute('alt') || el.className?.baseVal || el.className || el.tagName).toString().slice(0, 40)
		if (c.l < 200 - 1 || c.r > 1720 + 1 || c.t < 100 - 1 || c.b > 990 + 1) issues.push(`outside safe box: "${label}" [${c.l.toFixed(0)},${c.t.toFixed(0)} to ${c.r.toFixed(0)},${c.b.toFixed(0)}]`)
		if (hasText) {
			const fs = parseFloat(getComputedStyle(el).fontSize)
			if (fs < 26) issues.push(`small text ${fs}px: "${label}"`)
			{
				const fg = rgba(getComputedStyle(el).color)
				const bg = bgOf(el)
				const [a, b] = [lum(fg), lum(bg)].sort((x, y) => y - x)
				const ratio = (a + 0.05) / (b + 0.05)
				if (!el.closest('[data-chrome]')) window.__minContrast = Math.min(window.__minContrast, ratio)
				// [data-chrome]: faithful app chrome copied from the original screenshots (date labels, input placeholder), exempt from the contrast floor.
				if (ratio < 4.5 && !el.closest('[data-chrome]')) issues.push(`low contrast ${ratio.toFixed(2)}:1: "${label}"`)
			}
			if (el.scrollWidth > el.clientWidth + 2 && getComputedStyle(el).overflow !== 'visible') issues.push(`clipped text: "${label}"`)
			// Text must sit inside every clipping ancestor.
			for (let a = el.parentElement; a && a !== section; a = a.parentElement) {
				const ov = getComputedStyle(a).overflow
				if (ov === 'visible') continue
				const ar = toCanvas(a.getBoundingClientRect())
				if (c.l < ar.l - 1 || c.r > ar.r + 1 || c.t < ar.t - 1 || c.b > ar.b + 1) issues.push(`text cut off by ${a.className || a.tagName}: "${label}"`)
				break
			}
			// Rotated boxes over-report overlap; skip text inside a rotated ancestor.
			let rotated = false
			for (let a = el; a && a !== section; a = a.parentElement) if (getComputedStyle(a).rotate !== 'none' && getComputedStyle(a).rotate !== '0deg') rotated = true
			// Inline text that wraps has one box per line; compare those, not their union.
			// Hidden text: whatever is on top at the text's centre must be the text itself (or its own box).
			{
				const r0 = el.getClientRects()[0]
				if (r0 && getComputedStyle(el).pointerEvents !== 'none') {
					const top = document.elementFromPoint(r0.left + r0.width / 2, r0.top + r0.height / 2)
					if (top && !(top === el || el.contains(top) || top.contains(el))) issues.push(`text hidden under ${top.className?.baseVal ?? top.className ?? top.tagName}: "${label}"`)
				}
			}
			if (!rotated) texts.push({ el, c, label, boxes: [...el.getClientRects()].filter((r) => r.width > 1 && r.height > 1).map(toCanvas) })
		}
	}
	for (let i = 0; i < texts.length; i++) {
		for (let j = i + 1; j < texts.length; j++) {
			const a = texts[i], b = texts[j]
			if (a.el.contains(b.el) || b.el.contains(a.el)) continue
			const hit = a.boxes.some((p) => b.boxes.some((q) => Math.min(p.r, q.r) - Math.max(p.l, q.l) > 3 && Math.min(p.b, q.b) - Math.max(p.t, q.t) > 3))
			if (hit) issues.push(`overlap: "${a.label}" and "${b.label}"`)
		}
	}
	const text = section.innerText
	if (/[–—]/.test(text + section.querySelector('aside.notes')?.textContent)) issues.push('em or en dash')
	const notes = section.querySelector('aside.notes')?.textContent.trim()
	if (!notes) issues.push('no speaker notes')
	return { id: section.id, step: Number(section.dataset.step ?? 0), steps: Number(section.dataset.steps ?? 0), issues }
}

// The physics plank (the-work, agi): settled pose must equal the beat's precomputed equilibrium,
// the plank must never overhang the stilt pads or leave its canvas, and its canvas labels must
// meet 4.5:1 contrast on their fills.
const plankCheck = () => {
	const section = window.__deck.getCurrentSlide()
	const plank = window.__plank?.[section.id]
	if (!plank) return []
	const issues = []
	const step = Number(section.dataset.step ?? 0)
	const s = plank.state
	const eq = plank.equilibrium(step)
	if (plank.step !== step) issues.push(`plank on beat ${plank.step}, slide on ${step}`)
	if (Math.abs(s.theta - eq.theta) > 1e-9 || Math.abs(s.droop - eq.droop) > 1e-9) issues.push(`plank pose ${s.theta} / ${s.droop} is not the settled pose ${eq.theta} / ${eq.droop}`)
	const g = plank.geometry()
	const [l, r] = [g.pads[0][0], g.pads[1][1]]
	for (const [x, y] of g.corners) {
		// No tolerance: every corner, tipped or level, must be within the pads' outer edges with 8px to spare.
		if (x < l + 8 || x > r - 8) issues.push(`plank overhangs the stilts: corner x ${x.toFixed(1)} not within ${l + 8}..${r - 8}`)
		if (x < 0 || x > g.size[0] || y < 0 || y > g.size[1]) issues.push(`plank leaves its canvas at ${x.toFixed(0)},${y.toFixed(0)}`)
	}
	const cv = document.createElement('canvas').getContext('2d', { willReadFrequently: true })
	const rgb = (c) => { cv.clearRect(0, 0, 1, 1); cv.fillStyle = c; cv.fillRect(0, 0, 1, 1); return [...cv.getImageData(0, 0, 1, 1).data] }
	const lum = ([r, g, b]) => [r, g, b].map((v) => { v /= 255; return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4 }).reduce((s, v, i) => s + v * [0.2126, 0.7152, 0.0722][i], 0)
	const v = (n) => getComputedStyle(section).getPropertyValue(n).trim()
	for (const [fg, bg] of [['--ink', '--soft'], ['--ink', '--surface'], ['--muted', '--soft']]) {
		const [a, b] = [lum(rgb(v(fg))), lum(rgb(v(bg)))].sort((x, y) => y - x)
		const ratio = (a + 0.05) / (b + 0.05)
		window.__minContrast = Math.min(window.__minContrast ?? 99, ratio)
		if (ratio < 4.5) issues.push(`plank label ${fg} on ${bg} contrast ${ratio.toFixed(2)}:1`)
	}
	return issues
}

const themes = ['dark', 'light']
const viewports = [{ width: 1280, height: 720 }, { width: 1920, height: 1080 }]
rmSync(out, { recursive: true, force: true })
const buzzSigs = new Set()
let slideTotal = 0
let beatTotal = 0
for (const theme of themes) {
	mkdirSync(join(out, theme), { recursive: true })
	if (allSteps) mkdirSync(join(out, 'steps', theme), { recursive: true })
	for (const viewport of viewports) {
		const { context, page } = await open(viewport, `theme=${theme}&static`)
		if ((await page.evaluate(() => document.documentElement.dataset.theme)) !== theme) fail(`?theme=${theme} not applied`)
		const fontsOk = await page.evaluate(() => document.fonts.check('700 60px "Bricolage Grotesque"') && document.fonts.check('600 44px "Wix Madefor Text"'))
		if (!fontsOk) fail('embedded fonts not loaded')
		let index = 0
		let beats = 0
		const seen = new Set()
		while (true) {
			const m = await page.evaluate(measure)
			beats++
			for (const issue of m.issues) fail(`${theme} ${viewport.width}x${viewport.height} ${m.id} step ${m.step}: ${issue}`)
			{
				const b = await page.evaluate(() => window.__deck.getCurrentSlide().id === 'what-do-you-do' ? { step: window.__buzz.step, mode: window.__buzz.mode, running: window.__buzz.running, sig: window.__buzz.signature() } : null)
				if (b) {
					buzzSigs.add(b.sig.toFixed(6))
					if (b.mode !== 'rest' || b.running || b.step !== m.step) fail(`${theme} ${viewport.width}x${viewport.height} buzz wall not settled in ?static: ${JSON.stringify(b)}`)
				}
			}
			for (const issue of await page.evaluate(() => {
				const section = window.__deck.getCurrentSlide()
				const step = Number(section.dataset.step ?? 0)
				const nums = (s) => (s.match(/-?\d*\.?\d+/g) ?? []).map(Number)
				return [...section.querySelectorAll('path[data-morph]')].flatMap((el) => {
					const want = nums(step >= Number(el.dataset.morph) ? el.dataset.to : el.dataset.from)
					const got = nums(el.getAttribute('d'))
					return got.length === want.length && got.every((v, i) => Math.abs(v - want[i]) < 0.01) ? [] : [`path morph not at its end state for beat ${step}`]
				})
			})) fail(`${theme} ${viewport.width}x${viewport.height} ${m.id} step ${m.step}: ${issue}`)
			for (const issue of await page.evaluate(plankCheck)) fail(`${theme} ${viewport.width}x${viewport.height} ${m.id} step ${m.step}: ${issue}`)
			if (allSteps && viewport.width === 1280) await page.screenshot({ path: join(out, 'steps', theme, `${String(index + 1).padStart(2, '0')}-${m.id}-${m.step}.png`) })
			if (m.step === m.steps) {
				seen.add(m.id)
				index++
				if (viewport.width === 1920) await page.screenshot({ path: join(out, theme, `${String(index).padStart(2, '0')}-${m.id}.png`) })
			}
			if (await page.evaluate(() => window.__deck.isLastSlide() && !window.__deck.availableFragments().next)) break
			await page.evaluate(() => window.__deck.next())
			await page.waitForTimeout(40)
		}
		const minC = await page.evaluate(() => window.__minContrast)
		console.log(`${theme} ${viewport.width}x${viewport.height}: lowest text contrast ${minC.toFixed(2)}:1`)
		slideTotal = seen.size
		beatTotal = beats
		await context.close()
	}
}

// Motion on, T toggle, speaker view.
{
	const { context, page } = await open({ width: 1280, height: 720 }, '')
	const theme = () => page.evaluate(() => document.documentElement.dataset.theme)
	// Default follows the OS, live.
	if ((await theme()) !== 'dark') fail('with a dark OS the deck is not dark')
	await page.emulateMedia({ colorScheme: 'light' })
	await page.waitForTimeout(100)
	if ((await theme()) !== 'light') fail('the deck did not follow the OS switching to light')
	await page.emulateMedia({ colorScheme: 'dark' })
	await page.waitForTimeout(100)
	if ((await theme()) !== 'dark') fail('the deck did not follow the OS switching back to dark')
	await page.keyboard.press('t')
	if ((await theme()) !== 'light') fail('T did not switch to light')
	await page.emulateMedia({ colorScheme: 'light' })
	await page.emulateMedia({ colorScheme: 'dark' })
	await page.waitForTimeout(100)
	if ((await theme()) !== 'light') fail('a T pick did not stick when the OS changed')
	await page.keyboard.press('t')
	if ((await theme()) !== 'dark') fail('T did not switch back to dark')
	{
		const lightOs = await open({ width: 1280, height: 720 }, '', 'light')
		if ((await lightOs.page.evaluate(() => document.documentElement.dataset.theme)) !== 'light') fail('with a light OS the deck is not light')
		await lightOs.page.evaluate(() => localStorage.setItem('last-swe-deck-theme', 'dark'))
		await lightOs.page.goto(lightOs.page.url().split('?')[0] + '?theme=system')
		await lightOs.page.waitForFunction(() => window.__deck?.isReady())
		if ((await lightOs.page.evaluate(() => [document.documentElement.dataset.theme, localStorage.getItem('last-swe-deck-theme')].join())) !== 'light,') fail('?theme=system did not clear the saved pick')
		await lightOs.context.close()
	}
	// Physics with motion on: every beat, forward then back, must come to rest on exactly the
	// precomputed pose; also measure the frame rate during a transition.
	for (const id of ['the-work', 'agi']) {
		await page.evaluate((id) => { const s = document.getElementById(id); const i = window.__deck.getIndices(s); window.__deck.slide(i.h, i.v, -1) }, id)
		await page.waitForTimeout(400)
		const n = await page.evaluate((id) => Number(document.getElementById(id).dataset.steps), id)
		const order = [...Array.from({ length: n }, (_, k) => ['next', k + 1]), ...Array.from({ length: n }, (_, k) => ['prev', n - k - 1])]
		for (const [dir, expect] of order) {
			const fps = await page.evaluate(async (dir) => {
				window.__deck[dir]()
				let frames = 0
				const t0 = performance.now()
				await new Promise((done) => { const f = () => { frames++; performance.now() - t0 < 1000 ? requestAnimationFrame(f) : done() }; requestAnimationFrame(f) })
				return frames
			}, dir)
			const settled = await page.waitForFunction((id) => window.__plank[id].settled, id, { timeout: 12000 }).then(() => true, () => false)
			const r = await page.evaluate(({ id, expect }) => { const p = window.__plank[id]; const s = p.state; const e = p.equilibrium(expect); return { step: p.step, d: Math.abs(s.theta - e.theta) + Math.abs(s.droop - e.droop), deg: p.geometry().thetaDeg } }, { id, expect })
			console.log(`plank ${id} ${dir} -> beat ${expect}: settled ${settled}, ${r.deg.toFixed(2)} deg, ${fps} fps during the move`)
			if (!settled || r.step !== expect || r.d > 1e-9) fail(`plank ${id} ${dir} to beat ${expect} did not settle on its equilibrium (step ${r.step}, error ${r.d})`)
			if (fps < 50) fail(`plank ${id} ran at ${fps} fps`)
		}
	}
	// Curve morph with motion on: forward interpolates (no cut) to the plateau; back restores it.
	{
		await page.evaluate(() => { const s = document.getElementById('curve'); const i = window.__deck.getIndices(s); window.__deck.slide(i.h, i.v, 0) }) // fragment 0 shown = beat 1
		await page.waitForTimeout(500)
		const read = () => page.evaluate(() => { const el = document.querySelector('#curve path[data-morph]'); const n = (s) => (s.match(/-?\d*\.?\d+/g) ?? []).map(Number); const d = n(el.getAttribute('d')), a = n(el.dataset.from), b = n(el.dataset.to); const dist = (x) => Math.max(...x.map((v, i) => Math.abs(v - d[i]))); return { toFrom: dist(a), toTo: dist(b) } })
		await page.evaluate(() => window.__deck.next())
		await page.waitForTimeout(600)
		const mid = await read()
		await page.waitForTimeout(1200)
		const end = await read()
		await page.evaluate(() => window.__deck.prev())
		await page.waitForTimeout(1800)
		const back = await read()
		console.log(`curve morph: mid ${JSON.stringify(mid)} end ${JSON.stringify(end)} back ${JSON.stringify(back)}`)
		if (!(mid.toFrom > 1 && mid.toTo > 1)) fail('curve morph cut instead of interpolating')
		if (end.toTo > 0.01) fail('curve morph did not land on the plateau')
		if (back.toFrom > 0.01) fail('curve morph did not return to the exponential going back')
	}
	// Buzzword wall with motion on: piles in and keeps moving, drops away going back.
	{
		await page.evaluate(() => { const s = document.getElementById('what-do-you-do'); const i = window.__deck.getIndices(s); window.__deck.slide(i.h, i.v, -1) })
		await page.waitForTimeout(400)
		const fps = await page.evaluate(async () => {
			window.__deck.next()
			let frames = 0
			const t0 = performance.now()
			await new Promise((done) => { const f = () => { frames++; performance.now() - t0 < 1000 ? requestAnimationFrame(f) : done() }; requestAnimationFrame(f) })
			return frames
		})
		await page.waitForTimeout(6000)
		const still = await page.evaluate(() => ({ step: window.__buzz.step, mode: window.__buzz.mode, running: window.__buzz.running }))
		await page.evaluate(() => window.__deck.prev())
		await page.waitForTimeout(1200)
		const back = await page.evaluate(() => ({ step: window.__buzz.step, mode: window.__buzz.mode, running: window.__buzz.running }))
		console.log(`buzz: ${fps} fps piling in; after 6s ${JSON.stringify(still)}; back ${JSON.stringify(back)}`)
		if (fps < 50) fail(`buzz wall ran at ${fps} fps`)
		if (still.step !== 1 || still.mode !== 'in' || !still.running) fail(`buzz wall stopped moving: ${JSON.stringify(still)}`)
		if (back.step !== 0 || back.mode !== 'rest' || back.running) fail(`buzz wall did not settle empty going back: ${JSON.stringify(back)}`)
	}
	// Going backward: every slide, entered from the next one with Prev, must appear in its final
	// state at once (no entry animation or transitions running, widgets settled), and the slide
	// being left must rewind its entry animation.
	{
		const total = await page.evaluate(() => window.__deck.getTotalSlides())
		let checked = 0
		for (let h = 0; h < total - 1; h++) {
			await page.evaluate((h) => window.__deck.slide(h + 1, 0, -1), h)
			await page.waitForTimeout(1600)
			await page.evaluate(() => window.__deck.prev())
			await page.waitForTimeout(90)
			const r = await page.evaluate(() => {
				const s = window.__deck.getCurrentSlide()
				const left = s.nextElementSibling
				const running = s.getAnimations({ subtree: true }).filter((a) => a.playState === 'running' && a.effect?.getTiming().iterations !== Infinity)
				const plank = window.__plank?.[s.id]
				const morph = s.querySelector('path[data-morph]')
				const nums = (x) => (x.match(/-?\d*\.?\d+/g) ?? []).map(Number)
				const morphEnd = !morph || nums(morph.getAttribute('d')).every((v, i) => Math.abs(v - nums(morph.dataset.to)[i]) < 0.01)
				const leftHasM = !!left?.querySelector('[data-m]')
				return {
					id: s.id,
					step: Number(s.dataset.step ?? 0),
					steps: Number(s.dataset.steps ?? 0),
					running: running.map((a) => a.animationName ?? a.transitionProperty ?? a.constructor.name).slice(0, 4),
					entry: s.classList.contains('m-go'),
					plankSettled: !plank || plank.settled,
					morphEnd,
					rewinding: !leftHasM || left.classList.contains('m-rev'),
				}
			})
			if (r.step !== r.steps) fail(`back into ${r.id}: landed on beat ${r.step}, not the last beat ${r.steps}`)
			if (r.running.length) fail(`back into ${r.id}: animations still running on arrival: ${r.running.join(', ')}`)
			if (r.entry) fail(`back into ${r.id}: the entry animation replayed`)
			if (!r.plankSettled) fail(`back into ${r.id}: the plank animated instead of arriving settled`)
			if (!r.morphEnd) fail(`back into ${r.id}: the curve morph replayed`)
			if (!r.rewinding) fail(`leaving the slide after ${r.id} backward did not rewind its entry`)
			checked++
		}
		console.log(`backward navigation: ${checked} slides arrive settled; leaving slides rewind`)
	}
	// Reflow audit: with motion on, no text may move in layout (offset box, which ignores
	// transforms) while a slide enters or while a beat plays, and text already on screen must not
	// shift when a beat adds or removes siblings. Motion must be transforms and opacity only.
	{
		const snap = () => page.evaluate(() => {
			const section = window.__deck.getCurrentSlide()
			const out = []
			for (const el of section.querySelectorAll('*')) {
				if (!(el instanceof HTMLElement) || el.closest('aside.notes') || el.matches('style, script, .step-marker')) continue
				if (![...el.childNodes].some((n) => n.nodeType === 3 && n.textContent.trim())) continue
				let x = 0, y = 0
				// Sum offsets up to the section, adding each intermediate offsetParent's border (Chrome makes
				// a transformed element the offsetParent mid-animation, so borders must be counted).
				for (let e = el; e && e !== section; e = e.offsetParent) {
					x += e.offsetLeft
					y += e.offsetTop
					if (e.offsetParent && e.offsetParent !== section) { x += e.offsetParent.clientLeft; y += e.offsetParent.clientTop }
				}
				out.push({ t: el.textContent.trim().slice(0, 30), box: [x, y, el.offsetWidth, el.offsetHeight] })
			}
			return { id: section.id, step: Number(section.dataset.step ?? 0), out }
		})
		const diff = (a, b, what) => {
			a.out.forEach((e, i) => {
				const f = b.out[i]
				if (!f || f.t !== e.t) return
				if (e.box[2] === 0 || f.box[2] === 0) return
				const d = Math.max(...e.box.map((v, k) => Math.abs(v - f.box[k])))
				if (d > 1) fail(`reflow jump on ${b.id} ${what}: "${e.t}" moved ${d}px (${e.box.join(',')} -> ${f.box.join(',')})`)
			})
		}
		const total = await page.evaluate(() => window.__deck.getTotalSlides())
		let jumpsChecked = 0
		for (let h = 0; h < total; h++) {
			await page.evaluate((h) => window.__deck.slide(h, 0, -1), h)
			await page.waitForTimeout(40)
			const first = await snap()
			await page.waitForTimeout(3000)
			let settled = await snap()
			diff(first, settled, 'entering')
			jumpsChecked++
			const steps = await page.evaluate(() => Number(window.__deck.getCurrentSlide().dataset.steps ?? 0))
			for (let k = 1; k <= steps; k++) {
				await page.evaluate(() => window.__deck.next())
				await page.waitForTimeout(40)
				const early = await snap()
				await page.waitForTimeout(2000)
				const late = await snap()
				diff(settled, early, `beat ${k} start`)
				diff(early, late, `beat ${k}`)
				settled = late
				jumpsChecked++
			}
		}
		console.log(`reflow audit: ${jumpsChecked} slide entries and beats checked`)
	}
	await page.evaluate(() => window.__deck.slide(0))
	// Walk with motion on to catch runtime errors in animations.
	for (let i = 0; i < 80; i++) {
		await page.keyboard.press('ArrowRight')
		await page.waitForTimeout(30)
	}
	await page.evaluate(() => window.__deck.slide(0))
	const [popup] = await Promise.all([context.waitForEvent('page'), page.keyboard.press('s')])
	await popup.waitForLoadState()
	await popup.waitForTimeout(1500)
	const notesText = await popup.evaluate(() => document.querySelector('.speaker-controls-notes')?.textContent ?? '')
	if (!/React Summit/.test(notesText)) fail(`speaker view does not show the title notes: ${notesText.slice(0, 80)}`)
	await context.close()
}

if (buzzSigs.size !== 1) fail(`buzz wall layout differs between loads: ${[...buzzSigs].join(', ')}`)
// PWA: serve the built app the way Kody mounts it (mount stripped), let the service worker
// install, then cut the network and make sure the deck (and the speaker view) still load.
{
	const { createServer } = await import('node:http')
	const router = (await import(new URL('../app/router.ts', import.meta.url).href)).default
	const mount = '/the-last-software-engineer'
	const server = createServer(async (req, res) => {
		const url = new URL(req.url, 'http://localhost')
		if (url.pathname !== mount && !url.pathname.startsWith(`${mount}/`)) { res.writeHead(404); return res.end() }
		url.pathname = url.pathname.slice(mount.length) || '/'
		const response = await router.fetch(new Request(url, { method: req.method }))
		res.writeHead(response.status, Object.fromEntries(response.headers))
		res.end(Buffer.from(await response.arrayBuffer()))
	})
	await new Promise((done) => server.listen(0, done))
	const origin = `http://localhost:${server.address().port}`
	const context = await browser.newContext({ viewport: { width: 1280, height: 720 } })
	const page = await context.newPage()
	page.on('pageerror', (e) => fail(`pwa page error: ${e.message}`))
	await page.goto(`${origin}${mount}/`)
	await page.waitForFunction(() => window.__pwa?.state === 'ready' || window.__pwa?.state === 'error', null, { timeout: 15000 })
	const reg = await page.evaluate(() => ({ ...window.__pwa }))
	if (reg.state !== 'ready') fail(`service worker did not install: ${JSON.stringify(reg)}`)
	await page.reload()
	await page.waitForFunction(() => window.__deck?.isReady())
	if (!(await page.evaluate(() => !!navigator.serviceWorker.controller))) fail('service worker is not controlling the page after a reload')
	// Every file the hosted page references must be in the cache once the worker is ready.
	const cacheCheck = await page.evaluate(async () => {
		const refs = new Set()
		const html = await (await fetch(location.href)).text()
		for (const m of html.matchAll(/(?:src|href)="(a\/[^"]+)"|url\('(a\/[^']+)'\)/g)) refs.add(new URL(m[1] ?? m[2], location.href).href)
		const missing = []
		for (const url of refs) if (!(await caches.match(url))) missing.push(url)
		return { refs: refs.size, missing }
	})
	if (!cacheCheck.refs) fail('hosted page references no a/ files (assets inlined again?)')
	if (cacheCheck.missing.length) fail(`not precached: ${cacheCheck.missing.join(', ')}`)
	await context.setOffline(true)
	const offline = []
	for (const path of [`${mount}/`, mount, `${mount}/#/curve`, `${mount}/#/sustainability`]) {
		const tab = await context.newPage()
		const ok = await tab.goto(`${origin}${path}`).then(() => tab.waitForFunction(() => window.__deck?.isReady(), null, { timeout: 8000 })).then(() => true, () => false)
		const r = ok && (await tab.evaluate(async () => {
			await Promise.all([...document.images].map((i) => (i.complete ? null : new Promise((d) => { i.onload = i.onerror = d }))))
			const loaded = await Promise.all(['700 60px "Bricolage Grotesque"', '400 40px "Wix Madefor Text"'].map((f) => document.fonts.load(f).then((x) => x.length > 0)))
			return { broken: [...document.images].filter((i) => !i.naturalWidth).length, images: document.images.length, fonts: loaded.every(Boolean) }
		}))
		const good = ok && r.broken === 0 && r.images > 20 && r.fonts
		offline.push(`${path}: ${good ? 'ok' : `FAILED ${JSON.stringify(r)}`}`)
		if (!good) fail(`offline load of ${path} failed: ${JSON.stringify(r)}`)
		await tab.close()
	}
	await page.goto(`${origin}${mount}/`)
	await page.waitForFunction(() => window.__deck?.isReady())
	const [popup] = await Promise.all([context.waitForEvent('page'), page.keyboard.press('s')])
	await popup.waitForLoadState()
	await popup.waitForTimeout(2000)
	const speaker = await popup.evaluate(() => document.querySelector('.speaker-controls-notes')?.textContent ?? '')
	if (!/React Summit/.test(speaker)) fail('speaker view does not work offline')
	console.log(`pwa: worker ${reg.state}, scope ${reg.scope}; ${cacheCheck.refs} hashed files all precached; offline ${offline.join(', ')}; speaker view offline ${/React Summit/.test(speaker) ? 'ok' : 'FAILED'}`)
	await context.close()
	server.close()
}

await browser.close()
console.log(`Checked ${slideTotal} slides, ${beatTotal} beats, 2 themes x 2 sizes.`)
if (problems.length) {
	console.error(`${problems.length} problem(s):\n${[...new Set(problems)].join('\n')}`)
	process.exit(1)
}
console.log('verify: all good')