Files
image-workbench-desktop/tests/clipboard-kind-test.js
T

100 lines
7.5 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.
'use strict'
// 剪贴板内容判定(src/clipboard-kind.js)行为测试 —— 独立文件
// 背景(用户 2026-11-11 反馈):粘 PDF / 视频时既不认成图片也不认成文本,
// 判定落进「Ctrl+V 无 paste 事件 → 回退贴节点组」分支,把上一条复制的节点组贴到了画布上。
// 关键断言:非图片文件必须单独成一格(kind=files),且**不会被当成文本**。
const fs = require('fs')
const path = require('path')
const root = path.join(__dirname, '..')
const SRC = fs.readFileSync(path.join(root, 'src/clipboard-kind.js'), 'utf8')
const box = {}
// v3.24.74:图片准入判据外置到 src/image-admit.js,classify 通过 window.IWBImageAdmit 取用
// ⇒ 造沙箱时必须先把它挂上,否则测的是「模块缺失时的旧兜底路径」,新判据等于没测。
new Function('window', 'globalThis', fs.readFileSync(path.join(root, 'src/image-admit.js'), 'utf8'))(box, box)
new Function('window', 'globalThis', SRC)(box, box)
const K = box.IWBClipboardKind
let pass = 0, fail = 0
const ok = (name, cond) => { if (cond) { pass++; console.log(' ✅ ' + name) } else { fail++; console.log(' ❌ ' + name) } }
const eq = (name, got, want) => ok(name + '(' + JSON.stringify(got) + ' === ' + JSON.stringify(want) + ')', got === want)
// 造 File 形状的假对象(classify 只读 name / size / type)
const fakeFile = (name, type, size) => ({ name, type: type || '', size: size == null ? 1024 : size })
// 造 DataTransfer 形状;items 默认由 files 推导(模拟浏览器的标准行为)
function dt(opts) {
const o = opts || {}
const files = o.files || []
const items = o.items || files.map((f) => ({ kind: 'file', type: f.type, getAsFile: () => f }))
return {
items,
files: o.filesOnly || files,
getData: (t) => (t === 'text/plain' ? (o.text || '') : ''),
}
}
ok('模块加载并导出 IWBClipboardKind', !!K && typeof K.classify === 'function')
// ---- ① 空的 ----
eq('空剪贴板 → empty', K.classify(dt({})).kind, 'empty')
eq('只有空白字符 → empty', K.classify(dt({ text: ' \n\t ' })).kind, 'empty')
eq('无 clipboardData → empty', K.classify(null).kind, 'empty')
// ---- ② 纯文本 ----
eq('中文纯文本 → text', K.classify(dt({ text: '一只猫在太空' })).kind, 'text')
eq('纯文本保留原文(含换行)', K.classify(dt({ text: 'a\nb' })).text, 'a\nb')
// ---- ③ 图片文件 ----
eq('单张 image/png → image', K.classify(dt({ files: [fakeFile('a.png', 'image/png')] })).kind, 'image')
eq('多张图片全给(旧实现只取第一张)', K.classify(dt({ files: [fakeFile('a.png', 'image/png'), fakeFile('b.jpg', 'image/jpeg')] })).images.length, 2)
eq('image/svg+xml 也算图片', K.classify(dt({ files: [fakeFile('a.svg', 'image/svg+xml')] })).kind, 'image')
eq('图片优先于同批文本(截图粘贴常带空 text/plain)', K.classify(dt({ files: [fakeFile('a.png', 'image/png')], text: '随手写的备注' })).kind, 'image')
// 浏览器差异:个别实现只填 dt.files,items 为空
const onlyFiles = { items: [], files: [fakeFile('a.png', 'image/png')], getData: () => '' }
eq('items 为空、只看 dt.files 也能认出图片', K.classify(onlyFiles).kind, 'image')
// 同一文件同时出现在 items 与 files 时不重复
const dup = fakeFile('a.png', 'image/png')
eq('items 与 files 同一文件 → 不重复', K.classify({ items: [{ kind: 'file', type: 'image/png', getAsFile: () => dup }], files: [dup], getData: () => '' }).images.length, 1)
// ---- ④ 非图片文件(本次修复的核心)----
eq('单个 PDF → files', K.classify(dt({ files: [fakeFile('合同.pdf', 'application/pdf')] })).kind, 'files')
eq('视频 → files', K.classify(dt({ files: [fakeFile('a.mp4', 'video/mp4')] })).kind, 'files')
eq('压缩包 → files', K.classify(dt({ files: [fakeFile('a.zip', 'application/zip')] })).kind, 'files')
eq('带文件名的文本也不当提示词(files 优先于 text)', K.classify(dt({ files: [fakeFile('a.pdf', 'application/pdf')], text: 'a.pdf' })).kind, 'files')
eq('文件名可用于提示', K.classify(dt({ files: [fakeFile('合同.pdf', 'application/pdf')] })).name, '合同.pdf')
eq('图片 + PDF 混着粘 → 图片优先', K.classify(dt({ files: [fakeFile('a.png', 'image/png'), fakeFile('a.pdf', 'application/pdf')] })).kind, 'image')
eq('无 MIME 的未知文件 → files(不猜成图片)', K.classify(dt({ files: [fakeFile('blob', '')] })).kind, 'files')
// ---- ④b 图片准入(v3.24.74):MIME 不可靠时按扩展名承认;解不了的格式必须单独拒 ----
// 实测场景:Windows 资源管理器 / 部分网盘「复制文件」不填 type。旧判据把它们划进「非图片文件」,
// 提示还写「图片请先转成 PNG/JPG」——用户粘的正是 PNG。
eq('type 为空 + .png → image(旧判据当非图片)', K.classify(dt({ files: [fakeFile('a.png', '')] })).kind, 'image')
eq('octet-stream + .jpg → image', K.classify(dt({ files: [fakeFile('a.jpg', 'application/octet-stream')] })).kind, 'image')
eq('大小写扩展名 .PNG 也认', K.classify(dt({ files: [fakeFile('A.PNG', '')] })).kind, 'image')
// HEIC/PSD 明确「是图片但浏览器解不了」:必须落进 files(不可静默、也不可落一张坏图)
eq('image/heic → files(不是 image)', K.classify(dt({ files: [fakeFile('IMG_0001.HEIC', 'image/heic')] })).kind, 'files')
eq('type 为空 + .heic → files(扩展名兜底)', K.classify(dt({ files: [fakeFile('IMG_0001.heic', '')] })).kind, 'files')
eq('.psd → files', K.classify(dt({ files: [fakeFile('poster.psd', '')] })).kind, 'files')
eq('HEIC + PNG 混着粘 → 只收 PNG(坏图不占坑)', K.classify(dt({ files: [fakeFile('a.heic', 'image/heic'), fakeFile('b.png', 'image/png')] })).images.length, 1)
eq('HEIC + PNG 混着粘 → 被拒的那张记在 files 里(提示要用)', K.classify(dt({ files: [fakeFile('a.heic', 'image/heic'), fakeFile('b.png', 'image/png')] })).files.length, 1)
// ---- ⑤ 图片链接 ----
eq('.png 链接 → image-url', K.classify(dt({ text: 'https://x.com/a.png' })).kind, 'image-url')
eq('带 query 的 .jpg 链接 → image-url', K.classify(dt({ text: 'https://x.com/a.jpg?w=800' })).kind, 'image-url')
eq('无扩展名的 pinimg 链接 → image-url', K.classify(dt({ text: 'https://i.pinimg.com/736x/ab/cd/ef' })).kind, 'image-url')
eq('普通网页链接 → text(不误判成图片)', K.classify(dt({ text: 'https://example.com/article' })).kind, 'text')
eq('多行文本里含链接 → text', K.classify(dt({ text: '看图 https://x.com/a.png' })).kind, 'text')
// ---- ⑥ data:image 文本 ----
const realDataUrl = 'data:image/png;base64,' + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/iZk9HQAAAABJRU5ErkJggg=='
eq('完整 dataURL → image-data', K.classify(dt({ text: realDataUrl })).kind, 'image-data')
eq('正文里提到 data:image 的短句 → text(不误判)', K.classify(dt({ text: 'data:image/png;base64, 这种格式' })).kind, 'text')
eq('isDataImage 对短串返回 false', K.isDataImage('data:image/png;base64,AAA'), false)
eq('isDataImage 对真 dataURL 返回 true', K.isDataImage(realDataUrl), true)
// ---- ⑦ 判定辅助 ----
eq('isImageUrl(.php) 为假', K.isImageUrl('https://x.com/a.php'), false)
eq('isImageUrl(blob:) 为假', K.isImageUrl('blob:https://x.com/6f1c'), false)
eq('textOf 不因 dt.getData 抛错而崩', K.textOf({ getData: () => { throw new Error('boom') } }), '')
console.log('\n clipboard-kind: ' + pass + ' passed, ' + fail + ' failed')
if (fail) process.exit(1)