Files
image-workbench-desktop/tests/design-token-ratchet-test.js
T
qiqi cab473c3cd feat(tokens): M5 闸门上线 —— 新增裸值拦截 + 13 项棘轮 + 114 对对比度(3.26.50)
- scripts/token-lint.js:只查 diff 新增行(默认 HEAD,--base 查已提交批次,--file 整文件)
  · 禁裸 #hex/rgba()、禁裸 px(字号/圆角/内距/间距/外边距)、禁裸 z-index(插件不得高于 --iwb-z-anchor)
  · 四类白名单:内容色 /*iwb:content*/、var() 兜底、布局尺寸不查、临时例外 /*iwb:raw-until=X*/
  · JS 两套判据:CSS 文本按 px 单位判(不误伤 { gap: 20 } 布局参数)、React 对象按 camelCase 判
- tests/design-token-ratchet-test.js + fixtures/token-baseline.json:13 项计数只降不升;var() 目标必须存在;禁自我兜底
- tests/theme-contrast-test.js:亮/暗 × 文字 × 表面 114 对,阈值取 theme-tokens.js 的 RATIO(text 7 / text2 4.5 / dim 3 / canvas 6 / onBrand 4.5)
- 三条接进 verify:ci(lint:tokens + 两个测试进 test:unit),各做一次打红验证
- 顺手堵漏:首启动「选择资料目录」弹窗是全仓唯一完全未 token 化的界面(12.5px / #fff / #2563eb),已 token 化并给每个 var() 加字面兜底;遮罩 rgba 登记为例外待 RFC
- 存量计数零变化(字号 170 / 圆角 103 / 间距 415 / z-index 31)
2026-11-23 17:47:09 +08:00

96 lines
4.7 KiB
JavaScript
Raw 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'
// 设计 token 棘轮 + 完整性守卫(M5-T5.2 / T5.3)
//
// 为什么是「棘轮」而不是「全清零」:M4 结束时 style.css 里还有 170 处写死字号、415 处写死间距,
// 一次清零等于重写一遍样式表。棘轮把「数字不许涨」变成机械规则,迁移可以分版本做,
// 而**新代码**从今天起不可能再增加存量(涨一个就红)。
//
// 数字口径唯一来源是 scripts/token-audit.js --json —— 这里不重新实现一遍正则:
// 两套口径一旦漂移,就会变成「守卫绿、审计红」这种最难查的分歧。
//
// 三条断言:
// A. 每一项计数 ≤ tests/fixtures/token-baseline.json(只降不升)
// B. 引用了但从未定义的变量 = 0(`var(--iwb-radius-pill)` 这种拼错必须当场红,
// 而不是等到浏览器里那条声明静默失效)
// C. 没有 `var(--x, var(--x))` 自我兜底 —— 兜底值等于引用值,等于「失效时再失效一次」,
// 既掩盖问题又没有默认值可用
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const { execFileSync } = require('child_process')
const root = path.resolve(__dirname, '..')
const baseline = JSON.parse(fs.readFileSync(path.join(root, 'tests', 'fixtures', 'token-baseline.json'), 'utf8'))
const audit = JSON.parse(execFileSync(process.execPath, [path.join(root, 'scripts', 'token-audit.js'), '--json'], { cwd: root, encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 }))
const now = {
undefinedRefs: audit.undefinedRefs.length,
deadTokens: audit.deadTokens.length,
hex: audit.hard['十六进制色值'],
rgb: audit.hard['rgba()/rgb()'],
fontSize: audit.hard['font-size 写死'],
radius: audit.hard['border-radius 写死'],
spacing: audit.hard['padding/margin/gap 写死'],
zIndexDecl: audit.hard['z-index 声明'],
fontSizeDistinct: audit.fontSizes.distinct.length,
radiusDistinct: audit.radii.distinct.length,
spacingDistinct: audit.spacings.distinct.length,
zIndexDistinct: audit.zindex.distinct.length,
shadowDistinct: audit.shadows.distinct
}
// ── A. 只降不升 ──
// 基线里有的项必须都在(防手滑删项让某项「凭空消失」),当前也必须有同名项(防改名后静默跳过)
assert.deepStrictEqual(
Object.keys(now).sort(),
Object.keys(baseline.counts).sort(),
'棘轮口径与基线键不一致:审计脚本加了/改了指标却没更新基线(或反之)'
)
const LABEL = {
undefinedRefs: '引用未定义变量',
deadTokens: '定义了没用的变量',
hex: '十六进制色值',
rgb: 'rgba()/rgb()',
fontSize: 'font-size 写死',
radius: 'border-radius 写死',
spacing: 'padding/margin/gap 写死',
zIndexDecl: 'z-index 声明',
fontSizeDistinct: '字号取值种数',
radiusDistinct: '圆角取值种数',
spacingDistinct: '间距取值种数',
zIndexDistinct: 'z-index 取值种数',
shadowDistinct: '阴影写法种数'
}
const grew = []
for (const k of Object.keys(now)) {
if (now[k] > baseline.counts[k]) grew.push(LABEL[k] + ':' + baseline.counts[k] + ' → ' + now[k] + '(+' + (now[k] - baseline.counts[k]) + ')')
}
assert.deepStrictEqual(grew, [],
'以下计数涨了 —— 新增样式请用 token(--iwb-font-* / --iwb-radius-* / --iwb-space-* / --iwb-z-* / 语义色):\n ' + grew.join('\n ') +
'\n确属必要的例外:用行内 /*iwb:raw-until=<版本>*/(会到期)或 /*iwb:content*/(画布内容色),不要直接把基线数字调大。')
// ── B. 引用必须存在 ──
assert.deepStrictEqual(audit.undefinedRefs, [],
'有 var() 引用了从未定义的变量:' + audit.undefinedRefs.join(' ') + '(拼错的 token 名会让整条声明静默失效)')
// ── C. 自我兜底 ──
const cssFiles = ['style.css'].concat(
fs.readdirSync(path.join(root, 'src')).filter((f) => f.endsWith('.css') && f !== 'design-tokens.css').map((f) => 'src/' + f)
)
const selfFallback = []
for (const f of cssFiles) {
const src = fs.readFileSync(path.join(root, f), 'utf8')
src.split('\n').forEach((line, i) => {
const m = /var\(\s*(--[a-z0-9-]+)\s*,\s*var\(\s*(--[a-z0-9-]+)/i.exec(line)
if (m && m[1] === m[2]) selfFallback.push(f + ':' + (i + 1) + ' ' + m[1])
})
}
assert.deepStrictEqual(selfFallback, [], 'var() 用自己做兜底(等于没有兜底):' + selfFallback.join(' | '))
const drop = Object.keys(now).filter((k) => now[k] < baseline.counts[k])
console.log('token-ratchet: ' + Object.keys(now).length + ' 项计数均未超过基线'
+ (drop.length ? '(可下调基线:' + drop.map((k) => LABEL[k] + ' ' + baseline.counts[k] + '→' + now[k]).join(',') + ')' : ''))
console.log('token-ratchet: 引用未定义变量 0、自我兜底 0')