1
1
import { mock } from 'jest-mock-extended' ;
2
- import type { IConnections , INode , INodeType , INodeTypes , IPinData } from 'n8n-workflow' ;
2
+ import type { IConnections , INode , INodeType , INodeTypes , IPinData , IRunData } from 'n8n-workflow' ;
3
3
import { Workflow } from 'n8n-workflow' ;
4
4
5
- import { toIConnections } from './helpers' ;
5
+ import { createNodeData , toIConnections , toITaskData } from './helpers' ;
6
+ import { DirectedGraph } from '../directed-graph' ;
6
7
import { findTriggerForPartialExecution } from '../find-trigger-for-partial-execution' ;
7
8
8
9
describe ( 'findTriggerForPartialExecution' , ( ) => {
@@ -188,7 +189,7 @@ describe('findTriggerForPartialExecution', () => {
188
189
'$description' ,
189
190
( { nodes, connections, destinationNodeName, expectedTrigger, pinData } ) => {
190
191
const workflow = createMockWorkflow ( nodes , toIConnections ( connections ) , pinData ) ;
191
- expect ( findTriggerForPartialExecution ( workflow , destinationNodeName ) ) . toBe (
192
+ expect ( findTriggerForPartialExecution ( workflow , destinationNodeName , { } ) ) . toBe (
192
193
expectedTrigger ,
193
194
) ;
194
195
} ,
@@ -199,19 +200,39 @@ describe('findTriggerForPartialExecution', () => {
199
200
describe ( 'Error and Edge Case Handling' , ( ) => {
200
201
it ( 'should handle non-existent destination node gracefully' , ( ) => {
201
202
const workflow = createMockWorkflow ( [ ] , { } ) ;
202
- expect ( findTriggerForPartialExecution ( workflow , 'NonExistentNode' ) ) . toBeUndefined ( ) ;
203
+ expect ( findTriggerForPartialExecution ( workflow , 'NonExistentNode' , { } ) ) . toBeUndefined ( ) ;
203
204
} ) ;
204
205
205
206
it ( 'should handle empty workflow' , ( ) => {
206
207
const workflow = createMockWorkflow ( [ ] , { } ) ;
207
- expect ( findTriggerForPartialExecution ( workflow , '' ) ) . toBeUndefined ( ) ;
208
+ expect ( findTriggerForPartialExecution ( workflow , '' , { } ) ) . toBeUndefined ( ) ;
208
209
} ) ;
209
210
210
211
it ( 'should handle workflow with no connections' , ( ) => {
211
212
const workflow = createMockWorkflow ( [ manualTriggerNode ] , { } ) ;
212
- expect ( findTriggerForPartialExecution ( workflow , manualTriggerNode . name ) ) . toBe (
213
+ expect ( findTriggerForPartialExecution ( workflow , manualTriggerNode . name , { } ) ) . toBe (
213
214
manualTriggerNode ,
214
215
) ;
215
216
} ) ;
217
+
218
+ it ( 'should prefer triggers that have run data' , ( ) => {
219
+ // ARRANGE
220
+ const trigger1 = createNodeData ( { name : 'trigger1' , type : 'n8n-nodes-base.manualTrigger' } ) ;
221
+ const trigger2 = createNodeData ( { name : 'trigger2' , type : 'n8n-nodes-base.manualTrigger' } ) ;
222
+ const node = createNodeData ( { name : 'node' } ) ;
223
+ const workflow = new DirectedGraph ( )
224
+ . addNodes ( trigger1 , trigger2 , node )
225
+ . addConnections ( { from : trigger1 , to : node } , { from : trigger2 , to : node } )
226
+ . toWorkflow ( { name : '' , active : false , nodeTypes } ) ;
227
+ const runData : IRunData = {
228
+ [ trigger1 . name ] : [ toITaskData ( [ { data : { nodeName : 'trigger1' } } ] ) ] ,
229
+ } ;
230
+
231
+ // ACT
232
+ const chosenTrigger = findTriggerForPartialExecution ( workflow , node . name , runData ) ;
233
+
234
+ // ASSERT
235
+ expect ( chosenTrigger ) . toBe ( trigger1 ) ;
236
+ } ) ;
216
237
} ) ;
217
238
} ) ;
0 commit comments