'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('') async function boot (opts) { const dom = new JSDOM('
', { 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 () => { // v1.6.1:搜索框已移除(用户反馈没意义)——只守失败统计/右键菜单/多选拖拽 // ---------- ② 失败统计 + 全部重试 ---------- { 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) } // ---------- ⑤ 图幅=显示宽度上限(v1.6.2 不再拉满面板;v1.6.3 新增极小 64)---------- { const env = await boot({ items: Array.from({ length: 6 }, (_, i) => ({ name: 'p' + i + '.png', w: 200, h: 300 })), thumb: () => GOOD, }) const widths = () => Array.from(env.host.querySelectorAll('.iwb-history-col')).map((c) => c.style.width) // v1.6.6:列宽改为「均分铺满」——图幅只决定列数;各列宽度应一致且总宽 ≈ 容器宽 const wNums = widths().map((w) => parseInt(w, 10)) assert.ok(wNums.length >= 2, '应至少 2 列,实际 ' + wNums.length) assert.ok(wNums.every((w) => w === wNums[0]), '各列宽度应一致,实际 ' + wNums.join(',')) assert.ok(wNums[0] > 40, '列宽应铺满可用宽度,实际 ' + wNums[0]) const cols = env.host.querySelectorAll('.iwb-history-col').length assert.ok(cols >= 2, '应至少 2 列,实际 ' + cols) // 切到「特大」=200px:列宽应变 200 env.host.querySelector('.iwb-gal-setbtn').dispatchEvent(new env.window.MouseEvent('click', { bubbles: true })) const panel = env.host.querySelector('.iwb-gal-settings') const tileSel = panel.querySelectorAll('select')[1] tileSel.value = '200' tileSel.dispatchEvent(new env.window.Event('change')) await wait(60) { const big = widths().map((w) => parseInt(w, 10)); assert.ok(big.every((w) => w === big[0] && w > 40), '切到特大后各列仍应等宽铺满,实际 ' + big.join(',')) } // v1.6.3 新增「极小」=64px:列宽 64,且列数比「特大」更多(图幅小 = 一行看更多) tileSel.value = '64' tileSel.dispatchEvent(new env.window.Event('change')) await wait(60) assert.ok(widths().length >= 3, '极小图幅应该更多列,实际 ' + widths().join(',')) assert.ok(env.host.querySelectorAll('.iwb-history-col').length > cols, '极小的列数应多于默认列数') } console.log('gallery ux checks passed(失败统计+全部重试 / 右键菜单 5 项 / 多选拖拽批量插入 / 图幅列宽上限含 64 档)') })().catch((e) => { console.error(e); process.exit(1) })