@@ -14,7 +14,6 @@ jest.mock('vite', () => ({
1414 build : ( ...args ) => mockViteBuild ( ...args ) ,
1515} ) ) ;
1616
17- const WORKFLOW_ID = '380e7df1-729c-420c-b15e-a3b8e6347d49' ;
1817const DD_SITE = 'datadoghq.com' ;
1918
2019const mockFunctions = [
@@ -134,13 +133,13 @@ describe('Dev Server Middleware', () => {
134133
135134 // Mock the Datadog API via nock.
136135 const apiScope = nock ( `https://${ DD_SITE } ` )
137- . post ( ` /api/v2/workflows/ ${ WORKFLOW_ID } /single_action_runs` )
138- . reply ( 200 , { data : { id : 'exec -123' } } )
139- . get ( ` /api/v2/workflows/ ${ WORKFLOW_ID } /single_action_runs/exec -123` )
136+ . post ( ' /api/v2/app-builder/queries/preview-async' )
137+ . reply ( 200 , { data : { id : 'receipt -123' } } )
138+ . get ( ' /api/v2/app-builder/queries/execution-long-polling/receipt -123' )
140139 . reply ( 200 , {
141140 data : {
142141 attributes : {
143- state : 'SUCCEEDED' ,
142+ done : true ,
144143 outputs : { result : 'hello' } ,
145144 } ,
146145 } ,
@@ -156,8 +155,8 @@ describe('Dev Server Middleware', () => {
156155 middleware ( req , res , next ) ;
157156 expect ( next ) . not . toHaveBeenCalled ( ) ;
158157
159- // Wait for async handler + polling to complete.
160- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
158+ // Wait for async handler to complete.
159+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
161160
162161 expect ( res . statusCode ) . toBe ( 200 ) ;
163162 const body = JSON . parse ( res . getBody ( ) ) ;
@@ -263,7 +262,7 @@ describe('Dev Server Middleware', () => {
263262 mockViteBuild . mockResolvedValue ( mockBuildResult ( '// code' ) ) ;
264263
265264 nock ( `https://${ DD_SITE } ` )
266- . post ( ` /api/v2/workflows/ ${ WORKFLOW_ID } /single_action_runs` )
265+ . post ( ' /api/v2/app-builder/queries/preview-async' )
267266 . reply ( 403 , 'Forbidden' ) ;
268267
269268 const req = createMockRequest ( '/__dd/executeAction' , {
@@ -290,11 +289,11 @@ describe('Dev Server Middleware', () => {
290289 'DD-APPLICATION-KEY' : 'test-app-key' ,
291290 } ,
292291 } )
293- . post ( ` /api/v2/workflows/ ${ WORKFLOW_ID } /single_action_runs` )
294- . reply ( 200 , { data : { id : 'exec -1' } } )
295- . get ( ` /api/v2/workflows/ ${ WORKFLOW_ID } /single_action_runs/exec-1` )
292+ . post ( ' /api/v2/app-builder/queries/preview-async' )
293+ . reply ( 200 , { data : { id : 'receipt -1' } } )
294+ . get ( ' /api/v2/app-builder/queries/execution-long-polling/receipt-1' )
296295 . reply ( 200 , {
297- data : { attributes : { state : 'SUCCEEDED' , outputs : { value : 42 } } } ,
296+ data : { attributes : { done : true , outputs : { value : 42 } } } ,
298297 } ) ;
299298
300299 const req = createMockRequest ( '/__dd/executeAction' , {
@@ -304,13 +303,68 @@ describe('Dev Server Middleware', () => {
304303 const res = createMockResponse ( ) ;
305304
306305 middleware ( req , res , jest . fn ( ) ) ;
307- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
306+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
308307
309308 expect ( res . statusCode ) . toBe ( 200 ) ;
310309 const body = JSON . parse ( res . getBody ( ) ) ;
311310 expect ( body . success ) . toBe ( true ) ;
312311 expect ( body . result ) . toEqual ( { value : 42 } ) ;
313312 expect ( apiScope . isDone ( ) ) . toBe ( true ) ;
314313 } ) ;
314+
315+ test ( 'Should handle errors array from long-polling endpoint' , async ( ) => {
316+ mockViteBuild . mockResolvedValue ( mockBuildResult ( '// code' ) ) ;
317+
318+ nock ( `https://${ DD_SITE } ` )
319+ . post ( '/api/v2/app-builder/queries/preview-async' )
320+ . reply ( 200 , { data : { id : 'receipt-err' } } )
321+ . get ( '/api/v2/app-builder/queries/execution-long-polling/receipt-err' )
322+ . reply ( 200 , {
323+ errors : [ { title : 'ExecutionFailed' , detail : 'Script threw an error' } ] ,
324+ } ) ;
325+
326+ const req = createMockRequest ( '/__dd/executeAction' , {
327+ functionName : 'greet' ,
328+ args : [ ] ,
329+ } ) ;
330+ const res = createMockResponse ( ) ;
331+
332+ middleware ( req , res , jest . fn ( ) ) ;
333+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
334+
335+ expect ( res . statusCode ) . toBe ( 500 ) ;
336+ const body = JSON . parse ( res . getBody ( ) ) ;
337+ expect ( body . success ) . toBe ( false ) ;
338+ expect ( body . error ) . toContain ( 'Script threw an error' ) ;
339+ } ) ;
340+
341+ test ( 'Should retry when long-poll returns done: false' , async ( ) => {
342+ mockViteBuild . mockResolvedValue ( mockBuildResult ( '// code' ) ) ;
343+
344+ const apiScope = nock ( `https://${ DD_SITE } ` )
345+ . post ( '/api/v2/app-builder/queries/preview-async' )
346+ . reply ( 200 , { data : { id : 'receipt-retry' } } )
347+ . get ( '/api/v2/app-builder/queries/execution-long-polling/receipt-retry' )
348+ . reply ( 200 , { data : { attributes : { done : false } } } )
349+ . get ( '/api/v2/app-builder/queries/execution-long-polling/receipt-retry' )
350+ . reply ( 200 , {
351+ data : { attributes : { done : true , outputs : { ok : true } } } ,
352+ } ) ;
353+
354+ const req = createMockRequest ( '/__dd/executeAction' , {
355+ functionName : 'greet' ,
356+ args : [ ] ,
357+ } ) ;
358+ const res = createMockResponse ( ) ;
359+
360+ middleware ( req , res , jest . fn ( ) ) ;
361+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
362+
363+ expect ( res . statusCode ) . toBe ( 200 ) ;
364+ const body = JSON . parse ( res . getBody ( ) ) ;
365+ expect ( body . success ) . toBe ( true ) ;
366+ expect ( body . result ) . toEqual ( { ok : true } ) ;
367+ expect ( apiScope . isDone ( ) ) . toBe ( true ) ;
368+ } ) ;
315369 } ) ;
316370} ) ;
0 commit comments