Files
image-workbench-desktop/tests/clipboard-graph-fallback-test.js

44 lines
1.9 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.
const fs = require('fs')
const path = require('path')
const code = fs.readFileSync(path.join(__dirname, '../src/clipboard-graph.js'), 'utf8')
function makeGlobal (withClipboard) {
const calls = []
const g = {
TextEncoder,
TextDecoder,
btoa: (s) => Buffer.from(s, 'binary').toString('base64'),
atob: (s) => Buffer.from(s, 'base64').toString('binary'),
navigator: {},
__TAURI__: { core: { invoke: (cmd, args) => { calls.push({ cmd, args }); return Promise.resolve(true) } } },
console,
}
if (withClipboard) {
g.ClipboardItem = function (items) { this.items = items }
g.Blob = function (parts, opts) { this.parts = parts; this.type = (opts || {}).type }
g.navigator.clipboard = { write: () => Promise.reject(new Error('not allowed')) }
}
return { g, calls }
}
function run (withClipboard) {
const { g, calls } = makeGlobal(withClipboard)
const fn = new Function('window', 'globalThis', code + '\nreturn window.IWBClipboardGraph')
const G = fn(g, g)
const clip = { nodes: [{ id: 'n1', type: 'prompt', text: '两只猫在窗台' }], edges: [] }
return G.write(clip).then((ok) => {
const matched = G.matchPlain(G.plainFor(clip))
return { ok, calls, plain: G.plainFor(clip), matched: !!matched, id: matched && matched.nodes[0].id }
})
}
run(false).then((r) => {
console.log('无 clipboard.write(WKWebView 场景):', JSON.stringify({ ok: r.ok, native: r.calls.map(c => c.cmd), matched: r.matched, id: r.id }))
if (!(r.ok && r.calls.some(c => c.cmd === 'clipboard_write_text') && r.matched)) process.exit(1)
return run(true)
}).then((r) => {
console.log('有 clipboard.write 且被拒绝:', JSON.stringify({ ok: r.ok, native: r.calls.map(c => c.cmd), matched: r.matched }))
if (!(r.ok && r.matched)) process.exit(1)
console.log('PASS 图剪贴板兜底(原生写文本 + 摘要匹配)')
}).catch((e) => { console.error('FAIL', e); process.exit(1) })