#!/usr/bin/env node /** * 把 Playwright 的文本输出转成**逐名清单**(name / project / status)。 * * 为什么要它:§11.6 记的是「104 failed / 21 passed」这样一个**聚合数**, * 没有名单 ⇒ 本轮重采实取 113 张时,那 +9 无法从证据归因,只能写「推断」。 * 聚合数经不起二次追问,逐名清单才可以。纪律同 §5.42/§5.45: * **计数为 0 一律走「未能度量」(EXIT=2),不与"全部通过"共用 0**。 * * 用法:node scripts/visual-manifest.mjs * 0 = 解析成功且计数>0 · 1 = 有 failed 项 · 2 = 未能度量(解析不到任何用例行) */ import { readFileSync, writeFileSync } from 'node:fs'; const [, , logPath, outPath] = process.argv; if (!logPath || !outPath) { console.error('用法:visual-manifest.mjs <输出 json>'); process.exit(2); } const text = readFileSync(logPath, 'utf8'); // Playwright 的行形如: ✘ 25 [visual-chromium-desktop] › visual-regression.spec.ts:76:9 › 分组 › 用例名 (3.1s) // ✓ 3 [visual-webkit-desktop] › … const RE = /^[✘✓─\s|]*[✓✘✖×]?\s*[✓✘]?\s*\d+\s+\[([a-z0-9-]+)\]\s+›\s+(\S+:\d+:\d+)\s+›\s+(.+?)\s*(?:\((?:[\d.]+)(?:ms|s)\))?\s*$/; const rows = []; for (const line of text.split('\n')) { const passed = line.includes('✓'); const failed = /[✘✖]|✗/.test(line) || line.includes('×'); if (!passed && !failed) continue; const m = line.match(RE); if (!m) continue; rows.push({ project: m[1], at: m[2], title: m[3].trim(), status: passed ? 'passed' : 'failed' }); } const uniq = new Map(); for (const r of rows) uniq.set(`${r.project}::${r.at}::${r.title}`, r); const manifest = [...uniq.values()].sort((a, b) => `${a.project}${a.at}`.localeCompare(`${b.project}${b.at}`)); const failed = manifest.filter((m) => m.status === 'failed').length; if (manifest.length === 0) { console.error(`未能度量:${logPath} 里解析不到任何用例行(不是"全部通过")`); process.exit(2); } writeFileSync(outPath, JSON.stringify({ source: logPath, total: manifest.length, failed, passed: manifest.length - failed, cases: manifest }, null, 2)); console.log(`逐名清单写出 ${outPath}:total=${manifest.length} passed=${manifest.length - failed} failed=${failed}`); process.exit(failed > 0 ? 1 : 0);