1- /* eslint-disable @typescript-eslint/no-explicit-any */
1+ import { TriggerStrategy } from '@openops/blocks-framework' ;
2+ import {
3+ AppConnectionStatus ,
4+ AppConnectionsWithSupportedBlocks ,
5+ AppConnectionType ,
6+ BlockType ,
7+ flowHelper ,
8+ FlowStatus ,
9+ FlowVersionState ,
10+ openOpsId ,
11+ PackageType ,
12+ ScheduleType ,
13+ TriggerType ,
14+ } from '@openops/shared' ;
15+ import {
16+ bulkCreateAndPublishFlows ,
17+ WorkflowTemplate ,
18+ } from '../../../src/app/flows/flow/flow-bulk-create' ;
19+ import { triggerHooks } from '../../../src/app/flows/trigger' ;
20+ import { triggerUtils } from '../../../src/app/flows/trigger/hooks/trigger-utils' ;
21+
222const mockInsert = jest . fn ( ) . mockResolvedValue ( undefined ) ;
323const mockQuery = jest . fn ( ) . mockResolvedValue ( undefined ) ;
424const mockGetRepository = jest . fn ( ) . mockReturnValue ( { insert : mockInsert } ) ;
@@ -12,7 +32,7 @@ const mockFlowRepo = jest.fn().mockReturnValue({
1232} ) ;
1333
1434jest . mock ( '../../../src/app/flows/flow/flow.repo' , ( ) => ( {
15- flowRepo : mockFlowRepo ,
35+ flowRepo : ( ) : object => mockFlowRepo ( ) ,
1636} ) ) ;
1737
1838jest . mock ( '@openops/shared' , ( ) => ( {
@@ -24,34 +44,73 @@ jest.mock('@openops/shared', () => ({
2444 } ,
2545} ) ) ;
2646
27- import {
28- flowHelper ,
29- FlowStatus ,
30- FlowVersionState ,
31- openOpsId ,
32- } from '@openops/shared' ;
33- import { bulkCreateAndPublishFlows } from '../../../src/app/flows/flow/flow-bulk-create' ;
47+ jest . mock ( '../../../src/app/flows/trigger/hooks/trigger-utils' , ( ) => ( {
48+ triggerUtils : {
49+ getBlockTriggerOrThrow : jest . fn ( ) ,
50+ } ,
51+ } ) ) ;
52+
53+ jest . mock ( '../../../src/app/flows/trigger' , ( ) => ( {
54+ triggerHooks : {
55+ enable : jest . fn ( ) ,
56+ } ,
57+ } ) ) ;
3458
3559const mockOpenOpsId = openOpsId as jest . Mock ;
3660const mockGetImportOperations = flowHelper . getImportOperations as jest . Mock ;
3761const mockApply = flowHelper . apply as jest . Mock ;
62+ const mockGetBlockTriggerOrThrow =
63+ triggerUtils . getBlockTriggerOrThrow as jest . Mock ;
64+ const mockTriggerHooksEnable = triggerHooks . enable as jest . Mock ;
3865
39- const baseTemplate = {
66+ const baseTemplate : WorkflowTemplate = {
4067 template : {
4168 displayName : 'Test Flow' ,
42- trigger : { type : 'WEBHOOK' , name : 'trigger' } as any ,
69+ trigger : {
70+ type : TriggerType . BLOCK ,
71+ name : 'trigger' ,
72+ displayName : 'Trigger' ,
73+ settings : {
74+ blockName : 'test-block' ,
75+ blockVersion : '1.0.0' ,
76+ triggerName : 'test-trigger' ,
77+ blockType : BlockType . OFFICIAL ,
78+ packageType : PackageType . REGISTRY ,
79+ input : { } ,
80+ inputUiInfo : { } ,
81+ } ,
82+ valid : true ,
83+ nextAction : null ,
84+ } ,
4385 } ,
4486} ;
4587
46- const baseConnections = [
47- { id : 'conn-1' , name : 'Test' , authProviderKey : 'aws' , supportedBlocks : [ ] } ,
48- ] as any [ ] ;
88+ const baseConnections : AppConnectionsWithSupportedBlocks [ ] = [
89+ {
90+ id : 'conn-1' ,
91+ name : 'Test' ,
92+ authProviderKey : 'aws' ,
93+ supportedBlocks : [ ] ,
94+ projectId : 'project-1' ,
95+ type : AppConnectionType . SECRET_TEXT ,
96+ value : {
97+ type : AppConnectionType . SECRET_TEXT ,
98+ secret_text : 'secret' ,
99+ } ,
100+ created : '2021-01-01T00:00:00Z' ,
101+ updated : '2021-01-01T00:00:00Z' ,
102+ status : AppConnectionStatus . ACTIVE ,
103+ } ,
104+ ] as unknown as AppConnectionsWithSupportedBlocks [ ] ;
49105
50106describe ( 'bulkCreateAndPublishFlows' , ( ) => {
51107 beforeEach ( ( ) => {
52108 jest . clearAllMocks ( ) ;
53109 mockGetImportOperations . mockReturnValue ( [ ] ) ;
54110 mockApply . mockImplementation ( ( version ) => version ) ;
111+ mockGetBlockTriggerOrThrow . mockResolvedValue ( {
112+ type : TriggerStrategy . WEBHOOK ,
113+ } ) ;
55114
56115 let idCounter = 0 ;
57116 mockOpenOpsId . mockImplementation ( ( ) => `generated-id-${ ++ idCounter } ` ) ;
@@ -222,9 +281,54 @@ describe('bulkCreateAndPublishFlows', () => {
222281 ) ;
223282
224283 expect ( mockGetImportOperations ) . toHaveBeenCalledWith (
225- expect . objectContaining ( { type : 'WEBHOOK' } ) ,
284+ expect . objectContaining ( { type : TriggerType . BLOCK } ) ,
226285 baseConnections ,
227286 ) ;
228287 expect ( mockApply ) . toHaveBeenCalledWith ( expect . any ( Object ) , mockOp ) ;
229288 } ) ;
289+
290+ it ( 'enables polling triggers and sets schedule' , async ( ) => {
291+ mockGetBlockTriggerOrThrow . mockResolvedValue ( {
292+ type : TriggerStrategy . POLLING ,
293+ } ) ;
294+ mockTriggerHooksEnable . mockResolvedValue ( {
295+ result : {
296+ scheduleOptions : {
297+ cronExpression : '*/5 * * * *' ,
298+ timezone : 'UTC' ,
299+ } ,
300+ } ,
301+ } ) ;
302+
303+ await bulkCreateAndPublishFlows (
304+ [ baseTemplate ] ,
305+ baseConnections ,
306+ 'project-1' ,
307+ 'folder-1' ,
308+ ) ;
309+
310+ const flowInsertCall = mockInsert . mock . calls [ 0 ] [ 0 ] ;
311+ expect ( flowInsertCall [ 0 ] . schedule ) . toEqual ( {
312+ cronExpression : '*/5 * * * *' ,
313+ timezone : 'UTC' ,
314+ type : ScheduleType . CRON_EXPRESSION ,
315+ failureCount : 0 ,
316+ } ) ;
317+ expect ( mockTriggerHooksEnable ) . toHaveBeenCalled ( ) ;
318+ } ) ;
319+
320+ it ( 'does not call triggerHooks.enable for WEBHOOK triggers' , async ( ) => {
321+ mockGetBlockTriggerOrThrow . mockResolvedValue ( {
322+ type : TriggerStrategy . WEBHOOK ,
323+ } ) ;
324+
325+ await bulkCreateAndPublishFlows (
326+ [ baseTemplate ] ,
327+ baseConnections ,
328+ 'project-1' ,
329+ 'folder-1' ,
330+ ) ;
331+
332+ expect ( mockTriggerHooksEnable ) . not . toHaveBeenCalled ( ) ;
333+ } ) ;
230334} ) ;
0 commit comments