oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/engines/navigate.mjs — moving between slides never shows a
2// frame without a slide, in Chromium and in WebKit (Astra's finding 4 on
3// #27; Paul, 2026-10-26: "No flashing, ever!" — a visible gap counts).
4//
5// Run by internal/slidestest/fonts_engines_browser_test.go with the same
6// SLIDES_ENGINES_CONFIG as first-reveal.mjs. Every font file is held
7// 800 ms on the way out, so a page load — whose slide the font gate
8// hides until its faces are confirmed — would show an empty stage for
9// most of a second.
10//
11// Each scenario opens a surface, waits for its first slide, then pages:
12// the viewer's Next, Next, Previous and the browser's Back; the same in
13// the viewer's scriptless presenting mode; the presenter's own buttons
14// on the projector; and a next slide whose family the page has not
15// loaded (the deck changed after the page was opened). Captures every
16// ~50 ms. The verdict: from the first capture with text, no capture is
17// blank, and every picture captured is one the slide rested on (seen in
18// at least three captures) — never a half-drawn one in between.
19//
20// The control pages by loading the page, as the links do without a
21// script. It must fail the verdict in each engine, or the check is blind.
22
23process.env.PW_TEST_SCREENSHOT_NO_FONTS_READY = '1'
24const { chromium, webkit } = await import('playwright')
25const { PNG } = await import('pngjs')
26const { createHash } = await import('node:crypto')
27
28const config = JSON.parse(process.env.SLIDES_ENGINES_CONFIG)
29const pause = (ms) => new Promise((r) => setTimeout(r, ms))
30
31const click = (sel) => `document.querySelector(${JSON.stringify(sel)}).click()`
32const submit = (sel) => `document.querySelector(${JSON.stringify(sel)}).form.requestSubmit()`
33const scenarios = [
34 { name: 'viewer: next, next, previous, back', deck: 'three', path: (s) => `/d/${s}?slide=1`,
35 steps: [click('[slides-pager] a[rel="next"]'), click('[slides-pager] a[rel="next"]'), click('[slides-pager] a[rel="prev"]'), 'history.back()'] },
36 { name: 'presenting by link: next, next, previous, back', deck: 'three', path: (s) => `/d/${s}?slide=1&present=1`,
37 steps: [click('[slides-pager] a[rel="next"]'), click('[slides-pager] a[rel="next"]'), click('[slides-pager] a[rel="prev"]'), 'history.back()'] },
38 { name: 'projector: the presenter\'s next and previous', deck: 'projectorToken', path: (s) => s,
39 steps: [submit('[slides-bar] form input[value="next"]'), submit('[slides-bar] form input[value="next"]'), submit('[slides-bar] form input[value="prev"]')] },
40 { name: 'viewer: a next slide whose family is not in', deck: 'plainThree', path: (s) => `/d/${s}?slide=1`, addCode: true, slowFont: /LiberationMono-/, fontDelay: 2000,
41 steps: [click('[slides-pager] a[rel="next"]')], gap: 3000 },
42 { name: 'control: paging by loading the page', deck: 'three', path: (s) => `/d/${s}?slide=1`, control: true,
43 steps: [`location.assign(document.querySelector('[slides-pager] a[rel="next"]').href)`] },
44]
45
46const slideBox = () => {
47 const s = document.querySelector('[slides-slide]')
48 if (!s) return null
49 const r = s.getBoundingClientRect()
50 if (r.width < 50 || r.height < 50 || Math.abs(r.width / r.height - 16 / 9) > 0.05) return null
51 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) }
52}
53
54function verdict(frames) {
55 const first = frames.findIndex((f) => !f.blank)
56 if (first < 0) return 'the slide was never seen with text on it'
57 const counts = new Map()
58 for (const f of frames.slice(first)) counts.set(f.hash, (counts.get(f.hash) || 0) + 1)
59 for (const f of frames.slice(first)) {
60 if (f.blank) return `a slide was on screen at ${frames[first].ms}ms and the stage was empty at ${f.ms}ms`
61 if (counts.get(f.hash) < 3) return `at ${f.ms}ms a picture the slide never rested on (${f.hash.slice(0, 8)})`
62 }
63 const rested = new Set(frames.slice(first).map((f) => f.hash))
64 if (rested.size < 2) return 'the slide never changed'
65 return null
66}
67
68const failures = []
69const engines = Object.entries({ chromium, webkit }).filter(([n]) => !process.env.SLIDES_ENGINES_ENGINE || n === process.env.SLIDES_ENGINES_ENGINE)
70for (const [engineName, engine] of engines) {
71 // Chromium is the full browser in its new headless mode (channel
72 // 'chromium'), not Playwright's default headless shell: the shell
73 // re-rasterises a few glyph edges differently when a late editor
74 // replaces a text's DOM with an identical one, which no browser a
75 // person uses does, and these checks compare pixels exactly.
76 const browser = await engine.launch(engineName === 'chromium' ? { headless: true, channel: 'chromium' } : { headless: true })
77 for (const s of scenarios) {
78 const label = `${engineName}, ${s.name}`
79 const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 })
80 await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' })))
81 await context.addInitScript(config.probe)
82 await context.route('**/*', async (route) => {
83 const url = route.request().url()
84 const wait = /\/static\/fonts\//.test(url) ? (s.slowFont && s.slowFont.test(url) ? s.fontDelay : 800) : 0
85 if (wait) {
86 const response = await route.fetch()
87 await pause(wait)
88 try { await route.fulfill({ response }) } catch {}
89 return
90 }
91 return route.continue()
92 })
93 const page = await context.newPage()
94 const target = config.decks[s.deck].shift()
95 if (!target) throw new Error(`no fresh ${s.deck} deck left for ${label}`)
96 await page.goto(config.origin + s.path(target.path || target))
97 await page.waitForFunction(() => document.documentElement.getAttribute('data-slides-fonts') === 'shown', null, { timeout: 15000 })
98 if (s.addCode) {
99 // The deck changes after the page is open: slide 2 gains code.
100 const res = await fetch(`${config.origin}/api/v1/decks/${target}/ops`, {
101 method: 'POST',
102 headers: { Authorization: `Bearer ${config.token}`, 'Content-Type': 'application/json' },
103 body: JSON.stringify({ ops: [{ op: 'add_block', slide: 'slidetwo0002', type: 'code', region: 'body', markdown: 'late(code)' }] }),
104 })
105 if (!res.ok) throw new Error(`${label}: ops ${res.status}`)
106 }
107 await pause(1200) // the prefetch of the neighbours
108 let clip = null
109 const frames = []
110 const start = Date.now()
111 const gap = s.gap || 1100
112 let step = 0
113 while (Date.now() - start < gap * (s.steps.length + 1)) {
114 const ms = Date.now() - start
115 if (step < s.steps.length && ms >= gap * (step + 0.5)) {
116 await page.evaluate(s.steps[step]).catch(() => {})
117 step++
118 }
119 if (!clip) clip = await page.evaluate(slideBox).catch(() => null)
120 if (!clip) continue
121 const visible = await page.evaluate(() => {
122 const sl = document.querySelector('[slides-slide]')
123 return !!sl && getComputedStyle(sl).visibility === 'visible'
124 }).catch(() => false)
125 const raw = await page.screenshot({ clip, caret: 'hide', timeout: 2000 }).catch(() => null)
126 if (!raw) continue
127 const shot = PNG.sync.read(raw)
128 const counts = new Map()
129 for (let i = 0; i < shot.data.length; i += 4 * 17) {
130 const k = (shot.data[i] >> 3) << 10 | (shot.data[i + 1] >> 3) << 5 | (shot.data[i + 2] >> 3)
131 counts.set(k, (counts.get(k) || 0) + 1)
132 }
133 let bg = 0
134 let most = -1
135 for (const [k, n] of counts) if (n > most) { most = n; bg = k }
136 const [br, bgc, bb] = [((bg >> 10) & 31) << 3, ((bg >> 5) & 31) << 3, (bg & 31) << 3]
137 let ink = 0
138 for (let i = 0; i < shot.data.length; i += 4) {
139 if (Math.abs(shot.data[i] - br) + Math.abs(shot.data[i + 1] - bgc) + Math.abs(shot.data[i + 2] - bb) > 160) ink++
140 }
141 frames.push({ ms, blank: !visible || ink < 20, hash: createHash('sha256').update(shot.data).digest('hex') })
142 }
143 let v = verdict(frames)
144 const probe = await page.evaluate(() => window.__fontProbe || null).catch(() => null)
145 if (!v && probe && probe.violations.length) v = `text visible in a face that had not loaded: ${probe.violations[0]}`
146 if (!v && probe && probe.errors && probe.errors.length) v = `the page: ${probe.errors[0]}`
147 if (!v && probe && probe.gaps && probe.gaps.length) v = `the DOM probe saw slide text gone at ${probe.gaps[0]}ms`
148 const transitions = frames.filter((f, i) => !i || f.hash !== frames[i - 1].hash || f.blank !== frames[i - 1].blank).map((f) => `${f.ms}:${f.blank ? 'blank' : f.hash.slice(0, 6)}`)
149 console.log(JSON.stringify({ engine: engineName, scenario: s.name, frames: frames.length, verdict: v, transitions: transitions.slice(0, 14) }))
150 if (s.control && !v) failures.push(`${label}: the check did not see the page load's empty stage; it is blind`)
151 else if (!s.control && v) failures.push(`${label}: ${v}`)
152 await context.close()
153 }
154 await browser.close()
155}
156if (failures.length) {
157 console.error(failures.join('\n'))
158 process.exit(1)
159}
160