Files
image-workbench-desktop/tests/theme-static-snapshot-test.js
T
qiqi 69e98b9ecc
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m43s
fix(tokens): 暗色卡片内控件面改用 --iwb-field(D10)
暗色下节点卡里的下拉框/任务卡是 #1a1a1a / #151515,卡片却是 #3d3d3d
—— 差 33~39 级,看着是「在卡片上挖了几个洞」(用户验收反馈)。

根因:卡片内控件吃的是 --iwb-panel / --iwb-layer,这两个是贴着应用
底色派生的(panel #1c / layer #16),而节点卡是画布派生的 #3d,两者
不在同一亮度带。D6 把 layer 钉成「比 panel 暗」只对**面板里**成立。

- 新增 --iwb-field:卡片内控件面,跟 nodeBg 相对派生
  (亮 #ffffff / 暗 mix(nodeBg,#fff,6%) = #494949),差降到 12 级
- 8 处节点内控件切过去:task / gen 参数框 / 节点内 input / 输出组 /
  进度条 / 组合与批量来源行
- field 进 textSurfaces:控件变亮后正文重夹,暗色 text #d1→#e3、
  text2 #aa→#ba、textDim #8a→#98(实测 7.02 / 4.64 / 3.12)
- pluginInset 暗色 #101010→#191919(对 pluginCard 差 28 级 → 19 级)
- 修 .iwb-node .iwb-btn-primary 的文字色(白字压浅灰 1.2:1 → --iwb-bg 13.9:1)
- 亮色 12 张基线逐像素 0 diff,暗色 4 张已重建
2026-11-23 14:10:07 +08:00

112 lines
5.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.
// CSS 静态 token 必须等于 IWBThemeTokens.derive() 的输出(v3.26.44,M2/D2 守卫)
//
// 背景:style.css 的 :root / .theme-dark 是设计 token 的「静态快照」,用途是顶住 JS 执行前
// 那一帧。历史上这两段是**另一套独立设计**(暗色是中灰底 #757575 + 深字 #111),与 derive
// 的「近黑底 + 浅字」同名不同值 —— 结果切主题像换了个产品,而且首屏会闪一下再跳变。
//
// 这个测试把「快照 == 派生」钉成硬约束:改派生公式忘了同步 CSS,这里就红。
// 结构层(chrome/home/dock/edge/hr/plugin 别名)刻意用 color-mix 表达关系式,跳过。
const fs = require('fs')
const path = require('path')
const ROOT = path.resolve(__dirname, '..')
const css = fs.readFileSync(path.join(ROOT, 'style.css'), 'utf8')
// 载入 theme-tokens(它是 UMD,挂到传入的对象上)
const sandbox = {}
new Function('window', fs.readFileSync(path.join(ROOT, 'src/theme-tokens.js'), 'utf8'))(sandbox)
const T = sandbox.IWBThemeTokens
assert(T && typeof T.derive === 'function', 'theme-tokens 未正确导出 derive')
function assert (cond, msg) {
if (!cond) { console.error('✗ ' + msg); process.exit(1) }
}
// 取出某个选择器块里的 `--var: value;` 声明
function block (selector) {
const i = css.indexOf(selector + ' {')
assert(i >= 0, '找不到选择器块: ' + selector)
const start = css.indexOf('{', i)
let depth = 0
for (let j = start; j < css.length; j++) {
if (css[j] === '{') depth++
else if (css[j] === '}') {
depth--
if (depth === 0) return css.slice(start + 1, j)
}
}
assert(false, '选择器块未闭合: ' + selector)
}
function decls (body) {
const out = {}
// 去掉注释,避免注释里的示例声明被当成真声明
const clean = body.replace(/\/\*[\s\S]*?\*\//g, '')
const re = /--(?:iwb-)?([a-z0-9-]+)\s*:\s*([^;]+);/g
let m
while ((m = re.exec(clean))) out[m[1]] = m[2].trim()
return out
}
const rootVars = decls(block(':root'))
const darkVars = decls(block('.theme-dark'))
// token 键(derive 返回的驼峰)→ CSS 变量名(kebab)
const KEYS = [
'bg', 'canvasBg', 'panel', 'layer', 'layer2', 'nodeBg', 'field', 'border', 'border2',
'text', 'text2', 'textDim', 'canvasText', 'nodeText', 'nodeText2', 'tint',
'surface', 'hover', 'menuBg', 'brand', 'brandText', 'gridDot',
'pluginSurface', 'pluginCard', 'pluginInset', 'pluginHover',
'err', 'ok', 'warn', 'onBrand',
]
// derive 键 → CSS 变量名(去掉 --iwb- 前缀)。canvasText 的变量名是特例(--iwb-color-text-on-canvas)
const VAR_ALIAS = { canvasText: 'color-text-on-canvas', menuBg: 'bg-menu' }
const varOf = (k) => VAR_ALIAS[k] || k.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase())
let checked = 0
const bad = []
function checkOne (mode, dark, vars, table) {
const tokens = T.derive(T.DEFAULTS, dark)
for (const k of KEYS) {
const name = '--iwb-' + varOf(k)
const want = tokens[k]
const got = vars[varOf(k)]
if (!want) { bad.push(`${mode}: derive 未返回 ${k}`); continue }
if (!got) { bad.push(`${mode}: CSS 缺少 ${name}(期望 ${want})`); continue }
// 只比对字面量色值;color-mix/var() 表达式归结构层,跳过
if (!/^#[0-9a-f]{6}$/i.test(got)) continue
checked++
if (got.toLowerCase() !== String(want).toLowerCase()) {
bad.push(`${mode}: ${name} = ${got},derive 给 ${want}`)
}
}
}
checkOne(':root', false, rootVars)
checkOne('.theme-dark', true, darkVars)
// 结构层:CSS 用 color-mix 表达,必须与 derive 用同一组系数
assert(/^color-mix\(in srgb, var\(--iwb-bg\) 60%, var\(--iwb-canvas-bg\) 40%\)$/.test(rootVars.chrome || ''),
'chrome 公式应为 bg 60% + canvas 40%(derive 用 mix(bg, canvas, 0.4))')
assert(/^color-mix\(in srgb, var\(--iwb-chrome\) 55%, var\(--iwb-canvas-bg\) 45%\)$/.test(rootVars['dock-bg'] || ''),
'dockBg 公式应为 chrome 55% + canvas 45%(derive 用 mix(chrome, canvas, 0.45);曾有三套写法)')
assert(/^color-mix\(in srgb, var\(--iwb-panel\) 55%, var\(--iwb-dock-bg\)\)$/.test(rootVars['dock-head-bg'] || ''),
'dockHeadBg 公式应为 panel 55% + dockBg 45%')
// 暗色块不应再重复定义结构层(否则又会分叉出第二套公式)
for (const k of ['chrome', 'home-bg', 'dock-bg', 'dock-head-bg']) {
assert(!darkVars[k], `暗色块不应重新定义 --iwb-${k}(应让 :root 的 color-mix 在 html 上重新求值)`)
}
if (bad.length) {
console.error('✗ CSS 静态快照与 derive() 不一致:\n ' + bad.join('\n '))
console.error('\n修法:改 style.css 里对应的字面量,或(若刚改了派生公式)用')
console.error(' node -e "const s={};new Function(\'window\',require(\'fs\').readFileSync(\'src/theme-tokens.js\',\'utf8\'))(s);console.log(s.IWBThemeTokens.derive(s.IWBThemeTokens.DEFAULTS,true))"')
console.error('重新生成快照。')
process.exit(1)
}
console.log(`theme static snapshot checks passed(${checked} 个色值与 derive 一致,亮/暗各一套)`)