import 'reflect-metadata' ; import { AvailabilityTestConfig , ServiceConfiguration } from 'common' ; import * as durableFunctions from 'durable-functions' ; import { IOrchestrationFunctionContext } from 'durable-functions/lib/src/classes' ; import { TestEnvironment } from 'functional-tests' ; import { It , Mock } from 'typemoq' ; import { ContextBindingData } from '@azure/functions' ; import { OrchestrationSteps } from '../orchestration/orchestration-steps' ; import { GeneratorExecutor } from '../test-utilities/generator-executor' ; import { MockableLogger } from '../test-utilities/mockable-logger' ; import { finalizerTestGroupName } from '../e2e-test-group-names' ; import { generatorStub } from '../test-utilities/generator-function' ; import { ScanScenarioDriver } from '../e2e-test-scenarios/scan-scenario-driver' ; import { HealthMonitorOrchestrationController } from './health-monitor-orchestration-controller' ; import { TestIdentifier } from './activity-request-data' ; describe ( 'HealthMonitorOrchestrationController' , ( ) => { let testSubject ; let orchestratorIterator ; const scanScenarioDriverMock = Mock . ofType < ScanScenarioDriver > ( ) ; const orchestrationStepsMock = Mock . ofType < OrchestrationSteps > ( ) ; const loggerMock = Mock . ofType ( MockableLogger ) ; const allTestIdentifiers = [ { testGroupName : 'PostScan' , scenarioName : 'TestScenario' } ] ; const availabilityTestConfig = { urlToScan : 'some-url' , scanWaitIntervalInSeconds : 10 , maxScanWaitTimeInSeconds : 20 , logQueryTimeRange : 'P1D' , environmentDefinition : TestEnvironment [ TestEnvironment . canary ] , consolidatedIdBase : 'some-report-id' , scanNotifyApiEndpoint : '/some-endpoint' , maxScanCompletionNotificationWaitTimeInSeconds : 30 , scanNotifyFailApiEndpoint : '/some-fail-endpoint' , maxDeepScanWaitTimeInSeconds : 40 , } ; const contextStub = { bindingData : { } , executionContext : { functionName : 'function-name' , invocationId : 'id' , } , } as unknown as IOrchestrationFunctionContext ; beforeEach ( ( ) => { contextStub . bindingData = { } as unknown as ContextBindingData ; const serviceConfigurationMock = Mock . ofType ( ServiceConfiguration ) ; serviceConfigurationMock . setup ( async ( sc ) => sc . getConfigValue ( 'availabilityTestConfig' ) ) . returns ( async ( ) => availabilityTestConfig ) ; const df = Mock . ofType < typeof durableFunctions > ( undefined ) ; const orchestratorGeneratorMock = Mock . ofInstance < ( contextObj : IOrchestrationFunctionContext ) => void > ( ( ) => { } ) ; df . setup ( ( d ) => d . orchestrator ( It . isAny ( ) ) ) . callback ( ( fn ) => { orchestratorIterator = new GeneratorExecutor ( fn ( contextStub ) ) ; } ) . returns ( ( ) => orchestratorGeneratorMock . object ) ; testSubject = new HealthMonitorOrchestrationController ( serviceConfigurationMock . object , loggerMock . object , df . object , ( _ , __ ) => [ scanScenarioDriverMock . object ] , ( _ , __ , ___ ) => generatorStub ( ( ) => null , orchestrationStepsMock . object ) , ( _ ) => allTestIdentifiers ) ; } ) ; it ( 'does not invoke orchestrator executor on construction' , ( ) => { expect ( orchestratorIterator ) . toBeUndefined ( ) ; } ) ; describe ( 'invoke' , ( ) => { it ( 'sets context required for orchestrator execution' , async ( ) => { await testSubject . invoke ( contextStub ) ; expect ( contextStub . bindingData . controller ) . toBe ( testSubject ) ; expect ( contextStub . bindingData . availabilityTestConfig ) . toEqual ( availabilityTestConfig ) ; } ) ; it ( 'executes activities in sequence' , async ( ) => { await testSubject . invoke ( contextStub ) ; orchestrationStepsMock . setup ( ( m ) => m . logTestRunStart ( allTestIdentifiers ) ) . returns ( ( _ ) => generatorStub ( ) ) . verifiable ( ) ; orchestrationStepsMock . setup ( ( m ) => m . invokeHealthCheckRestApi ( ) ) . returns ( ( _ ) => generatorStub ( ) ) . verifiable ( ) ; orchestrationStepsMock . setup ( ( m ) => m . runFunctionalTestGroups ( It . isAny ( ) , It . isAny ( ) , [ finalizerTestGroupName ] ) ) . returns ( ( _ ) => generatorStub ( ) ) . verifiable ( ) ; scanScenarioDriverMock . setup ( ( m ) => m . submitScanPhase ( ) ) . returns ( ( _ ) => generatorStub ( ) ) . verifiable ( ) ; scanScenarioDriverMock . setup ( ( m ) => m . waitForScanCompletionPhase ( ) ) . returns ( ( _ ) => generatorStub ( ) ) . verifiable ( ) ; scanScenarioDriverMock . setup ( ( m ) => m . afterScanCompletedPhase ( ) ) . returns ( ( _ ) => generatorStub ( ) ) . verifiable ( ) ; orchestratorIterator . runTillEnd ( ) ; orchestrationStepsMock . verifyAll ( ) ; scanScenarioDriverMock . verifyAll ( ) ; } ) ; } ) ; } ) ;