Skip to content

Commit

Permalink
feat: testservice can send poll message buttons (WPB-2633) (#2377)
Browse files Browse the repository at this point in the history
* 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
mythsunwind and mchenani authored Jan 18, 2024
1 parent 11425d1 commit 63755e9
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import com.wire.kalium.logic.feature.asset.UpdateAssetMessageDownloadStatusUseCa
import com.wire.kalium.logic.feature.asset.UpdateAssetMessageUploadStatusUseCase
import com.wire.kalium.logic.feature.asset.UpdateAssetMessageUploadStatusUseCaseImpl
import com.wire.kalium.logic.feature.message.composite.SendButtonActionMessageUseCase
import com.wire.kalium.logic.feature.message.composite.SendButtonMessageUseCase
import com.wire.kalium.logic.feature.message.ephemeral.DeleteEphemeralMessageForSelfUserAsReceiverUseCaseImpl
import com.wire.kalium.logic.feature.message.ephemeral.DeleteEphemeralMessageForSelfUserAsSenderUseCaseImpl
import com.wire.kalium.logic.feature.message.ephemeral.EnqueueMessageSelfDeletionUseCase
Expand Down Expand Up @@ -352,6 +353,19 @@ class MessageScope internal constructor(
currentClientIdProvider = currentClientIdProvider,
messageMetadataRepository = messageMetadataRepository
)

val sendButtonMessage: SendButtonMessageUseCase
get() = SendButtonMessageUseCase(
persistMessage = persistMessage,
selfUserId = selfUserId,
provideClientId = currentClientIdProvider,
slowSyncRepository = slowSyncRepository,
messageSender = messageSender,
messageSendFailureHandler = messageSendFailureHandler,
userPropertyRepository = userPropertyRepository,
scope = scope
)

private val deleteEphemeralMessageForSelfUserAsReceiver: DeleteEphemeralMessageForSelfUserAsReceiverUseCaseImpl
get() = DeleteEphemeralMessageForSelfUserAsReceiverUseCaseImpl(
messageRepository = messageRepository,
Expand Down
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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.data.message.MessageContent
import com.wire.kalium.logic.feature.selfDeletingMessages.ObserveSelfDeletionTimerSettingsForConversationUseCase
import com.wire.kalium.logic.data.message.SelfDeletionTimer
import com.wire.kalium.logic.framework.TestClient
Expand All @@ -38,6 +39,7 @@ 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
Expand Down Expand Up @@ -73,11 +75,14 @@ class SendTextMessageCaseTest {
.wasInvoked(once)
verify(arrangement.persistMessage)
.suspendFunction(arrangement.persistMessage::invoke)
.with(any())
.with(matching { message -> message.content is MessageContent.Text })
.wasInvoked(once)
verify(arrangement.messageSender)
.suspendFunction(arrangement.messageSender::sendMessage)
.with(any(), any())
.with(
matching { message -> message.content is MessageContent.Text },
any()
)
.wasInvoked(once)
verify(arrangement.messageSendFailureHandler)
.suspendFunction(arrangement.messageSendFailureHandler::handleFailureAndUpdateMessageStatus)
Expand Down Expand Up @@ -149,39 +154,44 @@ class SendTextMessageCaseTest {
.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 withMessageTimer(result: SelfDeletionTimer) = apply {
fun withMessageTimer(result: SelfDeletionTimer) = apply {
given(observeSelfDeletionTimerSettingsForConversation)
.suspendFunction(observeSelfDeletionTimerSettingsForConversation::invoke)
.whenInvokedWith(any())
Expand Down
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
)
}
}
Loading

0 comments on commit 63755e9

Please sign in to comment.