// P4 闸二:amis 渲染冒烟(抽样自动化)。 // // 口径:对每个模板类型(crud/form/tree/report/group)从闸一通过的 schema 中抽 ≥N(默认 5)个, // 用本地 amis SDK 6.13.0 + 无头 chromium(playwright)逐个渲染,判定: // ① 无未捕获 JS 错误(window.__amisRenderError 为空); // ② 根组件渲染出 DOM(#root 有子节点)。 // 全量渲染不要求——本脚本是抽样冒烟,样本与判定写进 闸二结果.json。 // // 用法:node smoke.mjs [产物目录=../转换产物] [每类型样本数=5] import http from 'node:http'; import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { chromium } from 'playwright'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const OUT_DIR = path.resolve(__dirname, process.argv[2] || '../转换产物'); const PER_TYPE = parseInt(process.argv[3] || '5', 10); const SDK_DIR = path.resolve(__dirname, '../../../eip4-poc/src/main/resources/static/sdk-local'); const SCHEMA_DIR = path.join(OUT_DIR, 'schema'); const CSV = path.join(OUT_DIR, '分级清单.csv'); // ── 从 CSV 选样:每个 root 抽 PER_TYPE 个闸一通过(gate1=true)的模板 ────────── function pickSamples() { const lines = fs.readFileSync(CSV, 'utf8').replace(/^/, '').split('\n').filter(Boolean); lines.shift(); // 表头 const byRoot = {}; for (const line of lines) { // CSV 首列 id、4 列 root、5 列 grade、6 列 gate1;用简易解析(前 6 列无逗号转义风险) const cols = line.split(','); const id = cols[0], root = cols[3], gate1 = cols[5]; if (gate1 !== 'true') continue; if (!byRoot[root]) byRoot[root] = []; if (byRoot[root].length < PER_TYPE) byRoot[root].push(id); } return byRoot; } // ── 最小静态服务器:/host.html + /sdk-local/* ──────────────────────────────── function startServer() { const types = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.woff': 'font/woff', '.woff2': 'font/woff2', '.ttf': 'font/ttf', '.eot': 'application/vnd.ms-fontobject', '.svg': 'image/svg+xml', '.png': 'image/png', '.json': 'application/json' }; const server = http.createServer((req, res) => { const [urlPathRaw, query] = req.url.split('?'); const urlPath = decodeURIComponent(urlPathRaw); // schema 经独立端点拉取(避免大 schema 进 URL header 触发 HTTP 431) if (urlPath === '/schema') { const id = new URLSearchParams(query || '').get('id'); const file = path.join(SCHEMA_DIR, id + '.json'); fs.readFile(file, (err, buf) => { if (err) { res.writeHead(404); res.end('{}'); return; } res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(buf); }); return; } let file; if (urlPath === '/host.html' || urlPath === '/') { file = path.join(__dirname, 'host.html'); } else if (urlPath.startsWith('/sdk-local/')) { file = path.join(SDK_DIR, urlPath.replace('/sdk-local/', '')); } else { res.writeHead(404); res.end('not found'); return; } fs.readFile(file, (err, buf) => { if (err) { res.writeHead(404); res.end('not found: ' + file); return; } res.writeHead(200, { 'Content-Type': types[path.extname(file)] || 'application/octet-stream' }); res.end(buf); }); }); return new Promise(resolve => server.listen(0, () => resolve(server))); } async function run() { const samples = pickSamples(); const server = await startServer(); const port = server.address().port; const browser = await chromium.launch({ headless: true }); const results = { perType: {}, samples: [], generatedAt: new Date().toISOString(), perTypeTarget: PER_TYPE }; for (const [root, ids] of Object.entries(samples)) { if (root === '(null-xml)') continue; results.perType[root] = { sampled: 0, pass: 0, fail: 0 }; for (const id of ids) { const schemaPath = path.join(SCHEMA_DIR, id + '.json'); if (!fs.existsSync(schemaPath)) continue; const url = `http://127.0.0.1:${port}/host.html?id=${encodeURIComponent(id)}`; const page = await browser.newPage(); const consoleErrors = []; page.on('console', m => { if (m.type() === 'error') consoleErrors.push(m.text()); }); page.on('pageerror', e => consoleErrors.push(String(e))); let pass = false, reason = ''; try { await page.goto(url, { waitUntil: 'networkidle', timeout: 20000 }); // 等根组件渲染出 DOM await page.waitForFunction( () => document.querySelector('#root') && document.querySelector('#root').children.length > 0, { timeout: 8000 } ).catch(() => {}); const state = await page.evaluate(() => ({ err: window.__amisRenderError, rootChildren: document.querySelector('#root') ? document.querySelector('#root').children.length : 0 })); // 过滤已知无害噪声(字体 404、桩接口告警不算渲染错误) const fatal = consoleErrors.filter(e => !/iconfont|\.woff|\.ttf|favicon|Failed to load resource/i.test(e)); if (state.err) { reason = 'JS错误:' + state.err; } else if (state.rootChildren === 0) { reason = '根组件未渲染出 DOM'; } else if (fatal.length) { reason = 'console错误:' + fatal[0].slice(0, 120); } else { pass = true; } } catch (e) { reason = '导航/渲染超时: ' + String(e).slice(0, 100); } await page.close(); results.perType[root].sampled++; results.perType[root][pass ? 'pass' : 'fail']++; results.samples.push({ root, id, pass, reason }); process.stdout.write(`${root} ${id}: ${pass ? 'PASS' : 'FAIL ' + reason}\n`); } } await browser.close(); server.close(); fs.writeFileSync(path.join(OUT_DIR, '闸二结果.json'), JSON.stringify(results, null, 2), 'utf8'); console.log('\n=== 闸二渲染冒烟汇总(抽样)==='); for (const [root, r] of Object.entries(results.perType)) { console.log(` ${root}: 抽样 ${r.sampled},通过 ${r.pass},失败 ${r.fail}`); } } run().catch(e => { console.error(e); process.exit(1); });