From 2ae102cdef55ea81d72bf7a4ebc6d0107f024554 Mon Sep 17 00:00:00 2001 From: PatrykBuniX Date: Wed, 15 Nov 2023 13:12:36 +0100 Subject: [PATCH] test: inject migration finalisation when call is ongoing --- .../ConversationRepository.test.ts | 49 +++++++++++++++++++ .../conversation/ConversationRepository.ts | 5 +- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/script/conversation/ConversationRepository.test.ts b/src/script/conversation/ConversationRepository.test.ts index c8c59b781ba..0ab41e0630d 100644 --- a/src/script/conversation/ConversationRepository.test.ts +++ b/src/script/conversation/ConversationRepository.test.ts @@ -2073,6 +2073,10 @@ describe('ConversationRepository', () => { }); describe('updateConversationProtocol', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + it('should update the protocol-related fields after protocol was updated to mixed and inject event', async () => { const conversation = _generateConversation(); const conversationRepository = await testFactory.exposeConversationActors(); @@ -2100,6 +2104,7 @@ describe('ConversationRepository', () => { protocol: newProtocol, overwites: {cipher_suite: newCipherSuite, epoch: newEpoch}, }); + jest .spyOn(conversationRepository['conversationService'], 'getConversationById') .mockResolvedValueOnce(mockedConversationResponse); @@ -2120,6 +2125,50 @@ describe('ConversationRepository', () => { expect(updatedConversation.cipherSuite).toEqual(newCipherSuite); expect(updatedConversation.epoch).toEqual(newEpoch); }); + + it('should inject a system message if conversation protocol changed to mls during a call', async () => { + jest.useFakeTimers(); + const conversation = _generateConversation(); + const selfUser = generateUser(); + conversation.selfUser(selfUser); + const conversationRepository = await testFactory.exposeConversationActors(); + const newProtocol = ConversationProtocol.MLS; + + const mockedProtocolUpdateEventResponse = { + data: { + protocol: newProtocol, + }, + qualified_conversation: conversation.qualifiedId, + time: '2020-10-13T14:00:00.000Z', + type: CONVERSATION_EVENT.PROTOCOL_UPDATE, + } as ConversationProtocolUpdateEvent; + + jest + .spyOn(conversationRepository['conversationService'], 'updateConversationProtocol') + .mockResolvedValueOnce(mockedProtocolUpdateEventResponse); + + const newCipherSuite = 1; + const newEpoch = 2; + const mockedConversationResponse = generateAPIConversation({ + protocol: newProtocol, + overwites: {cipher_suite: newCipherSuite, epoch: newEpoch}, + }); + jest + .spyOn(conversationRepository['conversationService'], 'getConversationById') + .mockResolvedValueOnce(mockedConversationResponse); + + const injectEventMock = jest + .spyOn(conversationRepository['eventRepository'], 'injectEvent') + .mockImplementation(jest.fn()); + jest.spyOn(conversationRepository['callingRepository'], 'findCall').mockReturnValue({} as any); + + await conversationRepository.updateConversationProtocol(conversation, newProtocol); + + expect(injectEventMock.mock.calls).toEqual([ + [mockedProtocolUpdateEventResponse, EventRepository.SOURCE.BACKEND_RESPONSE], + [expect.objectContaining({type: ClientEvent.CONVERSATION.MLS_MIGRATION_FINALISATION_ONGOING_CALL})], + ]); + }); }); describe('addUsers', () => { diff --git a/src/script/conversation/ConversationRepository.ts b/src/script/conversation/ConversationRepository.ts index 063bee2ef5e..8618b24eca0 100644 --- a/src/script/conversation/ConversationRepository.ts +++ b/src/script/conversation/ConversationRepository.ts @@ -3084,11 +3084,12 @@ export class ConversationRepository { private readonly injectMLSMigrationFinalisationOngoingCallMessage = (conversation: Conversation): void => { const currentTimestamp = this.serverTimeHandler.toServerTimestamp(); - const joinedAfterMLSMigrationFinalisationEvent = EventBuilder.buildMLSMigrationFinalisationOngoingCall( + const mlsMigrationFinalisationOngoingCallEvent = EventBuilder.buildMLSMigrationFinalisationOngoingCall( conversation, currentTimestamp, ); - return void this.eventRepository.injectEvent(joinedAfterMLSMigrationFinalisationEvent); + + return void this.eventRepository.injectEvent(mlsMigrationFinalisationOngoingCallEvent); }; /**