import { readFileSync } from 'node:fs' 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] const CLASSIC = resolve(HERE, '../../../release/web/detail_v2.html') const state = { apis: {}, cache: new Map() } function installFetch(window, state) { window.fetch = async (input) => { const raw = typeof input === 'string' ? input : (input && input.url) || String(input) const url = raw.startsWith('http') ? raw : GATEWAY + (raw.startsWith('/') ? raw : '/' + raw) if (state.cache.has(url)) { const c = state.cache.get(url); return { ok: true, status: 200, headers: new Map(), text: async () => c, json: async () => JSON.parse(c) } } const r = await fetch(url); const b = await r.text(); if (r.ok) state.cache.set(url, b) return { ok: r.ok, status: r.status, headers: r.headers, text: async () => b, json: async () => JSON.parse(b) } } } const dom = new JSDOM(readFileSync(CLASSIC, 'utf8'), { url: GATEWAY + '/detail/v2#tab=component', runScripts: 'dangerously', pretendToBeVisual: true, virtualConsole: new VirtualConsole(), beforeParse: (w) => installFetch(w, state) }) const doc = dom.window.document for (let i = 0; i < 120; i++) { const r = doc.getElementById('root'); if (r && r.querySelector('.page-head')) break; await new Promise((x) => setTimeout(x, 500)) } let last = -1, same = 0 for (let i = 0; i < 40; i++) { const r = doc.getElementById('root'); const n = r ? r.querySelectorAll('*').length : 0; if (n === last) { if (++same >= 3) break } else same = 0; last = n; await new Promise((x) => setTimeout(x, 700)) } function sections(root, tag) { const all = Array.from(root.querySelectorAll('*')) console.log(tag + '_TOTAL ' + all.length) all.forEach((el, i) => { if (el.tagName.toLowerCase() === 'section') { const kids = Array.from(el.children).slice(0, 4).map((c) => c.tagName.toLowerCase() + (c.className ? '.' + String(c.className).trim().split(/\s+/).join('.') : '')) console.log(tag + '_SEC #' + i + ' kids=' + JSON.stringify(kids)) } }) } sections(doc.getElementById('root'), 'BASE') const fleetRaw = state.cache.get(GATEWAY + '/api/fleet') const entry = pathToFileURL(resolve(HERE, '../target/ssr/entry-server.js')).href const html = await (await import(entry)).renderTab(fleetRaw ? JSON.parse(fleetRaw) : null, 'component', null, null, null, {}) const vd = new JSDOM('