oficina / estilo Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fold.browser.mjs: drive the side's fold and rail tooltips in each
2// engine and report what happened. Driven by side_browser_test.go (go
3// test -tags browser ./side), which serves the pages and makes every
4// assertion; this file only drives the browsers and reports, as
5// {name, got, want} pairs.
6//
7// ESTILO_PLAYWRIGHT=<path to the playwright package> \
8// node fold.browser.mjs <job JSON>
9import { createRequire } from 'node:module'
10
11const require = createRequire(import.meta.url)
12const pw = require(process.env.ESTILO_PLAYWRIGHT || 'playwright')
13const job = JSON.parse(process.argv[2])
14
15// focused names the focused element by what a test can tell apart.
16const focused = () => {
17 const a = document.activeElement
18 if (!a || a === document.body) return 'body'
19 for (const n of ['estilo-side-fold-to', 'estilo-side-holds']) if (a.hasAttribute(n)) return n + '=' + a.getAttribute(n)
20 if (a.hasAttribute('estilo-side-page')) return 'page ' + a.getAttribute('href')
21 return a.tagName.toLowerCase() + (a.id ? '#' + a.id : '')
22}
23const state = () => document.querySelector('[estilo-side-frame]')?.getAttribute('estilo-side-frame') ?? 'no frame'
24const tip = (href) => getComputedStyle(document.querySelector(`[estilo-side-item][href="${href}"]:not([hidden]) [estilo-side-label]`)).opacity
25
26// The shortcut's event, as the page saw it after every listener ran.
27const watchKeys = () => {
28 window.__keys = []
29 window.addEventListener('keydown', (e) => { if (e.key === '\\') window.__keys.push(e.defaultPrevented ? 'prevented' : 'left alone') })
30}
31
32async function run(browser, origin, checks) {
33 const check = (name, got, want) => checks.push({ name, got: String(got), want: String(want) })
34 const open = async (query, width = 1440) => {
35 const page = await browser.newPage({ viewport: { width, height: 700 } })
36 await page.goto(origin + '/' + query)
37 await page.waitForFunction(() => document.readyState === 'complete')
38 return page
39 }
40
41 // 1. The fold's own buttons, by keyboard: focus moves to the other one.
42 let page = await open('?state=open')
43 await page.focus('[estilo-side-fold-to="rail"]')
44 await page.keyboard.press('Enter')
45 check('Hide: state', await page.evaluate(state), 'rail')
46 check('Hide: focus', await page.evaluate(focused), 'estilo-side-fold-to=open')
47 await page.keyboard.press('Enter')
48 check('Show again by Enter: state', await page.evaluate(state), 'open')
49 check('Show again by Enter: focus', await page.evaluate(focused), 'estilo-side-fold-to=rail')
50 await page.close()
51
52 // 2. A menu row folds, closes its menu and returns focus to its title.
53 page = await open('?state=open')
54 await page.click('#view > summary')
55 await page.focus('#view-fold')
56 await page.keyboard.press('Enter')
57 check('menu row: state', await page.evaluate(state), 'rail')
58 check('menu row: menu closed', await page.evaluate(() => document.querySelector('#view').open), false)
59 check('menu row: focus', await page.evaluate(focused), 'summary')
60 await page.close()
61
62 // 3. The shortcut from a page row the rail hides: focus goes to the lit
63 // place; and back again from the rail.
64 page = await open('?state=open')
65 await page.evaluate(watchKeys)
66 await page.focus('[estilo-side-page][href="/p/a"]')
67 await page.keyboard.press('Control+Backslash')
68 check('shortcut from a page row: state', await page.evaluate(state), 'rail')
69 check('shortcut from a page row: focus', await page.evaluate(focused), 'estilo-side-holds=rail')
70 await page.keyboard.press('Control+Backslash')
71 check('shortcut from the rail place: state', await page.evaluate(state), 'open')
72 check('shortcut from the rail place: focus', await page.evaluate(focused), 'estilo-side-holds=open')
73 check('shortcut: event', await page.evaluate(() => window.__keys.join()), 'prevented,prevented')
74 await page.close()
75
76 // 4. Tooltips, on the explicit rail and the width's rail, with a fold
77 // and without one (Side.Fold nil): the rail and its tooltips are there
78 // either way.
79 for (const [label, query, width] of [['rail', '?state=rail', 1440], ['width rail', '?state=', 1000],
80 ['rail, no fold', '?state=rail&fold=no', 1440], ['width rail, no fold', '?state=&fold=no', 1000]]) {
81 page = await open(query, width)
82 // Keyboard focus shows it; Escape hides it and leaves focus.
83 await page.focus('[estilo-side-item][href="/search"]')
84 await page.keyboard.press('Shift')
85 check(`${label}: focus shows the tooltip`, await page.evaluate(tip, '/search'), '1')
86 await page.keyboard.press('Escape')
87 check(`${label}: Escape hides it`, await page.evaluate(tip, '/search'), '0')
88 check(`${label}: Escape keeps focus`, await page.evaluate(() => document.activeElement.getAttribute('href')), '/search')
89 // Pointer: hover the row, cross the gap to the tooltip, stay on it.
90 const row = await page.locator('[estilo-side-item][href="/search"]').boundingBox()
91 const y = row.y + row.height / 2
92 await page.mouse.move(row.x + row.width / 2, y)
93 check(`${label}: hover shows it again`, await page.evaluate(tip, '/search'), '1')
94 const box = await page.evaluate(() => {
95 const r = document.querySelector('[estilo-side-item][href="/search"] [estilo-side-label]').getBoundingClientRect()
96 return { x: r.x, y: r.y, w: r.width, h: r.height }
97 })
98 let lost = 0
99 for (let x = row.x + row.width - 2; x <= box.x + box.w / 2; x += 2) {
100 await page.mouse.move(x, box.y + box.h / 2)
101 if (await page.evaluate(tip, '/search') !== '1') lost++
102 }
103 check(`${label}: crossing to the tooltip keeps it`, lost, 0)
104 check(`${label}: the pointer is on the tooltip`, await page.evaluate(({ x, y }) =>
105 !!document.elementFromPoint(x, y)?.closest('[estilo-side-label]'), { x: box.x + box.w / 2, y: box.y + box.h / 2 }), true)
106 await page.keyboard.press('Escape')
107 check(`${label}: Escape hides it under the pointer`, await page.evaluate(tip, '/search'), '0')
108 await page.mouse.move(row.x + row.width / 2, y + 200)
109 await page.mouse.move(row.x + row.width / 2, y)
110 check(`${label}: a new hover shows it`, await page.evaluate(tip, '/search'), '1')
111 check(`${label}: the label names the link`, await page.getByRole('link', { name: 'Search', exact: true }).count(), 1)
112 await page.close()
113 }
114
115 // 5. No fold, and no frame: the shortcut does nothing and leaves the
116 // key event alone.
117 for (const [label, query] of [['no fold', '?state=open&fold=no'], ['no frame', '?frame=no']]) {
118 page = await open(query)
119 await page.evaluate(watchKeys)
120 await page.keyboard.press('Control+Backslash')
121 check(`${label}: state`, await page.evaluate(state), label === 'no fold' ? 'open' : 'no frame')
122 check(`${label}: event`, await page.evaluate(() => window.__keys.join()), 'left alone')
123 await page.close()
124 }
125}
126
127const results = []
128for (const engine of job.engines) {
129 const result = { engine, error: '', checks: [] }
130 results.push(result)
131 let browser
132 try {
133 browser = await pw[engine].launch()
134 await run(browser, job.origin, result.checks)
135 } catch (err) {
136 result.error = String(err && err.stack || err)
137 } finally {
138 if (browser) await browser.close()
139 }
140}
141process.stdout.write(JSON.stringify(results))
142