struct_diff.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // P20 结构对拍:把 v2.9.2 原文页(本地经典页,jsdom 真跑)与 Vue SSR 输出逐页签比「结构骨架」
  2. // = #root 下每个元素的 `tag.class…`(忽略 svg/canvas/script/style 与文本)
  3. // 用法:node tools/struct_diff.mjs [gateway] [tab…]
  4. import { readFileSync } from 'node:fs'
  5. import { fileURLToPath, pathToFileURL } from 'node:url'
  6. import { dirname, resolve } from 'node:path'
  7. import { JSDOM, VirtualConsole } from 'jsdom'
  8. const HERE = dirname(fileURLToPath(import.meta.url))
  9. const GATEWAY = process.argv[2] || 'http://106.120.102.238:28084'
  10. const TABS = process.argv.slice(3).length ? process.argv.slice(3)
  11. : ['overview', 'energy', 'component', 'fault', 'vibration', 'generation', 'decision', 'assistant', 'report', 'system']
  12. const CLASSIC = resolve(HERE, '../../../release/web/detail_v2.html')
  13. const norm = (s) => String(s || '').replace(/\s+/g, ' ').trim()
  14. function skeletonIn(root) {
  15. const out = []
  16. const walk = (el) => {
  17. for (const c of Array.from(el.children)) {
  18. const tag = c.tagName.toLowerCase()
  19. if (tag === 'canvas' || tag === 'svg' || tag === 'script' || tag === 'style') continue
  20. const cls = (c.getAttribute('class') || '').split(/\s+/).filter(Boolean).sort().join('.')
  21. out.push(tag + (cls ? '.' + cls : ''))
  22. walk(c)
  23. }
  24. }
  25. walk(root)
  26. return out
  27. }
  28. const API_KEEP = ['/api/maint_survey', '/api/facts', '/api/ont_chain', '/api/ask_models',
  29. '/api/ask_status', '/api/vibcms', '/api/turbine', '/api/problem', '/api/channel']
  30. function installFetch(window, state) {
  31. state.apis = state.apis || {}
  32. window.fetch = async (input, init) => {
  33. const raw = typeof input === 'string' ? input : (input && input.url) || String(input)
  34. const url = raw.startsWith('http') ? raw : GATEWAY + (raw.startsWith('/') ? raw : '/' + raw)
  35. const r = await fetch(url, init)
  36. const body = await r.text()
  37. let j = null
  38. try { j = JSON.parse(body) } catch { /* ignore */ }
  39. const path = url.slice(GATEWAY.length).split('?')[0]
  40. if (path === '/api/fleet' && !state.fleet) state.fleet = j
  41. if (path === '/api/vibcms' && !state.cms) state.cms = j
  42. if (API_KEEP.includes(path) && !state.apis[path]) state.apis[path] = j
  43. return { ok: r.ok, status: r.status, headers: r.headers, text: async () => body, json: async () => j }
  44. }
  45. }
  46. /** generation 前置:把 /api/curves 追到出图(与呈现对拍同口径)。 */
  47. async function awaitCurves(tries = 20, gapMs = 3000) {
  48. for (let k = 0; k < tries; k++) {
  49. const r = await fetch(GATEWAY + '/api/curves')
  50. if (r.ok) {
  51. const j = await r.json()
  52. if (!j.building && (j.figs || []).length) return j
  53. }
  54. await new Promise((r2) => setTimeout(r2, gapMs))
  55. }
  56. throw new Error('等 /api/curves 出图超时')
  57. }
  58. async function baseline(tab) {
  59. const state = {}
  60. if (tab === 'generation') { state.curves = await awaitCurves(); console.log(' [i] curves figs ' + (state.curves.figs || []).length) }
  61. const html = readFileSync(CLASSIC, 'utf8')
  62. const dom = new JSDOM(html, {
  63. url: GATEWAY + `/detail/v2#tab=${tab}`,
  64. runScripts: 'dangerously', pretendToBeVisual: true,
  65. virtualConsole: new VirtualConsole(),
  66. beforeParse: (w) => installFetch(w, state),
  67. })
  68. const doc = dom.window.document
  69. for (let i = 0; i < 120; i++) {
  70. const root = doc.getElementById('root')
  71. if (root && root.querySelector('.page-head')) break
  72. await new Promise((r) => setTimeout(r, 500))
  73. }
  74. return { root: doc.getElementById('root'), data: state.fleet, cms: state.cms ?? null,
  75. curves: state.curves ?? null, apis: state.apis ?? {} }
  76. }
  77. async function vueSkel(tab, data, cms, curves, apis) {
  78. const entry = pathToFileURL(resolve(HERE, '../target/ssr/entry-server.js')).href
  79. const html = await (await import(entry)).renderTab(data, tab, null, cms, curves, apis)
  80. const dom = new JSDOM(`<div id="wrap">${html}</div>`)
  81. const wrap = dom.window.document.getElementById('wrap')
  82. const nav = wrap.querySelector('.wb-tabs')
  83. if (nav) nav.remove()
  84. return skeletonIn(wrap)
  85. }
  86. let bad = 0
  87. for (const tab of TABS) {
  88. const { root, data, cms, curves, apis } = await baseline(tab)
  89. if (!root) { console.log(`[X] ${tab}: 基线未渲染`); bad++; continue }
  90. const a = skeletonIn(root)
  91. const b = await vueSkel(tab, data, cms, curves, apis)
  92. if (a.length === b.length && a.every((x, i) => x === b[i])) {
  93. console.log(`[OK] ${tab} 结构一致(${a.length} 个元素)`)
  94. } else {
  95. bad++
  96. const diffs = []
  97. for (let i = 0; i < Math.max(a.length, b.length) && diffs.length < 4; i++) {
  98. if (a[i] !== b[i]) diffs.push(`#${i} 基准=${a[i] || '∅'} · Vue=${b[i] || '∅'}`)
  99. }
  100. console.log(`[X] ${tab} 元素数 基准 ${a.length} · Vue ${b.length}`)
  101. for (const d of diffs) console.log(` ${d}`)
  102. }
  103. }
  104. console.log(`\n结构对拍结论: ${TABS.length - bad}/${TABS.length} 页签结构一致`)
  105. process.exit(bad ? 9 : 0)