_check_tabapis.mjs 1.7 KB

1234567891011121314151617181920212223242526
  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 fleet = readFileSync(resolve(HERE, '_fleet_fixture.json'), 'utf8')
  13. const calls = []
  14. const dom = new JSDOM(idx.replace('</head>', '<style>' + css + '</style></head>'),
  15. { url: 'http://127.0.0.1:28084/detail/v2#tab=system', runScripts: 'outside-only', pretendToBeVisual: true, virtualConsole: new VirtualConsole() })
  16. const w = dom.window
  17. w.fetch = async (u) => { const s = String(u); calls.push(s)
  18. const body = /fleet/.test(s) ? fleet : '{}'
  19. return { ok: true, status: 200, json: async () => JSON.parse(body), text: async () => body } }
  20. try { w.eval(code) } catch (e) { console.log('EVAL_ERR ' + String(e).slice(0, 120)) }
  21. for (let i = 0; i < 24; i++) { await new Promise((r) => setTimeout(r, 400)); if (calls.some((x) => /maint_survey/.test(x))) break }
  22. await new Promise((r) => setTimeout(r, 1500))
  23. console.log('CALLS ' + JSON.stringify(calls))
  24. console.log('MAINT_CALLED ' + calls.some((x) => /maint_survey/.test(x)))
  25. console.log('TABS_ACTIVE ' + (Array.from(w.document.querySelectorAll('nav.tabs#tabs > button')).find((b) => String(b.className).includes('on')) || {}).textContent)