_check_behavior.mjs 2.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import { readFileSync } from 'node:fs'
  2. import { fileURLToPath } from 'node:url'
  3. import { dirname, resolve } from 'node:path'
  4. import { JSDOM, VirtualConsole } from 'jsdom'
  5. const HERE = dirname(fileURLToPath(import.meta.url))
  6. const dist = resolve(HERE, '../../../release/web')
  7. const idx = readFileSync(dist + '/index.html', 'utf8')
  8. const jsFile = (idx.match(/src="\.\/assets\/([^"]+\.js)"/) || [])[1]
  9. const cssFile = (idx.match(/href="\.\/assets\/([^"]+\.css)"/) || [])[1]
  10. const code = readFileSync(dist + '/assets/' + jsFile, 'utf8')
  11. const css = readFileSync(dist + '/assets/' + cssFile, 'utf8')
  12. const fleet = { win: '2026年', tabs: {}, faults: {}, rel: {}, sysdist: {}, watch: [] }
  13. async function run(path, label) {
  14. const dom = new JSDOM(idx.replace('</head>', '<style>' + css + '</style></head>'),
  15. { url: 'http://127.0.0.1:28084' + path, runScripts: 'outside-only', pretendToBeVisual: true, virtualConsole: new VirtualConsole() })
  16. const w = dom.window
  17. w.fetch = async (u) => { const s = String(u)
  18. const body = s.includes('fleet') ? JSON.stringify(fleet) : '{}'
  19. return { ok: true, status: 200, json: async () => JSON.parse(body), text: async () => body } }
  20. try { w.eval(code) } catch (e) { console.log(label + ' EVAL_ERR ' + String(e).slice(0, 120)) }
  21. for (let i = 0; i < 12; i++) { await new Promise((r) => setTimeout(r, 500)); if (w.document.querySelector('.wb-tabs')) break }
  22. const d = w.document
  23. const active = Array.from(d.querySelectorAll('.wb-tabs button')).find((b) => String(b.className).includes('on'))
  24. console.log(label + ' | wrap=' + !!d.querySelector('.wrap') + ' portal=' + !!d.querySelector('.portal-full')
  25. + ' tabs=' + d.querySelectorAll('.wb-tabs button').length
  26. + ' active=' + (active ? active.textContent.trim() : '-')
  27. + ' fsubs=' + JSON.stringify(Array.from(d.querySelectorAll('[data-fsub]')).map((b) => b.textContent.trim()))
  28. + ' bodyLen=' + d.body.innerHTML.length)
  29. }
  30. await run('/detail/v2', 'DETAIL_NO_HASH')
  31. await run('/detail/v2#tab=vibration', 'DETAIL_VIB')
  32. await run('/', 'ROOT')