import { retryCallClusterMock } from './repository_opensearch_client.test.mock' ; import { createRepositoryOpenSearchClient , RepositoryOpenSearchClient , } from './repository_opensearch_client' ; import { opensearchClientMock } from '../../../opensearch/client/mocks' ; import { SavedObjectsErrorHelpers } from './errors' ; describe ( 'RepositoryOpenSearchClient' , ( ) => { let client ; let repositoryClient ; beforeEach ( ( ) => { client = opensearchClientMock . createOpenSearchClient ( ) ; repositoryClient = createRepositoryOpenSearchClient ( client ) ; retryCallClusterMock . mockClear ( ) ; } ) ; it ( 'delegates call to OpenSearch client method' , async ( ) => { expect ( repositoryClient . bulk ) . toStrictEqual ( expect . any ( Function ) ) ; await repositoryClient . bulk ( { body : [ ] } ) ; expect ( client . bulk ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'wraps a method call in retryCallCluster' , async ( ) => { await repositoryClient . bulk ( { body : [ ] } ) ; expect ( retryCallClusterMock ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'sets maxRetries: 0 to delegate retry logic to retryCallCluster' , async ( ) => { expect ( repositoryClient . bulk ) . toStrictEqual ( expect . any ( Function ) ) ; await repositoryClient . bulk ( { body : [ ] } ) ; expect ( client . bulk ) . toHaveBeenCalledWith ( expect . any ( Object ) , expect . objectContaining ( { maxRetries : 0 } ) ) ; } ) ; it ( 'transform opensearch errors into saved objects errors' , async ( ) => { expect . assertions ( 1 ) ; client . bulk = jest . fn ( ) . mockRejectedValue ( new Error ( 'reason' ) ) ; try { await repositoryClient . bulk ( { body : [ ] } ) ; } catch ( e ) { expect ( SavedObjectsErrorHelpers . isSavedObjectsClientError ( e ) ) . toBe ( true ) ; } } ) ; } ) ;