// 基线变更的**区域约束核验** —— 替代"人肉读 98 张图"的机器判据。 // // 为什么要有它:本轮预期有 98 张基线随字标替换而变。逐张读图既慢又不可复核(98 次主观判断, // 没有一条是可重跑的断言)。真正要守的命题是:**变化只允许发生在页眉带内**(字标在那里, // 以及因它变窄而重排的导航项/CTA)。如果 diff 的最小包围盒越过了页眉底边,就说明改动漏到了正文, // 那才是需要停下来重划范围的信号。 // // 用法:node check-baseline-region.cjs (读 git HEAD 版与工作区版,逐张算 diff 包围盒) 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, '..', '..'); // deliverables/<本目录> → 上两级才是仓根(写错一层就会在 Novalon/ 下 git 失败) const SNAP = 'e2e/visual-snapshots'; const HEADER_BAND = 96; // px:页眉 h-10=40px 加安全余量;正文从 main 起,远在此之下 function changedFiles() { 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 diffBox(rel) { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'bd-')); try { const oldBuf = execFileSync('git', ['show', `HEAD:${rel}`], { cwd: REPO, maxBuffer: 1 << 28 }); fs.writeFileSync(path.join(tmp, 'old.png'), oldBuf); const newPath = path.join(REPO, rel); if (!fs.existsSync(newPath)) return { skip: '新文件(无 HEAD 版)' }; const dims = await sharp(path.join(tmp, 'old.png')).metadata(); const a = await sharp(path.join(tmp, 'old.png')).resize(dims.width, dims.height, { fit: 'inside' }) .raw().toBuffer({ resolveWithObject: true }); const b = await sharp(newPath).resize(dims.width, dims.height, { fit: 'inside' }) .raw().toBuffer({ resolveWithObject: true }); const { width, height } = a.info; if (b.info.width !== width || b.info.height !== height) { return { sizeChanged: true, from: `${dims.width}×${dims.height}`, to: `${b.info.width}×${b.info.height}` }; } let minX = width, minY = height, maxX = -1, maxY = -1, n = 0; const ch = a.info.channels; for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { const i = (y * width + x) * ch; let d = 0; for (let c = 0; c < Math.min(ch, 3); c++) d = Math.max(d, Math.abs(a.data[i + c] - b.data[i + c])); if (d > 8) { n++; if (x < minX) minX = x; if (x > maxX) maxX = x; if (y < minY) minY = y; if (y > maxY) maxY = y; } } } return { w: width, h: height, diffPx: n, box: maxX < 0 ? null : [minX, minY, maxX, maxY] }; } finally { fs.rmSync(tmp, { recursive: true, force: true }); } } if (process.argv[2] === '--selftest') { // 自测:合成"只在页眉带内变"与"在正文区变"两张图,判据必须一绿一红。 // 没有这一条,phase 1 的"通过"可能只是判据根本没在测东西。 (async () => { const dir = '/tmp/rgnt'; const cases = [['header-only.png', true], ['body-changed.png', false]]; let ok = true; for (const [name, shouldPass] of cases) { const r = await diffBoxFile(path.join(dir, 'orig.png'), path.join(dir, name)); const within = r.box && r.box[3] <= HEADER_BAND; const good = within === shouldPass; ok = ok && good; console.log(` ${good ? '✓' : '✗'} ${name}:包围盒 y=${r.box ? r.box[1] + '..' + r.box[3] : 'null'},` + `判为${within ? '页眉带内' : '越界'}(期望${shouldPass ? '页眉带内' : '越界'})`); } console.log(ok ? '自测通过:判据对"变在哪一带"敏感' : '自测失败:判据不可信,别用它下结论'); process.exit(ok ? 0 : 1); })(); } async function diffBoxFile(oldPath, newPath) { const o = await sharp(oldPath).raw().toBuffer({ resolveWithObject: true }); const n = await sharp(newPath).raw().toBuffer({ resolveWithObject: true }); const { width, height } = o.info, ch = o.info.channels; let minX = width, minY = height, maxX = -1, maxY = -1; for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) { const i = (y * width + x) * ch; let d = 0; for (let c = 0; c < 3; c++) d = Math.max(d, Math.abs(o.data[i + c] - n.data[i + c])); if (d > 8) { if (x < minX) minX = x; if (x > maxX) maxX = x; if (y < minY) minY = y; if (y > maxY) maxY = y; } } return { box: maxX < 0 ? null : [minX, minY, maxX, maxY] }; } (async () => { if (process.argv[2] === '--selftest') return; const files = changedFiles(); if (!files.length) { console.log('没有已修改的基线可比对 ⇒ 本判据 0 目标,不算通过'); process.exit(1); } const bad = []; let sizeChanged = 0, noDiff = 0; for (const rel of files) { const r = await diffBox(rel); const short = rel.split('/').slice(-2).join('/'); if (r.skip) { console.log(` ○ ${short} —— ${r.skip}`); continue; } if (r.sizeChanged) { sizeChanged++; console.log(` ↻ ${short} 尺寸 ${r.from} → ${r.to}`); continue; } if (!r.box) { noDiff++; console.log(` = ${short} 无像素差(被重写但内容相同)`); continue; } const [, minY, , maxY] = r.box; const within = maxY <= HEADER_BAND; console.log(` ${within ? '✓' : '✗'} ${short} 差 ${r.diffPx}px 包围盒 y=${minY}..${maxY}(页眉带 ≤${HEADER_BAND})`); if (!within) bad.push(`${short}:diff 最低到 y=${maxY},越过页眉带 ⇒ 改动漏进正文`); } console.log(`\n合计 ${files.length} 张已改 | 越界 ${bad.length} | 尺寸变化 ${sizeChanged} | 无差 ${noDiff}`); for (const b of bad) console.log(' ✗ ' + b); process.exit(bad.length ? 1 : 0); })();