Files
image-workbench-desktop/tests/plugin-scale-contract-test.js
qiqi b99af53c49
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m7s
feat(tokens): UI 改动约束清单 + token-audit --plugins + 插件尺度棘轮
2026-11-23 19:59:31 +08:00

78 lines
3.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict'
// 插件尺度契约棘轮(2026-11-23)
// 目的:插件 CSS 的字号 / 圆角 / 间距散值「只降不升」。
// 规则(docs/standards/design-tokens.md):
// 字号 11/12/13/14/15/17/20/24;圆角 6/8/10/12/14/999;间距 0/2/4/6/8/10/12/16/20/24/32/40/48/64
// 存量不一次清零(会逼人加白名单),用基线棘轮:新增文件必须干净,存量文件计数不得增加。
// 迁移时刻意下调后基线不会自动降 —— 用 --update 重新生成(人工确认)。
const fs = require('fs')
const path = require('path')
const assert = require('assert')
const ROOT = path.join(__dirname, '..')
const PLUGINS = path.join(ROOT, 'plugins')
const BASELINE = path.join(__dirname, 'fixtures', 'plugin-scale-baseline.json')
const FONT = new Set([11, 12, 13, 14, 15, 17, 20, 24])
const RADIUS = new Set([6, 8, 10, 12, 14, 999])
const SPACE = new Set([0, 2, 4, 6, 8, 10, 12, 16, 20, 24, 32, 40, 48, 64])
function walk (dir, out) {
let ents = []
try { ents = fs.readdirSync(dir, { withFileTypes: true }) } catch (e) { return out }
for (const e of ents) {
const p = path.join(dir, e.name)
if (e.isDirectory()) walk(p, out)
else if (e.name.endsWith('.css')) out.push(p)
}
return out
}
function scan () {
const res = {}
for (const f of walk(PLUGINS, [])) {
const rel = path.relative(ROOT, f).split(path.sep).join('/')
const src = fs.readFileSync(f, 'utf8')
const r = { font: 0, radius: 0, space: 0 }
for (const m of src.matchAll(/([a-z-]+)\s*:\s*([^;{}]+)/g)) {
const prop = m[1].toLowerCase()
const val = m[2].trim()
if (val.includes('var(')) continue
if (prop === 'font-size') {
const px = /([0-9.]+)px/.exec(val)
if (px && !FONT.has(Number(px[1]))) r.font++
} else if (prop === 'border-radius') {
for (const px of val.matchAll(/([0-9.]+)px/g)) if (!RADIUS.has(Number(px[1]))) r.radius++
} else if (/^(gap|row-gap|column-gap)$/.test(prop) || /^(padding|margin)/.test(prop)) {
for (const px of val.matchAll(/([0-9.]+)px/g)) if (!SPACE.has(Number(px[1]))) r.space++
}
}
if (r.font || r.radius || r.space) res[rel] = r
}
return res
}
const current = scan()
if (process.argv.includes('--update')) {
fs.mkdirSync(path.dirname(BASELINE), { recursive: true })
fs.writeFileSync(BASELINE, JSON.stringify({ files: current }, null, 2) + '\n')
console.log('plugin scale baseline updated:' + Object.keys(current).length + ' 个文件')
process.exit(0)
}
const baseline = fs.existsSync(BASELINE) ? JSON.parse(fs.readFileSync(BASELINE, 'utf8')).files || {} : {}
const bad = []
for (const [file, cur] of Object.entries(current)) {
const base = baseline[file] || { font: 0, radius: 0, space: 0 }
for (const k of ['font', 'radius', 'space']) {
if (cur[k] > (base[k] || 0)) bad.push(file + ' → ' + k + ' ' + (base[k] || 0) + ' → ' + cur[k])
}
}
if (bad.length) {
assert.fail('插件尺度散值不得增加(字号/圆角/间距用 design-tokens 闭集):\n' + bad.slice(0, 20).join('\n') +
'\n迁移完成后可运行 node tests/plugin-scale-contract-test.js --update 更新基线。')
}
const total = Object.values(current).reduce((n, r) => n + r.font + r.radius + r.space, 0)
console.log('plugin scale contract checks passed(' + Object.keys(current).length + ' 个插件样式文件,存量散值 ' + total + ' 处,未增加)')