import { AnomalyExplorerChartsService } from './anomaly_explorer_charts_service' ; import mockAnomalyChartRecords from '../explorer/explorer_charts/__mocks__/mock_anomaly_chart_records.json' ; import mockJobConfig from '../explorer/explorer_charts/__mocks__/mock_job_config.json' ; import mockSeriesPromisesResponse from '../explorer/explorer_charts/__mocks__/mock_series_promises_response.json' ; import { of } from 'rxjs' ; import { cloneDeep } from 'lodash' ; import type { CombinedJob } from '../../../common/types/anomaly_detection_jobs' ; import type { ExplorerChartsData } from '../explorer/explorer_charts/explorer_charts_container_service' ; import type { MlApiServices } from './ml_api_service' ; import type { MlResultsService } from './results_service' ; import { getDefaultChartsData } from '../explorer/explorer_charts/explorer_charts_container_service' ; import { timefilterMock } from '../contexts/kibana/__mocks__/use_timefilter' ; import { mlApiServicesMock } from './__mocks__/ml_api_services' ; const mockJobConfigClone = cloneDeep ( mockJobConfig ) ; const mockMetricClone = cloneDeep ( mockSeriesPromisesResponse [ 0 ] [ 0 ] ) ; mockMetricClone . results [ '1486712700000' ] = null ; mockMetricClone . results [ '1486713600000' ] = 0 ; export const mlResultsServiceMock = { getMetricData : jest . fn ( ( indices ) => { if ( indices [ 0 ] === 'farequote-2017' ) { return of ( mockSeriesPromisesResponse [ 0 ] [ 0 ] ) ; } return of ( mockMetricClone ) ; } ) , getRecordsForCriteria : jest . fn ( ( ) => { return of ( mockSeriesPromisesResponse [ 0 ] [ 1 ] ) ; } ) , getScheduledEventsByBucket : jest . fn ( ( ) => of ( mockSeriesPromisesResponse [ 0 ] [ 2 ] ) ) , getEventDistributionData : jest . fn ( ( indices ) => { if ( indices [ 0 ] === 'farequote-2017' ) { return Promise . resolve ( [ ] ) ; } return Promise . resolve ( [ { entity : 'mock' , } , ] ) ; } ) , } ; const assertAnomalyDataResult = ( anomalyData ) => { expect ( anomalyData . chartsPerRow ) . toBe ( 1 ) ; expect ( Array . isArray ( anomalyData . seriesToPlot ) ) . toBe ( true ) ; expect ( anomalyData . seriesToPlot . length ) . toBe ( 1 ) ; expect ( anomalyData . errorMessages ) . toMatchObject ( { } ) ; expect ( anomalyData . tooManyBuckets ) . toBe ( false ) ; expect ( anomalyData . timeFieldName ) . toBe ( 'timestamp' ) ; } ; describe ( 'AnomalyExplorerChartsService' , ( ) => { const jobId = 'mock-job-id' ; const combinedJobRecords = { [ jobId ] : mockJobConfigClone , } ; const anomalyExplorerService = new AnomalyExplorerChartsService ( timefilterMock , mlApiServicesMock as unknown as MlApiServices , mlResultsServiceMock as unknown as MlResultsService ) ; const timeRange = { earliestMs : 1486656000000 , latestMs : 1486670399999 , } ; beforeEach ( ( ) => { mlApiServicesMock . jobs . jobForCloning . mockImplementation ( ( ) => Promise . resolve ( { job : mockJobConfigClone , datafeed : mockJobConfigClone . datafeed_config } ) ) ; } ) ; test ( 'should return anomaly data without explorer service' , async ( ) => { const anomalyData = ( await anomalyExplorerService . getAnomalyData ( undefined , combinedJobRecords as unknown as Record < string , CombinedJob > , 1000 , mockAnomalyChartRecords , timeRange . earliestMs , timeRange . latestMs , timefilterMock , 0 , 12 ) ) as ExplorerChartsData ; assertAnomalyDataResult ( anomalyData ) ; } ) ; test ( 'call anomalyChangeListener with empty series config' , async ( ) => { const anomalyData = ( await anomalyExplorerService . getAnomalyData ( undefined , combinedJobRecords as unknown as Record < string , CombinedJob > , 1000 , [ ] , timeRange . earliestMs , timeRange . latestMs , timefilterMock , 0 , 12 ) ) as ExplorerChartsData ; expect ( anomalyData ) . toStrictEqual ( { ... getDefaultChartsData ( ) , chartsPerRow : 2 , } ) ; } ) ; test ( 'field value with trailing dot should not throw an error' , async ( ) => { const mockAnomalyChartRecordsClone = cloneDeep ( mockAnomalyChartRecords ) ; mockAnomalyChartRecordsClone [ 1 ] . partition_field_value = 'AAL.' ; const anomalyData = ( await anomalyExplorerService . getAnomalyData ( undefined , combinedJobRecords as unknown as Record < string , CombinedJob > , 1000 , mockAnomalyChartRecordsClone , timeRange . earliestMs , timeRange . latestMs , timefilterMock , 0 , 12 ) ) as ExplorerChartsData ; expect ( anomalyData ) . toBeDefined ( ) ; expect ( anomalyData ! . chartsPerRow ) . toBe ( 2 ) ; expect ( Array . isArray ( anomalyData ! . seriesToPlot ) ) . toBe ( true ) ; expect ( anomalyData ! . seriesToPlot . length ) . toBe ( 2 ) ; } ) ; } ) ;