gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchday39cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmreday39/tests/jb11.mjs

10.9 KB

  1. // tests/jb11.mjs — JB11 (jukebox#11): the next song must start by itself while the page gets NO frames (a locked tablet).
  2. // 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
  3. // 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
  4. // 'hidden', requestAnimationFrame stops and CSS animations stop). The check proves that the rAF counter stands still, then lets the
  5. // song end and requires that the next song plays (currentSrc changes, not paused), with no help from a frame or an animation.
  6. // node tests/jb11.mjs env: JB11_PORT (server, default 8730), JB11_DIR (scratch), JB11_ROOT (the checkout to test, default this one),
  7. // HL_PORT_BASE (Chrome debug ports: base+200..+209, default 8520 → 8720-8729), JUKEBOX_COLD_MS
  8. import { spawn, execFileSync } from 'node:child_process'
  9. import { rmSync, mkdirSync } from 'node:fs'
  10. import { dirname, join } from 'node:path'
  11. import { fileURLToPath } from 'node:url'
  12. process.env.HL_CHROME = process.env.HL_CHROME || '/opt/google/chrome/chrome'
  13. process.env.HL_PORT_BASE = process.env.HL_PORT_BASE || '8520'
  14. const { launchBrowser, sleep } = await import('./cdp.mjs')
  15. const root = process.env.JB11_ROOT || join(dirname(fileURLToPath(import.meta.url)), '..')
  16. const dir = process.env.JB11_DIR || '/media/STORAGE/claude-scratch/atlas/JB11/gate'
  17. const port = Number(process.env.JB11_PORT || 8730)
  18. const BASE = `http://127.0.0.1:${port}`
  19. const COLD_MS = Number(process.env.JUKEBOX_COLD_MS || 150000)
  20. let pass = 0, fail = 0
  21. const check = (name, ok, detail = '') => { console.log((ok ? 'ok ' : 'FAIL ') + name + (ok ? '' : ' — ' + detail)); if (ok) pass++; else fail++ }
  22. mkdirSync(dir, { recursive: true })
  23. process.env.TMPDIR = dir
  24. let server = null, browser = null, log = ''
  25. async function stop() {
  26. if (browser) { try { await browser.close() } catch {} browser = null }
  27. if (server) { try { server.kill('SIGTERM') } catch {} await sleep(500); server = null }
  28. }
  29. process.on('SIGINT', async () => { await stop(); process.exit(1) })
  30. const storage = join(dir, 'storage')
  31. const tone = join(dir, 'tone3.mp3')
  32. rmSync(storage, { recursive: true, force: true })
  33. execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=3', '-q:a', '9', tone])
  34. const cover = join(root, 'tests/fixtures/withcover/folder.jpg')
  35. 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 })
  36. 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'] })
  37. console.log('server pid ' + server.pid)
  38. server.stdout.on('data', (d) => { log += d }); server.stderr.on('data', (d) => { log += d })
  39. try {
  40. for (const t0 = Date.now(); ; ) {
  41. try { if ((await fetch(BASE + '/')).ok) break } catch {}
  42. if (Date.now() - t0 > COLD_MS) { console.log(log); throw new Error('the server did not come up') }
  43. await sleep(500)
  44. }
  45. browser = await launchBrowser()
  46. console.log('chrome pid ' + browser.proc?.pid)
  47. const page = await browser.newPage()
  48. const other = await browser.newPage()
  49. const ev = (e) => page.evaluate(e)
  50. await page.send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true })
  51. await page.goto(BASE + '/')
  52. await page.waitForSelector('virtual-list .vl-row:not(.blank)')
  53. await sleep(1000)
  54. await page.send('Page.bringToFront')
  55. await page.click('virtual-list .vl-row:not(.blank)')
  56. await page.waitFor(`(() => { const a = document.querySelector('media-player audio'); return a && a.currentTime > 0.3 && !a.paused })()`, { timeout: 20000, label: 'first song plays' })
  57. const first = await ev(`document.querySelector('media-player audio').currentSrc`)
  58. // count frames in the page, then hide the tab
  59. await ev(`window.__frames = 0; (function f() { window.__frames++; requestAnimationFrame(f) })(); 1`)
  60. 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`)
  61. 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 })()`)
  62. // JB21: count what the lock-screen card sees: loads of the media element (loadstart), a replaced element, every metadata write
  63. await 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`)
  64. await sleep(300)
  65. const visibleFrames = await ev(`window.__frames`)
  66. await other.send('Page.bringToFront')
  67. await sleep(500)
  68. const vis = await ev(`document.visibilityState`)
  69. const f1 = await ev(`window.__frames`)
  70. await sleep(1000)
  71. const f2 = await ev(`window.__frames`)
  72. console.log(`visible: ${visibleFrames} frames in 0.3 s; hidden: visibilityState=${vis}, frames ${f1} then ${f2} one second later`)
  73. 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}`)
  74. // `paused` is read at `loadstart` of every new source: that event comes before any data of the new song. A player that waits for
  75. // the data (loadedmetadata) or for a frame before it calls play() is still PAUSED there, and on the tablet that is where the
  76. // lock screen's notification disappears and the song never starts. A player that calls play() right in the ended event is not.
  77. await ev(`window.__ls = []; const a2 = document.querySelector('media-player audio'); a2.addEventListener('loadstart', () => window.__ls.push({ paused: a2.paused, src: a2.currentSrc })); 1`)
  78. await page.waitFor(`window.__ls.length > 0`, { timeout: 15000, label: 'the next song was loaded' })
  79. const ls = await ev(`window.__ls[0]`)
  80. // 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 plays
  81. check('hidden tab: the next song is loaded (a new source)', ls.src !== first, JSON.stringify(ls))
  82. await sleep(600)
  83. 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 } })()`)
  84. console.log('600 ms after: ' + JSON.stringify(st))
  85. 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 }))
  86. await page.waitFor(`document.querySelector('media-player audio').currentSrc !== ${JSON.stringify(st.src)}`, { timeout: 10000, label: 'the third song' })
  87. await sleep(500)
  88. const st3 = await ev(`(() => { const a = document.querySelector('media-player audio'); return { src: a.currentSrc, paused: a.paused, t: a.currentTime } })()`)
  89. 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))
  90. // JB21: two song changes by themselves (second and third song): one load and one metadata write per change, the same element
  91. const jb21 = await ev(`({ loads: window.__loads.length, meta: window.__meta, same: document.querySelector('media-player audio') === window.__el })`)
  92. console.log('JB21 across two changes: ' + JSON.stringify(jb21))
  93. 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))
  94. check('JB21: the lock-screen metadata is written once per song', jb21.meta === 2, JSON.stringify(jb21))
  95. console.log('timeline: ' + (await ev(`window.__log.join(' | ')`)))
  96. // back in view: the display shows the song that plays
  97. await page.send('Page.bringToFront')
  98. await sleep(800)
  99. 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 } })()`)
  100. console.log('visible again: ' + JSON.stringify(shown))
  101. 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))
  102. await other.close()
  103. } finally {
  104. await stop()
  105. }
  106. console.log(`${pass} passed, ${fail} failed`)
  107. 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