Files

57 lines
3.1 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.
'use strict'
// tests/canvas-ops.js 的单测(P2-9 快照变换:Workbench 的 deleteNodes/copyNodes 纯核 → src/)
// 注意:vm 沙箱是另一个 realm,数组/对象的原型不同 ⇒ 一律用 join(',') / JSON.stringify 比较,不用 deepStrictEqual。
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const root = path.resolve(__dirname, '..')
const sandbox = {}
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'canvas-ops.js'), 'utf8'), sandbox)
const M = sandbox.IWBCanvasOps
assert.ok(M && typeof M.create === 'function')
const calls = []
const api = M.create({ stripBigValueSync: (value, nodeId, k, write) => { calls.push([nodeId, k, write]); return { __stripped: true } } })
// deleteNodesOf:删边 + 失入边 textout 清空 + 组员过滤(<2 丢弃)
const nodes = [
{ id: 'a', type: 'ref', x: 10, y: 20, data: { url: 'u' } },
{ id: 'b', type: 'prompt', x: 30, y: 40, data: { text: 't' } },
{ id: 'c', type: 'textout', x: 50, y: 60, data: { text: 'keep' } },
{ id: 'd', type: 'textout', x: 70, y: 80, data: { text: 'clear' } },
]
const edges = [{ id: 'e1', from: 'a', to: 'b' }, { id: 'e2', from: 'b', to: 'c' }]
const groups = [{ id: 'g1', memberIds: ['a', 'b', 'c'] }, { id: 'g2', memberIds: ['a', 'b'] }]
const del = api.deleteNodesOf(nodes, edges, groups, ['b'])
assert.strictEqual(del.nodes.map((n) => n.id).join(','), 'a,c,d')
assert.strictEqual(del.edges.length, 0, 'b 的入/出边都应删除')
assert.strictEqual(del.nodes.find((n) => n.id === 'c').data.text, '', '失去入边的 textout 应清空')
assert.strictEqual(del.nodes.find((n) => n.id === 'd').data.text, '', '无入边的 textout 应清空')
assert.strictEqual(del.nodes.find((n) => n.id === 'a').data.url, 'u', '非 textout 不动')
assert.strictEqual(del.groups.length, 1, '<2 成员的组应丢弃')
assert.strictEqual(JSON.stringify(del.groups[0]), JSON.stringify({ id: 'g1', memberIds: ['a', 'c'] }))
// clipboardOf:相对坐标 + 内部边/组过滤 + 大图剥离注入
const clip = api.clipboardOf(nodes, edges, groups, ['a', 'b'])
assert.strictEqual(clip.type, 'iwb-clipboard')
assert.strictEqual(clip.nodes.length, 2)
assert.strictEqual(clip.nodes.find((n) => n.id === 'a').x, 0)
assert.strictEqual(clip.nodes.find((n) => n.id === 'b').x, 20)
assert.strictEqual(clip.nodes.find((n) => n.id === 'a').y, 0)
assert.strictEqual(JSON.stringify(clip.nodes[0].data), JSON.stringify({ __stripped: true }), '剥离助手应被调用')
assert.strictEqual(calls[0].join(','), 'a,data,true')
assert.strictEqual(clip.edges.length, 1, '只保留选中集合内部边')
assert.strictEqual(clip.groups.length, 2, '含选中成员的组都应保留')
assert.strictEqual(clip.groups[0].memberIds.join(','), 'a,b', '组员只保留选中集合内的')
assert.strictEqual(api.clipboardOf(nodes, edges, groups, []), null, '空选择返回 null')
// 无注入时退化为原样(不崩)
const bare = M.create({}).clipboardOf([{ id: 'x', type: 'ref', x: 5, y: 5, data: { a: 1 } }], [], [], ['x'])
assert.strictEqual(JSON.stringify(bare.nodes[0].data), JSON.stringify({ a: 1 }))
console.log('canvas ops checks passed')