// 第二批(全站色板+正文字族)基线重写的核验判据。 // // 为什么不能沿用第一批的区域包围盒判据:那一批只有字标变,"变化必须落在页眉带内"是可失败的命题; // 这一批整站中性色与衬线字族都变了,正文每个字都换色换字形,"越界"不再是异常而是常态。 // 于是命题换成两条仍然**能失败**的: // ① 覆盖率——每张基线都必须真的变了(diff 比例 ≥ 0.05%)。哪张几乎没变,说明色板/字族没作用到那条路径, // 那才是需要停下来查的信号(例如某页硬编码了旧色、或字体没被 @font-face 命中)。 // ② 尺寸——列出高度变化的基线并给出变化幅度;数量必须等于登记值 SIZE_CHANGES, // 多一个少一个都报红(字族换行宽会变,行数变化会推动页高)。 // 另附抽样读图清单(人眼确认字形与色温),但机器判据在前,人读在后。 const { execFileSync } = require('child_process'); const path = require('path'); const fs = require('fs'); const os = require('os'); const sharp = require('sharp'); const REPO = path.resolve(__dirname, '..', '..'); const SNAP = 'e2e/visual-snapshots'; const MIN_DIFF_RATIO = 0.0005; // 0.05%:低于此判为"几乎没变" const SIZE_CHANGES = Number(process.env.SIZE_CHANGES || 0); function changed() { const out = execFileSync('git', ['status', '--porcelain', '--', SNAP], { cwd: REPO, encoding: 'utf8' }); return out.split('\n').filter(l => l.trim() && l.endsWith('.png')).map(l => l.slice(3).trim()); } async function compare(rel) { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'bg-')); try { fs.writeFileSync(path.join(tmp, 'old.png'), execFileSync('git', ['show', `HEAD:${rel}`], { cwd: REPO, maxBuffer: 1 << 28 })); const o = await sharp(path.join(tmp, 'old.png')).metadata(); const n = await sharp(path.join(REPO, rel)).metadata(); if (o.width !== n.width || o.height !== n.height) { return { size: `${o.width}×${o.height} → ${n.width}×${n.height}`, dH: n.height - o.height, ratio: 1 }; } const a = await sharp(path.join(tmp, 'old.png')).raw().toBuffer({ resolveWithObject: true }); const b = await sharp(path.join(REPO, rel)).raw().toBuffer({ resolveWithObject: true }); const ch = a.info.channels; let diff = 0; const total = a.info.width * a.info.height; for (let i = 0; i < total; i++) { const k = i * ch; if (Math.abs(a.data[k] - b.data[k]) > 6 || Math.abs(a.data[k + 1] - b.data[k + 1]) > 6 || Math.abs(a.data[k + 2] - b.data[k + 2]) > 6) diff++; } return { ratio: diff / total, px: diff, w: a.info.width, h: a.info.height }; } finally { fs.rmSync(tmp, { recursive: true, force: true }); } } (async () => { const files = changed(); if (!files.length) { console.log('没有已改基线 ⇒ 0 目标,本判据不算通过'); process.exit(1); } const cold = [], sizes = []; for (const rel of files) { const r = await compare(rel); const s = rel.split('/').slice(-1)[0].replace('-visual-', '[v]'); if (r.size) { sizes.push([s, r.size, r.dH]); continue; } if (r.ratio < MIN_DIFF_RATIO) cold.push(`${s}:仅 ${(r.ratio * 100).toFixed(3)}% 像素变(阈值 ${MIN_DIFF_RATIO * 100}%)`); } console.log(`合计已改 ${files.length} 张 | 尺寸变化 ${sizes.length} 张(登记值 ${SIZE_CHANGES})| 几乎没变 ${cold.length} 张`); for (const [s, dim, dH] of sizes.slice(0, 12)) console.log(` ↻ ${s} ${dim}(ΔH=${dH})`); if (sizes.length > 12) console.log(` …另 ${sizes.length - 12} 张`); for (const c of cold) console.log(' ✗ ' + c); const bad = [...cold]; if (sizes.length !== SIZE_CHANGES) bad.push(`尺寸变化 ${sizes.length} 张 ≠ 登记 ${SIZE_CHANGES} 张`); console.log(bad.length ? `\n判据不成立:${bad.length} 项` : '\n判据成立:每张都真的变了,尺寸变化数与登记一致'); process.exit(bad.length ? 1 : 0); })();