|
18 | 18 | package com.wire.kalium.logic.data.call |
19 | 19 |
|
20 | 20 | import com.wire.kalium.common.error.StorageFailure |
| 21 | +import com.wire.kalium.common.functional.Either |
| 22 | +import com.wire.kalium.common.functional.left |
| 23 | +import com.wire.kalium.common.functional.right |
21 | 24 | import com.wire.kalium.logic.configuration.UserConfigRepository |
22 | 25 | import com.wire.kalium.logic.data.conversation.Conversation |
23 | 26 | import com.wire.kalium.logic.data.id.ConversationId |
24 | | -import com.wire.kalium.logic.data.id.GroupID |
25 | 27 | import com.wire.kalium.logic.data.id.QualifiedID |
26 | | -import com.wire.kalium.logic.data.mls.CipherSuite |
27 | | -import com.wire.kalium.common.functional.Either |
| 28 | +import com.wire.kalium.logic.data.user.UserId |
28 | 29 | import io.mockative.coEvery |
29 | | -import io.mockative.of |
30 | | -import io.mockative.every |
31 | 30 | import io.mockative.mock |
| 31 | +import io.mockative.of |
| 32 | +import kotlinx.coroutines.flow.flowOf |
32 | 33 | import kotlinx.coroutines.test.runTest |
33 | | -import kotlinx.datetime.Instant |
34 | 34 | import kotlin.test.Test |
35 | | -import kotlin.test.assertFalse |
36 | | -import kotlin.test.assertTrue |
| 35 | +import kotlin.test.assertEquals |
37 | 36 |
|
38 | 37 | class CallHelperTest { |
39 | 38 |
|
| 39 | + private fun testShouldEndSFT1on1Call( |
| 40 | + shouldUseSFTForOneOnOneCalls: Either<StorageFailure, Boolean> = true.right(), |
| 41 | + establishedCall: Call? = call, |
| 42 | + newCallParticipants: List<ParticipantMinimized> = listOf(participantMinimized1), |
| 43 | + expected: Boolean |
| 44 | + ) = runTest { |
| 45 | + val (_, callHelper) = Arrangement() |
| 46 | + .withShouldUseSFTForOneOnOneCallsReturning(shouldUseSFTForOneOnOneCalls) |
| 47 | + .withEstablishedCallsFlowReturning(listOfNotNull(establishedCall)) |
| 48 | + .arrange() |
| 49 | + assertEquals(expected, callHelper.shouldEndSFTOneOnOneCall(conversationId, newCallParticipants)) |
| 50 | + } |
| 51 | + |
40 | 52 | @Test |
41 | | - fun givenMlsProtocol_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnCorrectValue() = |
42 | | - runTest { |
43 | | - val (_, mLSCallHelper) = Arrangement() |
44 | | - .withShouldUseSFTForOneOnOneCallsReturning(Either.Right(true)) |
45 | | - .arrange() |
46 | | - |
47 | | - // one participant in the call |
48 | | - val shouldEndSFTOneOnOneCall1 = mLSCallHelper.shouldEndSFTOneOnOneCall( |
49 | | - conversationId = conversationId, |
50 | | - callProtocol = CONVERSATION_MLS_PROTOCOL_INFO, |
51 | | - conversationType = Conversation.Type.OneOnOne, |
52 | | - newCallParticipants = listOf(participantMinimized1), |
53 | | - previousCallParticipants = listOf(participant1) |
54 | | - ) |
55 | | - assertFalse { shouldEndSFTOneOnOneCall1 } |
56 | | - |
57 | | - // Audio not lost for the second participant |
58 | | - val shouldEndSFTOneOnOneCall2 = mLSCallHelper.shouldEndSFTOneOnOneCall( |
59 | | - conversationId = conversationId, |
60 | | - callProtocol = CONVERSATION_MLS_PROTOCOL_INFO, |
61 | | - conversationType = Conversation.Type.Group.Regular, |
62 | | - newCallParticipants = listOf(participantMinimized1, participantMinimized2), |
63 | | - previousCallParticipants = listOf(participant1, participant2) |
64 | | - ) |
65 | | - assertFalse { shouldEndSFTOneOnOneCall2 } |
66 | | - |
67 | | - // Audio lost for the second participant |
68 | | - val shouldEndSFTOneOnOneCall3 = mLSCallHelper.shouldEndSFTOneOnOneCall( |
69 | | - conversationId = conversationId, |
70 | | - callProtocol = CONVERSATION_MLS_PROTOCOL_INFO, |
71 | | - conversationType = Conversation.Type.OneOnOne, |
72 | | - previousCallParticipants = listOf(participant1, participant2), |
73 | | - newCallParticipants = listOf( |
74 | | - participantMinimized1, |
75 | | - participantMinimized2.copy(hasEstablishedAudio = false) |
76 | | - ) |
77 | | - ) |
78 | | - assertTrue { shouldEndSFTOneOnOneCall3 } |
79 | | - } |
| 53 | + fun givenSFTFor1on1CallsConfigNotFound_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnFalse() = |
| 54 | + testShouldEndSFT1on1Call(shouldUseSFTForOneOnOneCalls = StorageFailure.DataNotFound.left(), expected = false) |
80 | 55 |
|
81 | 56 | @Test |
82 | | - fun givenProteusProtocol_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnCorrectValue() = |
83 | | - runTest { |
84 | | - |
85 | | - val (_, mLSCallHelper) = Arrangement() |
86 | | - .withShouldUseSFTForOneOnOneCallsReturning(Either.Right(true)) |
87 | | - .arrange() |
88 | | - |
89 | | - // participants list has 2 items for the new list and the previous list |
90 | | - val shouldEndSFTOneOnOneCall1 = mLSCallHelper.shouldEndSFTOneOnOneCall( |
91 | | - conversationId = conversationId, |
92 | | - callProtocol = Conversation.ProtocolInfo.Proteus, |
93 | | - conversationType = Conversation.Type.OneOnOne, |
94 | | - newCallParticipants = listOf(participantMinimized1, participantMinimized2), |
95 | | - previousCallParticipants = listOf(participant1, participant2) |
96 | | - ) |
97 | | - assertFalse { shouldEndSFTOneOnOneCall1 } |
98 | | - |
99 | | - // new participants list has 1 participant |
100 | | - val shouldEndSFTOneOnOneCall2 = mLSCallHelper.shouldEndSFTOneOnOneCall( |
101 | | - conversationId = conversationId, |
102 | | - callProtocol = Conversation.ProtocolInfo.Proteus, |
103 | | - conversationType = Conversation.Type.OneOnOne, |
104 | | - newCallParticipants = listOf(participantMinimized1), |
105 | | - previousCallParticipants = listOf(participant1, participant2) |
106 | | - ) |
107 | | - assertTrue { shouldEndSFTOneOnOneCall2 } |
108 | | - } |
| 57 | + fun givenSFTShouldNotBeUsedFor1on1Calls_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnFalse() = |
| 58 | + testShouldEndSFT1on1Call(shouldUseSFTForOneOnOneCalls = false.right(), expected = false) |
| 59 | + |
| 60 | + @Test |
| 61 | + fun givenNotEstablishedCall_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnFalse() = |
| 62 | + testShouldEndSFT1on1Call(establishedCall = null, expected = false) |
| 63 | + |
| 64 | + @Test |
| 65 | + fun givenEstablishedNon1on1Call_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnFalse() = |
| 66 | + testShouldEndSFT1on1Call(establishedCall = call.copy(conversationType = Conversation.Type.Group.Regular), expected = false) |
| 67 | + |
| 68 | + @Test |
| 69 | + fun givenEstablished1on1CallWith1Participant_andParticipantsDidNotChange_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnFalse() = |
| 70 | + testShouldEndSFT1on1Call( |
| 71 | + establishedCall = call.copy(participants = listOf(participant1)), |
| 72 | + newCallParticipants = listOf(participantMinimized1), |
| 73 | + expected = false |
| 74 | + ) |
| 75 | + |
| 76 | + @Test |
| 77 | + fun givenEstablished1on1CallWith2Participants_andParticipantsDidNotChange_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnFalse() = |
| 78 | + testShouldEndSFT1on1Call( |
| 79 | + establishedCall = call.copy(participants = listOf(participant1, participant2)), |
| 80 | + newCallParticipants = listOf(participantMinimized1, participantMinimized2), |
| 81 | + expected = false |
| 82 | + ) |
| 83 | + |
| 84 | + @Test |
| 85 | + fun givenEstablished1on1CallWith1Participant_andOneParticipantJoined_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnFalse() = |
| 86 | + testShouldEndSFT1on1Call( |
| 87 | + establishedCall = call.copy(participants = listOf(participant1)), |
| 88 | + newCallParticipants = listOf(participantMinimized1, participantMinimized2), |
| 89 | + expected = false |
| 90 | + ) |
| 91 | + |
| 92 | + @Test |
| 93 | + fun givenEstablished1on1CallWith2Participants_andOneParticipantLeft_whenShouldEndSFTOneOnOneCallIsCalled_thenReturnTrue() = |
| 94 | + testShouldEndSFT1on1Call( |
| 95 | + establishedCall = call.copy(participants = listOf(participant1, participant2)), |
| 96 | + newCallParticipants = listOf(participantMinimized1), |
| 97 | + expected = true |
| 98 | + ) |
109 | 99 |
|
110 | 100 | private class Arrangement { |
111 | 101 |
|
112 | 102 | val userConfigRepository = mock(of<UserConfigRepository>()) |
113 | | - |
114 | | - private val mLSCallHelper: CallHelper = CallHelperImpl() |
| 103 | + val callRepository = mock(of<CallRepository>()) |
| 104 | + private val mLSCallHelper: CallHelper = CallHelperImpl(userConfigRepository, callRepository) |
115 | 105 |
|
116 | 106 | fun arrange() = this to mLSCallHelper |
117 | 107 |
|
118 | | - suspend fun withShouldUseSFTForOneOnOneCallsReturning(result: Either<StorageFailure, Boolean>) = |
119 | | - apply { |
120 | | - coEvery { userConfigRepository.shouldUseSFTForOneOnOneCalls() }.returns(result) |
121 | | - } |
| 108 | + suspend fun withShouldUseSFTForOneOnOneCallsReturning(result: Either<StorageFailure, Boolean>) = apply { |
| 109 | + coEvery { |
| 110 | + userConfigRepository.shouldUseSFTForOneOnOneCalls() |
| 111 | + }.returns(result) |
| 112 | + } |
| 113 | + |
| 114 | + suspend fun withEstablishedCallsFlowReturning(calls: List<Call>) = apply { |
| 115 | + coEvery { |
| 116 | + callRepository.establishedCallsFlow() |
| 117 | + }.returns(flowOf(calls)) |
| 118 | + } |
122 | 119 | } |
123 | 120 |
|
124 | 121 | companion object { |
125 | 122 | val conversationId = ConversationId(value = "convId", domain = "domainId") |
126 | | - val CONVERSATION_MLS_PROTOCOL_INFO = Conversation.ProtocolInfo.MLS( |
127 | | - GroupID("GROUP_ID"), |
128 | | - Conversation.ProtocolInfo.MLSCapable.GroupState.ESTABLISHED, |
129 | | - 5UL, |
130 | | - Instant.parse("2021-03-30T15:36:00.000Z"), |
131 | | - cipherSuite = CipherSuite.MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519 |
132 | | - ) |
133 | 123 | val participant1 = Participant( |
134 | 124 | id = QualifiedID("participantId", "participantDomain"), |
135 | 125 | clientId = "abcd", |
@@ -159,5 +149,18 @@ class CallHelperTest { |
159 | 149 | id = QualifiedID("participantId2", "participantDomain2"), |
160 | 150 | clientId = "efgh" |
161 | 151 | ) |
| 152 | + val call = Call( |
| 153 | + conversationId = conversationId, |
| 154 | + status = CallStatus.ESTABLISHED, |
| 155 | + isMuted = true, |
| 156 | + isCameraOn = false, |
| 157 | + isCbrEnabled = false, |
| 158 | + callerId = UserId("callerId", "domain"), |
| 159 | + conversationName = "Conversation Name", |
| 160 | + conversationType = Conversation.Type.OneOnOne, |
| 161 | + callerName = "name", |
| 162 | + callerTeamName = "team", |
| 163 | + participants = listOf(participant1, participant2) |
| 164 | + ) |
162 | 165 | } |
163 | 166 | } |
0 commit comments