'use strict' 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-actions.js'), 'utf8'), sandbox) const actions = sandbox.CanvasActions assert.ok(actions) const nodes = [ { id: 'a', type: 'prompt', x: 10, y: 20, data: { text: 'A' } }, { id: 'b', type: 'gen', x: 100, y: 120, data: { result: null } }, { id: 'c', type: 'out', x: 300, y: 120, data: {} }, ] const edges = [ { id: 'ab', from: 'a', to: 'b' }, { id: 'bc', from: 'b', to: 'c' }, ] const groups = [{ id: 'g', name: '组', memberIds: ['a', 'b'] }] let serial = 0 const result = actions.duplicateGraph(nodes, edges, groups, ['a', 'b'], { x: 50, y: 60 }, (prefix) => prefix + (++serial)) assert.strictEqual(result.nodes.length, 2) assert.strictEqual(result.edges.length, 1) assert.strictEqual(result.groups.length, 1) assert.strictEqual(result.idMap.a, 'n1') assert.strictEqual(result.idMap.b, 'n2') assert.deepStrictEqual(result.nodes.map((n) => [n.id, n.x, n.y]), [['n1', 60, 80], ['n2', 150, 180]]) assert.strictEqual(result.edges[0].id, 'e3') assert.strictEqual(result.edges[0].from, 'n1') assert.strictEqual(result.edges[0].to, 'n2') assert.strictEqual(result.groups[0].memberIds.join(','), 'n1,n2') assert.notStrictEqual(result.nodes[0].data, nodes[0].data) result.nodes[0].data.text = 'changed' assert.strictEqual(nodes[0].data.text, 'A') assert.strictEqual(edges.length, 2) assert.strictEqual(actions.duplicateGraph(nodes, edges, groups, [], { x: 1, y: 1 }, () => 'x').nodes.length, 0) // dropResult:复制体不带源节点的生成结果(与 duplicateGroup 的 `data.result = null` 对齐)。 // 复制的是「配置/工作流」,产物不该跟过去 —— 否则新节点一出现就显示旧图,用户会以为已经出图了。 const withResult = [{ id: 'a', type: 'gen', x: 0, y: 0, data: { result: [{ promptId: 'p', items: [{ ok: true, url: 'u' }] }] } }] const keepRes = actions.duplicateGraph(withResult, [], [], ['a'], { x: 10, y: 10 }, () => 'k') assert.strictEqual(keepRes.nodes[0].data.result.length, 1, '不传 opts 时保留 result(旧行为不变)') const dropRes = actions.duplicateGraph(withResult, [], [], ['a'], { x: 10, y: 10 }, () => 'd', { dropResult: true }) assert.strictEqual(dropRes.nodes[0].data.result, null, 'dropResult 时复制体不带旧结果图') assert.strictEqual(withResult[0].data.result.length, 1, '源节点不受影响(data 已分片拷贝)') const removed = actions.removeNodes(nodes, edges, groups, ['a']) assert.strictEqual(removed.nodes.length, 2) assert.strictEqual(removed.edges.map((e) => e.id).join(','), 'bc') assert.strictEqual(removed.groups.length, 0) console.log('canvas actions checks passed')