'use strict' // 桌面端剪贴板读图(src/clipboard-native.js)载荷解析测试 // 背景(2026-11-25 用户反馈「有时候粘贴不进来」):Rust 侧 clipboard_read_image 有两条来源 —— // · 位图:截图 / ⌘C 复制图片内容 → pngBase64 // · 文件:访达里选中图片文件按 ⌘C → bytesBase64 + mime + name(剪贴板里没有位图) // 前端必须两种都认,且失败时**留下原因**(旧实现把权限/编码/超时全压成 null → 用户只看到一句 // 「剪贴板里没有可粘贴的内容」)。真实字节链路在桌面端实测,这里测映射与降级。 const fs = require('fs') const path = require('path') const root = path.join(__dirname, '..') 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) let replies = [] const win = { btoa: (s) => Buffer.from(s, 'binary').toString('base64'), atob: (s) => Buffer.from(s, 'base64').toString('binary'), Blob: global.Blob, File: global.File, __TAURI__: { core: { invoke: async () => { const r = replies.shift(); if (r instanceof Error) throw r; return r } } }, } new Function('window', 'globalThis', fs.readFileSync(path.join(root, 'src/clipboard-native.js'), 'utf8'))(win, win) const N = win.IWBNativeClipboard ok('模块加载并导出 IWBNativeClipboard', !!N && typeof N.readImage === 'function') eq('桌面端 available()', N.available(), true) const pngB64 = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).toString('base64') ;(async () => { // 1) 位图:pngBase64 → PNG File replies = [{ width: 2, height: 2, pngBase64: pngB64 }] const a = await N.readImage() ok('位图 → 返回 File', !!a && typeof a.arrayBuffer === 'function') eq('位图 → mime 固定为 image/png', a && a.type, 'image/png') ok('位图 → 文件名以 .png 结尾', /\.png$/.test(String(a && a.name))) eq('位图 → 字节长度', a ? (await a.arrayBuffer()).byteLength : -1, 8) // 2) 文件:bytesBase64 + mime + name → 保留原始格式(不在这一层转码) const jpg = Buffer.from([0xff, 0xd8, 0xff, 0xe0]).toString('base64') replies = [{ name: 'IMG_0001.JPG', mime: 'image/jpeg', size: 4, bytesBase64: jpg }] const b = await N.readImage() eq('文件 → 保留原 mime', b && b.type, 'image/jpeg') eq('文件 → 保留原文件名', b && b.name, 'IMG_0001.JPG') eq('文件 → 字节长度', b ? (await b.arrayBuffer()).byteLength : -1, 4) // 3) 两种字段都没有 → null(剪贴板里确实没图) replies = [{}] eq('空载荷 → null', await N.readImage(), null) eq('成功读取后原因清空', N.lastReason(), '') // 4) Rust 报错(文件过大 / 无权限)→ null + 留下原因,供上层拼文案 replies = [new Error('图片文件太大(88 MB,上限 48 MB):请先压缩,或直接把它拖进画布')] eq('读取失败 → null', await N.readImage(), null) ok('读取失败 → lastReason 带上具体原因', N.lastReason().indexOf('太大') > 0) eq('下一次成功读取 → 原因清空', (replies = [{ pngBase64: pngB64 }], await N.readImage(), N.lastReason()), '') // 5) 非桌面端(没有 __TAURI__)→ null,不抛 const win2 = { btoa: win.btoa, atob: win.atob, Blob: global.Blob, File: global.File } new Function('window', 'globalThis', fs.readFileSync(path.join(root, 'src/clipboard-native.js'), 'utf8'))(win2, win2) eq('非桌面端 available()', win2.IWBNativeClipboard.available(), false) eq('非桌面端 readImage() → null', await win2.IWBNativeClipboard.readImage(), null) console.log(fail ? ('\n❌ ' + fail + ' 项失败') : '\n✅ clipboard-native 全部通过(' + pass + ')') process.exit(fail ? 1 : 0) })()