struct_diff_ctx.mjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // ★P23:Vue 的 SVG 图外壳(旧页直接内联 SVG,无包裹)⇒ 本层视为透明,不参与骨架
  21. if (tag === 'div' && (c.getAttribute('class') || '').includes('legacy-chart')) { walk(c); continue }
  22. // ★P27:`v-unwrap` 宿主在运行时被解包(SSR 不执行指令)⇒ 判据等价解包
  23. if ((c.getAttribute('class') || '').includes('unwrap-host')) { walk(c); continue }
  24. const cls = (c.getAttribute('class') || '').split(/\s+/).filter(Boolean).sort().join('.')
  25. out.push(tag + (cls ? '.' + cls : ''))
  26. walk(c)
  27. }
  28. }
  29. walk(root)
  30. return out
  31. }
  32. const API_KEEP = ['/api/maint_survey', '/api/facts', '/api/ont_chain', '/api/ask_models',
  33. '/api/ask_status', '/api/vibcms', '/api/turbine', '/api/problem', '/api/channel']
  34. function installFetch(window, state) {
  35. state.apis = state.apis || {}
  36. window.fetch = async (input, init) => {
  37. const raw = typeof input === 'string' ? input : (input && input.url) || String(input)
  38. const url = raw.startsWith('http') ? raw : GATEWAY + (raw.startsWith('/') ? raw : '/' + raw)
  39. const r = await fetch(url, init)
  40. const body = await r.text()
  41. let j = null
  42. try { j = JSON.parse(body) } catch { /* ignore */ }
  43. const path = url.slice(GATEWAY.length).split('?')[0]
  44. if (path === '/api/fleet' && !state.fleet) state.fleet = j
  45. if (path === '/api/vibcms' && !state.cms) state.cms = j
  46. if (API_KEEP.includes(path) && !state.apis[path]) state.apis[path] = j
  47. return { ok: r.ok, status: r.status, headers: r.headers, text: async () => body, json: async () => j }
  48. }
  49. }
  50. /** generation 前置:把 /api/curves 追到出图(与呈现对拍同口径)。 */
  51. async function awaitCurves(tries = 20, gapMs = 3000) {
  52. for (let k = 0; k < tries; k++) {
  53. const r = await fetch(GATEWAY + '/api/curves')
  54. if (r.ok) {
  55. const j = await r.json()
  56. if (!j.building && (j.figs || []).length) return j
  57. }
  58. await new Promise((r2) => setTimeout(r2, gapMs))
  59. }
  60. throw new Error('等 /api/curves 出图超时')
  61. }
  62. async function baseline(tab) {
  63. const state = {}
  64. if (tab === 'generation') { state.curves = await awaitCurves(); console.log(' [i] curves figs ' + (state.curves.figs || []).length) }
  65. const html = readFileSync(CLASSIC, 'utf8')
  66. const dom = new JSDOM(html, {
  67. url: GATEWAY + `/detail/v2#tab=${tab}`,
  68. runScripts: 'dangerously', pretendToBeVisual: true,
  69. virtualConsole: new VirtualConsole(),
  70. beforeParse: (w) => installFetch(w, state),
  71. })
  72. const doc = dom.window.document
  73. for (let i = 0; i < 120; i++) {
  74. const root = doc.getElementById('root')
  75. if (root && root.querySelector('.page-head')) break
  76. await new Promise((r) => setTimeout(r, 500))
  77. }
  78. return { root: doc.getElementById('root'), data: state.fleet, cms: state.cms ?? null,
  79. curves: state.curves ?? null, apis: state.apis ?? {} }
  80. }
  81. async function vueSkel(tab, data, cms, curves, apis) {
  82. const entry = pathToFileURL(resolve(HERE, '../target/ssr/entry-server.js')).href
  83. const html = await (await import(entry)).renderTab(data, tab, null, cms, curves, apis)
  84. const dom = new JSDOM(`<div id="wrap">${html}</div>`)
  85. const wrap = dom.window.document.getElementById('wrap')
  86. const nav = wrap.querySelector('.wb-tabs')
  87. if (nav) nav.remove()
  88. return skeletonIn(wrap)
  89. }
  90. let bad = 0
  91. for (const tab of TABS) {
  92. const { root, data, cms, curves, apis } = await baseline(tab)
  93. if (!root) { console.log(`[X] ${tab}: 基线未渲染`); bad++; continue }
  94. const a = skeletonIn(root)
  95. const b = await vueSkel(tab, data, cms, curves, apis)
  96. if (a.length === b.length && a.every((x, i) => x === b[i])) {
  97. console.log(`[OK] ${tab} 结构一致(${a.length} 个元素)`)
  98. } else {
  99. bad++
  100. const diffs = []
  101. let first = -1
  102. for (let i = 0; i < Math.max(a.length, b.length); i++) {
  103. if (a[i] !== b[i]) { if (first < 0) first = i; if (diffs.length < 2) diffs.push(`#${i} 基准=${a[i] || '∅'} · Vue=${b[i] || '∅'}`) }
  104. }
  105. if (first >= 0) {
  106. const lo = Math.max(0, first - 8), hi = Math.min(Math.max(a.length, b.length), first + 8)
  107. for (let i = lo; i < hi; i++) {
  108. if (a[i] !== b[i]) diffs.push(` #${i} 基准=${a[i] || '∅'} ⟷ Vue=${b[i] || '∅'}`)
  109. }
  110. }
  111. console.log(`[X] ${tab} 元素数 基准 ${a.length} · Vue ${b.length}`)
  112. for (const d of diffs) console.log(` ${d}`)
  113. }
  114. }
  115. console.log(`\n结构对拍结论: ${TABS.length - bad}/${TABS.length} 页签结构一致`)
  116. process.exit(bad ? 9 : 0)