@@ -40,6 +40,7 @@ import { omit } from 'lodash';
4040import { UntypedNormalizedAlertType } from '../rule_type_registry' ;
4141import { ruleTypeRegistryMock } from '../rule_type_registry.mock' ;
4242import { ExecuteOptions } from '../../../actions/server/create_execute_function' ;
43+ import moment from 'moment' ;
4344
4445const alertType : jest . Mocked < UntypedNormalizedAlertType > = {
4546 id : 'test' ,
@@ -55,6 +56,10 @@ const alertType: jest.Mocked<UntypedNormalizedAlertType> = {
5556
5657let fakeTimer : sinon . SinonFakeTimers ;
5758
59+ export const mockRunNowResponse = {
60+ id : 1 ,
61+ } as jest . ResolvedValue < unknown > ;
62+
5863describe ( 'Task Runner' , ( ) => {
5964 let mockedTaskInstance : ConcreteTaskInstance ;
6065
@@ -865,7 +870,136 @@ describe('Task Runner', () => {
865870 }
866871 ) ;
867872
868- test ( 'actionsPlugin.execute is not called when notifyWhen=onActionGroupChange and alert instance state does not change' , async ( ) => {
873+ testAgainstEphemeralSupport (
874+ 'skips firing actions for active alert if alert is throttled %s' ,
875+ (
876+ customTaskRunnerFactoryInitializerParams : TaskRunnerFactoryInitializerParamsType ,
877+ enqueueFunction : ( options : ExecuteOptions ) => Promise < void | RunNowResult >
878+ ) =>
879+ async ( ) => {
880+ (
881+ customTaskRunnerFactoryInitializerParams as TaskRunnerFactoryInitializerParamsType
882+ ) . actionsPlugin . isActionTypeEnabled . mockReturnValue ( true ) ;
883+ customTaskRunnerFactoryInitializerParams . actionsPlugin . isActionExecutable . mockReturnValue (
884+ true
885+ ) ;
886+ actionsClient . ephemeralEnqueuedExecution . mockResolvedValue ( mockRunNowResponse ) ;
887+ alertType . executor . mockImplementation (
888+ async ( {
889+ services : executorServices ,
890+ } : AlertExecutorOptions <
891+ AlertTypeParams ,
892+ AlertTypeState ,
893+ AlertInstanceState ,
894+ AlertInstanceContext ,
895+ string
896+ > ) => {
897+ executorServices . alertInstanceFactory ( '1' ) . scheduleActions ( 'default' ) ;
898+ executorServices . alertInstanceFactory ( '2' ) . scheduleActions ( 'default' ) ;
899+ }
900+ ) ;
901+ const taskRunner = new TaskRunner (
902+ alertType ,
903+ {
904+ ...mockedTaskInstance ,
905+ state : {
906+ ...mockedTaskInstance . state ,
907+ alertInstances : {
908+ '2' : {
909+ meta : {
910+ lastScheduledActions : { date : moment ( ) . toISOString ( ) , group : 'default' } ,
911+ } ,
912+ state : {
913+ bar : false ,
914+ start : '1969-12-31T00:00:00.000Z' ,
915+ duration : 86400000000000 ,
916+ } ,
917+ } ,
918+ } ,
919+ } ,
920+ } ,
921+ taskRunnerFactoryInitializerParams
922+ ) ;
923+ rulesClient . get . mockResolvedValue ( {
924+ ...mockedAlertTypeSavedObject ,
925+ throttle : '1d' ,
926+ } ) ;
927+ encryptedSavedObjectsClient . getDecryptedAsInternalUser . mockResolvedValue ( {
928+ id : '1' ,
929+ type : 'alert' ,
930+ attributes : {
931+ apiKey : Buffer . from ( '123:abc' ) . toString ( 'base64' ) ,
932+ enabled : true ,
933+ } ,
934+ references : [ ] ,
935+ } ) ;
936+ await taskRunner . run ( ) ;
937+ // expect(enqueueFunction).toHaveBeenCalledTimes(1);
938+
939+ const logger = customTaskRunnerFactoryInitializerParams . logger ;
940+ expect ( logger . debug ) . toHaveBeenCalledTimes ( 4 ) ;
941+ expect ( logger . debug ) . nthCalledWith (
942+ 3 ,
943+ `skipping scheduling of actions for '2' in alert test:1: 'alert-name': instance is throttled`
944+ ) ;
945+ }
946+ ) ;
947+
948+ testAgainstEphemeralSupport (
949+ 'skips firing actions for active alert when alert is muted even if notifyWhen === onActionGroupChange %s' ,
950+ (
951+ customTaskRunnerFactoryInitializerParams : TaskRunnerFactoryInitializerParamsType ,
952+ enqueueFunction : ( options : ExecuteOptions ) => Promise < void | RunNowResult >
953+ ) =>
954+ async ( ) => {
955+ customTaskRunnerFactoryInitializerParams . actionsPlugin . isActionExecutable . mockReturnValue (
956+ true
957+ ) ;
958+ alertType . executor . mockImplementation (
959+ async ( {
960+ services : executorServices ,
961+ } : AlertExecutorOptions <
962+ AlertTypeParams ,
963+ AlertTypeState ,
964+ AlertInstanceState ,
965+ AlertInstanceContext ,
966+ string
967+ > ) => {
968+ executorServices . alertInstanceFactory ( '1' ) . scheduleActions ( 'default' ) ;
969+ executorServices . alertInstanceFactory ( '2' ) . scheduleActions ( 'default' ) ;
970+ }
971+ ) ;
972+ const taskRunner = new TaskRunner (
973+ alertType ,
974+ mockedTaskInstance ,
975+ customTaskRunnerFactoryInitializerParams
976+ ) ;
977+ rulesClient . get . mockResolvedValue ( {
978+ ...mockedAlertTypeSavedObject ,
979+ mutedInstanceIds : [ '2' ] ,
980+ notifyWhen : 'onActionGroupChange' ,
981+ } ) ;
982+ encryptedSavedObjectsClient . getDecryptedAsInternalUser . mockResolvedValue ( {
983+ id : '1' ,
984+ type : 'alert' ,
985+ attributes : {
986+ apiKey : Buffer . from ( '123:abc' ) . toString ( 'base64' ) ,
987+ enabled : true ,
988+ } ,
989+ references : [ ] ,
990+ } ) ;
991+ await taskRunner . run ( ) ;
992+ expect ( enqueueFunction ) . toHaveBeenCalledTimes ( 1 ) ;
993+ const logger = customTaskRunnerFactoryInitializerParams . logger ;
994+ expect ( logger . debug ) . toHaveBeenCalledTimes ( 4 ) ;
995+ expect ( logger . debug ) . nthCalledWith (
996+ 3 ,
997+ `skipping scheduling of actions for '2' in alert test:1: 'alert-name': instance is muted`
998+ ) ;
999+ }
1000+ ) ;
1001+
1002+ test ( 'actionsPlugin.execute is not called when notifyWhen=onActionGroupChange and alert state does not change' , async ( ) => {
8691003 taskRunnerFactoryInitializerParams . actionsPlugin . isActionTypeEnabled . mockReturnValue ( true ) ;
8701004 taskRunnerFactoryInitializerParams . actionsPlugin . isActionExecutable . mockReturnValue ( true ) ;
8711005 alertType . executor . mockImplementation (
0 commit comments