| 1 | // fixtures/engines/first-reveal.mjs — once a slide's text is on screen, |
| 2 | // nothing about it changes, in Chromium and in WebKit, on every path: |
| 3 | // a script missing, the editor late, a family late, a change waiting for |
| 4 | // its family, nothing wrong (Paul, 2026-10-26: "No flashing, ever!"). |
| 5 | // Docs' fixtures/engines/first-reveal.mjs, adopted to Slides' surfaces. |
| 6 | // |
| 7 | // Run by internal/slidestest/fonts_engines_browser_test.go, which serves |
| 8 | // Slides through its production serving stack and passes the origin, a |
| 9 | // signed-in cookie, the decks and the DOM probe in SLIDES_ENGINES_CONFIG. |
| 10 | // |
| 11 | // Each scenario captures the slide about every 50 ms for several |
| 12 | // seconds, without Playwright's wait for document.fonts.ready, so the |
| 13 | // interval before a slow font is observed too. The verdict: every |
| 14 | // capture is either "nothing painted yet" (the slide hidden: no ink on |
| 15 | // it) before the first capture with text, or exactly the final picture. |
| 16 | // A capture that changes, disappears or blanks after text has appeared |
| 17 | // fails. A change made during the run (an action) may show the picture |
| 18 | // before it or the final picture, never a third. |
| 19 | // |
| 20 | // Two controls must fail the same verdicts in each engine, or this whole |
| 21 | // check fails as blind: a page that hides the slide for a second after |
| 22 | // it is shown, and a run put on the slide in a face that has not loaded |
| 23 | // (which the DOM probe, not the pixels, must see). |
| 24 | |
| 25 | process.env.PW_TEST_SCREENSHOT_NO_FONTS_READY = '1' |
| 26 | const { chromium, webkit } = await import('playwright') |
| 27 | const { PNG } = await import('pngjs') |
| 28 | const { createHash } = await import('node:crypto') |
| 29 | const { writeFileSync } = await import('node:fs') |
| 30 | |
| 31 | const config = JSON.parse(process.env.SLIDES_ENGINES_CONFIG) |
| 32 | const pause = (ms) => new Promise((r) => setTimeout(r, ms)) |
| 33 | |
| 34 | const scenarios = [ |
| 35 | { name: 'viewer, nothing missing', deck: 'paper', path: (s) => '/d/' + s + '?slide=1' }, |
| 36 | { name: 'viewer, the serif late', deck: 'paper', path: (s) => '/d/' + s + '?slide=1', slowFont: /LiberationSerif-/, fontDelay: 8000, length: 10000 }, |
| 37 | { name: 'viewer, no gate', deck: 'paper', path: (s) => '/d/' + s + '?slide=1', block: /\/static\/fontsgate\./, length: 7500 }, |
| 38 | { name: 'viewer, no hold', deck: 'paper', path: (s) => '/d/' + s + '?slide=1', block: /\/static\/fontshold\./ }, |
| 39 | { name: 'projector, nothing missing', deck: 'projector', path: (s) => s }, |
| 40 | { name: 'projector, the code late', deck: 'projector', path: (s) => s, slowFont: /LiberationMono-/, fontDelay: 8000, length: 10000 }, |
| 41 | { name: 'editor, nothing missing', deck: 'paper', path: (s) => '/d/' + s + '/edit', editor: true }, |
| 42 | { name: 'editor later than the layout deadline', deck: 'edges', path: (s) => '/d/' + s + '/edit', editor: true, editorDelay: 5000, length: 8000 }, |
| 43 | // Every shape of text the renderer and the editor could draw |
| 44 | // differently — no paragraphs, one empty, empties before, between and |
| 45 | // after words — in every type the editor binds and in table cells, on |
| 46 | // generated decks, with the editor late (Astra's finding 3 on #27). |
| 47 | ...[1, 2, 3, 4, 5, 6].map((n) => ({ name: `editor later than the layout deadline, generated deck ${n}`, deck: 'generated', path: (s) => '/d/' + s + '/edit', editor: true, editorDelay: 5000, length: 7000 })), |
| 48 | // A change that needs a family nobody has loaded, its files two |
| 49 | // seconds away (inside the four seconds a change waits): the picture |
| 50 | // before it, then the final one, and the wait said at once. |
| 51 | { name: 'add a code block, its font late', deck: 'plain', path: (s) => '/d/' + s + '/edit', editor: true, slowFont: /LiberationMono-/, fontDelay: 2000, length: 5000, action: 'add-code', busy: true }, |
| 52 | { name: 'the theme\'s serif heading, its font late', deck: 'systemHeading', path: (s) => '/d/' + s + '/edit', editor: true, slowFont: /LiberationSerif-/, fontDelay: 2000, length: 5000, action: 'theme-heading', busy: true }, |
| 53 | // The prefetch has brought the family in: no wait at all. |
| 54 | { name: 'add a code block once the prefetch is done', deck: 'plain', path: (s) => '/d/' + s + '/edit', editor: true, length: 3000, action: 'add-code', prefetched: true, busy: false }, |
| 55 | // The probe's control: a run in a face that is loading, on purpose. |
| 56 | { name: 'control: text in a face that has not loaded', deck: 'plain', path: (s) => '/d/' + s + '?slide=1', length: 2500, probeControl: true }, |
| 57 | // The control: shown, then hidden for a second. Must be caught. |
| 58 | { name: 'control: hidden for a second after it is shown', deck: 'paper', path: (s) => '/d/' + s + '?slide=1', control: true }, |
| 59 | ] |
| 60 | |
| 61 | const only = process.env.SLIDES_ENGINES_ONLY |
| 62 | const chosen = only ? scenarios.filter((s) => s.control || s.probeControl || s.name.includes(only)) : scenarios |
| 63 | |
| 64 | // unloadedOnce puts a run on the slide, once it is shown, in a face |
| 65 | // added to the page that is still loading. |
| 66 | const unloadedOnce = `(() => { |
| 67 | const tick = () => { |
| 68 | const s = document.querySelector('[slides-slide]') |
| 69 | if (s && document.documentElement.getAttribute('data-slides-fonts') === 'shown') { |
| 70 | const face = new FontFace('Liberation Mono', 'url("/static/fonts/LiberationMono-Regular.woff2?probe-control")', { weight: '400', style: 'normal' }) |
| 71 | document.fonts.add(face) |
| 72 | face.load().catch(() => {}) |
| 73 | const p = document.createElement('p'); p.style.fontFamily = '"Liberation Mono"'; p.textContent = 'Not loaded yet' |
| 74 | s.querySelector('[slides-block]').append(p) |
| 75 | return |
| 76 | } |
| 77 | requestAnimationFrame(tick) |
| 78 | } |
| 79 | requestAnimationFrame(tick) |
| 80 | })()` |
| 81 | |
| 82 | // flashOnce is the control's deliberate flash. |
| 83 | const flashOnce = `(() => { |
| 84 | const tick = () => { |
| 85 | const s = document.querySelector('[slides-slide]') |
| 86 | if (s && getComputedStyle(s).visibility === 'visible' && s.textContent.trim()) { |
| 87 | setTimeout(() => { |
| 88 | s.style.setProperty('visibility', 'hidden', 'important') |
| 89 | setTimeout(() => s.style.removeProperty('visibility'), 1000) |
| 90 | }, 600) |
| 91 | return |
| 92 | } |
| 93 | requestAnimationFrame(tick) |
| 94 | } |
| 95 | requestAnimationFrame(tick) |
| 96 | })()` |
| 97 | |
| 98 | const ACTIONS = { |
| 99 | 'add-code': () => { |
| 100 | document.querySelector('[slides-menu="insert"]').open = true |
| 101 | document.querySelector('[slides-menu="insert"] [data-insert-block="code"]').click() |
| 102 | }, |
| 103 | 'theme-heading': () => { |
| 104 | const s = document.querySelector('#rst-deck-font-heading') |
| 105 | s.value = '' |
| 106 | s.dispatchEvent(new Event('change', { bubbles: true })) |
| 107 | }, |
| 108 | } |
| 109 | |
| 110 | // The slide's box, once it is laid out: what every capture is clipped to. |
| 111 | const slideBox = () => { |
| 112 | const s = document.querySelector('[slides-stage] [slides-slide]') || document.querySelector('[slides-slide]') |
| 113 | if (!s) return null |
| 114 | const r = s.getBoundingClientRect() |
| 115 | // Laid out by slides.css (a 16:9 box), not the unstyled document a |
| 116 | // capture can catch before the stylesheets are in. |
| 117 | if (r.width < 50 || r.height < 50 || Math.abs(r.width / r.height - 16 / 9) > 0.05) return null |
| 118 | return { x: Math.max(0, Math.floor(r.left)), y: Math.max(0, Math.floor(r.top)), width: Math.floor(r.width), height: Math.floor(r.height) } |
| 119 | } |
| 120 | |
| 121 | function verdict(frames) { |
| 122 | const text = frames.findIndex((f) => !f.blank) |
| 123 | if (text < 0) return 'the slide was never seen with text on it' |
| 124 | const last = frames[frames.length - 1] |
| 125 | if (last.blank) return `the slide was blank at the end (${last.ms}ms)` |
| 126 | for (const f of frames.slice(text)) { |
| 127 | if (f.blank) return `text was on screen at ${frames[text].ms}ms, then gone at ${f.ms}ms` |
| 128 | if (f.hash !== last.hash) return `at ${f.ms}ms the slide was ${f.hash.slice(0, 8)}; finally ${last.hash.slice(0, 8)}` |
| 129 | } |
| 130 | return null |
| 131 | } |
| 132 | |
| 133 | function actionVerdict(frames, before, busy) { |
| 134 | const after = frames.filter((f) => f.afterAction) |
| 135 | const last = after[after.length - 1] |
| 136 | if (!last) return 'nothing was captured after the action' |
| 137 | if (last.ink === before.ink) return 'the action never took effect' |
| 138 | if (busy && !after[0].busy) return 'the wait for the font was not said at once' |
| 139 | if (!busy && after.some((f) => f.busy)) return 'the action waited for a font the prefetch had brought in' |
| 140 | for (const f of after) { |
| 141 | if (f.blank) return `the slide was blank at ${f.ms}ms` |
| 142 | if (f.ink !== before.ink && f.ink !== last.ink) return `at ${f.ms}ms text that was neither the text before the action nor the final text` |
| 143 | } |
| 144 | return null |
| 145 | } |
| 146 | |
| 147 | const failures = [] |
| 148 | const engines = Object.entries({ chromium, webkit }).filter(([n]) => !process.env.SLIDES_ENGINES_ENGINE || n === process.env.SLIDES_ENGINES_ENGINE) |
| 149 | for (const [engineName, engine] of engines) { |
| 150 | // Chromium is the full browser in its new headless mode (channel |
| 151 | // 'chromium'), not Playwright's default headless shell: the shell |
| 152 | // re-rasterises a few glyph edges differently when a late editor |
| 153 | // replaces a text's DOM with an identical one, which no browser a |
| 154 | // person uses does, and these checks compare pixels exactly. |
| 155 | const browser = await engine.launch(engineName === 'chromium' ? { headless: true, channel: 'chromium' } : { headless: true }) |
| 156 | for (const s of chosen) { |
| 157 | const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) |
| 158 | await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) |
| 159 | if (s.control) await context.addInitScript(flashOnce) |
| 160 | if (s.probeControl) await context.addInitScript(unloadedOnce) |
| 161 | await context.addInitScript(config.probe) |
| 162 | await context.route('**/*', async (route) => { |
| 163 | const url = route.request().url() |
| 164 | if (s.block && s.block.test(url)) return route.abort() |
| 165 | if (url.includes('probe-control')) { |
| 166 | await pause(8000) |
| 167 | return route.continue().catch(() => {}) |
| 168 | } |
| 169 | const editorJS = /\/static\/editor\.[0-9a-f.]+\.js/.test(url) |
| 170 | const wait = s.editorDelay && editorJS ? s.editorDelay : s.slowFont && s.slowFont.test(url) ? s.fontDelay : 0 |
| 171 | if (wait) { |
| 172 | const response = await route.fetch() |
| 173 | await pause(wait) |
| 174 | try { await route.fulfill({ response }) } catch {} |
| 175 | return |
| 176 | } |
| 177 | return route.continue() |
| 178 | }) |
| 179 | const page = await context.newPage() |
| 180 | const target = config.decks[s.deck].shift() |
| 181 | if (!target) throw new Error(`no fresh ${s.deck} deck left for ${engineName}, ${s.name}`) |
| 182 | await page.goto(config.origin + s.path(target), { waitUntil: 'commit' }) |
| 183 | const label = `${engineName}, ${s.name}` |
| 184 | const frames = [] |
| 185 | const skipped = { clip: 0, state: 0, shot: 0 } |
| 186 | let before = null |
| 187 | if (s.action) { |
| 188 | await page.waitForSelector('[slides-stage] [contenteditable="true"]', { timeout: 15000 }) |
| 189 | await page.waitForFunction(() => document.documentElement.getAttribute('data-slides-fonts') === 'shown', null, { timeout: 15000 }) |
| 190 | if (s.prefetched) await page.waitForFunction(() => document.documentElement.getAttribute('data-slides-fonts-prefetch') === 'done', null, { timeout: 60000 }) |
| 191 | await pause(800) |
| 192 | } |
| 193 | let clip = null |
| 194 | const start = Date.now() |
| 195 | let acted = false |
| 196 | while (Date.now() - start < (s.length || (s.editorDelay || 0) + 3500)) { |
| 197 | if (s.action && !acted && before) { |
| 198 | await page.evaluate(ACTIONS[s.action]) |
| 199 | acted = true |
| 200 | } |
| 201 | const ms = Date.now() - start |
| 202 | if (!clip) clip = await page.evaluate(slideBox).catch(() => null) |
| 203 | if (!clip) { skipped.clip++; await pause(20); continue } |
| 204 | const state = await page.evaluate(() => { |
| 205 | const sl = document.querySelector('[slides-stage] [slides-slide]') || document.querySelector('[slides-slide]') |
| 206 | const st = document.querySelector('[slides-fonts-status]') |
| 207 | return sl ? { visible: getComputedStyle(sl).visibility === 'visible', busy: !!st && ['loading', 'refused'].includes(st.getAttribute('data-state')) } : null |
| 208 | }).catch(() => null) |
| 209 | if (!state) { skipped.state++; continue } |
| 210 | // Nothing is added to the page for the capture: a stylesheet |
| 211 | // inserted per screenshot makes WebKit rebuild its font set (Docs' |
| 212 | // review 7). |
| 213 | const raw = await page.screenshot({ clip, caret: 'hide', timeout: 2000 }).catch(() => null) |
| 214 | if (!raw) { skipped.shot++; continue } |
| 215 | const shot = PNG.sync.read(raw) |
| 216 | // The background is the commonest colour in a sparse sample; ink |
| 217 | // is every pixel well away from it. The ink mask is compared for |
| 218 | // an action, so a selection or focus ring that the editor draws a |
| 219 | // frame later is not the text changing. |
| 220 | const counts = new Map() |
| 221 | for (let i = 0; i < shot.data.length; i += 4 * 17) { |
| 222 | const k = (shot.data[i] >> 3) << 10 | (shot.data[i + 1] >> 3) << 5 | (shot.data[i + 2] >> 3) |
| 223 | counts.set(k, (counts.get(k) || 0) + 1) |
| 224 | } |
| 225 | let bg = 0 |
| 226 | let most = -1 |
| 227 | for (const [k, n] of counts) if (n > most) { most = n; bg = k } |
| 228 | const [br, bgc, bb] = [((bg >> 10) & 31) << 3, ((bg >> 5) & 31) << 3, (bg & 31) << 3] |
| 229 | const mask = Buffer.alloc(shot.data.length / 4) |
| 230 | let ink = 0 |
| 231 | for (let i = 0; i < shot.data.length; i += 4) { |
| 232 | const d = Math.abs(shot.data[i] - br) + Math.abs(shot.data[i + 1] - bgc) + Math.abs(shot.data[i + 2] - bb) |
| 233 | if (d > 160) { mask[i / 4] = 1; ink++ } |
| 234 | } |
| 235 | const blank = !state.visible || ink < 20 |
| 236 | if (process.env.SLIDES_ENGINES_SHOTS && !blank) writeFileSync(`${process.env.SLIDES_ENGINES_SHOTS}/${engineName}-${s.name.replace(/\W+/g, '_')}-${frames.length}.png`, raw) |
| 237 | const frame = { ms, blank, hash: createHash('sha256').update(shot.data).digest('hex'), ink: createHash('sha256').update(mask).digest('hex'), busy: state.busy, afterAction: acted } |
| 238 | if (s.action && !acted && !blank) before = frame |
| 239 | frames.push(frame) |
| 240 | } |
| 241 | let v = s.action ? actionVerdict(frames, before || frames[0], s.busy) : verdict(frames) |
| 242 | const violations = await page.evaluate(() => (window.__fontProbe ? window.__fontProbe.violations : null)).catch(() => null) |
| 243 | if (!v && violations && violations.length) v = `text visible in a face that had not loaded: ${violations[0]}` |
| 244 | const transitions = frames.filter((f, i) => !i || f.hash !== frames[i - 1].hash || f.blank !== frames[i - 1].blank).map((f) => `${f.ms}${f.afterAction ? '*' : ''}:${f.blank ? 'blank' : f.hash.slice(0, 6)}`) |
| 245 | console.log(JSON.stringify({ engine: engineName, scenario: s.name, frames: frames.length, skipped, verdict: v, transitions: transitions.slice(0, 12) })) |
| 246 | if (s.probeControl) { |
| 247 | if (!violations || !violations.length) failures.push(`${label}: the font probe did not see text in a face that had not loaded; it is blind`) |
| 248 | } else if (s.control && !v) failures.push(`${label}: the check did not see the control's flash; it is blind`) |
| 249 | else if (!s.control && v) failures.push(`${label}: ${v}`) |
| 250 | await context.close() |
| 251 | } |
| 252 | await browser.close() |
| 253 | } |
| 254 | if (failures.length) { |
| 255 | console.error(failures.join('\n')) |
| 256 | process.exit(1) |
| 257 | } |
| 258 | |