import type { EncryptedSavedObjectsService , EncryptedSavedObjectTypeRegistration , SavedObjectDescriptor , } from './encrypted_saved_objects_service' ; function createEncryptedSavedObjectsServiceMock ( ) { return { isRegistered : jest . fn ( ) , stripOrDecryptAttributes : jest . fn ( ) , encryptAttributes : jest . fn ( ) , decryptAttributes : jest . fn ( ) , encryptAttributesSync : jest . fn ( ) , decryptAttributesSync : jest . fn ( ) , stripOrDecryptAttributesSync : jest . fn ( ) , } as unknown as jest . Mocked < EncryptedSavedObjectsService > ; } export const encryptedSavedObjectsServiceMock = { create : createEncryptedSavedObjectsServiceMock , createWithTypes ( registrations = [ ] ) { const mock = createEncryptedSavedObjectsServiceMock ( ) ; function processAttributes < T extends Record < string , any > > ( descriptor , attrs , action ) { const registration = registrations . find ( ( r ) => r . type === descriptor . type ) ; if ( ! registration ) { return attrs ; } const clonedAttrs = { ... attrs } ; for ( const attr of registration . attributesToEncrypt ) { const [ attrName , shouldExpose ] = typeof attr === 'string' ? [ attr , false ] : [ attr . key , attr . dangerouslyExposeValue === true ] ; if ( attrName in clonedAttrs ) { action ( clonedAttrs , attrName , shouldExpose ) ; } } return clonedAttrs ; } mock . isRegistered . mockImplementation ( ( type ) => registrations . findIndex ( ( r ) => r . type === type ) >= 0 ) ; mock . encryptAttributes . mockImplementation ( async ( descriptor , attrs ) => processAttributes ( descriptor , attrs , ( clonedAttrs , attrName ) => ( clonedAttrs [ attrName ] = `*${ clonedAttrs [ attrName ] }*` ) ) ) ; mock . decryptAttributes . mockImplementation ( async ( descriptor , attrs ) => processAttributes ( descriptor , attrs , ( clonedAttrs , attrName ) => ( clonedAttrs [ attrName ] = ( clonedAttrs [ attrName ] as string ) . slice ( 1 , - 1 ) ) ) ) ; mock . stripOrDecryptAttributes . mockImplementation ( ( descriptor , attrs ) => Promise . resolve ( { attributes : processAttributes ( descriptor , attrs , ( clonedAttrs , attrName , shouldExpose ) => { if ( shouldExpose ) { clonedAttrs [ attrName ] = ( clonedAttrs [ attrName ] as string ) . slice ( 1 , - 1 ) ; } else { delete clonedAttrs [ attrName ] ; } } ) , } ) ) ; return mock ; } , } ;