Files
image-workbench-desktop/tests/theme-contrast-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

74 lines
4.0 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'
// 对比度回归守卫(M5-T5.4)
//
// 为什么需要它:暗色主题的对比度不是「写死一个值」就完了 —— derive() 用 mix + clampAgainst
// 现算,任何一次调色(把 panel 提亮、把 layer 压深、把文字权重从 .30 改成 .34)都可能让
// 某对「文字 × 表面」掉到阈值以下,而**肉眼在编辑器里看不出来**(唯一能看出来的用户会截图说
// 「这个字看不清」,但我们拿不到他的主题参数)。
//
// 阈值取 theme-tokens.js 的 RATIO(单一源,禁止在这里再写 4.5 / 7 这种魔数):
// text 7.0(AAA 正文)/ text2 4.5(AA 正文)/ dim 3.0(AA 大字下限)/ canvas 6.0(节点标题,预留 opacity .8)/ onBrand 4.5
// 计划里写的「text ≥4.5、tertiary ≥3.0、on-brand ≥4.5」是**下限**,实现取更严的 7.0/3.0/4.5。
//
// 亮暗两套主题都跑;DEFAULTS 之外的配色(用户在设置里改过的)不在本守卫范围 ——
// 那条路由 clampText 在运行时兜底。
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const root = path.resolve(__dirname, '..')
const sandbox = {}
new Function('window', fs.readFileSync(path.join(root, 'src', 'theme-tokens.js'), 'utf8'))(sandbox)
const T = sandbox.IWBThemeTokens
assert.ok(T && typeof T.derive === 'function', 'theme-tokens.js 没暴露 IWBThemeTokens.derive')
// 承载文字的整套表面。canvas 单独处理:画布上只有节点标题,它用 canvasText(阈值 6,
// 且要预留 opacity .8 的余量)—— 界面文字本来就不该直接压在画布底上,混进通用集合会误报。
const SURFACES = ['bg', 'panel', 'layer', 'layer2', 'nodeBg', 'field', 'pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover', 'hover', 'dockBg', 'chrome', 'inset']
const rows = []
function check (theme, fgName, fg, surfaces, min, label) {
for (const s of surfaces) {
const bg = theme[s]
if (!bg || !/^#[0-9a-f]{6}$/i.test(bg)) continue
const r = T.contrast(fg, bg)
rows.push({ name: fgName + ' × ' + s, ratio: r, min, label, ok: r + 1e-9 >= min })
}
}
for (const dark of [false, true]) {
const t = T.derive(T.DEFAULTS, dark)
const label = dark ? '暗色' : '亮色'
const present = SURFACES.filter((s) => t[s])
check(t, 'text', t.text, present, T.RATIO.text, label)
check(t, 'text2', t.text2, present, T.RATIO.text2, label)
check(t, 'textDim', t.textDim, present, T.RATIO.dim, label)
check(t, 'brandText', t.brandText, present, T.RATIO.text2, label)
check(t, 'canvasText', t.canvasText, ['canvasBg'], T.RATIO.canvas, label)
check(t, 'nodeText2', t.nodeText2, ['nodeBg', 'layer', 'panel'], T.RATIO.text2, label)
// 品牌底上的字:onBrand 是按对比度自动选的黑/白,选错了就会「主按钮白字压浅蓝」
rows.push({
name: 'onBrand × brand',
ratio: T.contrast(t.onBrand, t.brand),
min: T.RATIO.onBrand,
label,
ok: T.contrast(t.onBrand, t.brand) + 1e-9 >= T.RATIO.onBrand
})
}
const bad = rows.filter((r) => !r.ok)
if (bad.length) {
console.log('对比度不达标(阈值来自 theme-tokens.js 的 RATIO):')
for (const r of bad) console.log(' ✗ ' + r.label + ' ' + r.name.padEnd(26) + r.ratio.toFixed(2) + ' < ' + r.min)
console.log('\n最低的三对(含达标的):')
for (const r of rows.slice().sort((a, b) => a.ratio - b.ratio).slice(0, 3)) {
console.log(' · ' + r.label + ' ' + r.name.padEnd(26) + r.ratio.toFixed(2) + '(下限 ' + r.min + ')')
}
}
assert.deepStrictEqual(bad.map((r) => r.label + ' ' + r.name + ' ' + r.ratio.toFixed(2) + '<' + r.min), [],
'文字与表面的对比度掉到阈值以下 —— 调色时改坏了,改回去或用 clampAgainst 重新夹紧')
const minRow = rows.slice().sort((a, b) => a.ratio - b.ratio)[0]
console.log('token-contrast: ' + rows.length + ' 对(亮/暗 × 文字 × 表面)全部达标;最紧的一对是 '
+ minRow.label + ' ' + minRow.name + ' = ' + minRow.ratio.toFixed(2) + '(下限 ' + minRow.min + ')')