jukebox
All repositories: gitoria
12.2 KB
// tests/touch.mjs — jukebox#12: THE LIST'S SCROLL HANDLE FOR A FINGER. A real headless Chrome with touch emulation (so `pointer: coarse` is true) on a generated database of// 13,450 songs (tools/make-test-db.hl): the virtual list must show a handle at least 44 px wide, drawn slim (the width is a transparent border), that follows the scroll position,// and a touch drag from the top of the list to the bottom must land on the last song. Desktop (a mouse) keeps the browser's own scroll bar: no handle there.// Fails on the code before JB12 (no handle exists). Screenshots at 390, 800 and 1280 px.// node tests/touch.mjs env: JUKEBOX_TOUCH_PORT (server, default 8750), HL_PORT_BASE (Chrome debug ports base+200.., default 8540 -> 8740-8749),// JUKEBOX_TOUCH_DIR (scratch + test storage, kept between runs so the database is made once), JUKEBOX_TOUCH_SHOTS (screenshots dir)import { spawn, execFileSync } from 'node:child_process'import { mkdirSync, writeFileSync, existsSync } from 'node:fs'import { dirname, join } from 'node:path'import { fileURLToPath } from 'node:url'process.env.HL_CHROME = process.env.HL_CHROME || '/opt/google/chrome/chrome'process.env.HL_PORT_BASE = process.env.HL_PORT_BASE || '8540'const { launchBrowser } = await import('./cdp.mjs')const root = join(dirname(fileURLToPath(import.meta.url)), '..')const dir = process.env.JUKEBOX_TOUCH_DIR || '/media/STORAGE/claude-scratch/atlas/JB12/gate-touch'const shots = process.env.JUKEBOX_TOUCH_SHOTS || join(dir, 'shots')const port = Number(process.env.JUKEBOX_TOUCH_PORT || 8750)const BASE = `http://127.0.0.1:${port}`let pass = 0, fail = 0const check = (name, ok, detail = '') => { console.log((ok ? 'ok ' : 'FAIL ') + name + (ok ? '' : ' — ' + detail)); if (ok) pass++; else fail++ }const sleep = (ms) => new Promise((r) => setTimeout(r, ms))mkdirSync(shots, { recursive: true })let server = null, browser = null, log = ''async function stop() {if (browser) { try { await browser.close() } catch {} browser = null }if (server) { try { server.kill('SIGTERM') } catch {} await sleep(500); server = null }}process.on('SIGINT', async () => { await stop(); process.exit(1) })const storage = join(dir, 'storage')const tone = join(dir, 'tone.mp3')mkdirSync(dir, { recursive: true })if (!existsSync(join(storage, 'mpackdb'))) {execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=60', '-q:a', '9', tone])const made = execFileSync(join(root, 'bin/hybriel'), [join(root, 'tools/make-test-db.hl'), tone, join(root, 'tests/fixtures/withcover/folder.jpg')], { env: { ...process.env, JUKEBOX_STORAGE: storage }, encoding: 'utf8', timeout: 600000 })console.log(made.trim())}server = spawn(join(root, 'bin/hybriel'), ['project.hl'], { cwd: root, env: { ...process.env, JUKEBOX_PORT: String(port), JUKEBOX_STORAGE: storage, JUKEBOX_AUTOSCAN: '0', JUKEBOX_WATCH: '0', HL_HOST: '127.0.0.1' }, stdio: ['ignore', 'pipe', 'pipe'] })server.stdout.on('data', (d) => { log += d }); server.stderr.on('data', (d) => { log += d })for (const t0 = Date.now(); ; ) {try { if ((await fetch(BASE + '/')).ok) break } catch {}if (Date.now() - t0 > 150000) { console.log(log); await stop(); throw new Error('the server did not come up') }await sleep(500)}try {browser = await launchBrowser()const page = await browser.newPage()const ev = (e) => page.evaluate(e)const shot = async (name) => { const s = await page.send('Page.captureScreenshot', { format: 'png' }); writeFileSync(join(shots, name + '.png'), Buffer.from(s.data, 'base64')) }const touch = (type, x, y) => page.send('Input.dispatchTouchEvent', { type, touchPoints: type === 'touchEnd' ? [] : [{ x, y, id: 1 }] })const view = async (w, h, coarse) => {await page.send('Emulation.setDeviceMetricsOverride', { width: w, height: h, deviceScaleFactor: 1, mobile: coarse })await page.send('Emulation.setTouchEmulationEnabled', { enabled: coarse, maxTouchPoints: coarse ? 5 : 1 })await page.goto(BASE + '/'); await page.waitForSelector('jb-screen'); await page.waitForSelector('virtual-list .vl-row:not(.blank)'); await sleep(900)}const handleBox = () => ev(`(() => { const h = document.querySelector('virtual-list vl-handle'); if (!h) return null; const r = h.getBoundingClientRect(), s = getComputedStyle(h); const sc = document.querySelector('.vl-scroller').getBoundingClientRect(); return { x: r.left, y: r.top, w: r.width, h: r.height, shown: s.display !== 'none' && s.visibility !== 'hidden' && r.width > 0, bl: s.borderLeftWidth, blc: s.borderLeftColor, br: s.borderRightWidth, clip: s.backgroundClip, bg: s.backgroundColor, sTop: sc.top, sBottom: sc.bottom, sRight: sc.right } })()`)const where = () => ev(`(() => { const s = document.querySelector('.vl-scroller'); const rows = [...document.querySelectorAll('virtual-list .vl-row:not(.blank)')]; return { top: s.scrollTop, max: s.scrollHeight - s.clientHeight, firstIdx: Number(rows[0].getAttribute('data-index')), lastIdx: Number(rows[rows.length - 1].getAttribute('data-index')), blank: document.querySelectorAll('.vl-row.blank').length } })()`)const dragFromTo = async (x, y0, y1, steps = 12) => { await touch('touchStart', x, y0); for (let i = 1; i <= steps; i++) { await touch('touchMove', x, y0 + (y1 - y0) * i / steps); await sleep(30) } ; await touch('touchEnd') }// ---- 800 x 1280 tablet, touch ---------------------------------------------------------------------------------------------await view(800, 1280, true)check('touch emulation: the page sees pointer: coarse', await ev(`matchMedia('(pointer: coarse)').matches`))const total = (await (await fetch(BASE + '/api/stats')).json()).library.songsconsole.log('songs in the test database: ' + total)let hb = await handleBox()check('tablet: the list has a scroll handle', !!hb && hb.shown, JSON.stringify(hb))check('tablet: the handle is at least 44 px wide', hb && hb.w >= 44, JSON.stringify(hb))check('tablet: the handle is tall enough to hit (≥ 44 px) and starts at the top', hb && hb.h >= 44 && Math.abs(hb.y - hb.sTop) <= 1, JSON.stringify(hb))check('tablet: the handle sits at the right edge of the list', hb && Math.abs(hb.x + hb.w - hb.sRight) <= 1, JSON.stringify(hb))const drawn = hb ? hb.w - parseFloat(hb.bl) - parseFloat(hb.br) : 0check('tablet: design — the tap area is wide but what is drawn stays slim (transparent border, colour clipped to the padding box, 6–16 px drawn)', hb && hb.blc === 'rgba(0, 0, 0, 0)' && hb.clip === 'padding-box' && drawn >= 6 && drawn <= 16, JSON.stringify(hb))check('tablet: design — the handle has a visible colour', hb && hb.bg !== 'rgba(0, 0, 0, 0)', hb && hb.bg)const hit = await ev(`(() => { const r = document.querySelector('virtual-list vl-handle').getBoundingClientRect(); const e = document.elementFromPoint(r.left + 5, r.top + r.height / 2); return e && e.tagName.toLowerCase() })()`)check('tablet: a touch on the handle area reaches the handle, not a row', hit === 'vl-handle', '' + hit)const rowHit = await ev(`(() => { const r = document.querySelector('.vl-row:not(.blank)').getBoundingClientRect(); const e = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2); return e && !!e.closest('.vl-row') })()`)check('tablet: the rest of the row is still a row (tap target)', rowHit === true)const geo = await ev(`(() => { const rows = [...document.querySelectorAll('virtual-list .vl-row')].map((r) => r.getBoundingClientRect()); const aside = document.querySelector('.vl-row .vl-aside').getBoundingClientRect(); const hb = document.querySelector('vl-handle').getBoundingClientRect(); return { steps: rows.slice(1).map((r, i) => Math.round(r.top - rows[i].top)), h: rows[0].height, asideRight: aside.right, handleDrawnLeft: hb.left + 32 } })()`)check('tablet: rows are still 56 px apart and 56 px tall (dense list)', geo.steps.every((s) => s === 56) && geo.h === 56, JSON.stringify(geo.steps.slice(0, 5)))check('tablet: the rows\' right text stays clear of the drawn handle', geo.asideRight <= geo.handleDrawnLeft, JSON.stringify(geo))await shot('tablet-800-top')// a half drag first: the middle of the track is about the middle of the listconst cx = hb.x + hb.w / 2const trackTop = hb.sTop + hb.h / 2, trackBottom = hb.sBottom - hb.h / 2await dragFromTo(cx, hb.y + hb.h / 2, (trackTop + trackBottom) / 2)await sleep(1200)let w = await where()check('tablet: a touch drag to the middle of the track lands in the middle of the list (45–55 %)', w.top / w.max > 0.45 && w.top / w.max < 0.55, JSON.stringify(w))check('tablet: rows are loaded where the drag landed (no blank rows)', w.blank === 0, JSON.stringify(w))hb = await handleBox()check('tablet: the handle shows where we are (about the middle of the box)', Math.abs((hb.y + hb.h / 2) - (trackTop + trackBottom) / 2) <= 3, JSON.stringify(hb))await shot('tablet-800-middle')// the full drag: top to bottom with touch eventsawait dragFromTo(cx, hb.y + hb.h / 2, hb.sBottom - 2, 20)await sleep(1500)w = await where()check('tablet: a touch drag from the top of the track to the bottom lands at the end of the list', w.top >= w.max - 2, JSON.stringify(w))check('tablet: the last song (number 13,458) is in the page and loaded', w.lastIdx === 13457, JSON.stringify(w) + ' songs ' + total)check('tablet: no blank rows at the end', w.blank === 0, JSON.stringify(w))hb = await handleBox()check('tablet: the handle sits at the bottom of the box at the end', Math.abs((hb.y + hb.h) - hb.sBottom) <= 2, JSON.stringify(hb))await shot('tablet-800-end')// and back up with the grab offset: grab the handle at its lower part, move up to the topawait dragFromTo(cx, hb.y + hb.h - 6, hb.sTop - 40, 20)await sleep(1200)w = await where()check('tablet: dragged back up past the top: the list is at the first row', w.top <= 1 && w.firstIdx === 0, JSON.stringify(w))// the finger scroll (a swipe on the rows) moves the handle tooawait ev(`document.querySelector('.vl-scroller').scrollTop = 56 * 6000`); await sleep(700)hb = await handleBox()const frac = (6000 * 56) / (await where()).maxcheck('tablet: scrolling by other means moves the handle to the matching place', Math.abs((hb.y - hb.sTop) / (hb.sBottom - hb.sTop - hb.h) - frac) < 0.03, JSON.stringify({ hb, frac }))check('tablet: no script errors', page.problems().filter((m) => m.level === 'error').length === 0, page.dumpConsole())// ---- 390 phone, touch --------------------------------------------------------------------------------------------------------await view(390, 844, true)hb = await handleBox()check('phone 390: the handle is there and at least 44 px wide', !!hb && hb.shown && hb.w >= 44, JSON.stringify(hb))const g390 = await ev(`(() => { const rows = [...document.querySelectorAll('virtual-list .vl-row')].map((r) => r.getBoundingClientRect()); return rows.slice(1).map((r, i) => Math.round(r.top - rows[i].top)) })()`)check('phone 390: rows 56 px apart', g390.every((s) => s === 56))await dragFromTo(hb.x + hb.w / 2, hb.y + hb.h / 2, hb.sBottom - 2, 16)await sleep(1500)w = await where()check('phone 390: the touch drag lands at the end of the list', w.top >= w.max - 2 && w.lastIdx === 13457, JSON.stringify(w))await shot('phone-390-end')await ev(`document.querySelector('.vl-scroller').scrollTop = 0`); await sleep(700)await shot('phone-390-top')// ---- 1280 desktop, mouse: the look stays --------------------------------------------------------------------------------------await view(1280, 800, false)check('desktop: the page does not see a coarse pointer', await ev(`!matchMedia('(pointer: coarse)').matches`))hb = await handleBox()check('desktop: no handle is shown (the browser\'s own scroll bar stays)', !hb || !hb.shown, JSON.stringify(hb))const d = await ev(`(() => { const s = document.querySelector('.vl-scroller'); const c = getComputedStyle(s); return { pr: c.paddingRight, sw: c.scrollbarWidth, bar: s.offsetWidth - s.clientWidth } })()`)check('desktop: the list box is unchanged (no extra padding, native scroll bar kept)', d.pr === '0px' && d.sw === 'auto', JSON.stringify(d))await shot('desktop-1280')await page.close()} catch (e) {fail++; console.log('FAIL the gate stopped: ' + (e && e.stack || e)); if (log) console.log(log.slice(-2000))} finally {await stop()}console.log(`\n${pass} passed, ${fail} failed`)process.exit(fail ? 1 : 0)
Branches
Latest commits
- b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
- 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
- 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
- 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
- c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
- 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
- 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
- 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
- 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
- bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
- 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
- d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre