_check_empty_state.mjs 1.9 KB

12345678910111213141516171819202122232425262728293031
  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="([^"]+\.js)"/) || [])[1].replace(/^.*\//, '')
  9. const cssFile = (idx.match(/href="([^"]+\.css)"/) || [])[1].replace(/^.*\//, '')
  10. const code = readFileSync(dist + '/assets/' + jsFile, 'utf8')
  11. const css = readFileSync(dist + '/assets/' + cssFile, 'utf8')
  12. const empty = '{"win":"2026年","kpi":{},"fus":{},"faults":{},"sysdist":{},"all_months":[]}'
  13. for (const tab of ['overview','energy','component','vibration','generation','fault']) {
  14. const errs = []
  15. const vc = new VirtualConsole()
  16. vc.on('jsdomError', (e) => errs.push(String(e && e.message).slice(0, 90)))
  17. vc.on('error', (...a) => errs.push(a.map(String).join(' ').slice(0, 90)))
  18. let calls = 0
  19. const dom = new JSDOM(idx.replace('</head>', '<style>' + css + '</style></head>'),
  20. { url: 'http://127.0.0.1:28084/detail/v2#tab=' + tab, runScripts: 'outside-only', pretendToBeVisual: true, virtualConsole: vc })
  21. const w = dom.window
  22. w.fetch = async (u) => { calls++; const body = /fleet/.test(String(u)) ? empty : '{}'
  23. return { ok: true, status: 200, json: async () => JSON.parse(body), text: async () => body } }
  24. try { w.eval(code) } catch (e) { errs.push('EVAL ' + String(e).slice(0, 90)) }
  25. await new Promise((r) => setTimeout(r, 2600))
  26. const root = w.document.querySelector('#root') || w.document.body
  27. const txt = (root.textContent || '').replace(/\s+/g, ' ')
  28. console.log(tab.padEnd(11) + ' elements=' + String(root.querySelectorAll('*').length).padStart(5)
  29. + ' calls=' + calls + ' errs=' + errs.length + ' head=' + JSON.stringify(txt.slice(0, 46)))
  30. }