_check_cms_livewin.mjs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // P80:就地切换时间窗,看「振动 CMS」的 chip 是否随之变化(真实交互路径:#win 下拉 change → App 重新取数)
  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 fleetA = JSON.parse(readFileSync(resolve(HERE, '_fleet_fixture.json'), 'utf8')) // 2026年
  14. const fleetB = JSON.parse(JSON.stringify(fleetA)); fleetB.win = '全程'
  15. fleetB.kpi = Object.assign({}, fleetA.kpi || {}, { '关注台': 4, '报警台': 19, '全场': 38 })
  16. const vib = readFileSync(resolve(HERE, '_vibcms_fixture.json'), 'utf8')
  17. const calls = []
  18. let useB = false
  19. const dom = new JSDOM(idx.replace('</head>', '<style>' + css + '</style></head>'),
  20. { url: 'http://127.0.0.1:28084/detail/v2#tab=vibration:portal_cms', runScripts: 'outside-only', pretendToBeVisual: true, virtualConsole: new VirtualConsole() })
  21. const w = dom.window
  22. w.fetch = async (u) => {
  23. const s = String(u); calls.push(s)
  24. const f = useB ? fleetB : fleetA
  25. const body = /fleet/.test(s) ? JSON.stringify(f) : (/vibcms/.test(s) ? vib : '{}')
  26. return { ok: true, status: 200, json: async () => JSON.parse(body), text: async () => body }
  27. }
  28. try { w.eval(code) } catch (e) { console.log('EVAL_ERR ' + String(e).slice(0, 140)) }
  29. for (let i = 0; i < 20; i++) { await new Promise((r) => setTimeout(r, 400)); if (/振动与 SCADA/.test(w.document.body.textContent || '')) break }
  30. const chips = () => Array.from((w.document.querySelector('#root') || w.document.body).querySelectorAll('span'))
  31. .map((s) => s.textContent.trim()).filter((x) => /时间窗 ·|关注台|报警台|全场|证据窗末/.test(x))
  32. console.log('BEFORE ' + JSON.stringify(chips()))
  33. // 真实交互:改 #win 下拉并派发 change
  34. const sel = w.document.querySelector('#win')
  35. console.log('SELECT ' + (sel ? 'Y' : 'N') + ' options=' + (sel ? sel.querySelectorAll('option').length : 0))
  36. if (sel) {
  37. useB = true
  38. sel.value = '近30日'
  39. sel.dispatchEvent(new w.Event('change', { bubbles: true }))
  40. }
  41. for (let i = 0; i < 20; i++) { await new Promise((r) => setTimeout(r, 500)); if (calls.some((c) => /win=/.test(c))) break }
  42. await new Promise((r) => setTimeout(r, 2500))
  43. console.log('AFTER ' + JSON.stringify(chips()))
  44. console.log('CALLS ' + JSON.stringify(calls.slice(-6)))
  45. console.log('WINDOW_FETCH ' + calls.some((c) => /win=/.test(c)))