@@ -372,6 +372,205 @@ describe("TaskExecutor", () => {
372372 } ,
373373 } ) ;
374374 } ) ;
375+
376+ test ( "should call onComplete hooks in correct order with proper data" , async ( ) => {
377+ const globalCompleteOrder : string [ ] = [ ] ;
378+ const completePayloads : any [ ] = [ ] ;
379+ const completeResults : any [ ] = [ ] ;
380+ const completeInits : any [ ] = [ ] ;
381+
382+ // Register global init hook to provide init data
383+ lifecycleHooks . registerGlobalInitHook ( {
384+ id : "test-init" ,
385+ fn : async ( ) => {
386+ return {
387+ foo : "bar" ,
388+ } ;
389+ } ,
390+ } ) ;
391+
392+ // Register two global complete hooks
393+ lifecycleHooks . registerGlobalCompleteHook ( {
394+ id : "global-complete-1" ,
395+ fn : async ( { payload, result, init } ) => {
396+ console . log ( "Executing global complete hook 1" ) ;
397+ globalCompleteOrder . push ( "global-1" ) ;
398+ completePayloads . push ( payload ) ;
399+ completeResults . push ( result ) ;
400+ completeInits . push ( init ) ;
401+ } ,
402+ } ) ;
403+
404+ lifecycleHooks . registerGlobalCompleteHook ( {
405+ id : "global-complete-2" ,
406+ fn : async ( { payload, result, init } ) => {
407+ console . log ( "Executing global complete hook 2" ) ;
408+ globalCompleteOrder . push ( "global-2" ) ;
409+ completePayloads . push ( payload ) ;
410+ completeResults . push ( result ) ;
411+ completeInits . push ( init ) ;
412+ } ,
413+ } ) ;
414+
415+ // Register task-specific complete hook
416+ lifecycleHooks . registerTaskCompleteHook ( "test-task" , {
417+ id : "task-complete" ,
418+ fn : async ( { payload, result, init } ) => {
419+ console . log ( "Executing task complete hook" ) ;
420+ globalCompleteOrder . push ( "task" ) ;
421+ completePayloads . push ( payload ) ;
422+ completeResults . push ( result ) ;
423+ completeInits . push ( init ) ;
424+ } ,
425+ } ) ;
426+
427+ // Verify hooks are registered
428+ const globalHooks = lifecycleHooks . getGlobalCompleteHooks ( ) ;
429+ console . log (
430+ "Registered global hooks:" ,
431+ globalHooks . map ( ( h ) => h . id )
432+ ) ;
433+ const taskHook = lifecycleHooks . getTaskCompleteHook ( "test-task" ) ;
434+ console . log ( "Registered task hook:" , taskHook ? "yes" : "no" ) ;
435+
436+ const task = {
437+ id : "test-task" ,
438+ fns : {
439+ run : async ( payload : any , params : RunFnParams < any > ) => {
440+ return {
441+ output : "test-output" ,
442+ init : params . init ,
443+ } ;
444+ } ,
445+ } ,
446+ } ;
447+
448+ const result = await executeTask ( task , { test : "data" } ) ;
449+
450+ // Verify hooks were called in correct order
451+ expect ( globalCompleteOrder ) . toEqual ( [ "global-1" , "global-2" , "task" ] ) ;
452+
453+ // Verify each hook received the correct payload
454+ completePayloads . forEach ( ( payload ) => {
455+ expect ( payload ) . toEqual ( { test : "data" } ) ;
456+ } ) ;
457+
458+ // Verify each hook received the correct result
459+ completeResults . forEach ( ( result ) => {
460+ expect ( result ) . toEqual ( {
461+ ok : true ,
462+ data : {
463+ output : "test-output" ,
464+ init : { foo : "bar" } ,
465+ } ,
466+ } ) ;
467+ } ) ;
468+
469+ // Verify each hook received the correct init data
470+ completeInits . forEach ( ( init ) => {
471+ expect ( init ) . toEqual ( { foo : "bar" } ) ;
472+ } ) ;
473+
474+ // Verify the final result
475+ expect ( result ) . toEqual ( {
476+ result : {
477+ ok : true ,
478+ id : "test-run-id" ,
479+ output : '{"json":{"output":"test-output","init":{"foo":"bar"}}}' ,
480+ outputType : "application/super+json" ,
481+ } ,
482+ } ) ;
483+ } ) ;
484+
485+ test ( "should call onComplete hooks with error when task fails" , async ( ) => {
486+ const globalCompleteOrder : string [ ] = [ ] ;
487+ const completePayloads : any [ ] = [ ] ;
488+ const completeResults : any [ ] = [ ] ;
489+ const completeInits : any [ ] = [ ] ;
490+
491+ // Register global init hook to provide init data
492+ lifecycleHooks . registerGlobalInitHook ( {
493+ id : "test-init" ,
494+ fn : async ( ) => {
495+ return {
496+ foo : "bar" ,
497+ } ;
498+ } ,
499+ } ) ;
500+
501+ // Register global complete hooks
502+ lifecycleHooks . registerGlobalCompleteHook ( {
503+ id : "global-complete" ,
504+ fn : async ( { payload, result, init } ) => {
505+ console . log ( "Executing global complete hook" ) ;
506+ globalCompleteOrder . push ( "global" ) ;
507+ completePayloads . push ( payload ) ;
508+ completeResults . push ( result ) ;
509+ completeInits . push ( init ) ;
510+ } ,
511+ } ) ;
512+
513+ // Register task-specific complete hook
514+ lifecycleHooks . registerTaskCompleteHook ( "test-task" , {
515+ id : "task-complete" ,
516+ fn : async ( { payload, result, init } ) => {
517+ console . log ( "Executing task complete hook" ) ;
518+ globalCompleteOrder . push ( "task" ) ;
519+ completePayloads . push ( payload ) ;
520+ completeResults . push ( result ) ;
521+ completeInits . push ( init ) ;
522+ } ,
523+ } ) ;
524+
525+ const expectedError = new Error ( "Task failed intentionally" ) ;
526+
527+ const task = {
528+ id : "test-task" ,
529+ fns : {
530+ run : async ( payload : any , params : RunFnParams < any > ) => {
531+ throw expectedError ;
532+ } ,
533+ } ,
534+ } ;
535+
536+ const result = await executeTask ( task , { test : "data" } ) ;
537+
538+ // Verify hooks were called in correct order
539+ expect ( globalCompleteOrder ) . toEqual ( [ "global" , "task" ] ) ;
540+
541+ // Verify each hook received the correct payload
542+ completePayloads . forEach ( ( payload ) => {
543+ expect ( payload ) . toEqual ( { test : "data" } ) ;
544+ } ) ;
545+
546+ // Verify each hook received the error result
547+ completeResults . forEach ( ( result ) => {
548+ expect ( result ) . toEqual ( {
549+ ok : false ,
550+ error : expectedError ,
551+ } ) ;
552+ } ) ;
553+
554+ // Verify each hook received the correct init data
555+ completeInits . forEach ( ( init ) => {
556+ expect ( init ) . toEqual ( { foo : "bar" } ) ;
557+ } ) ;
558+
559+ // Verify the final result contains the error
560+ expect ( result ) . toEqual ( {
561+ result : {
562+ ok : false ,
563+ id : "test-run-id" ,
564+ error : {
565+ type : "BUILT_IN_ERROR" ,
566+ message : "Task failed intentionally" ,
567+ name : "Error" ,
568+ stackTrace : expect . any ( String ) ,
569+ } ,
570+ skippedRetrying : false ,
571+ } ,
572+ } ) ;
573+ } ) ;
375574} ) ;
376575
377576function executeTask ( task : TaskMetadataWithFunctions , payload : any ) {
0 commit comments