Files
image-workbench-desktop/tests/iwb-lod.spec.js
T
qiqi 319598ef65 test(e2e): LOD 用例让 fit 落在 lite 区间,断言跟随实测 scale
根因(3 条同一原因,测试假设过期,非产品缺陷):
- 用例用 40 列 × 200px 铺开,注释写「fit ≈0.18 ≥ 15% ⇒ 必须是 lite」;
- 停靠栏把可用宽度从 ~1384 压到 ~1014 后,实测 fit = 0.127(<15%)
  ⇒ 应用按设计整片降成 plain(300 个 .iwb-lod-plain),lite 断言全部失效。
- 实测对照:40 列 → scale 0.127 / plain 300;24 列 → scale 0.213 / lite 300。

修法:
1. COLS 40 → 24:fit ≈0.213,既 <64px/节点(触发 LOD)又 ≥15%(应为 lite),三个方向都还能守。
2. ① 的档位断言改为跟随实测 scale(<15% ⇒ 期望 plain,≥15% ⇒ 期望 lite),
   避免以后视口/停靠栏再变时静默失效。

验证:iwb-lod 5/5 全绿(此前 3 红);基线已知红 20 → 17 条。
2026-11-27 06:40:06 +08:00

172 lines
9.9 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.
// LOD 判据(v3.24.77 起)+ 两档细化(v3.24.78):节点在屏幕上宽 < 64px 时**不渲染完整节点体**。
//
// 为什么需要这个契约:
// 旧判据是 `viewport.scale < 0.05`,而 800 节点画布的 fit 视图 scale ≈ 0.2 —— 差 4 倍,永不生效。
// 于是 800 个节点在 17% 缩放下仍然各挂一个 textarea + 图片网格 + 按钮(实测 186 个 textarea),
// 切一次画布 488ms,其中 react-dom 271ms + react 173ms,业务逻辑只有 3.9ms。改成按屏幕宽度判定后 198ms。
//
// 三个方向都要守:
// ① fit 大画布 ⇒ 必须降级(否则性能回退到 488ms);
// ② 15% 以上 ⇒ 必须是 lite(画代表图/首行文字),**不许**直接变灰块 ——
// 用户实测反馈「图片简化后无法判断节点内容」,灰块等于把画布变成一片灰;
// ③ 放大后 ⇒ 必须恢复完整节点体(否则节点永久灰块,等于功能丢失)。
const { test, expect } = require('./_pw-coverage')
const { poll, waitHomeReady, waitCanvasOpen } = require('./_helpers/wait')
test.use({ baseURL: process.env.IWB_BASE_URL || 'http://127.0.0.1:8787', viewport: { width: 1440, height: 1000 } })
const CFG = { providers: [{ id: 'p-a', name: 'A', baseUrl: 'https://a.example/v1', keys: [{ id: 'a-1', name: 'K', value: 'sk' }], models: [{ name: 'gpt-image-2.5', visible: true }] }], canvas: { snap: true } }
// 2026-11-27:列数从 40 降到 24。停靠栏把可用宽度从 ~1384 压到 ~1014,
// 40 列的 fit 从 0.18 掉到 0.127(<15%)⇒ 按设计整片降成 plain,本 spec 的 lite 断言随之失效。
// 24 列 fit ≈0.213:既 <64px/节点(触发 LOD),又 ≥15%(应为 lite)。
const COLS = 24
// 1×1 红点(data URL,无需网络):用来验证 lite 档真的把「代表图」画出来了
const DOT = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='
// 40 列铺开 + 窄节点(w=160)⇒ fit 后 scale ≈ 0.18,即每节点屏幕宽约 29px
function mkBoard(id, name, n) {
const nodes = []
for (let i = 0; i < n; i++) {
const m = i % 13
const type = m === 0 ? 'gen' : (m <= 3 ? 'prompt' : 'ref')
const node = { id: id + '_n' + i, type, x: 60 + (i % COLS) * 200, y: 60 + Math.floor(i / COLS) * 150, w: 160, data: {} }
if (type === 'prompt') node.data = { text: 'p' + i }
if (type === 'gen') node.data = { model: 'gpt-image-2.5', provider: 'p-a', ratio: '1:1', res: '2K', quality: 'auto' }
nodes.push(node)
}
return { id, name, nodes, edges: [], groups: [], category: '', starred: false, deleted: false }
}
const seed = (page, boards, activeId) => page.addInitScript((d) => {
localStorage.clear()
localStorage.setItem('iwb_web_cfg', JSON.stringify(d.cfg))
localStorage.setItem('iwb_web_workspaces', JSON.stringify({ schemaVersion: 1, rev: 1, activeId: d.activeId, workspaces: d.boards }))
localStorage.setItem('iwb_ws_active', d.activeId)
for (const b of d.boards) localStorage.setItem('iwb_ws_' + b.id, JSON.stringify(b))
window.__IWB_E2E__ = true
}, { cfg: CFG, boards, activeId })
const readScale = (page) => page.evaluate(() => {
const w = document.querySelector('.iwb-world')
const m = w && (getComputedStyle(w).transform || '').match(/matrix\(([^,]+),/)
return m ? Number(m[1]) : 0
})
// 等两帧(替代固定毫秒等待;滚轮缩放是按帧合并的)
const nextFrame = (page) => page.evaluate(() => new Promise((res) => requestAnimationFrame(() => requestAnimationFrame(res))))
test('大画布 fit 视图:节点屏幕宽 < 64px 时降级,不挂 textarea', async ({ page }) => {
const board = mkBoard('w1', 'MAIN', 300)
await seed(page, [board], 'w1')
await page.goto('/', { waitUntil: 'domcontentloaded' })
await waitHomeReady(page)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await waitCanvasOpen(page)
await poll(page, () => page.locator('.iwb-node').count(), { timeout: 20000 }).toBe(300)
// fit 视图的 scale 是异步算出来的(要等容器尺寸 + measuredH),先等它落到 LOD 区间
await poll(page, () => readScale(page), { timeout: 20000 }).toBeLessThan(0.24)
// 160 × 0.24 = 38px,远低于 64px 阈值
await poll(page, () => page.locator('.iwb-node-body-lod').count(), { timeout: 20000 }).toBe(300)
// 断言跟随实测 scale(不再假设固定档位):<15% ⇒ plain,≥15% ⇒ lite(有内容,不许灰块)
const fitScale = await readScale(page)
const wantPlain = fitScale < 0.15
await expect(page.locator('.iwb-lod-plain'), '低于 15% 才允许纯占位(当前 ' + fitScale.toFixed(3) + ')').toHaveCount(wantPlain ? 300 : 0)
await expect(page.locator('.iwb-lod-lite'), '15% 以上必须是 lite(当前 ' + fitScale.toFixed(3) + ')').toHaveCount(wantPlain ? 0 : 300)
await expect(page.locator('.iwb-node textarea'), 'fit 视图不该渲染任何 textarea').toHaveCount(0)
await expect(page.locator('.iwb-node .iwb-node-body').first()).toBeVisible()
})
test('lite 档图片节点画出代表图(能判断是哪个节点),文本节点画出首行', async ({ page }) => {
const board = mkBoard('w1', 'MAIN', 300)
// 前 30 个参考图节点带上真图;提示词节点带一段文字(第 1 行会被画进 lite 档)
for (let i = 0; i < 30; i++) {
const n = board.nodes[i]
if (n.type === 'ref') n.data = { data: DOT, name: 'dot.png' }
if (n.type === 'prompt') n.data = { text: '海边日落 · 长焦压缩' }
}
await seed(page, [board], 'w1')
await page.goto('/', { waitUntil: 'domcontentloaded' })
await waitHomeReady(page)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await waitCanvasOpen(page)
await poll(page, () => page.locator('.iwb-node-body-lod').count(), { timeout: 20000 }).toBe(300)
await poll(page, () => page.locator('.iwb-lod-thumb').count(), { timeout: 20000 }).toBeGreaterThanOrEqual(10)
// src 可能是 data:(首次)或 SW 缓存路径(写入完成后的稳态)—— 两种都算「真把图画出来了」
await expect(page.locator('.iwb-lod-thumb').first()).toHaveAttribute('src', /^(data:image\/|.*__iwb_cache__)/)
await poll(page, () => page.locator('.iwb-lod-line').count(), { timeout: 20000 }).toBeGreaterThanOrEqual(4)
await expect(page.locator('.iwb-lod-line').first()).toHaveText(/长焦压缩/)
})
test('缩到 15% 以下才退成纯占位(lite → plain)', async ({ page }) => {
const board = mkBoard('w1', 'MAIN', 300)
for (let i = 0; i < 30; i++) if (board.nodes[i].type === 'ref') board.nodes[i].data = { data: DOT, name: 'dot.png' }
await seed(page, [board], 'w1')
await page.goto('/', { waitUntil: 'domcontentloaded' })
await waitHomeReady(page)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await waitCanvasOpen(page)
await poll(page, () => page.locator('.iwb-lod-lite').count(), { timeout: 20000 }).toBe(300)
const anchor = await page.evaluate(() => {
const n = document.querySelector('.iwb-node')
if (!n) return { x: 700, y: 500 }
const r = n.getBoundingClientRect()
return { x: Math.round(r.left + r.width / 2), y: Math.round(r.top + r.height / 2) }
})
await page.mouse.move(anchor.x, anchor.y)
for (let i = 0; i < 24; i++) {
if (await readScale(page) < 0.13) break
await page.mouse.wheel(0, 240)
await nextFrame(page)
}
await poll(page, () => readScale(page), { timeout: 20000 }).toBeLessThan(0.15)
await poll(page, () => page.locator('.iwb-lod-plain').count(), { timeout: 20000 }).toBe(300)
await expect(page.locator('.iwb-lod-thumb'), '纯占位档不再画图(省解码)').toHaveCount(0)
})
test('放大回来后恢复完整节点体(LOD 不是永久的)', async ({ page }) => {
const board = mkBoard('w1', 'MAIN', 300)
await seed(page, [board], 'w1')
await page.goto('/', { waitUntil: 'domcontentloaded' })
await waitHomeReady(page)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await waitCanvasOpen(page)
await poll(page, () => page.locator('.iwb-node-body-lod').count(), { timeout: 20000 }).toBe(300)
// ⚠️ 锚点必须是**节点中心**:宽扁布局 fit 后节点只占屏幕上方一小条,
// 用视口中心当锚点滚轮会把视野推进空白区(实测测出 dom=0)。
const anchor = await page.evaluate(() => {
const n = document.querySelector('.iwb-node')
if (!n) return { x: 700, y: 500 }
const r = n.getBoundingClientRect()
return { x: Math.round(r.left + r.width / 2), y: Math.round(r.top + r.height / 2) }
})
await page.mouse.move(anchor.x, anchor.y)
for (let i = 0; i < 20; i++) {
if (await readScale(page) >= 0.5) break
await page.mouse.wheel(0, -240)
await nextFrame(page)
}
// 0.24 是窄节点(160px)的临界点,放大到 0.5 ⇒ 屏幕宽 80px > 64 ⇒ 必须恢复
await poll(page, () => readScale(page), { timeout: 20000 }).toBeGreaterThan(0.4)
await poll(page, () => page.locator('.iwb-node-body-lod').count(), { timeout: 20000 }).toBe(0)
await expect(page.locator('.iwb-node textarea').first(), '放大后必须能看到内容').toBeVisible()
})
test('常规画布(节点屏幕宽足够)从一开头就不 LOD', async ({ page }) => {
const board = mkBoard('w1', 'SMALL', 5)
for (const n of board.nodes) { n.w = 400; n.x = 60 + board.nodes.indexOf(n) * 460; n.y = 120 }
await seed(page, [board], 'w1')
await page.goto('/', { waitUntil: 'domcontentloaded' })
await waitHomeReady(page)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await waitCanvasOpen(page)
await poll(page, () => page.locator('.iwb-node').count(), { timeout: 20000 }).toBe(5)
await poll(page, () => readScale(page), { timeout: 20000 }).toBeGreaterThan(0.3)
await expect(page.locator('.iwb-node-body-lod'), '5 个宽节点的画布不该出现占位体').toHaveCount(0)
await poll(page, () => page.locator('.iwb-node textarea').count(), { timeout: 20000 }).toBeGreaterThan(0)
})