gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchday418ed6da45|||PR README: media-player pin df9e7dfmreday41/tests/jb20.mjs

9.0 KB

  1. // 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.
  2. // 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
  3. // 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).
  4. // The centering of the playing song is NOT tested: after a reload the page does not know what played (jukebox#16 / hybriel#63).
  5. // 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_MS
  6. import { spawn, execFileSync } from 'node:child_process'
  7. import { rmSync, mkdirSync } 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 || '8530'
  12. const { launchBrowser, sleep } = await import('./cdp.mjs')
  13. const root = process.env.JB20_ROOT || join(dirname(fileURLToPath(import.meta.url)), '..')
  14. const dir = process.env.JB20_DIR || '/media/STORAGE/claude-scratch/jukebox/JB20/gate'
  15. const port = Number(process.env.JB20_PORT || 8737)
  16. const songs = process.env.JB20_SONGS || '3000'
  17. const BASE = `http://127.0.0.1:${port}`
  18. const COLD_MS = Number(process.env.JUKEBOX_COLD_MS || 150000)
  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, 'tone3.mp3')
  31. rmSync(storage, { recursive: true, force: true })
  32. execFileSync('ffmpeg', ['-v', 'error', '-y', '-f', 'lavfi', '-i', 'sine=frequency=440:duration=3', '-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, songs], { env: { ...process.env, JUKEBOX_STORAGE: storage }, encoding: 'utf8', timeout: 900000 })
  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. const api = async (path) => (await fetch(BASE + path)).json()
  39. try {
  40. for (const t0 = Date.now(); ; ) {
  41. try { if ((await fetch(BASE + '/api/stats')).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 ev = (e) => page.evaluate(e)
  49. await page.send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true })
  50. const rowsReady = `document.querySelectorAll('virtual-list .vl-row:not(.blank)').length > 4 && document.querySelector('virtual-list .vl-row:not(.blank) .vl-title').textContent !== ''`
  51. const settle = async (p = page) => { await p.waitFor(rowsReady, { label: 'rows' }); await sleep(900) }
  52. // the titles of the rows in view: the rows whose top lies inside the scrolling box
  53. const 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) })()`)
  54. const top = (p = page) => p.evaluate(`document.querySelector('virtual-list .vl-scroller').scrollTop`)
  55. const wheel = async (dy, n) => {
  56. 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 } })()`)
  57. 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) }
  58. await sleep(1200) // the list reports its offset at most every 100 ms, the server writes it
  59. }
  60. const same = (a, b) => a.length > 0 && JSON.stringify(a) === JSON.stringify(b)
  61. // 1. Songs, artist order: scroll far down, reload
  62. await page.goto(BASE + '/'); await settle()
  63. await wheel(3000, 14)
  64. const t1 = await top(), v1 = await inView()
  65. check('songs: scrolled far down (the list is not at the top)', t1 > 20000, 'scrollTop ' + t1)
  66. await page.send('Page.reload'); await settle()
  67. const v2 = await inView(), t2 = await top()
  68. check('songs: after a reload the same rows are in view', same(v1, v2), `before ${JSON.stringify(v1)} @${t1}, after ${JSON.stringify(v2)} @${t2}`)
  69. // scrolling on from there works (the rows around the place load)
  70. await wheel(600, 3)
  71. const t2b = await top()
  72. check('songs: scrolling on after the reload moves on from the saved place', t2b > t2, `${t2} -> ${t2b}`)
  73. // 2. a fresh tab opens there too (the server remembers, not the page)
  74. const v2b = await inView(), t2c = await top()
  75. const tab2 = await browser.newPage()
  76. await tab2.send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true })
  77. await tab2.goto(BASE + '/'); await settle(tab2)
  78. check('songs: a fresh tab opens at the saved place', same(v2b, await inView(tab2)), `${JSON.stringify(v2b)} vs ${JSON.stringify(await inView(tab2))}`)
  79. await tab2.close()
  80. // 3. another sort has its own place (a list is named by which list it is, not by a row)
  81. // 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)
  82. await wheel(-3000, 20)
  83. await page.focus('#sort')
  84. await page.send('Input.dispatchKeyEvent', { type: 'rawKeyDown', key: 'ArrowDown', code: 'ArrowDown', windowsVirtualKeyCode: 40 })
  85. await page.send('Input.dispatchKeyEvent', { type: 'keyUp', key: 'ArrowDown', code: 'ArrowDown', windowsVirtualKeyCode: 40 })
  86. await sleep(1500)
  87. 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 }
  88. check('songs: another sort opens at its own top, not at the old offset', (await top()) < 100, 'scrollTop ' + (await top()))
  89. await wheel(3000, 6)
  90. const v3 = await inView()
  91. await page.send('Page.reload'); await settle()
  92. 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())}`)
  93. // 4. Artists and Albums
  94. for (const [path, n] of [['/artists', 12], ['/albums', 12]]) {
  95. await page.goto(BASE + path); await settle()
  96. await wheel(900, n)
  97. const a = await inView(), ta = await top()
  98. await page.send('Page.reload'); await settle()
  99. 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())}`)
  100. }
  101. // 5. one album's songs (only when an album is long enough to scroll)
  102. const al = (await api('/api/albums?limit=500')).items.sort((x, y) => y.songCount - x.songCount)[0]
  103. if (al && al.songCount > 14) {
  104. await page.goto(BASE + '/albums/' + al.id); await settle()
  105. await wheel(300, 4)
  106. const b = await inView(), tb = await top()
  107. await page.send('Page.reload'); await settle()
  108. 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())}`)
  109. } else console.log('skip an album (no album with more than 14 songs: ' + (al ? al.songCount : 'none') + ')')
  110. // 6. the albums list is still at ITS place when another list had a different one
  111. await page.goto(BASE + '/'); await settle()
  112. check('songs: back on the Songs screen it still opens at the place of the title sort', (await top()) > 5000, 'scrollTop ' + (await top()))
  113. } finally {
  114. await stop()
  115. }
  116. console.log(`${pass} passed, ${fail} failed`)
  117. 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