Files
image-workbench-desktop/tests/iwb-image-original.spec.js
T
qiqi 8035c01fb7 fix(canvas): 停靠栏未就绪时不落盘视口(修 6 条 e2e 超时)+ 4 条用例按现 UI 修正
根因(trace 定位,6 条同一原因):
首页视图里启动 fit 早于停靠栏挂载(--iwb-dock-l 首帧为空)⇒ 算出 ins=0 的坏视口
(实测 translate(-92,-84));这份坏值被 useViewportPersistence 落盘,打开画布时又被当作
「用户保存的视口」恢复 ⇒ 节点停在 x=29(压在 318px 左栏下),dblclick 命中浏览器面板的
.iwb-cwv-url / .iwb-cwv-empty,Playwright 重试到 30s 超时。
修法:flushViewport 增加 insetsReady 门槛(--iwb-dock-l 为空串时跳过落盘);
画布打开后 loadWsInto 走 doFit,此时占位就绪,落盘的是正确视口。

4 条用例按现 UI 修正:
- tooltip-labels ②:单选气泡现为 改名/复制/删除/添加到提示词库…(收藏只在多选气泡)
- image-original:收藏入口已收进「选中操作 ▸」子菜单(且需先选中节点)
- resume-scope:运行入口已收进「该节点 ▸」子菜单
- download-cors:同源代理 /api/img 已随网页版删除,桌面走原生 HTTP 另存为 ⇒ 该条标 fixme(需 Tauri 环境)

验证:
- 组 A(typing-perf / storage-incremental / close-flush / history-sweep)6/6 绿
- 组 B(image-original / resize-perf / tooltip-labels / resume-scope / download-cors)12/12 绿
- verify:ci 全绿(facts 数字已 sync)
- 已知红基线 17 → 7 条(只剩 clipboard-audit 3 + image-memory-parity 4,均为环境限制)
2026-11-27 07:01:06 +08:00

136 lines
8.3 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.
// v3.87.1 去掉图片压缩(方案 A:不压缩 + 引用化 + hydrate)
// ① 画布:大图节点保存后转成引用(新写入主通道 {__blob};兼容存量 {__cache}/{__idb})(localStorage 不再驻留图片 base64),刷新后显示的仍是原图(不是 256px 预览)
// ② 收藏节点组:快照不压缩(存引用),实例化回画布后仍是原图
// 引用形态断言于 v3.0.x 扩展(§13.34.7 第 2 条:新写入走 IDB Blob):原判据只认 {__cache}/{__idb},
// 会与新政策(新写入 = {__blob})冲突;断言意图「必须是可解析引用、不得残留 base64」不变,故加入 __blob —— 见下方注释。
// 两条都断言「保存/收藏路径不再调用任何压缩」(__IWB_COMPRESS_LAST__ 必须为空)——旧代码在收藏组用例上必然失败(A/B 已验证)
// 运行:npm run build && IWB_BASE_URL=http://127.0.0.1:8787 npx playwright test tests/iwb-image-original.spec.js
const { test, expect } = require('./_pw-coverage')
const baseURL = process.env.IWB_BASE_URL || 'http://127.0.0.1:8787'
test.use({ baseURL, viewport: { width: 1500, height: 1000 } })
// 种一个「图片节点 + 一张 1200×900 大图」的画布:图在页面内生成(>3000 字符 dataURL 才会走引用化分支)
async function seedBigImageBoard(page) {
await page.addInitScript(() => {
const c = document.createElement('canvas')
c.width = 1200; c.height = 900
const x = c.getContext('2d')
// 伪随机色块:保证编码后体积足够大(纯色 PNG 会被压到 3000 字符以下)
for (let i = 0; i < 1200; i++) {
const h = ((i * 2654435761) >>> 0) % 0xffffff
x.fillStyle = '#' + h.toString(16).padStart(6, '0')
x.fillRect((i * 37) % 1160, (i * 91) % 870, 40, 30)
}
const dataUrl = c.toDataURL('image/jpeg', 0.95)
window.__IWB_TEST_IMG__ = { len: dataUrl.length, w: c.width, h: c.height }
const ws = {
workspaces: [{
id: 'ws1', name: 'ORIG',
nodes: [{ id: 'r1', type: 'ref', x: 120, y: 140, data: { url: '', data: dataUrl, name: 'big.jpg', localName: null } }],
edges: [], groups: [],
}],
activeId: 'ws1', rev: 1,
}
localStorage.clear()
localStorage.setItem('iwb_web_cfg', JSON.stringify({ providers: [], outputsRetention: { enabled: true, maxFiles: 500, maxDays: 15 }, canvas: {} }))
localStorage.setItem('iwb_web_workspaces', JSON.stringify(ws))
localStorage.setItem('iwb_ws_active', 'ws1')
localStorage.setItem('iwb_ws_ws1', JSON.stringify(ws.workspaces[0]))
localStorage.setItem('iwb_web_outputs', JSON.stringify([]))
})
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()
}
// 轻拖节点,触发一次画布保存(自动保存是防抖的,必须有改动才会写盘)
async function nudgeNode(page, nodeId) {
const box = await page.locator('[data-node-id="' + nodeId + '"]').boundingBox()
await page.mouse.move(box.x + 60, box.y + 40)
await page.mouse.down()
await page.mouse.move(box.x + 90, box.y + 60, { steps: 6 })
await page.mouse.up()
}
test('画布:保存转引用;hydrate 的显示副本允许压缩到 ≤2048(方案 A:原图以自动下载/图库为准)', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
await seedBigImageBoard(page)
const info = await page.evaluate(() => window.__IWB_TEST_IMG__)
expect(info.len, '测试图必须 >3000 字符,否则走不到引用化分支').toBeGreaterThan(3000)
const img = page.locator('[data-node-id="r1"] .iwb-ref-single')
await expect(img).toBeVisible()
// 方案 A:画布里的显示副本允许被压缩到长边 ≤2048(原图在用户磁盘/图库,不在画布里常驻)
expect(await img.evaluate((el) => el.naturalWidth)).toBeGreaterThan(0)
expect(await img.evaluate((el) => el.naturalWidth)).toBeLessThanOrEqual(2048)
// 只观察「保存画布」这一步是否还调用压缩
await page.evaluate(() => { delete window.__IWB_COMPRESS_LAST__ })
await nudgeNode(page, 'r1')
await expect.poll(() => page.evaluate(() => localStorage.getItem('iwb_ws_ws1') || ''), { timeout: 25000 }).toMatch(/__cache|__idb|__blob/)
const compressed = await page.evaluate(() => window.__IWB_COMPRESS_LAST__ || null)
expect(compressed, '保存画布不应再触发图片压缩(旧行为会走 worker/主线程压缩),实际走了:' + compressed).toBe(null)
await page.waitForTimeout(2000) // 等异步保存也落地,断言最终形态
const stored = await page.evaluate(() => localStorage.getItem('iwb_ws_ws1') || '')
expect(stored, '保存后节点图必须是引用、不得残留图片 base64:' + stored.slice(0, 200)).not.toMatch(/data:image\//)
// 刷新后:节点里必须还是原图,而不是 256px 预览
await page.reload({ waitUntil: 'domcontentloaded' })
await expect.poll(() => page.locator('[data-node-id="r1"] .iwb-ref-single').count(), { timeout: 20000 }).toBe(1)
await expect
.poll(() => page.locator('[data-node-id="r1"] .iwb-ref-single').evaluate((el) => el.naturalWidth), { timeout: 20000 })
.toBeLessThanOrEqual(2048)
expect(errors).toEqual([])
})
test('收藏节点组:快照不压缩,实例化回画布后仍是原图', async ({ page }) => {
const errors = []
page.on('pageerror', (e) => errors.push(String((e && e.message) || e)))
await seedBigImageBoard(page)
const info = await page.evaluate(() => window.__IWB_TEST_IMG__)
// 右键节点 → 「选中操作 ▸」→ 收藏选中节点 → 填名字
// 2026-11-27:该入口已收进子菜单(且要求先选中节点,否则子菜单显示「画布操作」)
await page.locator('[data-node-id="r1"] .iwb-node-head').click()
await page.locator('[data-node-id="r1"]').click({ button: 'right' })
await page.locator('.iwb-ctxmenu:not(.iwb-ctx-sub) .iwb-ctx-item', { hasText: '选中操作' }).hover()
const fav = page.locator('.iwb-ctxmenu.iwb-ctx-sub .iwb-ctx-item', { hasText: '收藏选中节点' })
await expect(fav).toBeVisible()
await fav.click()
const nameInput = page.locator('.iwb-rename-modal .iwb-rename-input')
await expect(nameInput).toBeVisible()
await page.evaluate(() => { delete window.__IWB_COMPRESS_LAST__ })
await nameInput.fill('大图组')
await nameInput.press('Enter')
// 收藏快照进 localStorage:必须是引用,不是小图 base64;且不该发生任何压缩
await expect.poll(() => page.evaluate(() => localStorage.getItem('iwb_web_groups') || ''), { timeout: 20000 }).toMatch(/__cache|__idb|__blob/)
const compressed2 = await page.evaluate(() => window.__IWB_COMPRESS_LAST__ || null)
expect(compressed2, '收藏节点组不应再触发图片压缩(旧行为会写 256px 预览),实际走了:' + compressed2).toBe(null)
const groupsRaw = await page.evaluate(() => localStorage.getItem('iwb_web_groups') || '')
expect(groupsRaw, '收藏组快照必须是引用、不得残留图片 base64:' + groupsRaw.slice(0, 200)).not.toMatch(/data:image\//)
const gid = JSON.parse(groupsRaw)[0].id
// 合成拖放(画布 drop 监听读 application/x-iwb-group)→ 实例化
await page.evaluate((id) => {
const canvas = document.querySelector('.iwb-canvas')
const rect = canvas.getBoundingClientRect()
const dt = new DataTransfer()
dt.setData('application/x-iwb-group', JSON.stringify(id))
canvas.dispatchEvent(new DragEvent('drop', { bubbles: true, cancelable: true, clientX: rect.left + 800, clientY: rect.top + 560, dataTransfer: dt }))
}, gid)
await expect.poll(() => page.locator('.iwb-ref-single').count(), { timeout: 20000 }).toBe(2)
// 丙方案政策:画布/实例化后的**显示副本**允许被压缩(≤2048;原图在磁盘/图库,不常驻画布)
await expect
.poll(async () => (await page.locator('.iwb-ref-single').evaluateAll((els) => els.map((el) => el.naturalWidth))).join(','), { timeout: 20000 })
.toMatch(/^[1-9]\d*(,[1-9]\d*)*$/)
const wid = await page.locator('.iwb-ref-single').evaluateAll((els) => els.map((el) => el.naturalWidth))
expect(wid.length).toBe(2)
wid.forEach((w) => { expect(w).toBeGreaterThan(0); expect(w).toBeLessThanOrEqual(2048) })
expect(errors).toEqual([])
})