Files
qiqi 8cbe1d5923
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m17s
feat(tokens): M3 单一源 + 生成产物(design-tokens.css / tokens.json)
D1「同一批 token 三个写入者」收尾:颜色快照从**手写**变成**产物**。

- 新增 scripts/gen-tokens.js:theme-tokens.js 的 derive() 是唯一源,
  --write 生成 src/design-tokens.css(158 行::root 77 / .theme-dark 39 / 亮色动效 6)
  与 dist/tokens.json(DTCG,暗色值在 $extensions.com.image-workbench.dark)
- 尺度层(间距/圆角/字号/阴影/过渡)也搬进生成器的 SCALE 表,同样只写一次
- style.css 2407 行只剩组件规则;build 时把 design-tokens.css 拼接在最前
  (不是 @import:Tauri 不靠它兜底),dist 里仍只有一个 style.css
- 三道闸门:tokens:check(进 verify:ci,报第一处差异行)、
  build 漂移即失败、theme-static-snapshot-test 逐 token 报错
- 12 张视觉基线 0 diff(换文件不动值;77/39/6 条声明逐条比对过)

偏差:tokens.json 不单独写 scripts/tokens-export.js —— 与 CSS 同进程产出,
避免「导出的 JSON」和「生效的 CSS」两次解析口径。
2026-11-23 14:39:39 +08:00

144 lines
8.7 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'
// 外观 v2 管线(v3.26.42):种子 → 派生 → 覆盖,一次写完;旧配置可迁移;插件变量名不变。
//
// 用户报障(2026-11-23):「用了自动配色后再点下面的选项就会破坏自动颜色」「字体颜色不能调」。
// 根因是两套系统(手写 14 项 / 派生 26 项)同一批变量、后写覆盖先写:
// · 自动配色开着时画布/连线/节点三组被整体隐藏,没有微调入口;
// · 「主题色」在自动配色下被 seedAccent 覆盖,等于无效;
// · 关掉自动配色后 19 个变量停在派生值 ⇒ 半新半旧、冷启动与热切换不一致。
// 现在改成「每项默认跟随派生,改哪项覆盖哪项」,本测试把这几条钉死。
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const ROOT = path.join(__dirname, '..')
function loadSandbox () {
const g = { React: {} }
g.window = g
g.globalThis = g
new Function('window', fs.readFileSync(path.join(ROOT, 'src', 'theme-tokens.js'), 'utf8'))(g)
new Function('window', fs.readFileSync(path.join(ROOT, 'src', 'ui-appearance.js'), 'utf8'))(g)
return g
}
function fakeRoot () {
const props = {}
return {
props,
style: { setProperty: (k, v) => { props[k] = v } },
setAttribute: function () {},
classList: { contains: () => false },
}
}
const wrap = (appearance) => ({ canvas: { appearance } })
const g = loadSandbox()
const A = g.IWBUIApearance
const T = g.IWBThemeTokens
assert.ok(A && typeof A.apply === 'function', 'ui-appearance 应导出 apply')
assert.ok(T && typeof T.derive === 'function', 'theme-tokens 应导出 derive')
// ① v1 默认(自动配色开着、没动过颜色)→ 迁移成「全部跟随派生」,不得凭空生成覆盖
const migrated = A.normalize(wrap({ auto: true, seedNeutral: '#808080', seedAccent: '#6b7280', depth: 22 }))
assert.strictEqual(migrated.v, 2)
assert.deepStrictEqual(migrated.overrides, {}, 'v1 自动配色下的手写字段不得变成覆盖')
assert.deepStrictEqual(migrated.overridesDark, {}, 'v1 自动配色下的手写字段不得变成覆盖(暗)')
assert.deepStrictEqual(migrated.seeds, { neutral: '#808080', accent: '#6b7280', depth: 22 })
// ② v1 明确关掉自动配色 → 那些手写颜色才是用户真实在用的,必须保留成覆盖
const legacyOff = A.normalize(wrap({
auto: false, canvasBgLight: '#eeeeee', canvasBgDark: '#222222',
nodeBgLight: '#ffffff', nodeBorderLight: '#cccccc', accent: '#3366ff',
}))
assert.strictEqual(legacyOff.overrides.canvasBg, '#eeeeee', 'v1 手写画布底色应迁成亮色覆盖')
assert.strictEqual(legacyOff.overridesDark.canvasBg, '#222222', 'v1 手写画布底色应迁成暗色覆盖')
assert.strictEqual(legacyOff.overrides.nodeBg, '#ffffff')
assert.strictEqual(legacyOff.overrides.border, '#cccccc')
assert.strictEqual(legacyOff.seeds.accent, '#3366ff', 'v1 主题色应迁到 seeds.accent(这正是从前在自动配色下失效的那项)')
// ③ v2 只收已知覆盖键(防止把脏字段写进配置)
const cleaned = A.normalize(wrap({ v: 2, seeds: { neutral: '#777777' }, overrides: { text: '#ff0000', evil: '#00ff00' } }))
assert.deepStrictEqual(cleaned.overrides, { text: '#ff0000' }, '未知覆盖键应被丢弃')
assert.strictEqual(cleaned.seeds.accent, A.DEFAULTS.seeds.accent, '缺省种子应回填默认')
// ④ 覆盖生效,且 tint 不跟着文字走(否则调一个字等于重排整套灰阶)
const base = T.derive(T.DEFAULTS, false)
const root = fakeRoot()
g.document = { documentElement: root }
A.apply(wrap({ v: 2, overrides: { text: '#ff0000' } }), false)
assert.strictEqual(root.props['--iwb-text'], '#ff0000', '文字覆盖应生效(字体颜色可调)')
assert.strictEqual(root.props['--iwb-tint'], base.tint, 'tint 必须保持派生文字色')
assert.notStrictEqual(root.props['--iwb-tint'], '#ff0000', 'tint 不得跟随文字覆盖')
// ⑤ 节点文字与界面文字互不影响
A.apply(wrap({ v: 2, overrides: { nodeText: '#00ff00' } }), false)
assert.strictEqual(root.props['--iwb-node-text'], '#00ff00', '节点主文字覆盖应生效')
assert.strictEqual(root.props['--iwb-text'], base.text, '改节点文字不得动界面文字')
A.apply(wrap({ v: 2, overrides: { text2: '#123456' } }), false)
assert.strictEqual(root.props['--iwb-node-text2'], base.nodeText2, '改界面次文字不得动节点次文字')
// ⑥ 亮/暗覆盖互不串味
A.apply(wrap({ v: 2, overrides: { text: '#ff0000' }, overridesDark: { text: '#00ff00' } }), true)
assert.strictEqual(root.props['--iwb-text'], '#00ff00', '暗色应使用 overridesDark')
const darkBase = T.derive(T.DEFAULTS, true)
A.apply(wrap({ v: 2, overrides: { text: '#ff0000' } }), true)
assert.strictEqual(root.props['--iwb-text'], darkBase.text, '只有亮色覆盖时,暗色应回到派生值(不得复用亮色值导致不可读)')
// ⑦ 一次写完:关键变量一个都不能停在「上一次的值」(旧实现关掉自动配色后 19 个变量没重写)
A.apply(wrap({ v: 2, seeds: { neutral: '#8b8578', accent: '#7a6a52', depth: 30 } }), true)
const required = [
'--iwb-bg', '--iwb-canvas-bg', '--iwb-chrome', '--iwb-home-bg', '--iwb-dock-bg', '--iwb-dock-head-bg',
'--iwb-panel', '--iwb-layer', '--iwb-layer2', '--iwb-node-bg', '--iwb-border', '--iwb-border2',
'--iwb-text', '--iwb-text2', '--iwb-text-dim', '--iwb-node-text', '--iwb-node-text2', '--iwb-tint',
'--iwb-surface', '--iwb-hover', '--iwb-brand', '--iwb-on-brand', '--iwb-edge', '--iwb-edge-hot',
'--iwb-grid-dot', '--iwb-err', '--iwb-ok', '--iwb-warn',
'--iwb-plugin-surface', '--iwb-plugin-card', '--iwb-plugin-inset', '--iwb-plugin-hover',
'--iwb-node-radius', '--iwb-node-sel-w', '--iwb-edge-w', '--iwb-edge-dash', '--iwb-grid-size', '--iwb-grid-gap', '--iwb-grid-on',
]
for (const k of required) assert.ok(Object.prototype.hasOwnProperty.call(root.props, k), '一次写完应包含 ' + k)
// ⑧ 内置「休息」预设:亮/暗两套都要落在 4.5:1 以上(低刺激但不能读不清)
const rest = A.PRESETS.filter((p) => p.name === '休息')[0]
assert.ok(rest, '应有「休息」预设')
for (const dark of [false, true]) {
const t = T.derive(rest.seeds, dark)
const ov = dark ? rest.overridesDark : rest.overrides
assert.ok(Object.keys(ov).length > 0, '休息预设应给亮/暗都写覆盖')
const fg = ov.text || t.text
const bg = ov.bg || t.bg
const r = T.contrast(fg, bg)
assert.ok(r >= 4.5, '休息预设' + (dark ? '暗色' : '亮色') + '正文对比度应 >= 4.5,实际 ' + r.toFixed(2) + ':1')
const nfg = ov.nodeText || t.nodeText
const nbg = ov.nodeBg || t.nodeBg
const nr = T.contrast(nfg, nbg)
assert.ok(nr >= 4.5, '休息预设' + (dark ? '暗色' : '亮色') + '节点文字对比度应 >= 4.5,实际 ' + nr.toFixed(2) + ':1')
}
// ⑨ 插件兼容:变量名只增不改(改名 = 插件落到硬编码兜底,暗色下会白底白字)
// M3 起 token 定义搬进生成的 src/design-tokens.css(build 时内联在 style.css 之前):
// 契约检查要连起来看,否则「插件能引用的变量」会误判为缺失
const css = fs.readFileSync(path.join(ROOT, 'style.css'), 'utf8') + '\n' +
fs.readFileSync(path.join(ROOT, 'src', 'design-tokens.css'), 'utf8')
for (const v of [
'--iwb-bg', '--iwb-panel', '--iwb-layer', '--iwb-layer2', '--iwb-border', '--iwb-border2',
'--iwb-text', '--iwb-text2', '--iwb-text-dim', '--iwb-brand', '--iwb-on-brand', '--iwb-warn',
'--iwb-node-bg', '--iwb-node-radius', '--iwb-canvas-bg', '--iwb-grid-dot',
'--iwb-node-text', '--iwb-node-text2', '--iwb-tint', '--iwb-surface', '--iwb-hover',
]) {
assert.ok(css.indexOf(v + ':') >= 0, 'style.css 必须定义 ' + v + '(插件可能引用)')
}
assert.ok(/--iwb-plugin-text:\s*var\(--iwb-text\)/.test(css), '插件文字别名仍应指向 --iwb-text')
assert.ok(/--iwb-plugin-bg:\s*var\(--iwb-bg\)/.test(css), '插件底色别名仍应指向 --iwb-bg')
assert.ok(/--iwb-plugin-surface:/.test(css), '插件面板四层变量应保留')
// 节点作用域重映射:插件节点零改动跟随节点文字色
assert.ok(/\.iwb-node\s*\{[^}]*--iwb-text:\s*var\(--iwb-node-text\)/.test(css), '.iwb-node 应把 --iwb-text 重映射到 --iwb-node-text')
assert.ok(/\.iwb-node\s*\{[^}]*--iwb-text2:\s*var\(--iwb-node-text2\)/.test(css), '.iwb-node 应把 --iwb-text2 重映射到 --iwb-node-text2')
// ⑩ 自引用声明是无效声明(此前 --iwb-canvas-bg/--iwb-border/--iwb-warn 都是 var(自己))
for (const v of ['--iwb-canvas-bg', '--iwb-border', '--iwb-warn']) {
const bad = new RegExp(v.replace(/--/, '--') + ':\\s*var\\(' + v + '\\)')
assert.ok(!bad.test(css), v + ' 不得自引用')
}
console.log('appearance v2 checks passed(迁移 / 覆盖 / 一次写完 / 休息预设 4.5:1 / 插件变量名不变)')