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/wait-keys.mjs — during a wait for a font, the keys a
2// person was editing with stay on the page, in Chromium and in WebKit
3// (Docs' review 8, adopted). The editor is inert while it waits, so focus
4// is on the page itself, where Backspace is the browser's: WebKit went
5// back in history and the waiting change was lost. Space and the arrows
6// scrolled.
7//
8// Run by internal/slidestest/fonts_engines_browser_test.go with the same
9// SLIDES_ENGINES_CONFIG as first-reveal.mjs.
10//
11// Each engine: the deck list, then an editor (so there is a page to go
12// back to and a page to scroll), with the code face two seconds away:
13//
14// 1. Add a code block; during the wait press Backspace, Space, Page
15// Down, the arrows, Home and End. The page stays where it is and the
16// code block lands.
17// 2. Add a code block; during the wait leaving the page asks first
18// (beforeunload). Staying, the code block lands.
19
20const { chromium, webkit } = await import('playwright')
21
22const config = JSON.parse(process.env.SLIDES_ENGINES_CONFIG)
23const pause = (ms) => new Promise((r) => setTimeout(r, ms))
24
25const addCode = () => {
26 document.querySelector('[slides-menu="insert"]').open = true
27 document.querySelector('[slides-menu="insert"] [data-insert-block="code"]').click()
28}
29const state = () => ({
30 path: location.pathname,
31 y: Math.round(scrollY),
32 waiting: document.querySelector('[slides-fonts-status]').getAttribute('data-state') === 'loading',
33 codes: document.querySelectorAll('[slides-stage] [data-block="code"]').length,
34})
35
36const failures = []
37for (const [engineName, engine] of Object.entries({ chromium, webkit }).filter(([n]) => !process.env.SLIDES_ENGINES_ENGINE || n === process.env.SLIDES_ENGINES_ENGINE)) {
38 // Chromium is the full browser in its new headless mode (channel
39 // 'chromium'), not Playwright's default headless shell: the shell
40 // re-rasterises a few glyph edges differently when a late editor
41 // replaces a text's DOM with an identical one, which no browser a
42 // person uses does, and these checks compare pixels exactly.
43 const browser = await engine.launch(engineName === 'chromium' ? { headless: true, channel: 'chromium' } : { headless: true })
44 for (const leave of [false, true]) {
45 const label = `${engineName}, ${leave ? 'leaving the page' : 'keys'} during a wait`
46 const context = await browser.newContext({ viewport: { width: 1280, height: 700 }, deviceScaleFactor: 1 })
47 await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' })))
48 await context.route('**/*', async (route) => {
49 if (/LiberationMono-/.test(route.request().url())) {
50 const response = await route.fetch()
51 await pause(2500)
52 try { await route.fulfill({ response }) } catch {}
53 return
54 }
55 return route.continue()
56 })
57 const page = await context.newPage()
58 const deck = config.decks.plain.shift()
59 await page.goto(config.origin + '/')
60 await page.goto(config.origin + '/d/' + deck + '/edit')
61 await page.waitForSelector('[slides-stage] [contenteditable="true"]', { timeout: 15000 })
62 await page.waitForFunction(() => document.documentElement.getAttribute('data-slides-fonts') === 'shown')
63 await pause(300)
64 const path = await page.evaluate(() => location.pathname)
65 await page.evaluate(addCode)
66 let dialog = null
67 if (leave) {
68 page.once('dialog', (d) => { dialog = d.type(); d.dismiss().catch(() => {}) })
69 await page.close({ runBeforeUnload: true })
70 await pause(500)
71 if (page.isClosed()) failures.push(`${label}: the page closed during the wait without asking`)
72 else if (dialog !== 'beforeunload') failures.push(`${label}: leaving during the wait did not ask first (${dialog})`)
73 } else {
74 for (const key of ['Backspace', 'Space', 'PageDown', 'ArrowDown', 'ArrowRight', 'End', 'Home', 'Backspace']) {
75 await page.keyboard.press(key)
76 await pause(60)
77 }
78 const during = await page.evaluate(state)
79 if (!during.waiting) failures.push(`${label}: the wait was over before the keys were pressed, so this proves nothing`)
80 if (during.path !== path) failures.push(`${label}: a key left the page for ${during.path}`)
81 if (during.y !== 0) failures.push(`${label}: a key scrolled the page to ${during.y}`)
82 }
83 if (!page.isClosed()) {
84 await page.waitForFunction(() => document.querySelectorAll('[slides-stage] [data-block="code"]').length > 0, null, { timeout: 10000 }).catch(() => {})
85 const after = await page.evaluate(state).catch(() => null)
86 if (!after || after.path !== path || after.codes !== 1) failures.push(`${label}: after the wait the page is ${JSON.stringify(after)}; want the editor with one code block`)
87 console.log(JSON.stringify({ engine: engineName, scenario: label, after, dialog }))
88 }
89 await context.close()
90 }
91 await browser.close()
92}
93if (failures.length) {
94 console.error(failures.join('\n'))
95 process.exit(1)
96}
97