143 lines
7.3 KiB
JavaScript
143 lines
7.3 KiB
JavaScript
'use strict'
|
||
// 统一剪贴板的序列化(src/clipboard-graph.js)—— 独立文件
|
||
// 背景(用户 2026-11-11 二次裁定):复制节点要能直接用 Ctrl+V 贴回来,不再要求 Ctrl+Shift+V。
|
||
// 做法是把节点组写进系统剪贴板的 text/html(带 iwb:graph 标记)+ text/plain(人话摘要)。
|
||
// 这个文件守的是「标记能不能原样往返」—— 一旦丢字/丢节点,Ctrl+V 就会贴出错节点。
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
const root = path.join(__dirname, '..')
|
||
const SRC = fs.readFileSync(path.join(root, 'src/clipboard-graph.js'), 'utf8')
|
||
const box = {}
|
||
new Function('window', 'globalThis', SRC)(box, box)
|
||
const G = box.IWBClipboardGraph
|
||
|
||
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)
|
||
|
||
const mkClip = (over) => Object.assign({
|
||
app: 'image-workbench', type: 'iwb-clipboard', at: 1760000000000,
|
||
nodes: [
|
||
{ id: 'n1', type: 'prompt', x: 0, y: 0, data: { text: '一只赛博朋克风格的猫', title: '' } },
|
||
{ id: 'n2', type: 'ref', x: 300, y: 0, data: { url: '', data: null, name: 'ref.png', localName: null } },
|
||
],
|
||
edges: [{ id: 'e1', from: 'n1', to: 'n2' }],
|
||
groups: [{ id: 'g1', name: '分组 1', memberIds: ['n1', 'n2'] }],
|
||
}, over || {})
|
||
|
||
// ---- ① 导出 ----
|
||
ok('模块导出完整', !!G && typeof G.htmlFor === 'function' && typeof G.decode === 'function' && typeof G.write === 'function')
|
||
eq('标记常量', G.MARK, 'iwb:graph:v1:')
|
||
|
||
// ---- ② 往返 ----
|
||
{
|
||
const c = mkClip()
|
||
const html = G.htmlFor(c)
|
||
ok('htmlFor 含标记', html.indexOf('iwb:graph:v1:') >= 0)
|
||
const back = G.decode(html)
|
||
ok('decode 能解回', !!back)
|
||
eq('节点数一致', back && back.nodes.length, 2)
|
||
eq('连线数一致', back && back.edges.length, 1)
|
||
eq('分组数一致', back && back.groups.length, 1)
|
||
eq('中文提示词不丢字', back && back.nodes[0].data.text, '一只赛博朋克风格的猫')
|
||
eq('分组 id 保留', back && back.groups[0].memberIds.join(','), 'n1,n2')
|
||
eq('at 时间戳保留', back && back.at, 1760000000000)
|
||
}
|
||
|
||
// ---- ③ 中文必须能编码:btoa 只吃 latin1,直接 btoa(JSON) 会抛 InvalidCharacterError ----
|
||
{
|
||
let threw = null
|
||
let html = ''
|
||
try { html = G.htmlFor(mkClip()) } catch (e) { threw = e }
|
||
ok('中文不会让编码抛异常(btoa latin1 坑)', threw === null)
|
||
ok('中文往返后仍是中文', (G.decode(html) || { nodes: [{ data: {} }] }).nodes[0].data.text === '一只赛博朋克风格的猫')
|
||
}
|
||
|
||
// ---- ④ Chrome 读回来会把 fragment 包成完整文档:decode 不能假设以注释开头 ----
|
||
{
|
||
const html = G.htmlFor(mkClip())
|
||
const wrapped = '<html><head></head><body>' + html + '</body></html>'
|
||
eq('被 <html> 包裹后仍能解', (G.decode(wrapped) || {}).nodes.length, 2)
|
||
eq('前面有别的节点也能解', (G.decode('<div>无关内容</div>' + html) || {}).nodes.length, 2)
|
||
}
|
||
|
||
// ---- ⑤ 坏输入一律返回 null,绝不抛 ----
|
||
eq('没有标记 → null', G.decode('<div>普通网页内容</div>'), null)
|
||
eq('空字符串 → null', G.decode(''), null)
|
||
eq('null → null', G.decode(null), null)
|
||
eq('标记后跟垃圾 → null', G.decode('<!--iwb:graph:v1:@@@@-->'), null)
|
||
eq('base64 但 JSON 坏 → null', G.decode('<!--iwb:graph:v1:' + G.encodeText('{not json') + '-->'), null)
|
||
eq('nodes 为空数组 → null', G.decode('<!--iwb:graph:v1:' + G.encodeText(JSON.stringify({ nodes: [] })) + '-->'), null)
|
||
eq('nodes 不是数组 → null', G.decode('<!--iwb:graph:v1:' + G.encodeText(JSON.stringify({ nodes: 'x' })) + '-->'), null)
|
||
eq('标记没闭合(被截断)仍尽力解出', (G.decode('<!--iwb:graph:v1:' + G.encodeText(JSON.stringify({ nodes: [{ id: 'a' }] }))) || {}).nodes.length, 1)
|
||
|
||
// ---- ⑥ text/plain 是给别的应用看的「人话」 ----
|
||
{
|
||
eq('有提示词 → 就是提示词全文', G.plainFor(mkClip()), '一只赛博朋克风格的猫')
|
||
eq('没有提示词 → N 个节点', G.plainFor({ nodes: [{ type: 'ref', data: {} }] }), '1 个节点')
|
||
eq('带分组名', G.plainFor({ nodes: [{ type: 'ref', data: {} }, { type: 'ref', data: {} }], groups: [{ name: '分组 1' }] }), '2 个节点(分组 1)')
|
||
const withB64 = G.plainFor({ nodes: [{ type: 'ref', data: { data: 'data:image/png;base64,' + 'A'.repeat(5000) } }] })
|
||
ok('绝不把图片 base64 塞进 text/plain(旧实现被撤回的原因)', withB64.indexOf('base64') < 0 && withB64.length < 40)
|
||
const long = G.plainFor({ nodes: [{ type: 'prompt', data: { text: 'x'.repeat(50000) } }] })
|
||
ok('超长提示词有上限(不把 5 万字塞进剪贴板)', long.length === 20000)
|
||
}
|
||
|
||
// ---- ⑦ htmlFor 里的摘要要转义,不能把提示词当标签 ----
|
||
{
|
||
const html = G.htmlFor({ nodes: [{ type: 'prompt', data: { text: '<img src=x onerror=alert(1)>' } }] })
|
||
ok('摘要里的尖括号被转义', html.indexOf('<img src=x') < 0 && html.indexOf('<img') >= 0)
|
||
eq('转义后仍能解回', (G.decode(html) || {}).nodes.length, 1)
|
||
}
|
||
|
||
// ---- ⑧ 体积:大 payload 不能爆栈(String.fromCharCode.apply 有参数上限)----
|
||
{
|
||
const big = mkClip({
|
||
nodes: [{ id: 'big', type: 'prompt', x: 0, y: 0, data: { text: '中文测试'.repeat(60000) } }],
|
||
edges: [], groups: [],
|
||
})
|
||
let threw = null, back = null
|
||
try { back = G.decode(G.htmlFor(big)) } catch (e) { threw = e }
|
||
ok('1MB 级中文 payload 编解码不抛(分块 base64)', threw === null)
|
||
eq('大 payload 节点数一致', back && back.nodes.length, 1)
|
||
eq('大 payload 文本长度一致', back && back.nodes[0].data.text.length, '中文测试'.repeat(60000).length)
|
||
}
|
||
|
||
// ---- ⑨ write:环境不支持时降级为 false(不 reject、不抛)----
|
||
;(async () => {
|
||
{
|
||
const r = await G.write(mkClip())
|
||
eq('没有 navigator.clipboard → false', r, false)
|
||
}
|
||
{
|
||
const b2 = {}
|
||
new Function('window', 'globalThis', SRC)(b2, b2)
|
||
const G2 = b2.IWBClipboardGraph
|
||
b2.navigator = { clipboard: { write: () => Promise.resolve() } }
|
||
b2.ClipboardItem = function (data) { this.data = data }
|
||
b2.Blob = function (parts, opts) { this.parts = parts; this.type = opts && opts.type }
|
||
const r = await G2.write(mkClip())
|
||
eq('能写时返回 true', r, true)
|
||
}
|
||
{
|
||
const b3 = {}
|
||
new Function('window', 'globalThis', SRC)(b3, b3)
|
||
const G3 = b3.IWBClipboardGraph
|
||
b3.navigator = { clipboard: { write: () => Promise.reject(new DOMException('denied', 'NotAllowedError')) } }
|
||
b3.ClipboardItem = function (data) { this.data = data }
|
||
b3.Blob = function (parts, opts) { this.parts = parts; this.type = opts && opts.type }
|
||
eq('写入被拒 → false(不抛)', await G3.write(mkClip()), false)
|
||
}
|
||
{
|
||
const b4 = {}
|
||
new Function('window', 'globalThis', SRC)(b4, b4)
|
||
const G4 = b4.IWBClipboardGraph
|
||
b4.navigator = { clipboard: { write: () => { throw new Error('boom') } } }
|
||
b4.ClipboardItem = function (data) { this.data = data }
|
||
b4.Blob = function (parts, opts) { this.parts = parts; this.type = opts && opts.type }
|
||
eq('写入同步抛错 → false(不抛)', await G4.write(mkClip()), false)
|
||
}
|
||
|
||
console.log('\n' + (fail ? '❌' : '✅') + ' clipboard-graph: ' + pass + ' passed, ' + fail + ' failed')
|
||
if (fail) process.exit(1)
|
||
})()
|