gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/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

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre