import { rulesClientMock } from './rules_client.mock' ; import { PluginSetupContract , PluginStartContract } from './plugin' ; import { AlertInstance } from './alert_instance' ; import { elasticsearchServiceMock , savedObjectsClientMock , } from '../../../../src/core/server/mocks' ; import { AlertInstanceContext , AlertInstanceState } from './types' ; export { rulesClientMock } ; const createSetupMock = ( ) => { const mock = { registerType : jest . fn ( ) , getSecurityHealth : jest . fn ( ) , } ; return mock ; } ; const createStartMock = ( ) => { const mock = { listTypes : jest . fn ( ) , getAlertingAuthorizationWithRequest : jest . fn ( ) , getRulesClientWithRequest : jest . fn ( ) . mockResolvedValue ( rulesClientMock . create ( ) ) , getFrameworkHealth : jest . fn ( ) , } ; return mock ; } ; export type AlertInstanceMock < State extends AlertInstanceState = AlertInstanceState , Context extends AlertInstanceContext = AlertInstanceContext > = jest . Mocked < AlertInstance < State , Context > > ; const createAlertInstanceFactoryMock = < InstanceState extends AlertInstanceState = AlertInstanceState , InstanceContext extends AlertInstanceContext = AlertInstanceContext > ( ) => { const mock = { hasScheduledActions : jest . fn ( ) , isThrottled : jest . fn ( ) , getScheduledActionOptions : jest . fn ( ) , unscheduleActions : jest . fn ( ) , getState : jest . fn ( ) , scheduleActions : jest . fn ( ) , replaceState : jest . fn ( ) , updateLastScheduledActions : jest . fn ( ) , toJSON : jest . fn ( ) , toRaw : jest . fn ( ) , } ; mock . replaceState . mockReturnValue ( mock ) ; mock . unscheduleActions . mockReturnValue ( mock ) ; mock . scheduleActions . mockReturnValue ( mock ) ; return mock as unknown as AlertInstanceMock < InstanceState , InstanceContext > ; } ; const createAbortableSearchClientMock = ( ) => { const mock = { search : jest . fn ( ) , } ; return mock ; } ; const createAbortableSearchServiceMock = ( ) => { return { asInternalUser : createAbortableSearchClientMock ( ) , asCurrentUser : createAbortableSearchClientMock ( ) , } ; } ; const createAlertServicesMock = < InstanceState extends AlertInstanceState = AlertInstanceState , InstanceContext extends AlertInstanceContext = AlertInstanceContext > ( ) => { const alertInstanceFactoryMock = createAlertInstanceFactoryMock < InstanceState , InstanceContext > ( ) ; return { alertInstanceFactory : jest . fn < jest . Mocked < AlertInstance < InstanceState , InstanceContext > > , [ string ] > ( ) . mockReturnValue ( alertInstanceFactoryMock ) , savedObjectsClient : savedObjectsClientMock . create ( ) , scopedClusterClient : elasticsearchServiceMock . createScopedClusterClient ( ) , shouldWriteAlerts : ( ) => true , shouldStopExecution : ( ) => true , search : createAbortableSearchServiceMock ( ) , } ; } ; export type AlertServicesMock = ReturnType < typeof createAlertServicesMock > ; export const alertsMock = { createAlertInstanceFactory : createAlertInstanceFactoryMock , createSetup : createSetupMock , createStart : createStartMock , createAlertServices : createAlertServicesMock , } ;