|
| 1 | +/* |
| 2 | + * Wire |
| 3 | + * Copyright (C) 2026 Wire Swiss GmbH |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see http://www.gnu.org/licenses/. |
| 17 | + */ |
| 18 | +package com.wire.kalium.logic.feature.conversation |
| 19 | + |
| 20 | +import com.wire.kalium.common.error.CoreFailure |
| 21 | +import com.wire.kalium.common.error.StorageFailure |
| 22 | +import com.wire.kalium.common.functional.Either |
| 23 | +import com.wire.kalium.logic.data.conversation.Conversation |
| 24 | +import com.wire.kalium.logic.data.conversation.ConversationRepository |
| 25 | +import com.wire.kalium.logic.data.id.ConversationId |
| 26 | +import com.wire.kalium.logic.feature.mlsmigration.MLSMigrator |
| 27 | +import com.wire.kalium.logic.framework.TestConversation |
| 28 | +import com.wire.kalium.logic.util.arrangement.provider.CryptoTransactionProviderArrangement |
| 29 | +import com.wire.kalium.logic.util.arrangement.provider.CryptoTransactionProviderArrangementMokkeryImpl |
| 30 | +import dev.mokkery.answering.returns |
| 31 | +import dev.mokkery.everySuspend |
| 32 | +import dev.mokkery.matcher.any |
| 33 | +import dev.mokkery.matcher.eq |
| 34 | +import dev.mokkery.mock |
| 35 | +import dev.mokkery.verify.VerifyMode |
| 36 | +import dev.mokkery.verifySuspend |
| 37 | +import kotlinx.coroutines.test.runTest |
| 38 | +import kotlin.test.Test |
| 39 | +import kotlin.test.assertEquals |
| 40 | +import kotlin.test.assertIs |
| 41 | + |
| 42 | +class MigrateConversationToMLSUseCaseTest { |
| 43 | + |
| 44 | + @Test |
| 45 | + fun givenMLSConversation_whenMigratingToMLS_thenReturnSuccessWithoutTransaction() = runTest { |
| 46 | + val (arrangement, useCase) = Arrangement() |
| 47 | + .withConversationProtocolInfo(TestConversation.MLS_PROTOCOL_INFO) |
| 48 | + .arrange() |
| 49 | + |
| 50 | + val result = useCase(CONVERSATION_ID) |
| 51 | + |
| 52 | + assertIs<MigrateConversationToMLSUseCase.Result.Success>(result) |
| 53 | + verifySuspend(VerifyMode.not) { |
| 54 | + arrangement.cryptoTransactionProvider.transaction<Unit>(any(), any()) |
| 55 | + } |
| 56 | + verifySuspend(VerifyMode.not) { |
| 57 | + arrangement.mlsMigrator.migrate(any(), any()) |
| 58 | + } |
| 59 | + verifySuspend(VerifyMode.not) { |
| 60 | + arrangement.mlsMigrator.finalise(any(), any()) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun givenMixedConversation_whenMigratingToMLS_thenFinaliseConversation() = runTest { |
| 66 | + val (arrangement, useCase) = Arrangement() |
| 67 | + .withConversationProtocolInfo(TestConversation.MIXED_PROTOCOL_INFO) |
| 68 | + .withFinaliseResult(Either.Right(Unit)) |
| 69 | + .arrange() |
| 70 | + |
| 71 | + val result = useCase(CONVERSATION_ID) |
| 72 | + |
| 73 | + assertIs<MigrateConversationToMLSUseCase.Result.Success>(result) |
| 74 | + verifySuspend { |
| 75 | + arrangement.mlsMigrator.finalise(eq(arrangement.transactionContext), eq(CONVERSATION_ID)) |
| 76 | + } |
| 77 | + verifySuspend(VerifyMode.not) { |
| 78 | + arrangement.mlsMigrator.migrate(any(), any()) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun givenProteusConversation_whenMigratingToMLS_thenMigrateAndFinaliseConversation() = runTest { |
| 84 | + val (arrangement, useCase) = Arrangement() |
| 85 | + .withConversationProtocolInfo(Conversation.ProtocolInfo.Proteus) |
| 86 | + .withMigrateResult(Either.Right(Unit)) |
| 87 | + .withFinaliseResult(Either.Right(Unit)) |
| 88 | + .arrange() |
| 89 | + |
| 90 | + val result = useCase(CONVERSATION_ID) |
| 91 | + |
| 92 | + assertIs<MigrateConversationToMLSUseCase.Result.Success>(result) |
| 93 | + verifySuspend { |
| 94 | + arrangement.mlsMigrator.migrate(eq(arrangement.transactionContext), eq(CONVERSATION_ID)) |
| 95 | + } |
| 96 | + verifySuspend { |
| 97 | + arrangement.mlsMigrator.finalise(eq(arrangement.transactionContext), eq(CONVERSATION_ID)) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun givenProteusConversationAndMigrationFails_whenMigratingToMLS_thenReturnFailureAndSkipFinalise() = runTest { |
| 103 | + val (arrangement, useCase) = Arrangement() |
| 104 | + .withConversationProtocolInfo(Conversation.ProtocolInfo.Proteus) |
| 105 | + .withMigrateResult(Either.Left(FAILURE)) |
| 106 | + .arrange() |
| 107 | + |
| 108 | + val result = useCase(CONVERSATION_ID) |
| 109 | + |
| 110 | + assertIs<MigrateConversationToMLSUseCase.Result.Failure>(result) |
| 111 | + assertEquals(FAILURE, result.cause) |
| 112 | + verifySuspend(VerifyMode.not) { |
| 113 | + arrangement.mlsMigrator.finalise(any(), any()) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + fun givenConversationProtocolInfoFails_whenMigratingToMLS_thenReturnFailure() = runTest { |
| 119 | + val (_, useCase) = Arrangement() |
| 120 | + .withConversationProtocolInfoFailure(FAILURE) |
| 121 | + .arrange() |
| 122 | + |
| 123 | + val result = useCase(CONVERSATION_ID) |
| 124 | + |
| 125 | + assertIs<MigrateConversationToMLSUseCase.Result.Failure>(result) |
| 126 | + assertEquals(FAILURE, result.cause) |
| 127 | + } |
| 128 | + |
| 129 | + private class Arrangement : CryptoTransactionProviderArrangement by CryptoTransactionProviderArrangementMokkeryImpl() { |
| 130 | + val conversationRepository = mock<ConversationRepository>() |
| 131 | + val mlsMigrator = mock<MLSMigrator>() |
| 132 | + |
| 133 | + suspend fun withConversationProtocolInfo(protocolInfo: Conversation.ProtocolInfo) = apply { |
| 134 | + everySuspend { |
| 135 | + conversationRepository.getConversationProtocolInfo(eq(CONVERSATION_ID)) |
| 136 | + } returns Either.Right(protocolInfo) |
| 137 | + } |
| 138 | + |
| 139 | + suspend fun withConversationProtocolInfoFailure(failure: StorageFailure) = apply { |
| 140 | + everySuspend { |
| 141 | + conversationRepository.getConversationProtocolInfo(eq(CONVERSATION_ID)) |
| 142 | + } returns Either.Left(failure) |
| 143 | + } |
| 144 | + |
| 145 | + suspend fun withMigrateResult(result: Either<CoreFailure, Unit>) = apply { |
| 146 | + everySuspend { |
| 147 | + mlsMigrator.migrate(eq(transactionContext), eq(CONVERSATION_ID)) |
| 148 | + } returns result |
| 149 | + } |
| 150 | + |
| 151 | + suspend fun withFinaliseResult(result: Either<CoreFailure, Unit>) = apply { |
| 152 | + everySuspend { |
| 153 | + mlsMigrator.finalise(eq(transactionContext), eq(CONVERSATION_ID)) |
| 154 | + } returns result |
| 155 | + } |
| 156 | + |
| 157 | + suspend fun arrange(): Pair<Arrangement, MigrateConversationToMLSUseCase> { |
| 158 | + withTransactionReturning(Either.Right(Unit)) |
| 159 | + return this to MigrateConversationToMLSUseCaseImpl( |
| 160 | + mlsMigrator = mlsMigrator, |
| 161 | + conversationRepository = conversationRepository, |
| 162 | + coreCryptoTransactionProvider = cryptoTransactionProvider |
| 163 | + ) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private companion object { |
| 168 | + val CONVERSATION_ID = ConversationId("conversation-id", "domain.example") |
| 169 | + val FAILURE = StorageFailure.DataNotFound |
| 170 | + } |
| 171 | +} |
0 commit comments