jukebox
All repositories: gitoria
10.9 KB
// tests/jb11.mjs — JB11 (jukebox#11): the next song must start by itself while the page gets NO frames (a locked tablet).// Own server on a small generated database whose songs are one 3 s tone (ffmpeg); headless Chrome. The tab is sent to the background the// way a locked screen looks to the page: a second tab is brought to the front, so the song's tab is hidden (document.visibilityState// 'hidden', requestAnimationFrame stops and CSS animations stop). The check proves that the rAF counter stands still, then lets the// song end and requires that the next song plays (currentSrc changes, not paused), with no help from a frame or an animation.// node tests/jb11.mjs env: JB11_PORT (server, default 8730), JB11_DIR (scratch), JB11_ROOT (the checkout to test, default this one),// HL_PORT_BASE (Chrome debug ports: base+200..+209, default 8520 → 8720-8729), 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 || '8520'const { launchBrowser, sleep } = await import('./cdp.mjs')const root = process.env.JB11_ROOT || join(dirname(fileURLToPath(import.meta.url)), '..')const dir = process.env.JB11_DIR || '/media/STORAGE/claude-scratch/atlas/JB11/gate'const port = Number(process.env.JB11_PORT || 8730)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, '300'], { env: { ...process.env, JUKEBOX_STORAGE: storage }, encoding: 'utf8', timeout: 600000 })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 })try {for (const t0 = Date.now(); ; ) {try { if ((await fetch(BASE + '/')).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 other = await browser.newPage()const ev = (e) => page.evaluate(e)await page.send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true })await page.goto(BASE + '/')await page.waitForSelector('virtual-list .vl-row:not(.blank)')await sleep(1000)await page.send('Page.bringToFront')await page.click('virtual-list .vl-row:not(.blank)')await page.waitFor(`(() => { const a = document.querySelector('media-player audio'); return a && a.currentTime > 0.3 && !a.paused })()`, { timeout: 20000, label: 'first song plays' })const first = await ev(`document.querySelector('media-player audio').currentSrc`)// count frames in the page, then hide the tabawait ev(`window.__frames = 0; (function f() { window.__frames++; requestAnimationFrame(f) })(); 1`)await ev(`window.__log = []; const a = document.querySelector('media-player audio'); for (const n of ['play','pause','ended','loadstart','loadedmetadata','emptied','abort','error','stalled','suspend','canplay','loadeddata','waiting','playing']) a.addEventListener(n, () => window.__log.push(n + ' @' + Math.round(performance.now()/100)/10 + ' t=' + a.currentTime.toFixed(1) + ' ' + a.currentSrc.slice(-4) + (a.error ? ' ERR' + a.error.code + ':' + a.error.message : ''))); window.__log.push('start @' + Math.round(performance.now()/100)/10 + ' t=' + a.currentTime.toFixed(1)); 1`)if (process.env.JB11_DEBUG) await ev(`(() => { window.__a0 = document.querySelector('media-player audio'); new MutationObserver((ms) => { for (const m of ms) for (const n of m.removedNodes) if (n.localName === 'audio' || (n.querySelector && n.querySelector('audio'))) window.__log.push('AUDIO REMOVED same=' + (n === window.__a0) + ' @' + Math.round(performance.now()/100)/10); for (const m of ms) for (const n of m.addedNodes) if (n.localName === 'audio') window.__log.push('AUDIO ADDED @' + Math.round(performance.now()/100)/10) }).observe(document.body, { childList: true, subtree: true }); const sa = Element.prototype.setAttribute; Element.prototype.setAttribute = function (n, v) { if (this.localName === 'audio' && n === 'src') window.__log.push('setAttribute src ' + String(v).slice(-4) + ' @' + Math.round(performance.now()/100)/10); return sa.call(this, n, v) }; const d = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, 'src'); Object.defineProperty(HTMLMediaElement.prototype, 'src', { set(v) { window.__log.push('prop src ' + String(v).slice(-4) + ' @' + Math.round(performance.now()/100)/10); d.set.call(this, v) }, get() { return d.get.call(this) } }); const p = HTMLMediaElement.prototype.pause; HTMLMediaElement.prototype.pause = function () { window.__log.push('pause() called'); return p.call(this) }; return 1 })()`)// JB21: count what the lock-screen card sees: loads of the media element (loadstart), a replaced element, every metadata writeawait ev(`window.__el = document.querySelector('media-player audio'); window.__loads = []; window.__meta = 0; window.__el.addEventListener('loadstart', () => window.__loads.push(window.__el.currentSrc.slice(-8))); const md = Object.getOwnPropertyDescriptor(MediaSession.prototype, 'metadata'); Object.defineProperty(navigator.mediaSession, 'metadata', { configurable: true, set(v) { window.__meta++; md.set.call(navigator.mediaSession, v) }, get() { return md.get.call(navigator.mediaSession) } }); 1`)await sleep(300)const visibleFrames = await ev(`window.__frames`)await other.send('Page.bringToFront')await sleep(500)const vis = await ev(`document.visibilityState`)const f1 = await ev(`window.__frames`)await sleep(1000)const f2 = await ev(`window.__frames`)console.log(`visible: ${visibleFrames} frames in 0.3 s; hidden: visibilityState=${vis}, frames ${f1} then ${f2} one second later`)check('the tab is hidden and requestAnimationFrame stands still (no frames, like a locked screen)', vis === 'hidden' && f2 === f1 && visibleFrames > 3, `vis=${vis} f1=${f1} f2=${f2} visible=${visibleFrames}`)// `paused` is read at `loadstart` of every new source: that event comes before any data of the new song. A player that waits for// the data (loadedmetadata) or for a frame before it calls play() is still PAUSED there, and on the tablet that is where the// lock screen's notification disappears and the song never starts. A player that calls play() right in the ended event is not.await ev(`window.__ls = []; const a2 = document.querySelector('media-player audio'); a2.addEventListener('loadstart', () => window.__ls.push({ paused: a2.paused, src: a2.currentSrc })); 1`)await page.waitFor(`window.__ls.length > 0`, { timeout: 15000, label: 'the next song was loaded' })const ls = await ev(`window.__ls[0]`)// JB19: media-player c96709c no longer plays in the ended event (that was the second load of every song, jukebox#21): the host sets the src once and the player starts it at loadedmetadata, so `paused` at loadstart is no longer asked; the checks after this one prove that the next song playscheck('hidden tab: the next song is loaded (a new source)', ls.src !== first, JSON.stringify(ls))await sleep(600)const st = await ev(`(() => { const a = document.querySelector('media-player audio'); return { src: a.currentSrc, paused: a.paused, t: a.currentTime, frames: window.__frames, vis: document.visibilityState } })()`)console.log('600 ms after: ' + JSON.stringify(st))check('hidden tab: the next song plays (currentSrc changed, not paused, time advances) and the page got no frame', st.src !== first && !st.paused && st.t > 0.2 && st.vis === 'hidden' && st.frames === f2, JSON.stringify({ first, ...st }))await page.waitFor(`document.querySelector('media-player audio').currentSrc !== ${JSON.stringify(st.src)}`, { timeout: 10000, label: 'the third song' })await sleep(500)const st3 = await ev(`(() => { const a = document.querySelector('media-player audio'); return { src: a.currentSrc, paused: a.paused, t: a.currentTime } })()`)check('hidden tab: the third song follows the second (the queue goes on)', st3.src !== st.src && !st3.paused && st3.t > 0.2, JSON.stringify(st3))// JB21: two song changes by themselves (second and third song): one load and one metadata write per change, the same elementconst jb21 = await ev(`({ loads: window.__loads.length, meta: window.__meta, same: document.querySelector('media-player audio') === window.__el })`)console.log('JB21 across two changes: ' + JSON.stringify(jb21))check('JB21: two song changes load the media element twice (once per song, no second load of the same song) and keep the element', jb21.loads === 2 && jb21.same, JSON.stringify(jb21))check('JB21: the lock-screen metadata is written once per song', jb21.meta === 2, JSON.stringify(jb21))console.log('timeline: ' + (await ev(`window.__log.join(' | ')`)))// back in view: the display shows the song that playsawait page.send('Page.bringToFront')await sleep(800)const shown = await ev(`(() => { const a = document.querySelector('media-player audio'); return { src: a.currentSrc, paused: a.paused, title: document.querySelector('.jb-bar .mp-cover h2').textContent, meta: navigator.mediaSession && navigator.mediaSession.metadata ? navigator.mediaSession.metadata.title : null, cur: document.querySelector('.jb-bar ol.mp-queue li.current .mp-q-title') ? document.querySelector('.jb-bar ol.mp-queue li.current .mp-q-title').textContent : null } })()`)console.log('visible again: ' + JSON.stringify(shown))check('visible again: the bar title and the lock-screen title are the song that plays', shown.title !== '' && shown.meta === shown.title && !shown.paused, JSON.stringify(shown))await other.close()} finally {await stop()}console.log(`${pass} passed, ${fail} failed`)process.exit(fail ? 1 : 0)
Branches
Latest commits
- 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
- a01f3aa8ui gate: back to the top before the sort checks (the screen reopens where it was left)mre
- f7936c20Lists keep their scroll position across a reload (jukebox#20): per-list offsets in storage/ui.json, gate jb20mre
- d67906devirtual-list and icons from components.hybriel e5b98ec (centerId, scrollTop, onScroll)mre
- df7920ceRecords: R3 startedmre
- 0549f038Mission 010: jb20 livemre
- 273494e2STATUS rewrittenmre