import { BehaviorSubject } from 'rxjs' ; import type { PublicMethodsOf } from '@osd/utility-types' ; import { ILegacyClusterClient , ILegacyCustomClusterClient } from './legacy' ; import { opensearchClientMock , ClusterClientMock , CustomClusterClientMock } from './client/mocks' ; import { OpenSearchClientConfig } from './client' ; import { legacyClientMock } from './legacy/mocks' ; import { OpenSearchConfig } from './opensearch_config' ; import { OpenSearchService } from './opensearch_service' ; import { InternalOpenSearchServiceSetup , OpenSearchStatusMeta } from './types' ; import { NodesVersionCompatibility } from './version_check/ensure_opensearch_version' ; import { ServiceStatus , ServiceStatusLevels } from '../status' ; export interface MockedOpenSearchServiceSetup { legacy : { config$ : BehaviorSubject < OpenSearchConfig > ; createClient : jest . Mock < ILegacyCustomClusterClient , any > ; client : jest . Mocked < ILegacyClusterClient > ; } ; } type MockedOpenSearchServiceStart = MockedOpenSearchServiceSetup & { client : ClusterClientMock ; createClient : jest . MockedFunction < ( name : string , config ? : Partial < OpenSearchClientConfig > ) => CustomClusterClientMock > ; } ; const createSetupContractMock = ( ) => { const setupContract = { legacy : { config$ : new BehaviorSubject ( { } as OpenSearchConfig ) , createClient : jest . fn ( ) , client : legacyClientMock . createClusterClient ( ) , } , } ; setupContract . legacy . createClient . mockReturnValue ( legacyClientMock . createCustomClusterClient ( ) ) ; setupContract . legacy . client . asScoped . mockReturnValue ( legacyClientMock . createScopedClusterClient ( ) ) ; return setupContract ; } ; const createStartContractMock = ( ) => { const startContract = { client : opensearchClientMock . createClusterClient ( ) , createClient : jest . fn ( ) , legacy : { config$ : new BehaviorSubject ( { } as OpenSearchConfig ) , createClient : jest . fn ( ) , client : legacyClientMock . createClusterClient ( ) , } , } ; startContract . legacy . createClient . mockReturnValue ( legacyClientMock . createCustomClusterClient ( ) ) ; startContract . legacy . client . asScoped . mockReturnValue ( legacyClientMock . createScopedClusterClient ( ) ) ; startContract . createClient . mockImplementation ( ( ) => opensearchClientMock . createCustomClusterClient ( ) ) ; return startContract ; } ; const createInternalStartContractMock = createStartContractMock ; type MockedInternalOpenSearchServiceSetup = jest . Mocked < InternalOpenSearchServiceSetup & { legacy : { client : jest . Mocked < ILegacyClusterClient > ; } ; } > ; const createInternalSetupContractMock = ( ) => { const setupContract = { opensearchNodesCompatibility$ : new BehaviorSubject < NodesVersionCompatibility > ( { isCompatible : true , incompatibleNodes : [ ] , warningNodes : [ ] , opensearchDashboardsVersion : '8.0.0' , } ) , status$ : new BehaviorSubject < ServiceStatus < OpenSearchStatusMeta > > ( { level : ServiceStatusLevels . available , summary : 'OpenSearch is available' , } ) , legacy : { ... createSetupContractMock ( ) . legacy , } , } ; setupContract . legacy . client . asScoped . mockReturnValue ( legacyClientMock . createScopedClusterClient ( ) ) ; return setupContract ; } ; type OpenSearchServiceContract = PublicMethodsOf < OpenSearchService > ; const createMock = ( ) => { const mocked = { setup : jest . fn ( ) , start : jest . fn ( ) , stop : jest . fn ( ) , } ; mocked . setup . mockResolvedValue ( createInternalSetupContractMock ( ) ) ; mocked . start . mockResolvedValueOnce ( createInternalStartContractMock ( ) ) ; mocked . stop . mockResolvedValue ( ) ; return mocked ; } ; export const opensearchServiceMock = { create : createMock , createInternalSetup : createInternalSetupContractMock , createSetup : createSetupContractMock , createInternalStart : createInternalStartContractMock , createStart : createStartContractMock , createLegacyClusterClient : legacyClientMock . createClusterClient , createLegacyCustomClusterClient : legacyClientMock . createCustomClusterClient , createLegacyScopedClusterClient : legacyClientMock . createScopedClusterClient , createLegacyOpenSearchClient : legacyClientMock . createOpenSearchClient , ... opensearchClientMock , } ;