Verify (Gitea) / gate (build · unit · facts) (push) Failing after 40s
toHaveScreenshot 的 threshold 默认 0.2(每像素 YCbCr 距离),maxDiffPixels: 0 只约束 「超阈值像素数」——两者叠加不等于逐像素精确。B5 端口描边从 #ababab 改成 color-mix(text 55%) 后,亮色只差约 19 级,全程被 0.2 吃掉:基线没更新、比对报 12/12 通过、--update-snapshots 也不写盘(它同样按阈值判断「有没有变」)。 压到 threshold: 0 后重生成 4 张亮色基线,12/12 仍通过 —— 同机渲染是确定性的, 所以这条不会变成 flaky 来源,而「0 diff」从此名副其实。 教训写进 docs/plans/design-tokens-rollout.md:此前各批记的「逐像素 0 diff」, 真实含义是「没有超过 0.2 阈值的像素」。
156 lines
7.4 KiB
JavaScript
156 lines
7.4 KiB
JavaScript
// 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,
|
||
// threshold 默认 0.2(每像素 YCbCr 距离),`maxDiffPixels: 0` 只是「超阈值的像素数为 0」——
|
||
// 两者叠加并不等于逐像素精确。B5 踩到:端口描边色改了约 19 级,全被 0.2 吃掉,
|
||
// 基线没更新、比对还显示 12/12 通过(假绿灯)。这里显式压到 0,让「0 diff」名副其实。
|
||
threshold: 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)
|
||
})
|
||
})
|
||
}
|