import { migrationRetryCallClusterMock } from './migration_opensearch_client.test.mock' ; import { createMigrationOpenSearchClient , MigrationOpenSearchClient , } from './migration_opensearch_client' ; import { opensearchClientMock } from '../../../opensearch/client/mocks' ; import { loggerMock } from '../../../logging/logger.mock' ; import { SavedObjectsErrorHelpers } from '../../service/lib/errors' ; describe ( 'MigrationOpenSearchClient' , ( ) => { let client ; let migrationOpenSearchClient ; beforeEach ( ( ) => { client = opensearchClientMock . createOpenSearchClient ( ) ; migrationOpenSearchClient = createMigrationOpenSearchClient ( client , loggerMock . create ( ) ) ; migrationRetryCallClusterMock . mockClear ( ) ; } ) ; it ( 'delegates call to OpenSearch client method' , async ( ) => { expect ( migrationOpenSearchClient . bulk ) . toStrictEqual ( expect . any ( Function ) ) ; await migrationOpenSearchClient . bulk ( { body : [ ] } ) ; expect ( client . bulk ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'wraps a method call in migrationRetryCallClusterMock' , async ( ) => { await migrationOpenSearchClient . bulk ( { body : [ ] } ) ; expect ( migrationRetryCallClusterMock ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'sets maxRetries: 0 to delegate retry logic to migrationRetryCallCluster' , async ( ) => { expect ( migrationOpenSearchClient . bulk ) . toStrictEqual ( expect . any ( Function ) ) ; await migrationOpenSearchClient . bulk ( { body : [ ] } ) ; expect ( client . bulk ) . toHaveBeenCalledWith ( expect . any ( Object ) , expect . objectContaining ( { maxRetries : 0 } ) ) ; } ) ; it ( 'do not transform opensearch errors into saved objects errors' , async ( ) => { expect . assertions ( 1 ) ; client . bulk = jest . fn ( ) . mockRejectedValue ( new Error ( 'reason' ) ) ; try { await migrationOpenSearchClient . bulk ( { body : [ ] } ) ; } catch ( e ) { expect ( SavedObjectsErrorHelpers . isSavedObjectsClientError ( e ) ) . toBe ( false ) ; } } ) ; } ) ;