|
| 1 | +package com.flipcash.shared.chat |
| 2 | + |
| 3 | +import androidx.core.app.NotificationManagerCompat |
| 4 | +import com.flipcash.app.featureflags.FeatureFlagController |
| 5 | +import com.flipcash.app.persistence.sources.ChatMemberDataSource |
| 6 | +import com.flipcash.app.persistence.sources.ChatMessageDataSource |
| 7 | +import com.flipcash.app.persistence.sources.ChatMetadataDataSource |
| 8 | +import com.flipcash.app.persistence.sources.ContactDataSource |
| 9 | +import com.flipcash.app.tokens.TokenCoordinator |
| 10 | +import com.flipcash.services.controllers.ChatController |
| 11 | +import com.flipcash.services.controllers.ChatMessagingController |
| 12 | +import com.flipcash.services.controllers.EventStreamingController |
| 13 | +import com.flipcash.services.models.chat.ChatId |
| 14 | +import com.flipcash.services.models.chat.ChatMessage |
| 15 | +import com.flipcash.services.models.chat.ChatUpdate |
| 16 | +import com.flipcash.services.models.chat.MessageContent |
| 17 | +import com.getcode.opencode.model.financial.CurrencyCode |
| 18 | +import com.getcode.opencode.model.financial.Fiat |
| 19 | +import com.getcode.solana.keys.Mint |
| 20 | +import com.getcode.utils.network.NetworkConnectivityListener |
| 21 | +import com.flipcash.services.user.UserManager |
| 22 | +import io.mockk.coEvery |
| 23 | +import io.mockk.coVerify |
| 24 | +import io.mockk.every |
| 25 | +import io.mockk.mockk |
| 26 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 27 | +import kotlinx.coroutines.channels.Channel |
| 28 | +import kotlinx.coroutines.flow.receiveAsFlow |
| 29 | +import kotlinx.coroutines.test.advanceUntilIdle |
| 30 | +import kotlinx.coroutines.test.runTest |
| 31 | +import org.junit.Before |
| 32 | +import org.junit.Test |
| 33 | +import org.junit.runner.RunWith |
| 34 | +import org.robolectric.RobolectricTestRunner |
| 35 | +import kotlin.time.Instant |
| 36 | + |
| 37 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 38 | +@RunWith(RobolectricTestRunner::class) |
| 39 | +class ChatCoordinatorEagerBalanceTest { |
| 40 | + |
| 41 | + private val selfId = listOf<Byte>(1, 2, 3) |
| 42 | + private val otherId = listOf<Byte>(4, 5, 6) |
| 43 | + private val chatId = ChatId("aabbccdd") |
| 44 | + private val mint = Mint("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaa") |
| 45 | + |
| 46 | + private val chatUpdatesChannel = Channel<ChatUpdate>(capacity = Channel.UNLIMITED) |
| 47 | + |
| 48 | + private lateinit var tokenCoordinator: TokenCoordinator |
| 49 | + private lateinit var coordinator: ChatCoordinator |
| 50 | + |
| 51 | + @Before |
| 52 | + fun setUp() { |
| 53 | + tokenCoordinator = mockk(relaxed = true) |
| 54 | + val userManager = mockk<UserManager>(relaxed = true) |
| 55 | + every { userManager.accountId } returns selfId |
| 56 | + val eventStreamingController = mockk<EventStreamingController>(relaxed = true) |
| 57 | + every { eventStreamingController.chatUpdates } returns chatUpdatesChannel.receiveAsFlow() |
| 58 | + every { eventStreamingController.isConnected } returns true |
| 59 | + every { eventStreamingController.isStreamActive } returns true |
| 60 | + |
| 61 | + val chatController = mockk<ChatController>(relaxed = true) |
| 62 | + coEvery { chatController.getDmChatFeed() } returns Result.failure(RuntimeException("not needed")) |
| 63 | + |
| 64 | + coordinator = ChatCoordinator( |
| 65 | + chatController = chatController, |
| 66 | + messagingController = mockk(relaxed = true), |
| 67 | + eventStreamingController = eventStreamingController, |
| 68 | + metadataDataSource = mockk(relaxed = true), |
| 69 | + messageDataSource = mockk(relaxed = true), |
| 70 | + memberDataSource = mockk(relaxed = true), |
| 71 | + contactDataSource = mockk(relaxed = true), |
| 72 | + networkObserver = mockk<NetworkConnectivityListener>(relaxed = true), |
| 73 | + notificationManager = mockk<NotificationManagerCompat>(relaxed = true), |
| 74 | + userManager = userManager, |
| 75 | + tokenCoordinator = tokenCoordinator, |
| 76 | + featureFlags = mockk<FeatureFlagController>(relaxed = true), |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + private fun cashMessage( |
| 81 | + senderId: List<Byte>?, |
| 82 | + amount: Fiat = Fiat(fiat = 5.0, currencyCode = CurrencyCode.USD), |
| 83 | + mint: Mint = this.mint, |
| 84 | + ) = ChatMessage( |
| 85 | + messageId = 1L, |
| 86 | + senderId = senderId, |
| 87 | + content = listOf(MessageContent.Cash( |
| 88 | + intentId = listOf(0), |
| 89 | + amount = amount, |
| 90 | + mint = mint, |
| 91 | + )), |
| 92 | + timestamp = Instant.fromEpochSeconds(1000), |
| 93 | + unreadSeq = 0, |
| 94 | + ) |
| 95 | + |
| 96 | + private fun textMessage(senderId: List<Byte>?) = ChatMessage( |
| 97 | + messageId = 2L, |
| 98 | + senderId = senderId, |
| 99 | + content = listOf(MessageContent.Text("hello")), |
| 100 | + timestamp = Instant.fromEpochSeconds(1000), |
| 101 | + unreadSeq = 0, |
| 102 | + ) |
| 103 | + |
| 104 | + private fun chatUpdate(vararg messages: ChatMessage) = ChatUpdate( |
| 105 | + chatId = chatId, |
| 106 | + newMessages = messages.toList(), |
| 107 | + pointerUpdates = emptyList(), |
| 108 | + typingNotifications = emptyList(), |
| 109 | + metadataUpdates = emptyList(), |
| 110 | + ) |
| 111 | + |
| 112 | + private suspend fun triggerCollection() { |
| 113 | + coordinator.onUserLoggedIn(mockk(relaxed = true)) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun `incoming cash message triggers tokenCoordinator add`() = runTest { |
| 118 | + triggerCollection() |
| 119 | + val amount = Fiat(fiat = 5.0, currencyCode = CurrencyCode.CAD) |
| 120 | + chatUpdatesChannel.send(chatUpdate(cashMessage(senderId = otherId, amount = amount))) |
| 121 | + advanceUntilIdle() |
| 122 | + |
| 123 | + coVerify(exactly = 1) { tokenCoordinator.add(mint, amount) } |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun `self-sent cash message does not trigger tokenCoordinator add`() = runTest { |
| 128 | + triggerCollection() |
| 129 | + chatUpdatesChannel.send(chatUpdate(cashMessage(senderId = selfId))) |
| 130 | + advanceUntilIdle() |
| 131 | + |
| 132 | + coVerify(exactly = 0) { tokenCoordinator.add(any<Mint>(), any()) } |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + fun `text message does not trigger tokenCoordinator add`() = runTest { |
| 137 | + triggerCollection() |
| 138 | + chatUpdatesChannel.send(chatUpdate(textMessage(senderId = otherId))) |
| 139 | + advanceUntilIdle() |
| 140 | + |
| 141 | + coVerify(exactly = 0) { tokenCoordinator.add(any<Mint>(), any()) } |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + fun `multiple cash messages in one update each trigger add`() = runTest { |
| 146 | + triggerCollection() |
| 147 | + val mintB = Mint("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBbbbbbbbbbbb") |
| 148 | + val amount1 = Fiat(fiat = 5.0, currencyCode = CurrencyCode.USD) |
| 149 | + val amount2 = Fiat(fiat = 10.0, currencyCode = CurrencyCode.USD) |
| 150 | + val msg1 = cashMessage(senderId = otherId, amount = amount1, mint = mint) |
| 151 | + val msg2 = cashMessage(senderId = otherId, amount = amount2, mint = mintB).copy(messageId = 3L) |
| 152 | + |
| 153 | + chatUpdatesChannel.send(chatUpdate(msg1, msg2)) |
| 154 | + advanceUntilIdle() |
| 155 | + |
| 156 | + coVerify(exactly = 1) { tokenCoordinator.add(mint, amount1) } |
| 157 | + coVerify(exactly = 1) { tokenCoordinator.add(mintB, amount2) } |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + fun `mixed self and incoming messages only triggers add for incoming`() = runTest { |
| 162 | + triggerCollection() |
| 163 | + val incoming = cashMessage(senderId = otherId) |
| 164 | + val outgoing = cashMessage(senderId = selfId).copy(messageId = 3L) |
| 165 | + |
| 166 | + chatUpdatesChannel.send(chatUpdate(incoming, outgoing)) |
| 167 | + advanceUntilIdle() |
| 168 | + |
| 169 | + coVerify(exactly = 1) { tokenCoordinator.add(any<Mint>(), any()) } |
| 170 | + } |
| 171 | +} |
0 commit comments