Files
image-workbench-desktop/tests/_visual-tokens.spec.js
T
qiqi 4ae0b4b156 feat(tokens): 设计 token 规范定稿 + 12 张视觉基线 + M1 清账
规范与计划
- docs/standards/design-tokens.md:UI 设计语言唯一数值来源(L1 值层 / L2 语义层 / L3 组件白名单),
  含实测现状(2505 行 CSS、83 变量、19 档字号 / 18 档圆角 / 30 档间距 / 42 档 z-index)、
  9 条色彩模型缺陷(D1–D9)与迁移映射表
- docs/plans/design-tokens-rollout.md:M0–M6 落地计划(约 8.5 人日、分 6 版交付、每批三条硬验收)
- §9 四个决定按建议项裁定:面板圆角 14 / 半像素字号并入整数 / 间距先等值替换 / 色彩模型全修但分两版

M0 视觉基线(tests/_visual-tokens.spec.js,带 iwb-dev-probe 标记,不进门禁)
- 6 场景 × 亮/暗 = 12 张:首页 / 画布 / 设置 / 图库抽屉 / 提示词库面板 / 右键菜单
- 固定 1280×800 + DPR1 + animations disabled + 等字体与三帧;连续两次跑 0 diff
- 不入 CI 的理由写在文件头:像素受字体/DPR 影响,跨机器必然漂移

M1 清账(视觉 0 diff,已用基线验证)
- 删两条引用 --iwb-accent 的死规则:.iwb-btn-active / .iwb-layout-lock-state 从 fork 起无 JS 生产者,
  原「按钮高亮失效」判断作废,改为「永不生效的规则」
- 修 6 个虚构 token(--sp-panel/line/text、--rd-12、--fs-12/13)→ 等值换成 --iwb-panel/border/text、--rd-lg、--fs-md/lg
- 修自引用 fallback var(--iwb-err, var(--iwb-err))
- 15 个死尺度 token 标 @deprecated(带替代名,保留到 M4 结束);--iwb-surface 单独标注「derive 不写它」
- 审计:引用未定义 7 → 0;定义未引用 28 → 25

工具
- scripts/token-audit.js:只读审计(表格 / --contrast 对比度矩阵 / --json)
- npm run audit:tokens / audit:tokens:contrast / test:visual / test:visual:update
2026-11-23 13:25:35 +08:00

152 lines
7.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.
// iwb-dev-probe: 设计 token 视觉基线(M0/T0.2),不入常规门禁(下划线前缀 + 此标记 = 被 scripts/list-e2e-specs.js 排除)
//
// 为什么不做成门禁 spec:
// ① 截图像素受**字体渲染**(系统是否装同版本中文字体)、**DPR**、**GPU 合成**影响,
// 跨机器必然漂移 → 挂门禁就是把机器差异变成 CI 红灯;
// ② 它的用途是「我改完这一批,和上一批比 0 diff」,属于本地验收工具,不是代码正确性断言。
//
// 用法:
// npm run build && node scripts/serve-test.js & # 端口 8787
// npx playwright test _visual-tokens --update-snapshots # 首次生成/确认变更
// npx playwright test _visual-tokens # 比对(0 diff 才算过)
//
// 6 个场景 × 亮/暗 = 12 张,覆盖「壳层 / 画布 / 浮层 / 抽屉 / 插件面板 / 右键菜单」六类界面。
// 稳定化:固定 1280×800 + deviceScaleFactor 1、animations disabled、caret hide、等字体与两帧。
const { test, expect } = require('./_pw-coverage')
const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787'
test.use({ baseURL, viewport: { width: 1280, height: 800 }, deviceScaleFactor: 1 })
// 预置一张「有东西可看」且**在视口内**的画布:提示词 → 文本 / 组合,组合 → 配置。
// 不放 out 节点:它的节点体是一张大卡片,会把画布基线变成「一张图 vs 一堆节点」的对比。
const WS = {
workspaces: [{
id: 'ws_visual', name: '视觉基线', groups: [],
nodes: [
{ id: 'p1', type: 'prompt', x: 240, y: 180, data: { text: '一只橘猫坐在窗台上,午后阳光,胶片质感' } },
{ id: 't1', type: 'textout', x: 600, y: 180, data: {} },
{ id: 'c1', type: 'combine', x: 240, y: 400, data: {} },
{ id: 'g1', type: 'gen', x: 600, y: 400, data: { size: '1024x1024', n: 1 } },
],
edges: [
{ id: 'e1', from: 'p1', to: 't1' },
{ id: 'e2', from: 'p1', to: 'c1' },
{ id: 'e3', from: 'c1', to: 'g1' },
],
}],
activeId: 'ws_visual', rev: 1, schemaVersion: 1,
}
const CFG = { providers: [], canvas: {} }
// 停靠栏状态:左栏停在「节点组」(默认「浏览器」面板在网页环境下只有一句客户端提示,
// 「风格库」首帧是异步加载态,两者都不适合做像素基线);右栏默认关,④⑤ 用例自己点开。
const DOCK = {
left: { open: true, w: 318, list: ['groups', 'browser', 'styles'], removed: [], active: 'groups' },
right: { open: false, w: 330, list: ['prompts', 'gallery'], removed: [], active: 'gallery' },
}
// 主题走 iwb_theme(app.js initTheme 的读取键);清掉外观覆盖,保证基线是「出厂派生色」
async function seed(page, theme) {
await page.addInitScript(({ cfg, ws, dock, theme }) => {
localStorage.clear()
localStorage.setItem('iwb_web_cfg', JSON.stringify(cfg))
localStorage.setItem('iwb_web_workspaces', JSON.stringify(ws))
localStorage.setItem('iwb_ws_active', ws.activeId)
localStorage.setItem('iwb_theme', theme)
localStorage.setItem('iwb.dock.v1', JSON.stringify(dock))
}, { cfg: CFG, ws: WS, dock: DOCK, theme })
}
// 等「真的画完了」:字体就绪 + 连续两帧 + 没有正在跑的动画帧(rAF 计数稳定)
async function settle(page) {
await page.evaluate(() => document.fonts && document.fonts.ready)
await page.evaluate(() => new Promise((resolve) => {
let left = 3
const step = () => { left -= 1; if (left <= 0) resolve(); else requestAnimationFrame(step) }
requestAnimationFrame(step)
}))
await page.waitForTimeout(250) // 少量固定等待:等 CSS 过渡(120ms)收尾;本文件不进门禁,不影响固定等待棘轮
}
async function shot(page, name) {
await settle(page)
await expect(page).toHaveScreenshot(name + '.png', {
animations: 'disabled',
caret: 'hide',
maxDiffPixels: 0,
})
}
async function bootHome(page, theme) {
await seed(page, theme)
await page.goto('/', { waitUntil: 'domcontentloaded' })
await page.waitForSelector('.iwb-home-card:not(.iwb-home-new)', { timeout: 20000 })
await settle(page)
}
async function bootCanvas(page, theme) {
await bootHome(page, theme)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await page.waitForSelector('.iwb-canvas:not(.iwb-canvas-hidden)', { timeout: 20000 })
await expect(page.locator('.iwb-node')).toHaveCount(4, { timeout: 15000 })
// 画布基线关掉左栏:打开时它浮在画布之上 318px,而首次进入的自动取景只按窗口宽度居中
// (不避让停靠栏),节点必然被压一半。停靠栏本身由 ④⑤ 两张右栏截图覆盖。
const toggle = page.locator('.iwb-top-nav-icon[title="显示 / 隐藏左栏"]').first()
await toggle.click()
await expect(page.locator('.iwb-dock[data-side="left"]')).toHaveCount(0)
await page.waitForTimeout(300)
}
// 右栏默认是关的(dock 状态 right.open=false),必须点顶栏开关;开完等面板挂载
async function openRightDock(page) {
await page.locator('.iwb-top-nav-icon[title="显示 / 隐藏右栏"]').first().click()
await expect(page.locator('.iwb-dock[data-side="right"]')).toBeVisible({ timeout: 15000 })
await page.waitForTimeout(400) // 插件面板挂载是异步的(pluginMount effect)
}
for (const theme of ['light', 'dark']) {
test.describe('视觉基线 · ' + theme, () => {
test('① 首页', async ({ page }) => {
await bootHome(page, theme)
await shot(page, '01-home-' + theme)
})
test('② 画布(节点 + 连线)', async ({ page }) => {
await bootCanvas(page, theme)
await shot(page, '02-canvas-' + theme)
})
test('③ 设置弹窗', async ({ page }) => {
await bootCanvas(page, theme)
await page.locator('button[title="设置"]').first().click()
await expect(page.locator('.iwb-settings-modal')).toBeVisible()
await shot(page, '03-settings-' + theme)
})
test('④ 库面板(右抽屉 · 图库)', async ({ page }) => {
await bootCanvas(page, theme)
await openRightDock(page)
await shot(page, '04-drawer-gallery-' + theme)
})
test('⑤ 插件面板(右抽屉 · 提示词库)', async ({ page }) => {
await bootCanvas(page, theme)
await openRightDock(page)
await page.locator('.iwb-dock-btn[data-panel="prompts"]').first().click()
await expect(page.locator('.iwb-dock[data-side="right"] .iwb-dock-body')).toBeVisible()
await shot(page, '05-panel-promptlib-' + theme)
})
test('⑥ 右键菜单', async ({ page }) => {
await bootCanvas(page, theme)
// 用合成 contextmenu 而不是 mouse.click(button:'right'):后者在 Chromium/macOS 上
// 会被节点/顶栏的命中判定吃掉,坐标依赖视口高度 → 基线不可复现。
await page.locator('.iwb-canvas').first().dispatchEvent('contextmenu', { clientX: 320, clientY: 520 })
await expect(page.locator('.iwb-ctxmenu').first()).toBeVisible()
await shot(page, '06-ctxmenu-' + theme)
})
})
}