techtools
Головна / proto / flow-old / test / canvas-graph.test.ts

canvas-graph.test.ts

TypeScript · 99 рядків

1// @ts-nocheck — тести перенесено з JS-версії без змін; типи фікстур не перевіряються
2import test from 'node:test'
3import assert from 'node:assert/strict'
4import { duplicateCanvasSelection, rectanglesIntersect, removeCanvasSelection, copyCanvasSelection, pasteCanvasSelection } from '../src/canvasGraph.js'
5
6test('duplicates selected nodes with params and only internal edges', () => {
7  const workflow = {
8    nodes: [
9      { id: 'a', x: 0, y: 0, config: { nested: { value: 1 } }, inputs: [], outputs: [{ id: 'next' }] },
10      { id: 'b', x: 200, y: 0, config: { value: 2 }, inputs: [{ id: 'in' }], outputs: [] },
11      { id: 'c', x: 400, y: 0, config: {}, inputs: [{ id: 'in' }], outputs: [] }
12    ],
13    edges: [
14      { id: 'ab', source: 'a', sourcePort: 'next', target: 'b', targetPort: 'in' },
15      { id: 'bc', source: 'b', sourcePort: 'next', target: 'c', targetPort: 'in' }
16    ]
17  }
18  const ids = ['copy-a', 'copy-b', 'copy-edge']
19  const result = duplicateCanvasSelection(workflow, ['a', 'b'], { createId: () => ids.shift() })
20  assert.deepEqual(result.nodes.map(node => node.id), ['copy-a', 'copy-b'])
21  assert.equal(result.nodes[0].config.nested.value, 1)
22  assert.deepEqual(result.edges, [{ id: 'copy-edge', source: 'copy-a', sourcePort: 'next', target: 'copy-b', targetPort: 'in' }])
23  result.nodes[0].config.nested.value = 9
24  assert.equal(workflow.nodes[0].config.nested.value, 1)
25})
26
27test('duplicates Vue-style reactive proxy graph values', () => {
28  const proxiedNode = new Proxy({ id: 'a', x: 0, y: 0, config: { nested: { value: 1 } } }, {})
29  const proxiedEdge = new Proxy({ id: 'loop', source: 'a', target: 'a' }, {})
30  const workflow = { nodes: [proxiedNode], edges: [proxiedEdge] }
31  const ids = ['copy-a', 'copy-edge']
32
33  const result = duplicateCanvasSelection(workflow, ['a'], { createId: () => ids.shift() })
34
35  assert.equal(result.nodes[0].config.nested.value, 1)
36  assert.equal(result.edges[0].source, 'copy-a')
37  assert.notEqual(result.nodes[0], proxiedNode)
38})
39test('single-block copy omits edges and offsets the copy', () => {
40  const workflow = { nodes: [{ id: 'a', x: 8, y: 16, config: { value: 1 } }], edges: [{ id: 'loop', source: 'a', target: 'a' }] }
41  const result = duplicateCanvasSelection(workflow, ['a'], { offset: { x: 32, y: 32 }, copyInternalEdges: false, createId: () => 'copy-a' })
42  assert.equal(result.nodes[0].x, 40)
43  assert.equal(result.nodes[0].y, 48)
44  assert.equal(result.edges.length, 0)
45})
46
47test('rectangle intersection supports dragging in either direction', () => {
48  const node = { x: 100, y: 100, width: 190, height: 92 }
49  assert.equal(rectanglesIntersect({ x1: 250, y1: 150, x2: 50, y2: 50 }, node), true)
50  assert.equal(rectanglesIntersect({ x1: 0, y1: 0, x2: 90, y2: 90 }, node), false)
51})
52test('removes every selected block and every connected edge', () => {
53  const workflow = {
54    nodes: [{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }],
55    edges: [
56      { id: 'ab', source: 'a', target: 'b' },
57      { id: 'bc', source: 'b', target: 'c' },
58      { id: 'cd', source: 'c', target: 'd' },
59      { id: 'dd', source: 'd', target: 'd' }
60    ]
61  }
62
63  const result = removeCanvasSelection(workflow, ['a', 'b'])
64
65  assert.deepEqual(workflow.nodes.map(node => node.id), ['c', 'd'])
66  assert.deepEqual(workflow.edges.map(edge => edge.id), ['cd', 'dd'])
67  assert.deepEqual(result, { removedNodes: 2, removedEdges: 2 })
68})
69
70test('copies a selected graph and pastes it into another workflow with fresh IDs', () => {
71  const source = {
72    nodes: [
73      { id: 'a', x: 8, y: 16, config: { nested: { value: 1 } } },
74      { id: 'b', x: 208, y: 16, config: { value: 2 } },
75      { id: 'outside', x: 408, y: 16, config: {} }
76    ],
77    edges: [
78      { id: 'ab', source: 'a', target: 'b' },
79      { id: 'external', source: 'b', target: 'outside' }
80    ]
81  }
82  const target = { nodes: [], edges: [] }
83  const clipboard = copyCanvasSelection(source, ['a', 'b'])
84  source.nodes[0].config.nested.value = 9
85  const ids = ['new-a', 'new-b', 'new-edge']
86
87  const result = pasteCanvasSelection(target, clipboard, {
88    offset: { x: 32, y: 32 },
89    createId: () => ids.shift()
90  })
91
92  assert.deepEqual(result.nodes.map(node => [node.id, node.x, node.y]), [
93    ['new-a', 40, 48],
94    ['new-b', 240, 48]
95  ])
96  assert.equal(result.nodes[0].config.nested.value, 1)
97  assert.deepEqual(result.edges, [{ id: 'new-edge', source: 'new-a', target: 'new-b' }])
98})
99