// fold.browser.mjs: drive the side's fold and rail tooltips in each // engine and report what happened. Driven by side_browser_test.go (go // test -tags browser ./side), which serves the pages and makes every // assertion; this file only drives the browsers and reports, as // {name, got, want} pairs. // // ESTILO_PLAYWRIGHT= \ // node fold.browser.mjs import { createRequire } from 'node:module' const require = createRequire(import.meta.url) const pw = require(process.env.ESTILO_PLAYWRIGHT || 'playwright') const job = JSON.parse(process.argv[2]) // focused names the focused element by what a test can tell apart. const focused = () => { const a = document.activeElement if (!a || a === document.body) return 'body' for (const n of ['estilo-side-fold-to', 'estilo-side-holds']) if (a.hasAttribute(n)) return n + '=' + a.getAttribute(n) if (a.hasAttribute('estilo-side-page')) return 'page ' + a.getAttribute('href') return a.tagName.toLowerCase() + (a.id ? '#' + a.id : '') } const state = () => document.querySelector('[estilo-side-frame]')?.getAttribute('estilo-side-frame') ?? 'no frame' const tip = (href) => getComputedStyle(document.querySelector(`[estilo-side-item][href="${href}"]:not([hidden]) [estilo-side-label]`)).opacity // The shortcut's event, as the page saw it after every listener ran. const watchKeys = () => { window.__keys = [] window.addEventListener('keydown', (e) => { if (e.key === '\\') window.__keys.push(e.defaultPrevented ? 'prevented' : 'left alone') }) } async function run(browser, origin, checks) { const check = (name, got, want) => checks.push({ name, got: String(got), want: String(want) }) const open = async (query, width = 1440) => { const page = await browser.newPage({ viewport: { width, height: 700 } }) await page.goto(origin + '/' + query) await page.waitForFunction(() => document.readyState === 'complete') return page } // 1. The fold's own buttons, by keyboard: focus moves to the other one. let page = await open('?state=open') await page.focus('[estilo-side-fold-to="rail"]') await page.keyboard.press('Enter') check('Hide: state', await page.evaluate(state), 'rail') check('Hide: focus', await page.evaluate(focused), 'estilo-side-fold-to=open') await page.keyboard.press('Enter') check('Show again by Enter: state', await page.evaluate(state), 'open') check('Show again by Enter: focus', await page.evaluate(focused), 'estilo-side-fold-to=rail') await page.close() // 2. A menu row folds, closes its menu and returns focus to its title. page = await open('?state=open') await page.click('#view > summary') await page.focus('#view-fold') await page.keyboard.press('Enter') check('menu row: state', await page.evaluate(state), 'rail') check('menu row: menu closed', await page.evaluate(() => document.querySelector('#view').open), false) check('menu row: focus', await page.evaluate(focused), 'summary') await page.close() // 3. The shortcut from a page row the rail hides: focus goes to the lit // place; and back again from the rail. page = await open('?state=open') await page.evaluate(watchKeys) await page.focus('[estilo-side-page][href="/p/a"]') await page.keyboard.press('Control+Backslash') check('shortcut from a page row: state', await page.evaluate(state), 'rail') check('shortcut from a page row: focus', await page.evaluate(focused), 'estilo-side-holds=rail') await page.keyboard.press('Control+Backslash') check('shortcut from the rail place: state', await page.evaluate(state), 'open') check('shortcut from the rail place: focus', await page.evaluate(focused), 'estilo-side-holds=open') check('shortcut: event', await page.evaluate(() => window.__keys.join()), 'prevented,prevented') await page.close() // 4. Tooltips, on the explicit rail and the width's rail, with a fold // and without one (Side.Fold nil): the rail and its tooltips are there // either way. for (const [label, query, width] of [['rail', '?state=rail', 1440], ['width rail', '?state=', 1000], ['rail, no fold', '?state=rail&fold=no', 1440], ['width rail, no fold', '?state=&fold=no', 1000]]) { page = await open(query, width) // Keyboard focus shows it; Escape hides it and leaves focus. await page.focus('[estilo-side-item][href="/search"]') await page.keyboard.press('Shift') check(`${label}: focus shows the tooltip`, await page.evaluate(tip, '/search'), '1') await page.keyboard.press('Escape') check(`${label}: Escape hides it`, await page.evaluate(tip, '/search'), '0') check(`${label}: Escape keeps focus`, await page.evaluate(() => document.activeElement.getAttribute('href')), '/search') // Pointer: hover the row, cross the gap to the tooltip, stay on it. const row = await page.locator('[estilo-side-item][href="/search"]').boundingBox() const y = row.y + row.height / 2 await page.mouse.move(row.x + row.width / 2, y) check(`${label}: hover shows it again`, await page.evaluate(tip, '/search'), '1') const box = await page.evaluate(() => { const r = document.querySelector('[estilo-side-item][href="/search"] [estilo-side-label]').getBoundingClientRect() return { x: r.x, y: r.y, w: r.width, h: r.height } }) let lost = 0 for (let x = row.x + row.width - 2; x <= box.x + box.w / 2; x += 2) { await page.mouse.move(x, box.y + box.h / 2) if (await page.evaluate(tip, '/search') !== '1') lost++ } check(`${label}: crossing to the tooltip keeps it`, lost, 0) check(`${label}: the pointer is on the tooltip`, await page.evaluate(({ x, y }) => !!document.elementFromPoint(x, y)?.closest('[estilo-side-label]'), { x: box.x + box.w / 2, y: box.y + box.h / 2 }), true) await page.keyboard.press('Escape') check(`${label}: Escape hides it under the pointer`, await page.evaluate(tip, '/search'), '0') await page.mouse.move(row.x + row.width / 2, y + 200) await page.mouse.move(row.x + row.width / 2, y) check(`${label}: a new hover shows it`, await page.evaluate(tip, '/search'), '1') check(`${label}: the label names the link`, await page.getByRole('link', { name: 'Search', exact: true }).count(), 1) await page.close() } // 5. No fold, and no frame: the shortcut does nothing and leaves the // key event alone. for (const [label, query] of [['no fold', '?state=open&fold=no'], ['no frame', '?frame=no']]) { page = await open(query) await page.evaluate(watchKeys) await page.keyboard.press('Control+Backslash') check(`${label}: state`, await page.evaluate(state), label === 'no fold' ? 'open' : 'no frame') check(`${label}: event`, await page.evaluate(() => window.__keys.join()), 'left alone') await page.close() } } const results = [] for (const engine of job.engines) { const result = { engine, error: '', checks: [] } results.push(result) let browser try { browser = await pw[engine].launch() await run(browser, job.origin, result.checks) } catch (err) { result.error = String(err && err.stack || err) } finally { if (browser) await browser.close() } } process.stdout.write(JSON.stringify(results))