_err_system.mjs 1.7 KB

1234567891011121314151617181920212223242526272829
  1. // P89 抓 system 页签在"清完产物"模拟下的错误
  2. import { readFileSync } from 'node:fs'
  3. import { fileURLToPath } from 'node:url'
  4. import { dirname, resolve } from 'node:path'
  5. import { JSDOM, VirtualConsole } from 'jsdom'
  6. const HERE = dirname(fileURLToPath(import.meta.url))
  7. const dist = resolve(HERE, '../../../release/web')
  8. const idx = readFileSync(dist + '/index.html', 'utf8')
  9. const jsFile = (idx.match(/src="([^"]+\.js)"/) || [])[1].replace(/^.*\//, '')
  10. const cssFile = (idx.match(/href="([^"]+\.css)"/) || [])[1].replace(/^.*\//, '')
  11. const code = readFileSync(dist + '/assets/' + jsFile, 'utf8')
  12. const css = readFileSync(dist + '/assets/' + cssFile, 'utf8')
  13. const errs = []
  14. const vc = new VirtualConsole()
  15. vc.on('jsdomError', (e) => errs.push('JSDOM ' + String(e && e.message).slice(0, 300)))
  16. vc.on('error', (...a) => errs.push('CONSOLE ' + a.map(String).join(' ').slice(0, 300)))
  17. const dom = new JSDOM(idx.replace('</head>', '<style>' + css + '</style></head>'),
  18. { url: 'http://127.0.0.1:28084/detail/v2#tab=system', runScripts: 'outside-only', pretendToBeVisual: true, virtualConsole: vc })
  19. const w = dom.window
  20. w.fetch = async (u) => {
  21. const s = String(u)
  22. if (/fleet/.test(s)) return { ok: false, status: 500, json: async () => { throw new Error('500') }, text: async () => 'Internal Server Error' }
  23. return { ok: true, status: 200, json: async () => ({}), text: async () => '{}' }
  24. }
  25. try { w.eval(code) } catch (e) { errs.push('EVAL ' + String(e).slice(0, 300)) }
  26. await new Promise((r) => setTimeout(r, 3000))
  27. console.log('ERRS ' + JSON.stringify(errs.slice(0, 2)))
  28. const root = w.document.querySelector('#root') || w.document.body
  29. console.log('HAS_RECALC ' + !!root.querySelector('[data-added="recalc"]'))