Files
qiqi ead32a5f5e test(e2e): 按「气泡压标题=有意设计」调整 canvas-ux;迟到的 fit 不再覆盖用户操作
背景:项目所有人确认「单选气泡贴住节点标题栏」是有意设计,因此不改产品。
- canvas-ux ①:标题被气泡覆盖 ⇒ 「双击标题栏放大」在真实操作里会被吃掉;改用底栏「放大」按钮制造非适应态。
- canvas-ux ②:已选中节点自己的标题会被气泡盖住 ⇒ 最后的「标题栏路径」块先点空白清空选择。
- 顺带:doFit 的延迟重试加 snapshot 守卫 —— 等待占位就绪期间若用户已缩放/平移,放弃这次迟到的 fit,
  不再覆盖用户刚做的取景。

验证:canvas-ux 5 条 × 2 轮全绿;已知红基线 9 → 7 条(只剩环境类)。
2026-11-27 07:26:48 +08:00

294 lines
15 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.
// v3.86.8 画布交互与图片复制(真实浏览器验收):
// ① 双击画布空白不再适应视图;底栏最右独立一格「适应视图」按钮 + Shift+F 仍适应全部节点
// ② 预览「复制」统一转 PNG:PNG 源与 JPEG 源都必须成功,navigator.clipboard.read() 读出 image/png
// ③ 画布 Shift 点「节点体」(非标题栏)多选:高亮数量 1→2→1
// ④ 全程 pageerror = 0
// 运行:npm run build && IWB_BASE_URL=http://127.0.0.1:8787 npx playwright test tests/iwb-canvas-ux.spec.js
const { test, expect } = require('./_pw-coverage')
const W = require('./_helpers/wait')
const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787'
test.use({ baseURL, viewport: { width: 1440, height: 1000 }, permissions: ['clipboard-read', 'clipboard-write'] })
// 两个空参考图节点(无内容 → 节点体是可点选的 .iwb-ref-emptybox,非交互控件)+ 一个生图节点拉开包围盒
const WS = {
workspaces: [{
id: 'ws1', name: 'UX',
nodes: [
{ id: 'ra', type: 'ref', x: 80, y: 120, data: { url: '', data: null, name: '', localName: null } },
{ id: 'rb', type: 'ref', x: 80, y: 520, data: { url: '', data: null, name: '', localName: null } },
{ id: 'p1', type: 'prompt', x: 620, y: 120, data: { text: '提示词', title: 'P1' } },
{ id: 'g1', type: 'gen', x: 1120, y: 140, w: 480, data: { model: 'gpt-image-2', provider: 'p-a', keyGroup: 'a-1', size: '1024x1024', ratio: '1:1', res: '1K', variants: 1 } },
{ id: 'o1', type: 'out', x: 1860, y: 520, data: {} },
],
edges: [], groups: [],
}],
activeId: 'ws1', rev: 1,
}
const CFG = {
providers: [{ id: 'p-a', name: '服务商 A', baseUrl: 'https://a.example/v1', keys: [{ id: 'a-1', name: 'K1' }], models: [{ name: 'gpt-image-2', visible: true }] }],
outputsRetention: { enabled: true, maxFiles: 500, maxDays: 15 },
canvas: { snap: true },
}
// 8x8 纯色图(PNG / JPEG)由页面内 canvas 生成:避免手写 base64 出错,且保证是真实可解码的 JPEG
async function makeDataUrls(page) {
return page.evaluate(() => {
const mk = (type, color) => {
const c = document.createElement('canvas')
c.width = 8; c.height = 8
const x = c.getContext('2d')
x.fillStyle = color
x.fillRect(0, 0, 8, 8)
return c.toDataURL(type, 0.92)
}
return { png: mk('image/png', '#2f6bff'), jpeg: mk('image/jpeg', '#e2453c') }
})
}
async function seed(page, outputs) {
await page.addInitScript((d) => {
localStorage.clear()
localStorage.setItem('iwb_web_cfg', JSON.stringify(d.cfg))
localStorage.setItem('iwb_web_workspaces', JSON.stringify(d.ws))
localStorage.setItem('iwb_ws_active', d.ws.activeId)
localStorage.setItem('iwb_ws_' + d.ws.activeId, JSON.stringify(d.ws.workspaces[0]))
localStorage.setItem('iwb_web_outputs', JSON.stringify(d.outputs))
}, { cfg: CFG, ws: WS, outputs })
await page.goto('/', { waitUntil: 'domcontentloaded' })
await page.waitForTimeout(700)
await page.locator('.iwb-home-card:not(.iwb-home-new)').first().dblclick()
await expect(page.locator('.iwb-canvas:not(.iwb-canvas-hidden)')).toBeVisible()
await page.waitForTimeout(400)
}
// 找一处「真正的画布空白」:elementFromPoint 命中网格/画布本身,且不属于任何节点与浮层
async function blankPoint(page) {
return page.evaluate(() => {
const W = window.innerWidth, H = window.innerHeight
for (let y = 90; y < H - 130; y += 16) {
for (let x = Math.round(W * 0.3); x < W - 24; x += 16) {
const el = document.elementFromPoint(x, y)
if (!el) continue
if (el.closest && el.closest('.iwb-node, .iwb-toolbar, .iwb-wsrail, .iwb-statusbar, .iwb-minimap, .iwb-fullmask, .iwb-fullpanel, .iwb-drawer, .iwb-lib, .iwb-imgpreview, .iwb-ctxmask, .iwb-hint-bottom')) continue
const cls = el.className || ''
if (typeof cls === 'string' && (cls.indexOf('iwb-grid') >= 0 || cls.indexOf('iwb-canvas') >= 0 || cls.indexOf('iwb-world') >= 0)) return { x, y }
}
}
return null
})
}
const transform = (page) => page.evaluate(() => (document.querySelector('.iwb-world') || {}).style ? document.querySelector('.iwb-world').style.transform : '')
const scaleOf = (tf) => { const m = /scale\(([0-9.]+)\)/.exec(tf || ''); return m ? parseFloat(m[1]) : 0 }
const fitButton = (page) => page.locator('.iwb-toolbar-bottom button[data-tip*="适应"]')
const selCount = (page) => page.locator('.iwb-node.iwb-node-selected').count()
async function nodesInsideViewport(page) {
return page.evaluate(() => {
const W = window.innerWidth, H = window.innerHeight
const bad = []
for (const el of Array.from(document.querySelectorAll('.iwb-node'))) {
const r = el.getBoundingClientRect()
if (r.width === 0 && r.height === 0) continue
if (r.left < -2 || r.top < -2 || r.right > W + 2 || r.bottom > H + 2) bad.push(el.getAttribute('data-node-id') + ':' + Math.round(r.left) + ',' + Math.round(r.top) + ',' + Math.round(r.right) + ',' + Math.round(r.bottom))
}
return bad
})
}
test('1. 双击画布空白不再适应视图;底栏「适应视图」按钮与 Shift+F 仍适应全部节点', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
await seed(page, [])
// 先放大,确保「视图本来就不是适应态」。
// 2026-11-27:单选气泡贴在节点顶边是**有意设计**(会盖住标题栏),所以「双击标题栏放大」
// 在真实操作里会被自己的气泡吃掉;这里改用底栏「放大」按钮,结果确定且与气泡无关。
const zoomIn = page.locator('.iwb-toolbar-bottom button[data-tip="放大"]').first()
for (let i = 0; i < 6; i++) { await zoomIn.click(); await page.waitForTimeout(60) }
await page.waitForTimeout(300)
const zoomed = await transform(page)
expect(scaleOf(zoomed), '连续点「放大」后 scale 应 > 1').toBeGreaterThan(1)
// ① 双击画布空白 → 视图必须不变(旧行为是 fitView)
const bp1 = await blankPoint(page)
expect(bp1, '应能找到画布空白点').not.toBeNull()
await page.mouse.dblclick(bp1.x, bp1.y)
expect(await transform(page), '双击画布空白后视图不应变化').toBe(zoomed)
// ② 点底栏最右「适应视图」按钮 → 视图变化、缩小、全部节点在窗口内
const fit = fitButton(page)
await expect(fit).toBeVisible()
await fit.click()
await page.waitForTimeout(500)
const fitted = await transform(page)
expect(fitted, '点「适应视图」后视图应变化').not.toBe(zoomed)
expect(scaleOf(fitted), '适应视图后 scale 应 ≤ 1').toBeLessThanOrEqual(1)
expect(scaleOf(fitted)).toBeGreaterThan(0)
expect(await nodesInsideViewport(page), '适应视图后全部节点应在窗口内').toEqual([])
const bp2 = await blankPoint(page)
await page.mouse.dblclick(bp2.x, bp2.y)
expect(await transform(page), '适应态下双击空白同样不应改变视图').toBe(fitted)
// ③ Shift+F 快捷键仍适应视图:先中键拖拽平移离开适应态,再按 Shift+F 回到「全部节点可见」
const cbox = await page.locator('.iwb-canvas').first().boundingBox()
const cx = cbox.x + cbox.width / 2, cy = cbox.y + cbox.height / 2
await page.mouse.move(cx, cy)
await page.mouse.down({ button: 'middle' })
await page.mouse.move(cx + 170, cy + 96, { steps: 8 })
await page.mouse.up({ button: 'middle' })
await page.waitForTimeout(350)
const panned = await transform(page)
expect(panned, '中键拖拽应平移画布').not.toBe(fitted)
await page.keyboard.press('Shift+F')
await page.waitForTimeout(500)
const fitted2 = await transform(page)
expect(fitted2, 'Shift+F 应改变视图').not.toBe(panned)
expect(scaleOf(fitted2)).toBeGreaterThan(0)
expect(scaleOf(fitted2)).toBeLessThanOrEqual(1)
expect(await nodesInsideViewport(page), 'Shift+F 后全部节点应在窗口内').toEqual([])
expect(errors).toEqual([])
})
test('2. Shift 点节点体多选:高亮 1→2→1(并出现节点组气泡)', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
await seed(page, [])
// 2026-11-27:空参考图节点的节点体是「点击上传参考图」热区(点它会开文件选择框)——
// 选节点改用标题栏(与真实操作一致;节点组气泡由多选触发,与点哪里无关)。
const bodyA = page.locator('[data-node-id="ra"] .iwb-node-head')
const bodyB = page.locator('[data-node-id="rb"] .iwb-node-head')
await expect(bodyA).toBeVisible()
await expect(bodyB).toBeVisible()
await bodyA.click()
await expect.poll(() => selCount(page), { timeout: 3000 }).toBe(1)
const one = await page.evaluate(() => Array.from(document.querySelectorAll('.iwb-node.iwb-node-selected')).map((e) => e.getAttribute('data-node-id')))
expect(one).toEqual(['ra'])
await bodyB.click({ modifiers: ['Shift'] })
await expect.poll(() => selCount(page), { timeout: 3000 }).toBe(2)
await expect(page.locator('.iwb-node-floatbar-group'), '2 选 1 后应出现节点组气泡').toBeVisible()
await bodyB.click({ modifiers: ['Shift'] })
await expect.poll(() => selCount(page), { timeout: 3000 }).toBe(1)
// 标题栏路径行为不变:Shift 点标题栏仍可多选
// 2026-11-27:先清空选择 —— 单选气泡按设计贴在节点顶边,已选中节点自己的标题会被气泡盖住
const bp2 = await blankPoint(page)
await page.mouse.click(bp2.x, bp2.y)
await expect.poll(() => selCount(page), { timeout: 3000 }).toBe(0)
await page.locator('[data-node-id="ra"] .iwb-node-head').click()
await page.waitForTimeout(200)
await page.locator('[data-node-id="rb"] .iwb-node-head').click({ modifiers: ['Shift'] })
await expect.poll(() => selCount(page), { timeout: 3000 }).toBe(2)
expect(errors).toEqual([])
})
// 打开资源中心 → 图库 → 双击指定条目 → 预览里点「复制」→ 读系统剪贴板
async function copyFromGalleryPreview(page, name) {
await W.openGallery(page)
// 图库条目:title 挂在条目容器上(插件 v1.7 起),不是 img 上
const thumb = page.locator('.iwb-history-item[title^="' + name + '"] img').first()
await expect(thumb).toBeVisible()
await thumb.dblclick()
await expect(page.locator('.iwb-imgpreview')).toBeVisible()
// 等「原图解析完成」(objectURL)再复制:避免拿到还在解析中的占位缩略图
await expect.poll(() => page.locator('.iwb-imgpreview img').getAttribute('src'), { timeout: 5000 }).toContain('blob:')
await page.locator('.iwb-imgpreview .iwb-prev-bar button', { hasText: '复制' }).click()
await expect(page.locator('.iwb-status-ok').filter({ hasText: '已复制图片到剪贴板' }), '复制成功应给出提示').toBeVisible()
return page.evaluate(async () => {
const items = await navigator.clipboard.read()
const types = items.map((it) => it.types.join('+'))
const pngItem = items.find((it) => it.types.includes('image/png'))
if (!pngItem) return { types, type: '', w: 0, h: 0 }
const blob = await pngItem.getType('image/png')
const bmp = await createImageBitmap(blob)
const out = { types, type: blob.type, w: bmp.width, h: bmp.height }
if (bmp.close) bmp.close()
return out
})
}
test('3. 预览复制 PNG 源 → 剪贴板为 image/png', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
const urls = await makeDataUrls(page)
await seed(page, [
{ name: 'png_src.png', url: urls.png, data: urls.png, size: 900, mtime: Date.now() - 1000, w: 8, h: 8 },
])
const clip = await copyFromGalleryPreview(page, 'png_src.png')
expect(clip.types, JSON.stringify(clip)).toContain('image/png')
expect(clip.type).toBe('image/png')
expect(clip.w).toBe(8)
expect(clip.h).toBe(8)
expect(errors).toEqual([])
})
test('4. 预览复制 JPEG 源 → 也必须成功且为 image/png', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
const urls = await makeDataUrls(page)
expect(urls.jpeg.indexOf('data:image/jpeg') === 0, 'JPEG 夹具应是 image/jpeg').toBe(true)
await seed(page, [
{ name: 'png_src.png', url: urls.png, data: urls.png, size: 900, mtime: Date.now() - 1000, w: 8, h: 8 },
{ name: 'jpeg_src.jpg', url: urls.jpeg, data: urls.jpeg, size: 900, mtime: Date.now() - 2000, w: 8, h: 8 },
])
const clip = await copyFromGalleryPreview(page, 'jpeg_src.jpg')
expect(clip.types, JSON.stringify(clip)).toContain('image/png')
expect(clip.type).toBe('image/png')
expect(clip.w).toBe(8)
expect(clip.h).toBe(8)
expect(errors).toEqual([])
})
// v3.24.83:底栏「缩小 / 放大」两个图标(放在「适应」左边)
// 断言的是**用户看得见的结果**:点一下缩放一档 15%,且画布中心那片内容不位移(锚点=中心)。
test('5. 底栏「缩小/放大」按钮:一档 15%,锚点为画布中心,且与「适应」同组相邻', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
await seed(page, [])
// ① 三个按钮都在同一格,顺序为 [缩小][放大][适应]
const order = await page.evaluate(() => {
const grp = document.querySelector('.iwb-toolbar-bottom .iwb-btn-fit')?.closest('.iwb-tgroup')
return grp ? Array.from(grp.querySelectorAll('button')).map((b) => b.getAttribute('data-tip')) : []
})
expect(order, '缩放按钮必须与「适应」同组相邻').toEqual(['缩小', '放大', '适应'])
// ② 解析 viewport(translate + scale)
const vp = () => page.evaluate(() => {
const el = document.querySelector('.iwb-world')
const t = (el && el.style && el.style.transform) || ''
const m = /translate\(\s*(-?[0-9.]+)px,\s*(-?[0-9.]+)px\)\s*scale\(\s*([0-9.]+)\s*\)/.exec(t)
return m ? { x: parseFloat(m[1]), y: parseFloat(m[2]), s: parseFloat(m[3]) } : null
})
const v0 = await vp()
expect(v0, '应能读到画布 transform').not.toBeNull()
// 屏幕中心对应的世界坐标:缩放后它必须还在屏幕中心
const W = 1440, H = 1000
const cxs = W / 2, cys = H / 2
const wx = (cxs - v0.x) / v0.s, wy = (cys - v0.y) / v0.s
// ③ 放大一档
await page.locator('.iwb-toolbar-bottom button[data-tip="放大"]').click()
await expect.poll(async () => (await vp()).s, { message: '点「放大」后 scale 应变为 ×1.15' })
.toBeCloseTo(v0.s * 1.15, 3)
const v1 = await vp()
expect(v1.x + wx * v1.s, '放大后画布中心那点不得左右位移').toBeCloseTo(cxs, 0)
expect(v1.y + wy * v1.s, '放大后画布中心那点不得上下位移').toBeCloseTo(cys, 0)
// ④ 缩小两档:回到并小于原始缩放(互逆性)
await page.locator('.iwb-toolbar-bottom button[data-tip="缩小"]').click()
await expect.poll(async () => (await vp()).s, { message: '缩小一档应回到原缩放' }).toBeCloseTo(v0.s, 3)
await page.locator('.iwb-toolbar-bottom button[data-tip="缩小"]').click()
await expect.poll(async () => (await vp()).s).toBeCloseTo(v0.s / 1.15, 3)
expect(errors).toEqual([])
})