File size: 8,247 Bytes
8a37e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { deepClone } from 'common/util/deepClone';
import {
  getFirstValidConnection,
  getSourceCandidateFields,
  getTargetCandidateFields,
} from 'features/nodes/store/util/getFirstValidConnection';
import { add, buildEdge, buildNode, img_resize, templates } from 'features/nodes/store/util/testUtils';
import { unset } from 'lodash-es';
import { describe, expect, it } from 'vitest';

describe('getFirstValidConnection', () => {
  it('should return null if the pending and candidate nodes are the same node', () => {
    const n = buildNode(add);
    expect(getFirstValidConnection(n.id, 'value', n.id, null, [n], [], templates, null)).toBe(null);
  });

  it('should return null if the sourceHandle and targetHandle are null', () => {
    const n1 = buildNode(add);
    const n2 = buildNode(add);
    expect(getFirstValidConnection(n1.id, null, n2.id, null, [n1, n2], [], templates, null)).toBe(null);
  });

  it('should return itself if both sourceHandle and targetHandle are provided', () => {
    const n1 = buildNode(add);
    const n2 = buildNode(add);
    expect(getFirstValidConnection(n1.id, 'value', n2.id, 'a', [n1, n2], [], templates, null)).toEqual({
      source: n1.id,
      sourceHandle: 'value',
      target: n2.id,
      targetHandle: 'a',
    });
  });

  describe('connecting from a source to a target', () => {
    const n1 = buildNode(img_resize);
    const n2 = buildNode(img_resize);

    it('should return the first valid connection if there are no connected fields', () => {
      const r = getFirstValidConnection(n1.id, 'width', n2.id, null, [n1, n2], [], templates, null);
      const c = {
        source: n1.id,
        sourceHandle: 'width',
        target: n2.id,
        targetHandle: 'width',
      };
      expect(r).toEqual(c);
    });
    it('should return the first valid connection if there is a connected field', () => {
      const e = buildEdge(n1.id, 'height', n2.id, 'width');
      const r = getFirstValidConnection(n1.id, 'width', n2.id, null, [n1, n2], [e], templates, null);
      const c = {
        source: n1.id,
        sourceHandle: 'width',
        target: n2.id,
        targetHandle: 'height',
      };
      expect(r).toEqual(c);
    });
    it('should return the first valid connection if there is an edgePendingUpdate', () => {
      const e = buildEdge(n1.id, 'width', n2.id, 'width');
      const r = getFirstValidConnection(n1.id, 'width', n2.id, null, [n1, n2], [e], templates, e);
      const c = {
        source: n1.id,
        sourceHandle: 'width',
        target: n2.id,
        targetHandle: 'width',
      };
      expect(r).toEqual(c);
    });
    it('should return null if the target has no valid fields', () => {
      const e1 = buildEdge(n1.id, 'width', n2.id, 'width');
      const e2 = buildEdge(n1.id, 'height', n2.id, 'height');
      const n3 = buildNode(add);
      const r = getFirstValidConnection(n3.id, 'value', n2.id, null, [n1, n2, n3], [e1, e2], templates, null);
      expect(r).toEqual(null);
    });
  });

  describe('connecting from a target to a source', () => {
    const n1 = buildNode(img_resize);
    const n2 = buildNode(img_resize);

    it('should return the first valid connection if there are no connected fields', () => {
      const r = getFirstValidConnection(n1.id, null, n2.id, 'width', [n1, n2], [], templates, null);
      const c = {
        source: n1.id,
        sourceHandle: 'width',
        target: n2.id,
        targetHandle: 'width',
      };
      expect(r).toEqual(c);
    });
    it('should return the first valid connection if there is a connected field', () => {
      const e = buildEdge(n1.id, 'height', n2.id, 'width');
      const r = getFirstValidConnection(n1.id, null, n2.id, 'height', [n1, n2], [e], templates, null);
      const c = {
        source: n1.id,
        sourceHandle: 'width',
        target: n2.id,
        targetHandle: 'height',
      };
      expect(r).toEqual(c);
    });
    it('should return the first valid connection if there is an edgePendingUpdate', () => {
      const e = buildEdge(n1.id, 'width', n2.id, 'width');
      const r = getFirstValidConnection(n1.id, null, n2.id, 'width', [n1, n2], [e], templates, e);
      const c = {
        source: n1.id,
        sourceHandle: 'width',
        target: n2.id,
        targetHandle: 'width',
      };
      expect(r).toEqual(c);
    });
    it('should return null if the target has no valid fields', () => {
      const e1 = buildEdge(n1.id, 'width', n2.id, 'width');
      const e2 = buildEdge(n1.id, 'height', n2.id, 'height');
      const n3 = buildNode(add);
      const r = getFirstValidConnection(n3.id, null, n2.id, 'a', [n1, n2, n3], [e1, e2], templates, null);
      expect(r).toEqual(null);
    });
  });
});

describe('getTargetCandidateFields', () => {
  it('should return an empty array if the nodes canot be found', () => {
    const r = getTargetCandidateFields('missing', 'value', 'missing', [], [], templates, null);
    expect(r).toEqual([]);
  });
  it('should return an empty array if the templates cannot be found', () => {
    const n1 = buildNode(add);
    const n2 = buildNode(add);
    const nodes = [n1, n2];
    const r = getTargetCandidateFields(n1.id, 'value', n2.id, nodes, [], {}, null);
    expect(r).toEqual([]);
  });
  it('should return an empty array if the source field template cannot be found', () => {
    const n1 = buildNode(add);
    const n2 = buildNode(add);
    const nodes = [n1, n2];

    const addWithoutOutputValue = deepClone(add);
    unset(addWithoutOutputValue, 'outputs.value');

    const r = getTargetCandidateFields(n1.id, 'value', n2.id, nodes, [], { add: addWithoutOutputValue }, null);
    expect(r).toEqual([]);
  });
  it('should return all valid target fields if there are no connected fields', () => {
    const n1 = buildNode(img_resize);
    const n2 = buildNode(img_resize);
    const nodes = [n1, n2];
    const r = getTargetCandidateFields(n1.id, 'width', n2.id, nodes, [], templates, null);
    expect(r).toEqual([img_resize.inputs['width'], img_resize.inputs['height']]);
  });
  it('should ignore the edgePendingUpdate if provided', () => {
    const n1 = buildNode(img_resize);
    const n2 = buildNode(img_resize);
    const nodes = [n1, n2];
    const edgePendingUpdate = buildEdge(n1.id, 'width', n2.id, 'width');
    const r = getTargetCandidateFields(n1.id, 'width', n2.id, nodes, [], templates, edgePendingUpdate);
    expect(r).toEqual([img_resize.inputs['width'], img_resize.inputs['height']]);
  });
});

describe('getSourceCandidateFields', () => {
  it('should return an empty array if the nodes canot be found', () => {
    const r = getSourceCandidateFields('missing', 'value', 'missing', [], [], templates, null);
    expect(r).toEqual([]);
  });
  it('should return an empty array if the templates cannot be found', () => {
    const n1 = buildNode(add);
    const n2 = buildNode(add);
    const nodes = [n1, n2];
    const r = getSourceCandidateFields(n2.id, 'a', n1.id, nodes, [], {}, null);
    expect(r).toEqual([]);
  });
  it('should return an empty array if the source field template cannot be found', () => {
    const n1 = buildNode(add);
    const n2 = buildNode(add);
    const nodes = [n1, n2];

    const addWithoutInputA = deepClone(add);
    unset(addWithoutInputA, 'inputs.a');

    const r = getSourceCandidateFields(n1.id, 'a', n2.id, nodes, [], { add: addWithoutInputA }, null);
    expect(r).toEqual([]);
  });
  it('should return all valid source fields if there are no connected fields', () => {
    const n1 = buildNode(img_resize);
    const n2 = buildNode(img_resize);
    const nodes = [n1, n2];
    const r = getSourceCandidateFields(n2.id, 'width', n1.id, nodes, [], templates, null);
    expect(r).toEqual([img_resize.outputs['width'], img_resize.outputs['height']]);
  });
  it('should ignore the edgePendingUpdate if provided', () => {
    const n1 = buildNode(img_resize);
    const n2 = buildNode(img_resize);
    const nodes = [n1, n2];
    const edgePendingUpdate = buildEdge(n1.id, 'width', n2.id, 'width');
    const r = getSourceCandidateFields(n2.id, 'width', n1.id, nodes, [], templates, edgePendingUpdate);
    expect(r).toEqual([img_resize.outputs['width'], img_resize.outputs['height']]);
  });
});