-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Missed call message does not get marked as read if self user cal…
- Loading branch information
Showing
2 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
logic/src/commonTest/kotlin/com/wire/kalium/logic/data/message/PersistMessageUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.data.message | ||
|
||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.data.conversation.Conversation | ||
import com.wire.kalium.logic.data.notification.NotificationEventsManager | ||
import com.wire.kalium.logic.data.user.UserId | ||
import com.wire.kalium.logic.framework.TestMessage | ||
import com.wire.kalium.logic.framework.TestUser | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.util.shouldFail | ||
import com.wire.kalium.logic.util.shouldSucceed | ||
import com.wire.kalium.persistence.dao.message.InsertMessageResult | ||
import io.mockative.Mock | ||
import io.mockative.any | ||
import io.mockative.coEvery | ||
import io.mockative.coVerify | ||
import io.mockative.eq | ||
import io.mockative.mock | ||
import io.mockative.once | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
|
||
class PersistMessageUseCaseTest { | ||
|
||
@Test | ||
fun givenMessageRepositoryFailure_whenPersistingMessage_thenReturnFailure() = runTest { | ||
val (arrangement, persistMessage) = Arrangement() | ||
.withPersistMessageFailure() | ||
.withReceiptMode() | ||
.arrange() | ||
val message = TestMessage.TEXT_MESSAGE | ||
|
||
val result = persistMessage.invoke(message) | ||
|
||
result.shouldFail() | ||
|
||
coVerify { | ||
arrangement.messageRepository.persistMessage(any(), any(), any()) | ||
}.wasInvoked(once) | ||
|
||
coVerify { | ||
arrangement.messageRepository.getReceiptModeFromGroupConversationByQualifiedID(any()) | ||
}.wasInvoked(once) | ||
} | ||
|
||
@Test | ||
fun givenAMessageAndMessageRepositorySuccess_whenPersistingMessage_thenScheduleRegularNotificationChecking() = | ||
runTest { | ||
val (arrangement, persistMessage) = Arrangement() | ||
.withPersistMessageSuccess() | ||
.withReceiptMode() | ||
.arrange() | ||
val message = TestMessage.TEXT_MESSAGE.copy( | ||
senderUserId = UserId("id", "domain"), | ||
) | ||
|
||
val result = persistMessage.invoke(message) | ||
|
||
result.shouldSucceed() | ||
|
||
coVerify { | ||
arrangement.messageRepository.persistMessage(any(), any(), any()) | ||
}.wasInvoked(once) | ||
|
||
coVerify { | ||
arrangement.messageRepository.getReceiptModeFromGroupConversationByQualifiedID(any()) | ||
}.wasInvoked(once) | ||
|
||
coVerify { | ||
arrangement.notificationEventsManager.scheduleRegularNotificationChecking() | ||
}.wasInvoked(once) | ||
} | ||
|
||
@Test | ||
fun givenSelfMessageAndMessageRepositorySuccess_whenPersistingMessage_thenDoNotScheduleRegularNotificationChecking() = | ||
runTest { | ||
val (arrangement, persistMessage) = Arrangement() | ||
.withPersistMessageSuccess() | ||
.withReceiptMode() | ||
.arrange() | ||
val message = TestMessage.TEXT_MESSAGE | ||
|
||
val result = persistMessage.invoke(message) | ||
|
||
result.shouldSucceed() | ||
|
||
coVerify { | ||
arrangement.messageRepository.persistMessage(any(), any(), any()) | ||
}.wasInvoked(once) | ||
|
||
coVerify { | ||
arrangement.messageRepository.getReceiptModeFromGroupConversationByQualifiedID(any()) | ||
}.wasInvoked(once) | ||
|
||
coVerify { | ||
arrangement.notificationEventsManager.scheduleRegularNotificationChecking() | ||
}.wasNotInvoked() | ||
} | ||
|
||
@Test | ||
fun givenMissedCallMessage_whenRunningUseCase_thenCallPersistMessageWithUpdateConversationReadDateIsFalse() = | ||
runTest { | ||
val (arrangement, persistMessage) = Arrangement() | ||
.withPersistMessageSuccess() | ||
.arrange() | ||
val missedCallMessage = TestMessage.MISSED_CALL_MESSAGE | ||
|
||
val result = persistMessage.invoke(missedCallMessage) | ||
|
||
result.shouldSucceed() | ||
coVerify { | ||
arrangement.messageRepository.persistMessage(any(), eq(false), any()) | ||
}.wasInvoked(once) | ||
} | ||
|
||
private class Arrangement { | ||
@Mock | ||
val messageRepository = mock(MessageRepository::class) | ||
|
||
@Mock | ||
val notificationEventsManager = mock(NotificationEventsManager::class) | ||
|
||
fun arrange() = this to PersistMessageUseCaseImpl( | ||
messageRepository = messageRepository, | ||
selfUserId = TestUser.USER_ID, | ||
notificationEventsManager = notificationEventsManager | ||
) | ||
|
||
suspend fun withPersistMessageSuccess() = apply { | ||
coEvery { | ||
messageRepository.persistMessage(any(), any(), any()) | ||
}.returns(Either.Right(InsertMessageResult.INSERTED_NEED_TO_NOTIFY_USER)) | ||
} | ||
|
||
suspend fun withPersistMessageFailure() = apply { | ||
coEvery { | ||
messageRepository.persistMessage(any(), any(), any()) | ||
}.returns(Either.Left(CoreFailure.InvalidEventSenderID)) | ||
} | ||
|
||
suspend fun withReceiptMode() = apply { | ||
coEvery { | ||
messageRepository.getReceiptModeFromGroupConversationByQualifiedID(any()) | ||
}.returns(Either.Right(Conversation.ReceiptMode.ENABLED)) | ||
} | ||
} | ||
} |