/** * 呈现对拍(P11)· 多页签版:把**重构前的页面**与**Vue 迁移后的组件**放在同一份数据上比一遍。 * * 口径(实逮决定): * · `/detail/v2` 是浏览器里现渲的单页(HTML 只有壳 + 内联 app.js/charts.js + 语言包,无 window.__DATA__) * ⇒ 基线必须**让旧代码真跑一遍**:jsdom 加载现网页面,并在 **beforeParse** 装 fetch 桩转发到真实网关 * (桩晚装一句,旧页面就 `fetch is not defined`,基线全空)。 * · 页签由 hash 决定(`#tab=energy`)⇒ 基线按同一 hash 加载,两边渲染同页签。 * · Vue 侧用**同一份数据**(fetch 桩记录下的 /api/fleet 响应)经 SSR 渲成 HTML。 * · 比对: * ① 文本序列(`#root` 下所有文本节点,按 DOM 顺序、空白归一)—— 结构 + 文案一致; * ② 表格单元格文本(逐表、逐行); * ③ **图表数据**:旧页面内联 SVG 里的数字 vs Vue 侧 ECharts option 里 `__data__` 的数字 * (视觉实现不同是用户令「引入 ECharts」的结果,**数据必须同源**)。 * * 用法:node tools/render_parity.mjs [gateway] [tab1] [tab2] ... * 退出码:0 全一致 · 9 有不一致 · 2 基准页取不到。 */ import { fileURLToPath, pathToFileURL } from 'node:url' import { dirname, resolve } from 'node:path' import { JSDOM, VirtualConsole } from 'jsdom' const HERE = dirname(fileURLToPath(import.meta.url)) const GATEWAY = process.argv[2] || 'http://127.0.0.1:28084' const TABS = process.argv.slice(3).length ? process.argv.slice(3) : ['overview', 'energy', 'component'] /** 让 jsdom 里的相对地址 fetch 打到真实网关;顺手记录 `/api/fleet` 的原始响应。 */ function installFetch(window, state) { window.fetch = async (input, init) => { const raw = typeof input === 'string' ? input : (input && input.url) || String(input) const url = raw.startsWith('http') ? raw : GATEWAY + (raw.startsWith('/') ? raw : '/' + raw) const resp = await fetch(url, init) const body = await resp.text() if (url.includes('/api/fleet')) { try { state.fleet = JSON.parse(body); state.fleetUrl = url } catch { /* 非 JSON */ } } return { ok: resp.ok, status: resp.status, headers: { get: (k) => resp.headers.get(k) }, json: async () => JSON.parse(body), text: async () => body, } } } const norm = (s) => String(s).replace(/\s+/g, ' ').trim() /** 文本节点序列(跳过画布/SVG —— 图形由"图表数据"那一项比)。 */ function textsIn(root) { const out = [] const walk = (n) => { for (const c of n.childNodes) { if (c.nodeType === 3) { const s = norm(c.textContent) if (s) out.push(s) } else if (c.nodeType === 1) { const tag = c.tagName.toLowerCase() if (tag === 'canvas' || tag === 'svg' || tag === 'script' || tag === 'style') continue walk(c) } } } walk(root) return out } /** 表格:每张表的行×单元格文本。 */ function tablesIn(root) { return Array.from(root.querySelectorAll('table')).map((tb) => Array.from(tb.querySelectorAll('tr')).map((tr) => Array.from(tr.querySelectorAll('th,td')).map((c) => norm(c.textContent)))) } /** 旧页面图表里的数字(内联 SVG 文本里的数值)。 */ function svgNumbers(root) { const out = [] for (const t of Array.from(root.querySelectorAll('svg text'))) { for (const m of norm(t.textContent).matchAll(/-?\d[\d,]*(?:\.\d+)?/g)) out.push(m[0].replace(/,/g, '')) } return out } /** Vue 侧:ECharts option 的 `__data__` 里的数字(数据级对拍)。 */ function chartNumbers(root) { const out = [] for (const el of Array.from(root.querySelectorAll('[data-chart]'))) { let opt = null try { opt = JSON.parse(el.getAttribute('data-chart') || 'null') } catch { /* ignore */ } const d = opt && opt.__data__ if (!d) continue const walk = (x) => { if (typeof x === 'number') { if (Number.isFinite(x)) out.push(String(Math.round(x))); return } if (Array.isArray(x)) { x.forEach(walk); return } if (x && typeof x === 'object') { Object.values(x).forEach(walk) } } walk(d) } return out } async function baseline(tab) { const html = await (await fetch(GATEWAY + '/detail/v2')).text() const state = {} const dom = new JSDOM(html, { url: GATEWAY + `/detail/v2#tab=${tab}`, runScripts: 'dangerously', pretendToBeVisual: true, virtualConsole: new VirtualConsole(), beforeParse: (window) => installFetch(window, state), }) const doc = dom.window.document // ★实逮:`#root` 初始就带一个 `.panel`(出错提示占位)——若拿 `.panel` 当"渲染好了"的判据, // 会在 app.js 取数之前就跳出循环,基线全空。判据改用 render() 一定产出的 `.page-head`, // 并且要求 fetch 桩已经记下 `/api/fleet` 的响应。 for (let i = 0; i < 120; i++) { const root = doc.getElementById('root') if (state.fleet && root && root.querySelector('.page-head')) break await new Promise((r) => setTimeout(r, 500)) } return { doc, data: state.fleet, root: doc.getElementById('root') } } async function vueAnchors(data, tab) { const entry = pathToFileURL(resolve(HERE, '../target/ssr/entry-server.js')).href const html = await (await import(entry)).renderTab(data, tab) const dom = new JSDOM(`