import sinon from 'sinon' ; import { ConcreteTaskInstance , TaskStatus } from '../../../task_manager/server' ; import { TaskRunnerContext , TaskRunnerFactory } from './task_runner_factory' ; import { encryptedSavedObjectsMock } from '../../../encrypted_saved_objects/server/mocks' ; import { loggingSystemMock , savedObjectsRepositoryMock , httpServiceMock , } from '../../../../../src/core/server/mocks' ; import { actionsMock } from '../../../actions/server/mocks' ; import { alertsMock , rulesClientMock } from '../mocks' ; import { eventLoggerMock } from '../../../event_log/server/event_logger.mock' ; import { UntypedNormalizedRuleType } from '../rule_type_registry' ; import { ruleTypeRegistryMock } from '../rule_type_registry.mock' ; import { executionContextServiceMock } from '../../../../../src/core/server/mocks' ; const executionContext = executionContextServiceMock . createSetupContract ( ) ; const ruleType = { id : 'test' , name : 'My test alert' , actionGroups : [ { id : 'default' , name : 'Default' } ] , defaultActionGroupId : 'default' , minimumLicenseRequired : 'basic' , isExportable : true , recoveryActionGroup : { id : 'recovered' , name : 'Recovered' , } , executor : jest . fn ( ) , producer : 'alerts' , } ; let fakeTimer ; describe ( 'Task Runner Factory' , ( ) => { let mockedTaskInstance ; beforeAll ( ( ) => { fakeTimer = sinon . useFakeTimers ( ) ; mockedTaskInstance = { id : '' , attempts : 0 , status : TaskStatus . Running , version : '123' , runAt : new Date ( ) , scheduledAt : new Date ( ) , startedAt : new Date ( ) , retryAt : new Date ( Date . now ( ) + 5 * 60 * 1000 ) , state : { startedAt : new Date ( Date . now ( ) - 5 * 60 * 1000 ) , } , taskType : 'alerting:test' , params : { alertId : '1' , } , ownerId : null , } ; } ) ; afterAll ( ( ) => fakeTimer . restore ( ) ) ; const encryptedSavedObjectsPlugin = encryptedSavedObjectsMock . createStart ( ) ; const services = alertsMock . createAlertServices ( ) ; const rulesClient = rulesClientMock . create ( ) ; const taskRunnerFactoryInitializerParams = { getServices : jest . fn ( ) . mockReturnValue ( services ) , getRulesClientWithRequest : jest . fn ( ) . mockReturnValue ( rulesClient ) , actionsPlugin : actionsMock . createStart ( ) , encryptedSavedObjectsClient : encryptedSavedObjectsPlugin . getClient ( ) , logger : loggingSystemMock . create ( ) . get ( ) , spaceIdToNamespace : jest . fn ( ) . mockReturnValue ( undefined ) , basePathService : httpServiceMock . createBasePath ( ) , eventLogger : eventLoggerMock . create ( ) , internalSavedObjectsRepository : savedObjectsRepositoryMock . create ( ) , ruleTypeRegistry : ruleTypeRegistryMock . create ( ) , kibanaBaseUrl : 'https://localhost:5601' , supportsEphemeralTasks : true , maxEphemeralActionsPerRule : 10 , cancelAlertsOnRuleTimeout : true , executionContext , } ; beforeEach ( ( ) => { jest . resetAllMocks ( ) ; taskRunnerFactoryInitializerParams . getServices . mockReturnValue ( services ) ; } ) ; test ( `throws an error if factory isn't initialized` , ( ) => { const factory = new TaskRunnerFactory ( ) ; expect ( ( ) => factory . create ( ruleType , { taskInstance : mockedTaskInstance } ) ) . toThrowErrorMatchingInlineSnapshot ( `"TaskRunnerFactory not initialized"` ) ; } ) ; test ( `throws an error if factory is already initialized` , ( ) => { const factory = new TaskRunnerFactory ( ) ; factory . initialize ( taskRunnerFactoryInitializerParams ) ; expect ( ( ) => factory . initialize ( taskRunnerFactoryInitializerParams ) ) . toThrowErrorMatchingInlineSnapshot ( `"TaskRunnerFactory already initialized"` ) ; } ) ; } ) ;