Files
image-workbench-desktop/tests/plugin-gallery-render-test.js
qiqi d5449e8b71 fix(gallery): 删除后不残留「图片已丢失」格子(deleteOutputs 返回 Promise + 插件本地先摘除)v3.25.55
- deleteOutputs 返回 Promise(删除+refreshOutputs 完成才 resolve)
- capability.remove 等删除完成再返回
- gallery 1.2.1:先本地摘除重排,250ms 后重拉校正
- 回归测试增加删除场景
2026-11-20 12:15:02 +08:00

84 lines
4.5 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.
'use strict'
// 图库插件渲染回归(防止「缩略图异步却当同步用」导致整墙"图片已丢失")
//
// 2026-11-20 真实事故:ctx.gallery.thumb() 返回 Promise,插件把 Promise 当字符串判空 ⇒ 7 张全丢。
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const { JSDOM } = require('jsdom')
const root = path.resolve(__dirname, '..')
const src = fs.readFileSync(path.join(root, '..', 'iwb-plugins-desktop', 'gallery', 'src', 'index.js'), 'utf8')
const dom = new JSDOM('<!doctype html><html><body><div id="host"></div></body></html>', { pretendToBeVisual: true })
const { window } = dom
global.window = window
global.document = window.document
let mounted = null
const host = {
plugins: [],
register (manifest, factory) { host.plugins.push({ manifest, factory }) },
}
window.IWBPluginHost = host
// 插件用到的浏览器 API
window.ResizeObserver = class { observe () {} disconnect () {} }
window.confirm = () => true
;(async () => {
// 在 vm 里跑插件源码(它直接用 window/document)
const vm = require('vm')
const sandbox = { window, document: window.document, console, setTimeout, clearTimeout, Promise, Object, Array, JSON, Math }
vm.runInNewContext(src, sandbox)
assert.strictEqual(host.plugins.length, 1, '插件应注册一次')
const { manifest, factory } = host.plugins[0]
assert.strictEqual(manifest.id, 'gallery')
assert.ok(manifest.capabilities.indexOf('gallery') >= 0, '应声明 gallery 能力')
const calls = { list: 0, thumb: 0 }
let current = [{ name: 'a.png', w: 200, h: 300 }, { name: 'b.png', w: 200, h: 120 }]
const ctx = {
ui: { registerPanel (p) { mounted = p.mount } },
data: { read: async () => ({ cfg: { tile: 130 } }), patch: async () => true },
gallery: {
list: async () => { calls.list++; return { items: current.slice(), total: current.length } },
thumb: async (n) => { calls.thumb++; return 'data:image/svg+xml;utf8,' + encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="2" height="3"/>') },
preview: () => {}, download: async () => ({ ok: true }), insert: async () => ({ ok: true }), remove: async () => ({ ok: true }),
},
}
factory(ctx)
assert.ok(mounted, '应注册面板 mount')
const node = window.document.getElementById('host')
mounted(node)
// 等 list + 2 次 thumb 的微任务跑完
for (let i = 0; i < 20; i++) await new Promise((r) => setTimeout(r, 10))
const imgs = Array.from(node.querySelectorAll('.iwb-history-item img'))
const missing = Array.from(node.querySelectorAll('.iwb-history-missing')).map((x) => x.textContent).filter((t) => t !== '加载中…')
assert.strictEqual(calls.list, 1, '应只拉一次列表')
assert.strictEqual(calls.thumb, 2, '每张图各取一次缩略图')
assert.strictEqual(imgs.length, 2, '两张图都应渲染出 img,实际 ' + imgs.length)
assert.ok(imgs.every((i) => String(i.getAttribute('src')).indexOf('data:image/svg') === 0), 'img.src 应来自 thumb()')
assert.strictEqual(missing.length, 0, '不应出现「图片已丢失」:' + missing.join(','))
// 删除后不得残留「图片已丢失」格子:本地先摘除 + 宿主删除完成后重拉
let removedNames = null
ctx.gallery.remove = async (names) => { removedNames = names.slice(); current = current.filter((x) => names.indexOf(x.name) < 0); return { ok: true, removed: names.length } }
// 选中第一张(点击 img),再点顶部「删除」
imgs[0].dispatchEvent(new window.MouseEvent('click', { bubbles: true }))
const delBtn = Array.from(node.querySelectorAll('.iwb-gal-actions button')).find((b) => b.textContent === '删除')
assert.ok(delBtn, '应有删除按钮')
delBtn.dispatchEvent(new window.MouseEvent('click', { bubbles: true }))
for (let i = 0; i < 30; i++) await new Promise((r) => setTimeout(r, 20))
assert.deepStrictEqual(removedNames, ['a.png'], '删除应把选中的名字交给宿主')
const left = node.querySelectorAll('.iwb-history-item img').length
assert.strictEqual(left, 1, '删除后应只剩 1 张,实际 ' + left)
const missingAfter = Array.from(node.querySelectorAll('.iwb-history-missing')).map((x) => x.textContent).filter((t) => t !== '加载中…')
assert.strictEqual(missingAfter.length, 0, '删除后不应出现「图片已丢失」:' + missingAfter.join(','))
console.log('plugin-gallery checks passed(异步缩略图 / 渲染 / 删除后不残留丢失格)')
})().catch((e) => { console.error(e); process.exit(1) })