Files
image-workbench-desktop/tests/plugin-gallery-ux-test.js
T

154 lines
8.1 KiB
JavaScript

'use strict'
// 图库插件 1.6.0 交互回归:搜索 / 失败统计+全部重试 / 右键菜单 / 多选拖拽
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const { JSDOM } = require('jsdom')
const src = fs.readFileSync(path.resolve(__dirname, '..', '..', 'iwb-plugins-desktop', 'gallery', 'src', 'index.js'), 'utf8')
const css = fs.readFileSync(path.resolve(__dirname, '..', '..', 'iwb-plugins-desktop', 'gallery', 'styles.css'), 'utf8')
const wait = (ms) => new Promise((r) => setTimeout(r, ms))
const GOOD = 'data:image/svg+xml;utf8,' + encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="2" height="3"/>')
async function boot (opts) {
const dom = new JSDOM('<!doctype html><html><body><div id="host"></div></body></html>', { pretendToBeVisual: true })
const { window } = dom
window.ResizeObserver = class { observe () {} disconnect () {} }
window.confirm = () => true
const copied = []
Object.defineProperty(window.navigator, 'clipboard', { value: { writeText: (t) => { copied.push(t); return Promise.resolve() } }, configurable: true })
let reg = null
window.IWBPluginHost = { register (m, f) { reg = { m, f } } }
const sandbox = { window, document: window.document, console, setTimeout, clearTimeout, Promise, Object, Array, JSON, Math, Date, String, Number }
vm.runInNewContext(src, sandbox)
const style = window.document.createElement('style')
style.textContent = css
window.document.head.appendChild(style)
const calls = { list: [], insert: [], remove: [], preview: [] }
const items = opts.items
const ctx = {
ui: { registerPanel (p) { boot.panel = p } },
data: { read: async () => ({ cfg: {} }), patch: async () => true },
gallery: {
list: async (o) => { calls.list.push(o || {}); return { items: items.slice(), total: items.length } },
thumb: async (n) => opts.thumb(n),
preview: (n) => { calls.preview.push(n) },
insert: async (names, o) => { calls.insert.push([names.slice(), o]); return { ok: true } },
download: async () => ({ ok: true }),
remove: async (names) => { calls.remove.push(names.slice()); return { ok: true } },
},
}
reg.f(ctx)
const host = window.document.getElementById('host')
boot.panel.mount(host)
boot.panel = boot.panel
await wait(80)
return { window, document: window.document, host, calls, copied }
}
;(async () => {
// ---------- ① 搜索:输入 → 带 query 重新拉列表 ----------
{
const env = await boot({
items: [{ name: 'a.png', w: 200, h: 300 }, { name: 'b.png', w: 200, h: 120 }],
thumb: () => GOOD,
})
assert.strictEqual(env.calls.list.length, 1, '初次应拉一次')
assert.strictEqual(env.calls.list[0].query, '', '初次 query 应为空')
const search = env.host.querySelector('.iwb-gal-search')
assert.ok(search, '应有搜索框')
search.value = 'a.'
search.dispatchEvent(new env.window.Event('input', { bubbles: true }))
await wait(320) // 防抖 220ms
assert.strictEqual(env.calls.list.length, 2, '输入后应重新拉列表,实际 ' + env.calls.list.length)
assert.strictEqual(env.calls.list[1].query, 'a.', '应把搜索词传给宿主')
}
// ---------- ② 失败统计 + 全部重试 ----------
{
let broken = true
const env = await boot({
items: [{ name: 'ok.png', w: 200, h: 300 }, { name: 'bad.png', w: 200, h: 300 }],
thumb: (n) => (n === 'bad.png' && broken) ? null : GOOD,
})
const fail = env.host.querySelector('.iwb-gal-fail')
assert.ok(fail, '应有失败统计元素')
assert.notStrictEqual(fail.style.display, 'none', '有失败时应显示')
assert.ok(/1 张失败/.test(fail.textContent), '应显示「1 张失败」,实际 ' + fail.textContent)
broken = false
fail.dispatchEvent(new env.window.MouseEvent('click', { bubbles: true }))
await wait(120)
assert.strictEqual(fail.style.display, 'none', '全部重试成功后统计应消失')
// jsdom 不解码图片(img 一直 display:none 等 load),所以断言「不再有失败占位」而不是「看见图」
const stillFailed = Array.from(env.host.querySelectorAll('.iwb-history-missing')).filter((x) => /失败/.test(x.textContent))
assert.strictEqual(stillFailed.length, 0, '重试后不应再有失败占位,实际 ' + stillFailed.length)
assert.strictEqual(env.host.querySelectorAll('.iwb-history-item img').length, 2, '两张都应重新挂上 img')
}
// ---------- ③ 右键菜单 ----------
{
const env = await boot({ items: [{ name: 'a.png', w: 200, h: 300 }], thumb: () => GOOD })
const box = env.host.querySelector('.iwb-history-item')
box.dispatchEvent(new env.window.MouseEvent('contextmenu', { bubbles: true, clientX: 40, clientY: 40 }))
await wait(20)
const menu = env.document.querySelector('.iwb-gal-menu')
assert.ok(menu, '右键应打开菜单')
const labels = Array.from(menu.querySelectorAll('.iwb-ctx-item')).map((b) => b.textContent)
assert.deepStrictEqual(labels, ['插入画布', '下载', '预览大图', '复制文件名', '删除这张'], '菜单项不符:' + labels.join('|'))
// 复制文件名
const copyBtn = Array.from(menu.querySelectorAll('.iwb-ctx-item')).find((b) => b.textContent === '复制文件名')
copyBtn.dispatchEvent(new env.window.MouseEvent('click', { bubbles: true }))
await wait(30)
assert.strictEqual(JSON.stringify(env.copied), JSON.stringify(['a.png']), '应把文件名写入剪贴板')
assert.strictEqual(env.document.querySelector('.iwb-gal-menu'), null, '点击后菜单应关闭')
// 插入画布:走 capability + 设置里的插入尺寸
box.dispatchEvent(new env.window.MouseEvent('contextmenu', { bubbles: true, clientX: 40, clientY: 40 }))
await wait(20)
const insBtn = Array.from(env.document.querySelectorAll('.iwb-gal-menu .iwb-ctx-item')).find((b) => b.textContent === '插入画布')
insBtn.dispatchEvent(new env.window.MouseEvent('click', { bubbles: true }))
await wait(30)
assert.strictEqual(JSON.stringify(env.calls.insert[0][0]), JSON.stringify(['a.png']), '插入应带文件名')
assert.strictEqual(env.calls.insert[0][1].insertK, '1k', '插入应带默认尺寸档')
}
// ---------- ④ 多选拖拽:拖已选中的一张 = 整批 ----------
{
const env = await boot({
items: [{ name: 'a.png', w: 200, h: 300 }, { name: 'b.png', w: 200, h: 300 }, { name: 'c.png', w: 200, h: 300 }],
thumb: () => GOOD,
})
// jsdom 里 img 一直是 display:none(等 load),交互仍然可点,所以不要按可见性过滤
const imgs = Array.from(env.host.querySelectorAll('.iwb-history-item img'))
assert.strictEqual(imgs.length, 3, '三张都应有 img 节点')
// Shift 单击选中 a、b
imgs[0].dispatchEvent(new env.window.MouseEvent('click', { bubbles: true }))
imgs[1].dispatchEvent(new env.window.MouseEvent('click', { bubbles: true, shiftKey: true }))
await wait(40)
assert.strictEqual(env.host.querySelectorAll('.iwb-history-item.iwb-history-selected').length, 2, '应选中 2 张')
const store = {}
const drag = new env.window.Event('dragstart', { bubbles: true })
drag.dataTransfer = { setData: (k, v) => { store[k] = v }, effectAllowed: '' }
env.host.querySelectorAll('.iwb-history-item img')[0].dispatchEvent(drag)
assert.ok(store['application/x-iwb-gallery-batch'], '多选拖拽应带批量 payload')
assert.strictEqual(JSON.parse(store['application/x-iwb-gallery-batch']).length, 2, '批量 payload 应含 2 张')
// 在 document 捕获阶段被插件接管(宿主 drop 契约不变)
const drop = new env.window.Event('drop', { bubbles: true, cancelable: true })
drop.dataTransfer = { getData: (k) => store[k] || '' }
env.document.dispatchEvent(drop)
await wait(40)
assert.strictEqual(env.calls.insert.length, 1, '应触发一次批量插入')
assert.strictEqual(env.calls.insert[0][0].length, 2, '批量插入应含 2 张,实际 ' + env.calls.insert[0][0].length)
}
console.log('gallery ux checks passed(搜索 query / 失败统计+全部重试 / 右键菜单 5 项 / 多选拖拽批量插入)')
})().catch((e) => { console.error(e); process.exit(1) })