jukebox
All repositories: gitoria
9.0 KB
// tests/jb20.mjs — JB20 (jukebox#20): after a reload a list keeps its scroll position (the same rows are in view). Songs (per sort), Artists, Albums, and one album's songs.// Own server on a generated database (one 3 s tone), headless Chrome, 390x844. A list is scrolled far down with the real mouse wheel, the page reloads, and the rows in view must be the// same ones (by title, the first row fully in view); a second sort has its own place; a fresh tab opens at the saved place too (the server keeps it, not the browser).// The centering of the playing song is NOT tested: after a reload the page does not know what played (jukebox#16 / hybriel#63).// node tests/jb20.mjs env: JB20_PORT (default 8737), JB20_DIR (scratch), JB20_ROOT (checkout to test), JB20_SONGS (default 3000), HL_PORT_BASE (default 8530), JUKEBOX_COLD_MSimport { spawn, execFileSync } from 'node:child_process'import { rmSync, mkdirSync } 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 || '8530'const { launchBrowser, sleep } = await import('./cdp.mjs')const root = process.env.JB20_ROOT || join(dirname(fileURLToPath(import.meta.url)), '..')const dir = process.env.JB20_DIR || '/media/STORAGE/claude-scratch/jukebox/JB20/gate'const port = Number(process.env.JB20_PORT || 8737)const songs = process.env.JB20_SONGS || '3000'const BASE = `http://127.0.0.1:${port}`const COLD_MS = Number(process.env.JUKEBOX_COLD_MS || 150000)let pass = 0, fail = 0const check = (name, ok, detail = '') => { console.log((ok ? 'ok ' : 'FAIL ') + name + (ok ? '' : ' — ' + detail)); if (ok) pass++; else fail++ }mkdirSync(dir, { recursive: true })process.env.TMPDIR = dirlet 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, 'tone3.mp3')rmSync(storage, { recursive: true, force: true })execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=3', '-q:a', '9', tone])const cover = join(root, 'tests/fixtures/withcover/folder.jpg')execFileSync(join(root, 'bin/hybriel'), [join(root, 'tools/make-test-db.hl'), tone, cover, songs], { env: { ...process.env, JUKEBOX_STORAGE: storage }, encoding: 'utf8', timeout: 900000 })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'] })console.log('server pid ' + server.pid)server.stdout.on('data', (d) => { log += d }); server.stderr.on('data', (d) => { log += d })const api = async (path) => (await fetch(BASE + path)).json()try {for (const t0 = Date.now(); ; ) {try { if ((await fetch(BASE + '/api/stats')).ok) break } catch {}if (Date.now() - t0 > COLD_MS) { console.log(log); throw new Error('the server did not come up') }await sleep(500)}browser = await launchBrowser()console.log('chrome pid ' + browser.proc?.pid)const page = await browser.newPage()const ev = (e) => page.evaluate(e)await page.send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true })const rowsReady = `document.querySelectorAll('virtual-list .vl-row:not(.blank)').length > 4 && document.querySelector('virtual-list .vl-row:not(.blank) .vl-title').textContent !== ''`const settle = async (p = page) => { await p.waitFor(rowsReady, { label: 'rows' }); await sleep(900) }// the titles of the rows in view: the rows whose top lies inside the scrolling boxconst inView = (p = page) => p.evaluate(`(() => { const b = document.querySelector('virtual-list .vl-scroller').getBoundingClientRect(); return [...document.querySelectorAll('virtual-list .vl-row:not(.blank)')].filter((r) => { const t = r.getBoundingClientRect(); return t.top >= b.top - 1 && t.bottom <= b.bottom + 1 }).slice(0, 6).map((r) => r.querySelector('.vl-title').textContent) })()`)const top = (p = page) => p.evaluate(`document.querySelector('virtual-list .vl-scroller').scrollTop`)const wheel = async (dy, n) => {const b = await ev(`(() => { const r = document.querySelector('virtual-list .vl-scroller').getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2 } })()`)for (let i = 0; i < n; i++) { await page.send('Input.dispatchMouseEvent', { type: 'mouseWheel', x: b.x, y: b.y, deltaX: 0, deltaY: dy }); await sleep(40) }await sleep(1200) // the list reports its offset at most every 100 ms, the server writes it}const same = (a, b) => a.length > 0 && JSON.stringify(a) === JSON.stringify(b)// 1. Songs, artist order: scroll far down, reloadawait page.goto(BASE + '/'); await settle()await wheel(3000, 14)const t1 = await top(), v1 = await inView()check('songs: scrolled far down (the list is not at the top)', t1 > 20000, 'scrollTop ' + t1)await page.send('Page.reload'); await settle()const v2 = await inView(), t2 = await top()check('songs: after a reload the same rows are in view', same(v1, v2), `before ${JSON.stringify(v1)} @${t1}, after ${JSON.stringify(v2)} @${t2}`)// scrolling on from there works (the rows around the place load)await wheel(600, 3)const t2b = await top()check('songs: scrolling on after the reload moves on from the saved place', t2b > t2, `${t2} -> ${t2b}`)// 2. a fresh tab opens there too (the server remembers, not the page)const v2b = await inView(), t2c = await top()const tab2 = await browser.newPage()await tab2.send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true })await tab2.goto(BASE + '/'); await settle(tab2)check('songs: a fresh tab opens at the saved place', same(v2b, await inView(tab2)), `${JSON.stringify(v2b)} vs ${JSON.stringify(await inView(tab2))}`)await tab2.close()// 3. another sort has its own place (a list is named by which list it is, not by a row)// back to the top by hand first: a sort change while the list is far down leaves it there with blank rows (the existing vlTop gap, on main too; reported, not part of #20)await wheel(-3000, 20)await page.focus('#sort')await page.send('Input.dispatchKeyEvent', { type: 'rawKeyDown', key: 'ArrowDown', code: 'ArrowDown', windowsVirtualKeyCode: 40 })await page.send('Input.dispatchKeyEvent', { type: 'keyUp', key: 'ArrowDown', code: 'ArrowDown', windowsVirtualKeyCode: 40 })await sleep(1500)try { await settle() } catch (e) { console.log('DEBUG', await ev(`JSON.stringify({ top: document.querySelector('virtual-list .vl-scroller').scrollTop, rows: document.querySelectorAll('virtual-list .vl-row').length, real: document.querySelectorAll('virtual-list .vl-row:not(.blank)').length, sel: document.querySelector('#sort').value, first: document.querySelector('virtual-list .vl-row') && document.querySelector('virtual-list .vl-row').textContent })`)); throw e }check('songs: another sort opens at its own top, not at the old offset', (await top()) < 100, 'scrollTop ' + (await top()))await wheel(3000, 6)const v3 = await inView()await page.send('Page.reload'); await settle()check('songs by title: after a reload the same rows are in view', same(v3, await inView()), `${JSON.stringify(v3)} vs ${JSON.stringify(await inView())}`)// 4. Artists and Albumsfor (const [path, n] of [['/artists', 12], ['/albums', 12]]) {await page.goto(BASE + path); await settle()await wheel(900, n)const a = await inView(), ta = await top()await page.send('Page.reload'); await settle()check(path + ': scrolled, reloaded, the same rows are in view', ta > 3000 && same(a, await inView()), `scrollTop ${ta}: ${JSON.stringify(a)} vs ${JSON.stringify(await inView())}`)}// 5. one album's songs (only when an album is long enough to scroll)const al = (await api('/api/albums?limit=500')).items.sort((x, y) => y.songCount - x.songCount)[0]if (al && al.songCount > 14) {await page.goto(BASE + '/albums/' + al.id); await settle()await wheel(300, 4)const b = await inView(), tb = await top()await page.send('Page.reload'); await settle()check('an album: scrolled, reloaded, the same rows are in view', tb > 300 && same(b, await inView()), `scrollTop ${tb}: ${JSON.stringify(b)} vs ${JSON.stringify(await inView())}`)} else console.log('skip an album (no album with more than 14 songs: ' + (al ? al.songCount : 'none') + ')')// 6. the albums list is still at ITS place when another list had a different oneawait page.goto(BASE + '/'); await settle()check('songs: back on the Songs screen it still opens at the place of the title sort', (await top()) > 5000, 'scrollTop ' + (await top()))} finally {await stop()}console.log(`${pass} passed, ${fail} failed`)process.exit(fail ? 1 : 0)
Branches
Latest commits
- 8ed6da45|||PR README: media-player pin df9e7dfmre
- d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
- 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
- 1fecd6a7Merge branch 'records-day39' into mainJukebox
- d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
- 5fa19e89Merge branch 'day39' into mainJukebox
- cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
- f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
- 9f5c6532Merge branch 'status-22-pr' into mainJukebox
- 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
- 29d2029cRecords: PR-only rule (Kunibert)mre
- adacb0abRecords: neutral ticket rule, #22 decision recordedmre
- b2d6f121Records: #308 hold liftedmre
- 7ab67533Records: day39 on hold (hybriel#308)mre
- 22f21739Records: note rebase before mergemre
- f7368ea3Records: mission 011 day39, waits for lisbethmre
- 9515d2cdRecords: jb20 livemre
- 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
- 277a2722README: provenance linemre
- 4169ca6aREADME: sync pinmre