-
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.
feat: testservice can send poll message buttons (WPB-2633) (#2377)
* feat: testservice can send poll message buttons (WPB-2633) * Remove not needed nullable & check for parameter * Add test for button change * Use specific use case for composite message * Revert change in SendTextMessageUseCase --------- Co-authored-by: Mojtaba Chenani <[email protected]>
- Loading branch information
1 parent
11425d1
commit 63755e9
Showing
6 changed files
with
329 additions
and
9 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
122 changes: 122 additions & 0 deletions
122
...onMain/kotlin/com/wire/kalium/logic/feature/message/composite/SendButtonMessageUseCase.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,122 @@ | ||
/* | ||
* 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.feature.message.composite | ||
|
||
import com.benasher44.uuid.uuid4 | ||
import com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.id.CurrentClientIdProvider | ||
import com.wire.kalium.logic.data.id.QualifiedID | ||
import com.wire.kalium.logic.data.message.Message | ||
import com.wire.kalium.logic.data.message.MessageContent | ||
import com.wire.kalium.logic.data.message.PersistMessageUseCase | ||
import com.wire.kalium.logic.data.message.mention.MessageMention | ||
import com.wire.kalium.logic.data.properties.UserPropertyRepository | ||
import com.wire.kalium.logic.data.sync.SlowSyncRepository | ||
import com.wire.kalium.logic.data.sync.SlowSyncStatus | ||
import com.wire.kalium.logic.feature.message.MessageSendFailureHandler | ||
import com.wire.kalium.logic.feature.message.MessageSender | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.functional.flatMap | ||
import com.wire.kalium.logic.functional.onFailure | ||
import com.wire.kalium.util.DateTimeUtil | ||
import com.wire.kalium.util.KaliumDispatcher | ||
import com.wire.kalium.util.KaliumDispatcherImpl | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.flow.first | ||
|
||
@Suppress("LongParameterList") | ||
/** | ||
* @sample samples.logic.MessageUseCases.sendingBasicTextMessage | ||
* @sample samples.logic.MessageUseCases.sendingTextMessageWithMentions | ||
*/ | ||
class SendButtonMessageUseCase internal constructor( | ||
private val persistMessage: PersistMessageUseCase, | ||
private val selfUserId: QualifiedID, | ||
private val provideClientId: CurrentClientIdProvider, | ||
private val slowSyncRepository: SlowSyncRepository, | ||
private val messageSender: MessageSender, | ||
private val messageSendFailureHandler: MessageSendFailureHandler, | ||
private val userPropertyRepository: UserPropertyRepository, | ||
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl, | ||
private val scope: CoroutineScope | ||
) { | ||
|
||
suspend operator fun invoke( | ||
conversationId: ConversationId, | ||
text: String, | ||
mentions: List<MessageMention> = emptyList(), | ||
quotedMessageId: String? = null, | ||
buttons: List<String> = listOf() | ||
): Either<CoreFailure, Unit> = scope.async(dispatchers.io) { | ||
slowSyncRepository.slowSyncStatus.first { | ||
it is SlowSyncStatus.Complete | ||
} | ||
|
||
val generatedMessageUuid = uuid4().toString() | ||
val expectsReadConfirmation = userPropertyRepository.getReadReceiptsStatus() | ||
|
||
provideClientId().flatMap { clientId -> | ||
val textContent = MessageContent.Text( | ||
value = text, | ||
mentions = mentions, | ||
quotedMessageReference = quotedMessageId?.let { quotedMessageId -> | ||
MessageContent.QuoteReference( | ||
quotedMessageId = quotedMessageId, | ||
quotedMessageSha256 = null, | ||
isVerified = true | ||
) | ||
} | ||
) | ||
|
||
val transform: (String) -> MessageContent.Composite.Button = { MessageContent.Composite.Button(it, it, false) } | ||
val buttonContent = buttons.map(transform) | ||
val content = MessageContent.Composite(textContent, buttonContent) | ||
|
||
val message = Message.Regular( | ||
id = generatedMessageUuid, | ||
content = content, | ||
expectsReadConfirmation = expectsReadConfirmation, | ||
conversationId = conversationId, | ||
date = DateTimeUtil.currentIsoDateTimeString(), | ||
senderUserId = selfUserId, | ||
senderClientId = clientId, | ||
status = Message.Status.Pending, | ||
editStatus = Message.EditStatus.NotEdited, | ||
// According to proto Ephemeral it is not possible to send a Composite message with timer | ||
expirationData = null, | ||
isSelfMessage = true | ||
) | ||
persistMessage(message).flatMap { | ||
messageSender.sendMessage(message) | ||
} | ||
}.onFailure { | ||
messageSendFailureHandler.handleFailureAndUpdateMessageStatus( | ||
failure = it, | ||
conversationId = conversationId, | ||
messageId = generatedMessageUuid, | ||
messageType = TYPE | ||
) | ||
} | ||
}.await() | ||
|
||
companion object { | ||
const val TYPE = "Text" | ||
} | ||
} |
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
165 changes: 165 additions & 0 deletions
165
...nTest/kotlin/com/wire/kalium/logic/feature/message/composite/SendButtonMessageCaseTest.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,165 @@ | ||
/* | ||
* 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.feature.message.composite | ||
|
||
import com.wire.kalium.logic.NetworkFailure | ||
import com.wire.kalium.logic.data.conversation.ClientId | ||
import com.wire.kalium.logic.data.id.CurrentClientIdProvider | ||
import com.wire.kalium.logic.data.message.MessageContent | ||
import com.wire.kalium.logic.data.message.PersistMessageUseCase | ||
import com.wire.kalium.logic.data.properties.UserPropertyRepository | ||
import com.wire.kalium.logic.data.sync.SlowSyncRepository | ||
import com.wire.kalium.logic.data.sync.SlowSyncStatus | ||
import com.wire.kalium.logic.feature.message.MessageSendFailureHandler | ||
import com.wire.kalium.logic.feature.message.MessageSender | ||
import com.wire.kalium.logic.feature.selfDeletingMessages.ObserveSelfDeletionTimerSettingsForConversationUseCase | ||
import com.wire.kalium.logic.framework.TestClient | ||
import com.wire.kalium.logic.framework.TestConversation | ||
import com.wire.kalium.logic.framework.TestUser | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.util.shouldSucceed | ||
import io.mockative.Mock | ||
import io.mockative.any | ||
import io.mockative.classOf | ||
import io.mockative.configure | ||
import io.mockative.given | ||
import io.mockative.matching | ||
import io.mockative.mock | ||
import io.mockative.once | ||
import io.mockative.verify | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
|
||
class SendButtonMessageCaseTest { | ||
|
||
@Test | ||
fun givenATextMessageContainsButtons_whenSendingIt_thenShouldBeCompositeAndReturnASuccessResult() = runTest { | ||
// Given | ||
val (arrangement, sendTextMessage) = SendButtonMessageCaseTest.Arrangement(this) | ||
.withToggleReadReceiptsStatus() | ||
.withCurrentClientProviderSuccess() | ||
.withPersistMessageSuccess() | ||
.withSlowSyncStatusComplete() | ||
.withSendMessageSuccess() | ||
.arrange() | ||
val buttons = listOf("OK", "Cancel") | ||
|
||
// When | ||
val result = sendTextMessage.invoke(TestConversation.ID, "some-text", listOf(), null, buttons) | ||
|
||
// Then | ||
result.shouldSucceed() | ||
|
||
verify(arrangement.userPropertyRepository) | ||
.suspendFunction(arrangement.userPropertyRepository::getReadReceiptsStatus) | ||
.wasInvoked(once) | ||
verify(arrangement.persistMessage) | ||
.suspendFunction(arrangement.persistMessage::invoke) | ||
.with(matching { message -> message.content is MessageContent.Composite }) | ||
.wasInvoked(once) | ||
verify(arrangement.messageSender) | ||
.suspendFunction(arrangement.messageSender::sendMessage) | ||
.with( | ||
matching { message -> message.content is MessageContent.Composite }, | ||
any() | ||
) | ||
.wasInvoked(once) | ||
verify(arrangement.messageSendFailureHandler) | ||
.suspendFunction(arrangement.messageSendFailureHandler::handleFailureAndUpdateMessageStatus) | ||
.with(any(), any(), any(), any(), any()) | ||
.wasNotInvoked() | ||
} | ||
|
||
private class Arrangement(private val coroutineScope: CoroutineScope) { | ||
|
||
@Mock | ||
val persistMessage = mock(classOf<PersistMessageUseCase>()) | ||
|
||
@Mock | ||
val currentClientIdProvider = mock(classOf<CurrentClientIdProvider>()) | ||
|
||
@Mock | ||
val slowSyncRepository = mock(classOf<SlowSyncRepository>()) | ||
|
||
@Mock | ||
val messageSender = mock(classOf<MessageSender>()) | ||
|
||
@Mock | ||
val userPropertyRepository = mock(classOf<UserPropertyRepository>()) | ||
|
||
@Mock | ||
val messageSendFailureHandler = configure(mock(classOf<MessageSendFailureHandler>())) { stubsUnitByDefault = true } | ||
|
||
fun withSendMessageSuccess() = apply { | ||
given(messageSender) | ||
.suspendFunction(messageSender::sendMessage) | ||
.whenInvokedWith(any(), any()) | ||
.thenReturn(Either.Right(Unit)) | ||
} | ||
|
||
fun withSendMessageFailure() = apply { | ||
given(messageSender) | ||
.suspendFunction(messageSender::sendMessage) | ||
.whenInvokedWith(any(), any()) | ||
.thenReturn(Either.Left(NetworkFailure.NoNetworkConnection(null))) | ||
} | ||
|
||
fun withCurrentClientProviderSuccess(clientId: ClientId = TestClient.CLIENT_ID) = apply { | ||
given(currentClientIdProvider) | ||
.suspendFunction(currentClientIdProvider::invoke) | ||
.whenInvoked() | ||
.thenReturn(Either.Right(clientId)) | ||
} | ||
|
||
fun withPersistMessageSuccess() = apply { | ||
given(persistMessage) | ||
.suspendFunction(persistMessage::invoke) | ||
.whenInvokedWith(any()) | ||
.thenReturn(Either.Right(Unit)) | ||
} | ||
|
||
fun withSlowSyncStatusComplete() = apply { | ||
val stateFlow = MutableStateFlow<SlowSyncStatus>(SlowSyncStatus.Complete).asStateFlow() | ||
given(slowSyncRepository) | ||
.getter(slowSyncRepository::slowSyncStatus) | ||
.whenInvoked() | ||
.thenReturn(stateFlow) | ||
} | ||
|
||
fun withToggleReadReceiptsStatus(enabled: Boolean = false) = apply { | ||
given(userPropertyRepository) | ||
.suspendFunction(userPropertyRepository::getReadReceiptsStatus) | ||
.whenInvoked() | ||
.thenReturn(enabled) | ||
} | ||
|
||
fun arrange() = this to SendButtonMessageUseCase( | ||
persistMessage, | ||
TestUser.SELF.id, | ||
currentClientIdProvider, | ||
slowSyncRepository, | ||
messageSender, | ||
messageSendFailureHandler, | ||
userPropertyRepository, | ||
scope = coroutineScope | ||
) | ||
} | ||
} |
Oops, something went wrong.