gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8ed6da458ed6da45|||PR README: media-player pin df9e7dfmre8ed6da45/tests/jb19.mjs

13.9 KB

  1. // tests/jb19.mjs — JB19 (jukebox#19): the player's controls. The bar reads Shuffle · Previous · Play/pause · Next · Repeat (no 10 second buttons);
  2. // Shuffle is a switch that changes what comes next and keeps the song that plays; Repeat steps off → all → one and shows it; at a song's end
  3. // 'one' plays it again, at the queue's end 'all' starts the list again and 'off' stops; both states survive a reload.
  4. // Own server on a generated database of 6 songs (the generator adds a few more) (one 1.5 s tone, ffmpeg), headless Chrome at 390 px.
  5. // node tests/jb19.mjs env: JB19_PORT (server, default 8821), JB19_DIR (scratch), JB19_ROOT (checkout to test), HL_PORT_BASE (Chrome debug ports base+200, default 8630), JUKEBOX_COLD_MS
  6. import { spawn, execFileSync } from 'node:child_process'
  7. import { rmSync, mkdirSync, readFileSync } from 'node:fs'
  8. import { dirname, join } from 'node:path'
  9. import { fileURLToPath } from 'node:url'
  10. process.env.HL_CHROME = process.env.HL_CHROME || '/opt/google/chrome/chrome'
  11. process.env.HL_PORT_BASE = process.env.HL_PORT_BASE || '8630'
  12. const { launchBrowser, sleep } = await import('./cdp.mjs')
  13. const root = process.env.JB19_ROOT || join(dirname(fileURLToPath(import.meta.url)), '..')
  14. const dir = process.env.JB19_DIR || '/media/STORAGE/claude-scratch/jukebox/P1/gate-jb19'
  15. const port = Number(process.env.JB19_PORT || 8821)
  16. const BASE = `http://127.0.0.1:${port}`
  17. const COLD_MS = Number(process.env.JUKEBOX_COLD_MS || 150000)
  18. const TONE = 1.5
  19. let pass = 0, fail = 0
  20. const check = (name, ok, detail = '') => { console.log((ok ? 'ok ' : 'FAIL ') + name + (ok ? '' : ' — ' + detail)); if (ok) pass++; else fail++ }
  21. mkdirSync(dir, { recursive: true })
  22. process.env.TMPDIR = dir
  23. let server = null, browser = null, log = ''
  24. async function stop() {
  25. if (browser) { try { await browser.close() } catch {} browser = null }
  26. if (server) { try { server.kill('SIGTERM') } catch {} await sleep(500); server = null }
  27. }
  28. process.on('SIGINT', async () => { await stop(); process.exit(1) })
  29. const storage = join(dir, 'storage')
  30. const tone = join(dir, 'tone.mp3')
  31. rmSync(storage, { recursive: true, force: true })
  32. execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', `sine=frequency=440:duration=${TONE}`, '-q:a', '9', tone])
  33. const cover = join(root, 'tests/fixtures/withcover/folder.jpg')
  34. execFileSync(join(root, 'bin/hybriel'), [join(root, 'tools/make-test-db.hl'), tone, cover, '6'], { env: { ...process.env, JUKEBOX_STORAGE: storage }, encoding: 'utf8', timeout: 600000 })
  35. 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'] })
  36. console.log('server pid ' + server.pid)
  37. server.stdout.on('data', (d) => { log += d }); server.stderr.on('data', (d) => { log += d })
  38. try {
  39. for (const t0 = Date.now(); ; ) {
  40. try { if ((await fetch(BASE + '/')).ok) break } catch {}
  41. if (Date.now() - t0 > COLD_MS) { console.log(log); throw new Error('the server did not come up') }
  42. await sleep(500)
  43. }
  44. const order = (await (await fetch(BASE + '/api/songs?sort=artist&offset=0&limit=100')).json()).items.map((s) => String(s.id))
  45. console.log('list order (artist): ' + order.join(' '))
  46. browser = await launchBrowser()
  47. console.log('chrome pid ' + browser.proc?.pid)
  48. const page = 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(800)
  54. const playing = () => ev(`(() => { const a = document.querySelector('media-player audio'); return a && a.currentSrc ? a.currentSrc.split('/').pop() : '' })()`)
  55. const state = () => ev(`(() => { const a = document.querySelector('media-player audio'); const s = document.querySelector('#jb-shuffle'), r = document.querySelector('#jb-repeat'); return { src: a.currentSrc.split('/').pop(), paused: a.paused, ended: a.ended, t: a.currentTime, loop: a.loop, shuffle: s.getAttribute('aria-pressed'), repeat: r.getAttribute('aria-pressed'), repeatTitle: r.title, shuffleTitle: s.title, repeatIcon: r.querySelector('path').getAttribute('d') } })()`)
  56. const press = async (sel) => { await page.click(sel); await sleep(500) }
  57. const next = async (hold) => { const before = await playing(); await page.click('.jb-bar .jb-next'); try { await page.waitFor(`document.querySelector('media-player audio').currentSrc.split('/').pop() !== ${JSON.stringify(before)}`, { timeout: 4000 }) } catch {} await sleep(300); if (hold) await ev(`document.querySelector('media-player audio').pause(); 1`); return playing() }
  58. // ---- the songs screen: Shuffle my favourites is an ordinary button
  59. const fav = await ev(`(() => { const b = document.querySelector('#shuffle-fav'), s = document.querySelector('#shuffle'); const rb = b.getBoundingClientRect(), rs = s.getBoundingClientRect(); return { cls: b.className, h: rb.height, hs: rs.height, fs: getComputedStyle(b).fontSize, fss: getComputedStyle(s).fontSize, w: rb.width } })()`)
  60. check('Songs: "Shuffle my favourites" is an ordinary button (no jb-big, same height and font as Shuffle)', !fav.cls.includes('jb-big') && fav.h <= fav.hs + 2 && fav.fs === fav.fss, JSON.stringify(fav))
  61. // ---- play the first song of the list
  62. await page.click('virtual-list .vl-row:not(.blank)')
  63. await page.waitFor(`(() => { const a = document.querySelector('media-player audio'); return a && a.currentTime > 0.2 && !a.paused })()`, { timeout: 20000, label: 'first song plays' })
  64. check('the first row of the list plays', (await playing()).endsWith(order[0]) || (await playing()) === order[0], await playing())
  65. // ---- the bar: order and sizes
  66. const bar = await ev(`(() => [...document.querySelectorAll('.jb-bar button')].map((b) => { const r = b.getBoundingClientRect(); return { id: b.id, cls: b.className.split(' ')[0], x: Math.round(r.left), w: Math.round(r.width), h: Math.round(r.height) } }).filter((b) => b.w > 0).sort((a, b) => a.x - b.x))()`)
  67. const names = bar.map((b) => b.id || b.cls).join(' ')
  68. console.log('bar: ' + JSON.stringify(bar))
  69. check('the bar reads shuffle · previous · play/pause · next · repeat', names === 'jb-shuffle jb-prev mp-play jb-next jb-repeat', names)
  70. check('no 10 second back / forward buttons exist', await ev(`document.querySelectorAll('.mp-back, .mp-forward').length === 0`))
  71. check('every bar button is at least 44 px', bar.every((b) => b.w >= 44 && b.h >= 44), JSON.stringify(bar))
  72. check('both switches start off: Shuffle off, Repeat off', (await state()).shuffle === 'false' && (await state()).repeat === 'false', JSON.stringify(await state()))
  73. // ---- Shuffle: off, the list order goes on; on, what comes next is shuffled and the playing song stays
  74. let id = await next()
  75. id = await next()
  76. check('shuffle off: Next follows the list order (third song)', id === order[2], id + ' vs ' + order[2])
  77. await ev(`window.__loads = 0; document.querySelector('media-player audio').addEventListener('loadstart', () => window.__loads++); 1`)
  78. await press('#jb-shuffle')
  79. const on = await state()
  80. check('Shuffle shows on (pressed) and keeps the song that plays, without loading anything', on.shuffle === 'true' && on.src === order[2] && !on.paused && (await ev('window.__loads')) === 0, JSON.stringify(on) + ' loads=' + await ev('window.__loads'))
  81. const col = await ev(`({ shuffle: getComputedStyle(document.querySelector('#jb-shuffle')).color, repeat: getComputedStyle(document.querySelector('#jb-repeat')).color })`)
  82. check('a switch that is on looks different from one that is off (colour)', col.shuffle !== col.repeat, JSON.stringify(col))
  83. const seq = []
  84. for (let i = 0; i < 3; i++) seq.push(await next())
  85. console.log('shuffled sequence after ' + order[2] + ': ' + seq.join(' ') + ' (list order would be ' + order.slice(3, 6).join(' ') + ')')
  86. check('shuffle on: the songs that come next are not the list order, and none comes twice or is the one that played', seq.join() !== order.slice(3, 6).join() && new Set([order[2], ...seq]).size === 4, seq.join())
  87. const mid = (await playing())
  88. await ev(`window.__loads = 0; 1`)
  89. await press('#jb-shuffle')
  90. const off = await state()
  91. check('Shuffle off keeps the song that plays too', off.shuffle === 'false' && off.src === mid && !off.paused && (await ev('window.__loads')) === 0, JSON.stringify(off))
  92. const after = await next()
  93. const place = order.indexOf(mid)
  94. check('shuffle off: the song after the current one is the list\'s next (by id)', place < 0 || place === order.length - 1 ? true : after === order[place + 1], `${mid} → ${after}, list says ${order[place + 1]}`)
  95. // ---- Repeat: off → all → one → off, shown
  96. let rp = await state()
  97. check('Repeat starts off', rp.repeat === 'false' && /off/.test(rp.repeatTitle), JSON.stringify(rp))
  98. const iconOff = rp.repeatIcon
  99. await press('#jb-repeat'); const all = await state()
  100. check('Repeat once: all (pressed, title says all)', all.repeat === 'true' && /all/.test(all.repeatTitle) && all.repeatIcon === iconOff && !all.loop, JSON.stringify(all))
  101. await press('#jb-repeat'); const one = await state()
  102. check('Repeat twice: one (pressed, other icon, the audio loops)', one.repeat === 'true' && /this song/.test(one.repeatTitle) && one.repeatIcon !== iconOff && one.loop === true, JSON.stringify(one))
  103. // 'one': the song plays again by itself at its end, same file, no new load
  104. await ev(`window.__loads = 0; 1`)
  105. const t0 = await state()
  106. await sleep(TONE * 1000 + 1500)
  107. const again = await state()
  108. check('repeat one: after the song\'s length the same song plays on (not ended, same src, no new load)', again.src === t0.src && !again.paused && !again.ended && (await ev('window.__loads')) === 0, JSON.stringify({ t0, again }))
  109. await press('#jb-repeat'); rp = await state()
  110. check('Repeat third press: off again', rp.repeat === 'false' && /off/.test(rp.repeatTitle) && !rp.loop, JSON.stringify(rp))
  111. // ---- the end of the queue: go to the last song (Next until it stops moving), repeat off → stops; repeat all → the list starts again
  112. let last = await playing()
  113. for (let i = 0; i < order.length + 2; i++) { const n = await next(true); if (n === last) break; last = n }
  114. check('Next walks to the last song of the list', last === order[order.length - 1], last)
  115. await ev(`window.__loads = 0; document.querySelector('media-player audio').play(); 1`)
  116. await sleep(TONE * 1000 + 2500)
  117. const end = await state()
  118. console.log('repeat off, after the last song ended: ' + JSON.stringify(end) + ' loads=' + await ev('window.__loads'))
  119. check('repeat off: at the queue\'s end the playing stops (the last song does not start again, no other song starts)', end.src === order[order.length - 1] && (end.paused || end.ended) && (await ev('window.__loads')) === 0, JSON.stringify(end))
  120. // all: play the last song again (tap Play), then let it end
  121. await press('#jb-repeat')
  122. await ev(`document.querySelector('media-player audio').currentTime = 0; document.querySelector('media-player audio').play(); 1`)
  123. await page.waitFor(`!document.querySelector('media-player audio').paused`, { timeout: 5000 })
  124. // repeat was switched while the last song was the only one queued: the queue gets the list's first songs after it
  125. await page.waitFor(`document.querySelector('media-player audio').currentSrc.split('/').pop() !== ${JSON.stringify(order[order.length - 1])}`, { timeout: 8000, label: 'the song after the last' }).catch(() => {})
  126. const wrap = await state()
  127. check('repeat all: at the queue\'s end the list starts again from its first song, by id', wrap.src === order[0] && !wrap.paused, JSON.stringify(wrap))
  128. await press('#jb-repeat'); await press('#jb-repeat') // one, then off
  129. // ---- shuffle on: one round of the pool, then it stops with Repeat off, goes on with Repeat all
  130. await press('#jb-shuffle')
  131. await ev(`document.querySelector('media-player audio').pause(); 1`)
  132. const round = [await playing()]
  133. for (let i = 0; i < order.length + 2; i++) { const n = await next(true); if (n === round[round.length - 1]) break; round.push(n) }
  134. check('shuffle on, repeat off: Next stops after one round (every song once)', round.length === order.length && new Set(round).size === order.length, round.join())
  135. await press('#jb-repeat') // all
  136. const more = await next(true)
  137. check('shuffle on, repeat all: Next goes on with a new round', more !== round[round.length - 1] && more !== '', more)
  138. // ---- states survive a reload (and are in storage/ui.json)
  139. const ui = JSON.parse(readFileSync(join(storage, 'ui.json'), 'utf8'))
  140. check('storage/ui.json holds shuffle on and repeat all', ui.shuffle === 'on' && ui.repeat === 'all', JSON.stringify(ui))
  141. await page.goto(BASE + '/')
  142. await page.waitForSelector('#jb-shuffle')
  143. await sleep(1000)
  144. const re = await state().catch(() => null) || await ev(`(() => { const s = document.querySelector('#jb-shuffle'), r = document.querySelector('#jb-repeat'); return { shuffle: s.getAttribute('aria-pressed'), repeat: r.getAttribute('aria-pressed'), repeatTitle: r.title, shuffleTitle: s.title } })()`)
  145. check('after a reload the buttons show shuffle on and repeat all', re.shuffle === 'true' && re.repeat === 'true' && /all/.test(re.repeatTitle) && /on/.test(re.shuffleTitle), JSON.stringify(re))
  146. // a song tapped with shuffle on: it plays, and what follows is a shuffle (not the list)
  147. await page.click('virtual-list .vl-row:not(.blank)')
  148. await page.waitFor(`(() => { const a = document.querySelector('media-player audio'); return a && a.currentTime > 0.2 && !a.paused })()`, { timeout: 20000 })
  149. check('a tapped row plays that song while shuffle is on', (await playing()) === order[0], await playing())
  150. const s2 = [await next(), await next(), await next()]
  151. check('…and the songs after it are shuffled, not the list order', s2.join() !== order.slice(1, 4).join(), s2.join() + ' vs ' + order.slice(1, 4).join())
  152. } finally {
  153. await stop()
  154. }
  155. console.log(`${pass} passed, ${fail} failed`)
  156. 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