-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathgraphTraversalUtil.test.ts
More file actions
1588 lines (1303 loc) · 51.7 KB
/
Copy pathgraphTraversalUtil.test.ts
File metadata and controls
1588 lines (1303 loc) · 51.7 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// oxlint-disable no-misused-spread
import { describe, expect, it, vi } from 'vitest'
import type {
LGraph,
LGraphNode,
Subgraph
} from '@/lib/litegraph/src/litegraph'
import {
collectAllNodes,
collectFromNodes,
findNodeInHierarchy,
findSubgraphByUuid,
forEachNode,
forEachSubgraphNode,
getAllNonIoNodesInSubgraph,
getExecutionIdsForSelectedNodes,
getLocalNodeIdFromExecutionId,
getNodeByExecutionId,
getNodeByLocatorId,
getRootGraph,
getSubgraphPathFromExecutionId,
getExecutionIdFromNodeData,
mapAllNodes,
mapSubgraphNodes,
parseExecutionId,
traverseNodesDepthFirst,
traverseSubgraphPath,
triggerCallbackOnAllNodes,
visitGraphNodes,
getExecutionIdByNode,
getExecutionIdForNodeInGraph,
isAncestorPathActive,
isCandidateScopeActive,
isExecutionPathActive,
isMissingCandidateActive
} from '@/utils/graphTraversalUtil'
import { LGraphEventMode } from '@/lib/litegraph/src/types/globalEnums'
import { toNodeId } from '@/types/nodeId'
import { createMockLGraphNode } from './__tests__/litegraphTestUtils'
// Mock node factory
function createMockNode(
id: string | number,
options: {
isSubgraph?: boolean
subgraph?: Subgraph
callback?: () => void
graph?: LGraph
} = {}
): LGraphNode {
const node = createMockLGraphNode({
id: toNodeId(id),
isSubgraphNode: options.isSubgraph ? () => true : undefined,
subgraph: options.subgraph,
onExecutionStart: options.callback,
graph: options.graph
}) satisfies Partial<LGraphNode> as LGraphNode
options.graph?.nodes?.push(node)
return node
}
// Mock graph factory
function createMockGraph(nodes: LGraphNode[]): LGraph {
return {
_nodes: nodes,
nodes: nodes,
isRootGraph: true,
getNodeById: (id) => nodes.find((n) => String(n.id) === String(id)) || null
} satisfies Partial<LGraph> as LGraph
}
// Mock subgraph factory
function createMockSubgraph(
id: string,
nodes: LGraphNode[],
rootGraph?: LGraph
): Subgraph {
const graph = {
id,
_nodes: nodes,
nodes: nodes,
isRootGraph: false,
rootGraph,
getNodeById: (nodeId) =>
nodes.find((n) => String(n.id) === String(nodeId)) || null
} satisfies Partial<Subgraph> as Subgraph
return graph
}
describe('graphTraversalUtil', () => {
describe('invalid raw node IDs', () => {
it('returns null instead of throwing', () => {
const graph = createMockGraph([])
expect(findNodeInHierarchy(graph, '')).toBeNull()
expect(getExecutionIdForNodeInGraph(graph, graph, '')).toBeNull()
expect(getExecutionIdFromNodeData(graph, { id: '' })).toBeNull()
})
})
describe('Pure utility functions', () => {
describe('parseExecutionId', () => {
it('should parse simple execution ID', () => {
expect(parseExecutionId('123')).toEqual(['123'])
})
it('should parse complex execution ID', () => {
expect(parseExecutionId('123:456:789')).toEqual(['123', '456', '789'])
})
it('should handle empty parts', () => {
expect(parseExecutionId('123::789')).toEqual(['123', '789'])
})
it('should return null for invalid input', () => {
expect(parseExecutionId('')).toBeNull()
expect(parseExecutionId(null!)).toBeNull()
expect(parseExecutionId(undefined!)).toBeNull()
})
})
describe('getLocalNodeIdFromExecutionId', () => {
it('should extract local node ID from simple ID', () => {
expect(getLocalNodeIdFromExecutionId('123')).toBe('123')
})
it('should extract local node ID from complex ID', () => {
expect(getLocalNodeIdFromExecutionId('123:456:789')).toBe('789')
})
it('should return null for invalid input', () => {
expect(getLocalNodeIdFromExecutionId('')).toBeNull()
})
})
describe('getSubgraphPathFromExecutionId', () => {
it('should return empty array for root node', () => {
expect(getSubgraphPathFromExecutionId('123')).toEqual([])
})
it('should return subgraph path for nested node', () => {
expect(getSubgraphPathFromExecutionId('123:456:789')).toEqual([
'123',
'456'
])
})
it('should return empty array for invalid input', () => {
expect(getSubgraphPathFromExecutionId('')).toEqual([])
})
})
describe('visitGraphNodes', () => {
it('should visit all nodes in graph', () => {
const visited: number[] = []
const nodes = [createMockNode(1), createMockNode(2), createMockNode(3)]
const graph = createMockGraph(nodes)
visitGraphNodes(graph, (node) => {
visited.push(Number(node.id))
})
expect(visited).toEqual([1, 2, 3])
})
it('should handle empty graph', () => {
const visited: number[] = []
const graph = createMockGraph([])
visitGraphNodes(graph, (node) => {
visited.push(Number(node.id))
})
expect(visited).toEqual([])
})
})
describe('traverseSubgraphPath', () => {
it('should return start graph for empty path', () => {
const graph = createMockGraph([])
const result = traverseSubgraphPath(graph, [])
expect(result).toBe(graph)
})
it('should traverse single level', () => {
const subgraph = createMockSubgraph('sub-uuid', [])
const node = createMockNode('1', { isSubgraph: true, subgraph })
const graph = createMockGraph([node])
const result = traverseSubgraphPath(graph, ['1'])
expect(result).toBe(subgraph)
})
it('should traverse multiple levels', () => {
const deepSubgraph = createMockSubgraph('deep-uuid', [])
const midNode = createMockNode('2', {
isSubgraph: true,
subgraph: deepSubgraph
})
const midSubgraph = createMockSubgraph('mid-uuid', [midNode])
const topNode = createMockNode('1', {
isSubgraph: true,
subgraph: midSubgraph
})
const graph = createMockGraph([topNode])
const result = traverseSubgraphPath(graph, ['1', '2'])
expect(result).toBe(deepSubgraph)
})
it('should return null for invalid path', () => {
const graph = createMockGraph([createMockNode('1')])
const result = traverseSubgraphPath(graph, ['999'])
expect(result).toBeNull()
})
})
})
describe('Main functions', () => {
describe('triggerCallbackOnAllNodes', () => {
it('should trigger callbacks on all nodes in a flat graph', () => {
const callback1 = vi.fn()
const callback2 = vi.fn()
const node1 = createMockNode(1, { callback: callback1 })
const node2 = createMockNode(2, { callback: callback2 })
const node3 = createMockNode(3) // No callback
const graph = createMockGraph([node1, node2, node3])
triggerCallbackOnAllNodes(graph, 'onExecutionStart')
expect(callback1).toHaveBeenCalledOnce()
expect(callback2).toHaveBeenCalledOnce()
})
it('should trigger callbacks on nodes in subgraphs', () => {
const callback1 = vi.fn()
const callback2 = vi.fn()
const callback3 = vi.fn()
// Create a subgraph with one node
const subNode = createMockNode(100, { callback: callback3 })
const subgraph = createMockSubgraph('sub-uuid', [subNode])
// Create main graph with two nodes, one being a subgraph
const node1 = createMockNode(1, { callback: callback1 })
const node2 = createMockNode(2, {
isSubgraph: true,
subgraph,
callback: callback2
})
const graph = createMockGraph([node1, node2])
triggerCallbackOnAllNodes(graph, 'onExecutionStart')
expect(callback1).toHaveBeenCalledOnce()
expect(callback2).toHaveBeenCalledOnce()
expect(callback3).toHaveBeenCalledOnce()
})
it('should handle nested subgraphs', () => {
const callbacks = [vi.fn(), vi.fn(), vi.fn(), vi.fn()]
// Create deeply nested structure
const deepNode = createMockNode(300, { callback: callbacks[3] })
const deepSubgraph = createMockSubgraph('deep-uuid', [deepNode])
const midNode1 = createMockNode(200, { callback: callbacks[2] })
const midNode2 = createMockNode(201, {
isSubgraph: true,
subgraph: deepSubgraph
})
const midSubgraph = createMockSubgraph('mid-uuid', [midNode1, midNode2])
const node1 = createMockNode(1, { callback: callbacks[0] })
const node2 = createMockNode(2, {
isSubgraph: true,
subgraph: midSubgraph,
callback: callbacks[1]
})
const graph = createMockGraph([node1, node2])
triggerCallbackOnAllNodes(graph, 'onExecutionStart')
callbacks.forEach((cb) => expect(cb).toHaveBeenCalledOnce())
})
})
describe('collectAllNodes', () => {
it('should collect all nodes from a flat graph', () => {
const nodes = [createMockNode(1), createMockNode(2), createMockNode(3)]
const graph = createMockGraph(nodes)
const collected = collectAllNodes(graph)
expect(collected).toHaveLength(3)
expect(collected.map((n) => Number(n.id))).toEqual([1, 2, 3])
})
it('should collect nodes from subgraphs', () => {
const subNode = createMockNode(100)
const subgraph = createMockSubgraph('sub-uuid', [subNode])
const nodes = [
createMockNode(1),
createMockNode(2, { isSubgraph: true, subgraph })
]
const graph = createMockGraph(nodes)
const collected = collectAllNodes(graph)
expect(collected).toHaveLength(3)
expect(collected.map((n) => Number(n.id))).toContain(100)
})
it('should filter nodes when filter function provided', () => {
const nodes = [createMockNode(1), createMockNode(2), createMockNode(3)]
const graph = createMockGraph(nodes)
const collected = collectAllNodes(graph, (node) => Number(node.id) > 1)
expect(collected).toHaveLength(2)
expect(collected.map((n) => Number(n.id))).toEqual([2, 3])
})
})
describe('mapAllNodes', () => {
it('should map over all nodes in a flat graph', () => {
const nodes = [createMockNode(1), createMockNode(2), createMockNode(3)]
const graph = createMockGraph(nodes)
const results = mapAllNodes(graph, (node) => Number(node.id))
expect(results).toEqual([1, 2, 3])
})
it('should map over nodes in subgraphs', () => {
const subNode = createMockNode(100)
const subgraph = createMockSubgraph('sub-uuid', [subNode])
const nodes = [
createMockNode(1),
createMockNode(2, { isSubgraph: true, subgraph })
]
const graph = createMockGraph(nodes)
const results = mapAllNodes(graph, (node) => Number(node.id))
expect(results).toHaveLength(3)
expect(results).toContain(100)
})
it('should exclude undefined results', () => {
const nodes = [createMockNode(1), createMockNode(2), createMockNode(3)]
const graph = createMockGraph(nodes)
const results = mapAllNodes(graph, (node) => {
return Number(node.id) > 1 ? Number(node.id) : undefined
})
expect(results).toEqual([2, 3])
})
it('should handle deeply nested structures', () => {
const deepNode = createMockNode(300)
const deepSubgraph = createMockSubgraph('deep-uuid', [deepNode])
const midNode = createMockNode(200)
const midSubgraphNode = createMockNode(201, {
isSubgraph: true,
subgraph: deepSubgraph
})
const midSubgraph = createMockSubgraph('mid-uuid', [
midNode,
midSubgraphNode
])
const nodes = [
createMockNode(1),
createMockNode(2, { isSubgraph: true, subgraph: midSubgraph })
]
const graph = createMockGraph(nodes)
const results = mapAllNodes(graph, (node) => `node-${node.id}`)
expect(results).toHaveLength(5)
expect(results).toContain('node-300')
})
})
describe('forEachNode', () => {
it('should execute function on all nodes in a flat graph', () => {
const nodes = [createMockNode(1), createMockNode(2), createMockNode(3)]
const graph = createMockGraph(nodes)
const visited: number[] = []
forEachNode(graph, (node) => {
visited.push(Number(node.id))
})
expect(visited).toHaveLength(3)
expect(visited).toContain(1)
expect(visited).toContain(2)
expect(visited).toContain(3)
})
it('should execute function on nodes in subgraphs', () => {
const subNode = createMockNode(100)
const subgraph = createMockSubgraph('sub-uuid', [subNode])
const nodes = [
createMockNode(1),
createMockNode(2, { isSubgraph: true, subgraph })
]
const graph = createMockGraph(nodes)
const visited: number[] = []
forEachNode(graph, (node) => {
visited.push(Number(node.id))
})
expect(visited).toHaveLength(3)
expect(visited).toContain(100)
})
it('should allow node mutations', () => {
const nodes = [createMockNode(1), createMockNode(2), createMockNode(3)]
const graph = createMockGraph(nodes)
// Add a title property to each node
forEachNode(graph, (node) => {
node.title = `Node ${node.id}`
})
expect(nodes[0]).toHaveProperty('title', 'Node 1')
expect(nodes[1]).toHaveProperty('title', 'Node 2')
expect(nodes[2]).toHaveProperty('title', 'Node 3')
})
it('should handle node type matching for subgraph references', () => {
const subgraphId = 'my-subgraph-123'
const nodes = [
createMockNode(1),
{ ...createMockNode(2), type: subgraphId } as LGraphNode,
createMockNode(3),
{ ...createMockNode(4), type: subgraphId } as LGraphNode
]
const graph = createMockGraph(nodes)
const matchingNodes: number[] = []
forEachNode(graph, (node) => {
if (node.type === subgraphId) {
matchingNodes.push(Number(node.id))
}
})
expect(matchingNodes).toEqual([2, 4])
})
})
describe('findNodeInHierarchy', () => {
it('should find node in root graph', () => {
const nodes = [createMockNode(1), createMockNode(2), createMockNode(3)]
const graph = createMockGraph(nodes)
const found = findNodeInHierarchy(graph, 2)
expect(found).toBeTruthy()
expect(Number(found?.id)).toBe(2)
})
it('should find node in subgraph', () => {
const subNode = createMockNode(100)
const subgraph = createMockSubgraph('sub-uuid', [subNode])
const nodes = [
createMockNode(1),
createMockNode(2, { isSubgraph: true, subgraph })
]
const graph = createMockGraph(nodes)
const found = findNodeInHierarchy(graph, 100)
expect(found).toBeTruthy()
expect(Number(found?.id)).toBe(100)
})
it('should return null for non-existent node', () => {
const nodes = [createMockNode(1), createMockNode(2)]
const graph = createMockGraph(nodes)
const found = findNodeInHierarchy(graph, 999)
expect(found).toBeNull()
})
})
describe('findSubgraphByUuid', () => {
it('should find subgraph by UUID', () => {
const targetUuid = 'target-uuid'
const subgraph = createMockSubgraph(targetUuid, [])
const nodes = [
createMockNode(1),
createMockNode(2, { isSubgraph: true, subgraph })
]
const graph = createMockGraph(nodes)
const found = findSubgraphByUuid(graph, targetUuid)
expect(found).toBe(subgraph)
expect(found?.id).toBe(targetUuid)
})
it('should find nested subgraph', () => {
const targetUuid = 'deep-uuid'
const deepSubgraph = createMockSubgraph(targetUuid, [])
const midSubgraph = createMockSubgraph('mid-uuid', [
createMockNode(200, { isSubgraph: true, subgraph: deepSubgraph })
])
const graph = createMockGraph([
createMockNode(1, { isSubgraph: true, subgraph: midSubgraph })
])
const found = findSubgraphByUuid(graph, targetUuid)
expect(found).toBe(deepSubgraph)
expect(found?.id).toBe(targetUuid)
})
it('should return null for non-existent UUID', () => {
const subgraph = createMockSubgraph('some-uuid', [])
const graph = createMockGraph([
createMockNode(1, { isSubgraph: true, subgraph })
])
const found = findSubgraphByUuid(graph, 'non-existent-uuid')
expect(found).toBeNull()
})
})
describe('getNodeByExecutionId', () => {
it('should find node in root graph', () => {
const nodes = [createMockNode('123'), createMockNode('456')]
const graph = createMockGraph(nodes)
const found = getNodeByExecutionId(graph, '123')
expect(found).toBeTruthy()
expect(found?.id).toBe('123')
})
it('should find node in subgraph using execution path', () => {
const targetNode = createMockNode('789')
const subgraph = createMockSubgraph('sub-uuid', [targetNode])
const subgraphNode = createMockNode('456', {
isSubgraph: true,
subgraph
})
const graph = createMockGraph([createMockNode('123'), subgraphNode])
const found = getNodeByExecutionId(graph, '456:789')
expect(found).toBe(targetNode)
expect(found?.id).toBe('789')
})
it('should handle deeply nested execution paths', () => {
const targetNode = createMockNode('999')
const deepSubgraph = createMockSubgraph('deep-uuid', [targetNode])
const midNode = createMockNode('456', {
isSubgraph: true,
subgraph: deepSubgraph
})
const midSubgraph = createMockSubgraph('mid-uuid', [midNode])
const topNode = createMockNode('123', {
isSubgraph: true,
subgraph: midSubgraph
})
const graph = createMockGraph([topNode])
const found = getNodeByExecutionId(graph, '123:456:999')
expect(found).toBe(targetNode)
expect(found?.id).toBe('999')
})
it('should return null for invalid path', () => {
const subgraph = createMockSubgraph('sub-uuid', [createMockNode('789')])
const graph = createMockGraph([
createMockNode('456', { isSubgraph: true, subgraph })
])
// Wrong path - node 123 doesn't exist
const found = getNodeByExecutionId(graph, '123:789')
expect(found).toBeNull()
})
it('should return null for invalid execution ID', () => {
const graph = createMockGraph([createMockNode('123')])
const found = getNodeByExecutionId(graph, '')
expect(found).toBeNull()
})
})
describe('getExecutionIdByNode', () => {
it('should return node id if graph is rootGraph', () => {
const node = createMockNode('123')
const graph = createMockGraph([node])
node.graph = graph
const execId = getExecutionIdByNode(graph, node)
expect(execId).toBe('123')
})
it('should return path using subgraph nodes if deeply nested', () => {
const targetNode = createMockNode('999')
const deepSubgraph = createMockSubgraph('deep-uuid', [targetNode])
const midNode = createMockNode('456', {
isSubgraph: true,
subgraph: deepSubgraph
})
const midSubgraph = createMockSubgraph('mid-uuid', [midNode])
const topNode = createMockNode('123', {
isSubgraph: true,
subgraph: midSubgraph
})
const rootGraph = createMockGraph([topNode])
// set up parent graphs
;(midSubgraph as Subgraph & { rootGraph: LGraph }).rootGraph = rootGraph
;(
deepSubgraph as Subgraph & { rootGraph: LGraph | Subgraph }
).rootGraph = midSubgraph
// also need a way for nodes to point to their parent graphs
// assuming node.graph === graph
targetNode.graph = deepSubgraph
midNode.graph = midSubgraph
topNode.graph = rootGraph
const execId = getExecutionIdByNode(rootGraph, targetNode)
expect(execId).toBe('123:456:999')
})
})
describe('getExecutionIdForNodeInGraph', () => {
it('returns local id when graph is rootGraph', () => {
const node = createMockNode('42')
const rootGraph = createMockGraph([node])
expect(getExecutionIdForNodeInGraph(rootGraph, rootGraph, '42')).toBe(
'42'
)
})
it('returns local id when graph.isRootGraph is true', () => {
const node = createMockNode('42')
const rootGraph = createMockGraph([node])
const otherRoot = createMockGraph([])
expect(getExecutionIdForNodeInGraph(otherRoot, rootGraph, '42')).toBe(
'42'
)
})
it('builds parentPath:nodeId for a single-level subgraph', () => {
const interior = createMockNode('63')
const subgraph = createMockSubgraph('sub-uuid', [interior])
const subgraphNode = createMockNode('65', {
isSubgraph: true,
subgraph
})
const rootGraph = createMockGraph([subgraphNode])
expect(getExecutionIdForNodeInGraph(rootGraph, subgraph, '63')).toBe(
'65:63'
)
})
it('builds nested parentPath:nodeId for deeply-nested subgraph', () => {
const interior = createMockNode('999')
const deep = createMockSubgraph('deep', [interior])
const midNode = createMockNode('456', {
isSubgraph: true,
subgraph: deep
})
const mid = createMockSubgraph('mid', [midNode])
const topNode = createMockNode('123', {
isSubgraph: true,
subgraph: mid
})
const rootGraph = createMockGraph([topNode])
expect(getExecutionIdForNodeInGraph(rootGraph, deep, '999')).toBe(
'123:456:999'
)
})
it('works when node is detached (node.graph = null)', () => {
// This is the primary use case — onNodeRemoved fires after
// LiteGraph nulls node.graph, but the hook closure still has
// the local graph instance, which is enough.
const interior = createMockNode('63')
const subgraph = createMockSubgraph('sub-uuid', [interior])
const subgraphNode = createMockNode('65', {
isSubgraph: true,
subgraph
})
const rootGraph = createMockGraph([subgraphNode])
interior.graph = null as unknown as LGraph
expect(
getExecutionIdForNodeInGraph(rootGraph, subgraph, interior.id)
).toBe('65:63')
})
it('falls back to local id when graph is not reachable from root', () => {
const interior = createMockNode('63')
const orphanSubgraph = createMockSubgraph('orphan', [interior])
const rootGraph = createMockGraph([])
expect(
getExecutionIdForNodeInGraph(rootGraph, orphanSubgraph, '63')
).toBe('63')
})
})
describe('isAncestorPathActive', () => {
function makeActiveSubgraph(id: string, nodes: LGraphNode[]) {
return createMockSubgraph(id, nodes)
}
it('returns true for root-level nodes (no ancestors)', () => {
const node = createMockNode('42')
const rootGraph = createMockGraph([node])
expect(isAncestorPathActive(rootGraph, '42')).toBe(true)
})
it('returns true when all ancestor containers are active', () => {
const interior = createMockNode('63')
const subgraph = makeActiveSubgraph('sub', [interior])
const container = createMockNode('65', {
isSubgraph: true,
subgraph
})
// container mode defaults to ALWAYS (active)
const rootGraph = createMockGraph([container])
expect(isAncestorPathActive(rootGraph, '65:63')).toBe(true)
})
it('returns false when the immediate parent container is bypassed', () => {
const interior = createMockNode('63')
const subgraph = makeActiveSubgraph('sub', [interior])
const container = createMockLGraphNode({
id: 65,
isSubgraphNode: () => true,
subgraph,
mode: LGraphEventMode.BYPASS
}) satisfies Partial<LGraphNode> as LGraphNode
const rootGraph = createMockGraph([container])
expect(isAncestorPathActive(rootGraph, '65:63')).toBe(false)
})
it('returns false when an outer ancestor is muted (deeply nested)', () => {
const interior = createMockNode('999')
const deep = makeActiveSubgraph('deep', [interior])
const midNode = createMockNode('456', {
isSubgraph: true,
subgraph: deep
})
const mid = makeActiveSubgraph('mid', [midNode])
const topNode = createMockLGraphNode({
id: 123,
isSubgraphNode: () => true,
subgraph: mid,
mode: LGraphEventMode.NEVER
}) satisfies Partial<LGraphNode> as LGraphNode
const rootGraph = createMockGraph([topNode])
expect(isAncestorPathActive(rootGraph, '123:456:999')).toBe(false)
})
it('returns true when ancestor node cannot be resolved (defensive)', () => {
const rootGraph = createMockGraph([])
// Unknown ancestor ID "99" — not found, treated as active.
expect(isAncestorPathActive(rootGraph, '99:63')).toBe(true)
})
it('returns true when rootGraph is null/undefined', () => {
expect(isAncestorPathActive(null, '65:63')).toBe(true)
expect(isAncestorPathActive(undefined, '65:63')).toBe(true)
})
})
describe('isExecutionPathActive', () => {
it('returns false when the target node itself is bypassed', () => {
const node = createMockLGraphNode({
id: 42,
mode: LGraphEventMode.BYPASS
}) satisfies Partial<LGraphNode> as LGraphNode
const rootGraph = createMockGraph([node])
expect(isExecutionPathActive(rootGraph, '42')).toBe(false)
})
it('returns false when an ancestor container is bypassed', () => {
const interior = createMockNode('63')
const subgraph = createMockSubgraph('sub', [interior])
const container = createMockLGraphNode({
id: 65,
isSubgraphNode: () => true,
subgraph,
mode: LGraphEventMode.BYPASS
}) satisfies Partial<LGraphNode> as LGraphNode
const rootGraph = createMockGraph([container])
expect(isExecutionPathActive(rootGraph, '65:63')).toBe(false)
})
})
describe('isMissingCandidateActive', () => {
function makeBypassedContainer(interiorId: string) {
const interior = createMockNode(interiorId)
const subgraph = createMockSubgraph('sub', [interior])
const container = createMockLGraphNode({
id: 65,
isSubgraphNode: () => true,
subgraph,
mode: LGraphEventMode.BYPASS
}) satisfies Partial<LGraphNode> as LGraphNode
return createMockGraph([container])
}
it('surfaces confirmed missing candidates under active ancestors', () => {
const interior = createMockNode('63')
const subgraph = createMockSubgraph('sub', [interior])
const container = createMockNode('65', {
isSubgraph: true,
subgraph
})
const rootGraph = createMockGraph([container])
expect(
isMissingCandidateActive(rootGraph, {
nodeId: toNodeId('65:63'),
isMissing: true
})
).toBe(true)
})
it('drops confirmed missing candidates whose ancestor is bypassed (cloud .then race)', () => {
// Mirrors the reopen gap: pipeline-start filter passed, then
// the user bypassed the container during verification, and the
// async resolver must not resurface the candidate.
const rootGraph = makeBypassedContainer('63')
expect(
isMissingCandidateActive(rootGraph, {
nodeId: toNodeId('65:63'),
isMissing: true
})
).toBe(false)
})
it('drops unverified candidates (isMissing !== true)', () => {
const rootGraph = createMockGraph([])
expect(
isMissingCandidateActive(rootGraph, {
nodeId: toNodeId('1'),
isMissing: undefined
})
).toBe(false)
expect(
isMissingCandidateActive(rootGraph, {
nodeId: toNodeId('1'),
isMissing: false
})
).toBe(false)
})
it('keeps workflow-level candidates (nodeId == null) when confirmed missing', () => {
const rootGraph = makeBypassedContainer('63')
expect(
isMissingCandidateActive(rootGraph, {
nodeId: undefined,
isMissing: true
})
).toBe(true)
})
it('uses sourceExecutionId for host-keyed promoted candidates', () => {
const rootGraph = makeBypassedContainer('63')
expect(
isMissingCandidateActive(rootGraph, {
nodeId: '65',
sourceExecutionId: '65:63',
isMissing: true
})
).toBe(false)
})
})
describe('isCandidateScopeActive', () => {
it('uses sourceExecutionId before nodeId', () => {
const rootNode = createMockNode('65')
const sourceNode = createMockLGraphNode({
id: 42,
mode: LGraphEventMode.BYPASS
}) satisfies Partial<LGraphNode> as LGraphNode
const rootGraph = createMockGraph([rootNode, sourceNode])
expect(
isCandidateScopeActive(rootGraph, {
nodeId: '65',
sourceExecutionId: '42'
})
).toBe(false)
})
})
describe('getExecutionIdFromNodeData', () => {
it('should return the correct execution ID for a normal node', () => {
const node = createMockNode('123')
const graph = createMockGraph([node])
node.graph = graph
const nodeData = { id: 123 }
const execId = getExecutionIdFromNodeData(graph, nodeData)
expect(execId).toBe('123')
})
it('should fallback to stringified nodeData id if node cannot be resolved', () => {
const graph = createMockGraph([])
const nodeData = { id: 777 }
const execId = getExecutionIdFromNodeData(graph, nodeData)
expect(execId).toBe('777')
})
it('should return full execution ID for node inside a subgraph', () => {
const targetNode = createMockNode('999')
const subgraphUuid = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890'
const subgraph = createMockSubgraph(subgraphUuid, [targetNode])
const topNode = createMockNode('123', {
isSubgraph: true,
subgraph
})
const rootGraph = createMockGraph([topNode])
;(subgraph as Subgraph & { rootGraph: LGraph }).rootGraph = rootGraph
targetNode.graph = subgraph
topNode.graph = rootGraph
const nodeData = { id: 999, subgraphId: subgraphUuid }
const execId = getExecutionIdFromNodeData(rootGraph, nodeData)
expect(execId).toBe('123:999')
})
})
describe('getNodeByLocatorId', () => {
it('should find node in root graph', () => {
const nodes = [createMockNode('123'), createMockNode('456')]
const graph = createMockGraph(nodes)
const found = getNodeByLocatorId(graph, '123')
expect(found).toBeTruthy()
expect(found?.id).toBe('123')
})
it('should find node in subgraph using subgraph locator format', () => {
const targetUuid = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890'
const targetNode = createMockNode('789')
const subgraph = createMockSubgraph(targetUuid, [targetNode])
const graph = createMockGraph([
createMockNode('123'),
createMockNode('456', { isSubgraph: true, subgraph })
])
const locatorId = `${targetUuid}:789`
const found = getNodeByLocatorId(graph, locatorId)
expect(found).toBe(targetNode)
expect(found?.id).toBe('789')
})