diff --git a/scripts/preview.mjs b/scripts/preview.mjs deleted file mode 100644 index 27f4264..0000000 --- a/scripts/preview.mjs +++ /dev/null @@ -1,1154 +0,0 @@ -#!/usr/bin/env node -/** - * scripts/preview.mjs — 零依赖侧栏预览生成器。 - * - * 做法: - * 1. 用 vm 加载真实的 panel/lib/client.js(复用 panel/test/client.test.js 的 - * window.__ModuleLoader__.load + 最小 React/document 替身思路),并捕获 - * bundle 注入到 - - -
- -
-
-${panelHtml} -
-
-
- - - - -`, - } -} - -// #endregion - -// #region 主流程 - -async function tick(times = 4) { - for (let i = 0; i < times; i += 1) { - await new Promise((resolveTick) => setImmediate(resolveTick)) - } -} - -async function settle(hooks, rootVNode) { - let tree = null - for (let pass = 0; pass < 40; pass += 1) { - hooks.beginRender() - tree = mountNode(rootVNode) - hooks.runEffects() - await tick() - if (!hooks.consumeDirty()) return tree - } - fail('面板在 40 次渲染后仍未稳定(可能存在状态更新死循环)。') -} - -function readPngSize(pngPath) { - const bytes = readFileSync(pngPath) - if (bytes.length < 33) fail(`截图文件不完整:${pngPath}(${bytes.length} 字节)`) - const signature = bytes.subarray(0, 8).toString('hex') - if (signature !== '89504e470d0a1a0a') fail(`截图不是合法 PNG:${pngPath}`) - return { width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20), bytes: bytes.length } -} - -function measureHeight(chrome, htmlPath, pageWidth, windowWidth, profileDir) { - const args = [ - '--headless=new', - '--disable-gpu', - '--hide-scrollbars', - '--no-first-run', - '--no-default-browser-check', - '--disable-extensions', - '--disable-background-networking', - '--disable-sync', - '--mute-audio', - `--user-data-dir=${profileDir}`, - '--virtual-time-budget=3000', - `--window-size=${windowWidth},600`, - '--dump-dom', - pathToFileURL(htmlPath).href, - ] - const result = chromeRun(chrome, args, '测量页面高度') - const match = String(result.stdout || '').match(/id="pv-height"[^>]*>(\d+) 20000) { - fail(`测量到的页面高度不合理:${height}`) - } - return height -} - -function screenshot(chrome, htmlPath, pngPath, pageWidth, height, scale, profileDir, windowWidth) { - const args = [ - '--headless=new', - '--disable-gpu', - '--hide-scrollbars', - `--force-device-scale-factor=${scale}`, - '--no-first-run', - '--no-default-browser-check', - '--disable-extensions', - '--disable-background-networking', - '--disable-sync', - '--mute-audio', - `--user-data-dir=${profileDir}`, - '--virtual-time-budget=3000', - `--window-size=${windowWidth},${height}`, - `--screenshot=${pngPath}`, - pathToFileURL(htmlPath).href, - ] - chromeRun(chrome, args, '截图') - if (!existsSync(pngPath)) fail(`Chrome 没有生成截图:${pngPath}`) - const size = readPngSize(pngPath) - if (size.bytes < 5000) fail(`截图文件过小,可能是空白图:${pngPath}(${size.bytes} 字节)`) - return size -} - -async function main() { - const options = parseArgs(process.argv.slice(2)) - const chrome = options.htmlOnly ? null : findChrome(options.chrome) - const hooks = createHooks() - const bundle = loadBundle(options.source, hooks) - - if (typeof bundle.exports.SanshengPanel !== 'function') { - fail('bundle 没有导出 SanshengPanel,无法渲染。') - } - - const { React } = hooks - const { ctx } = createDemoContext(options) - const host = bundle.exports.createHost(ctx) - const rootVNode = React.createElement(bundle.exports.SanshengPanel, { - host, - sessionId: LEAD_SESSION, - compact: true, - }) - - let tree = await settle(hooks, rootVNode) - - if (options.view === 'tasks') { - const tab = findNode(tree, (node) => node.props && node.props['data-side-tab'] === 'tasks') - if (!tab || typeof tab.props.onClick !== 'function') { - fail('找不到「任务」页签,无法切换到 tasks 视图。') - } - tab.props.onClick() - tree = await settle(hooks, rootVNode) - const activeTab = findNode(tree, (node) => node.props && node.props['data-side-tab'] === 'tasks') - if (!activeTab || activeTab.props['aria-selected'] !== true) { - fail('点击「任务」页签后没有切换到任务视图。') - } - } - - if (options.view === 'court' && options.seat !== 'lead') { - const card = findNode(tree, (node) => node.props && node.props['data-role'] === options.seat) - if (!card || typeof card.props.onClick !== 'function') { - fail(`找不到「${options.seat}」席位,无法切到该部门详情。`) - } - card.props.onClick() - tree = await settle(hooks, rootVNode) - const selected = findNode(tree, (node) => node.props && node.props['data-role'] === options.seat) - if (!selected || selected.props['aria-pressed'] !== 'true') { - fail(`点击「${options.seat}」席位后没有切换到该部门详情。`) - } - } - - const panelHtml = serializeNode(tree) - if (!panelHtml.trim()) fail('序列化后的面板 HTML 为空。') - if (panelHtml.length < 2000) fail(`面板 HTML 过短(${panelHtml.length} 字符),可能渲染不完整。`) - if (panelHtml.includes('sl-crash')) fail('面板渲染进了错误边界(出现 .sl-crash)。') - - const rootNode = findNode(tree, (node) => node.type === 'div') - if (!rootNode || rootNode.props['data-sansheng-sidebar'] !== '1') { - fail('渲染结果不是侧栏形态(缺少 data-sansheng-sidebar="1")。') - } - - // 先落中间 HTML:即使后面的校验/截图失败,也能直接打开排查。 - const page = buildPage({ viewport: options.viewport, - css: bundle.css, - panelHtml, - width: options.width, - theme: options.theme, - view: options.view, - }) - const htmlPath = join(os.tmpdir(), `dsh-preview-${process.pid}-${Date.now()}.html`) - writeFileSync(htmlPath, page.html, 'utf8') - const htmlTarget = options.html || (options.htmlOnly ? resolve(options.out, `${options.name}.html`) : null) - if (htmlTarget) { - mkdirSync(dirname(htmlTarget), { recursive: true }) - writeFileSync(htmlTarget, page.html, 'utf8') - } - - if (options.view === 'court') { - const seatCount = (panelHtml.match(/data-role="/g) || []).length - if (seatCount !== 10) fail(`朝堂应该渲染 10 个席位,实际 ${seatCount} 个。`) - const missing = Object.values(DEMO_LABELS).filter((label) => !panelHtml.includes(label)) - if (missing.length > 0) fail(`面板缺少席位:${missing.join('、')}`) - for (const key of ['在朝成员', '正在运行', '在办任务', '已完成']) { - if (!panelHtml.includes(key)) fail(`统计区缺少「${key}」。`) - } - if (!panelHtml.includes('GPT-6 Astra')) fail('模型下拉没有渲染出「GPT-6 Astra」。') - if (!panelHtml.includes('>高<')) fail('推理等级下拉没有渲染出「高」。') - } else { - if (!panelHtml.includes('任务队列')) fail('任务视图缺少「任务队列」标题。') - const taskRows = (panelHtml.match(/class="sl-task-copy"/g) || []).length - if (taskRows !== 4) fail(`任务视图应该渲染 4 条任务,实际 ${taskRows} 条。`) - } - - if (options.htmlOnly) { - if (!htmlTarget) fail('内部错误:--html-only 没有解析出 HTML 输出路径。') - const bytes = statSync(htmlTarget).size - if (bytes < 5000) fail(`HTML 过小,可能渲染不完整:${htmlTarget}(${bytes} 字节)`) - try { rmSync(htmlPath, { force: true }) } catch { /* 忽略清理失败 */ } - process.stdout.write( - `✓ ${htmlTarget}\n` + - ` HTML-only · ${bytes} 字节 · 侧栏 ${options.width}px · ` + - `视图 ${options.view} · 主题 ${options.theme}\n` + - ` CSS ${bundle.css.length} 字符 · 面板 ${panelHtml.length} 字符 · 源 ${bundle.sourcePath}\n`, - ) - return - } - - const outDir = options.out - mkdirSync(outDir, { recursive: true }) - const pngPath = join(outDir, `${options.name}.png`) - - const profileDir = join(os.tmpdir(), `dsh-preview-chrome-${process.pid}-${Date.now()}`) - mkdirSync(profileDir, { recursive: true }) - - let size - try { - const measured = measureHeight(chrome, htmlPath, page.pageWidth, page.windowWidth, profileDir) - const height = Math.max(200, Math.ceil(measured) + 2) - size = screenshot(chrome, htmlPath, pngPath, page.pageWidth, height, options.scale, profileDir, page.windowWidth) - - const expectedWidth = page.windowWidth * options.scale - if (size.width !== expectedWidth) { - fail( - `截图宽度 ${size.width}px 与预期 ${expectedWidth}px(${page.windowWidth}×${options.scale})不符,` + - '页面可能被 Chrome 缩放或窗口尺寸未生效。', - ) - } - } finally { - try { rmSync(profileDir, { recursive: true, force: true }) } catch { /* 忽略清理失败 */ } - try { rmSync(htmlPath, { force: true }) } catch { /* 忽略清理失败 */ } - } - - const seatNote = options.view === 'court' ? ' · 十席齐全' : ' · 任务队列' - process.stdout.write( - `✓ ${pngPath}\n` + - ` ${size.width}×${size.height}px @${options.scale}x · 侧栏 ${options.width}px · ` + - `视图 ${options.view} · 主题 ${options.theme}${seatNote}\n` + - ` CSS ${bundle.css.length} 字符 · HTML ${panelHtml.length} 字符 · 源 ${bundle.sourcePath}\n`, - ) - if (options.html) process.stdout.write(` 中间 HTML:${options.html}\n`) -} - -main().catch((error) => { - if (error && error.previewFailure) { - process.stderr.write(`\n[preview] 失败:${error.message}\n`) - } else { - process.stderr.write(`\n[preview] 未预期的错误:\n${error && error.stack ? error.stack : error}\n`) - } - process.exit(1) -}) - -// #endregion