|
| 1 | +package com.flipcash.app.balance.internal |
| 2 | + |
| 3 | +import androidx.arch.core.executor.testing.InstantTaskExecutorRule |
| 4 | +import com.flipcash.app.analytics.StubFlipcashAnalytics |
| 5 | +import com.flipcash.app.balance.internal.components.TutorialItem |
| 6 | +import com.flipcash.app.core.MainCoroutineRule |
| 7 | +import com.flipcash.app.core.dispatchers.TestDispatchers |
| 8 | +import com.flipcash.app.funding.PurchaseMethodController |
| 9 | +import com.flipcash.app.tokens.TokenCoordinator |
| 10 | +import com.flipcash.app.userflags.UserFlagsCoordinator |
| 11 | +import com.flipcash.services.user.UserManager |
| 12 | +import com.flipcash.shared.chat.ChatCoordinator |
| 13 | +import com.flipcash.shared.transactionhistory.ActivityFeedCoordinator |
| 14 | +import io.mockk.every |
| 15 | +import io.mockk.mockk |
| 16 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 17 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 18 | +import kotlinx.coroutines.flow.flowOf |
| 19 | +import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 20 | +import kotlinx.coroutines.test.advanceUntilIdle |
| 21 | +import kotlinx.coroutines.test.runTest |
| 22 | +import org.junit.Rule |
| 23 | +import org.junit.Test |
| 24 | +import kotlin.test.assertEquals |
| 25 | +import kotlin.test.assertNotNull |
| 26 | +import kotlin.test.assertNull |
| 27 | +import kotlin.test.assertTrue |
| 28 | + |
| 29 | +/** |
| 30 | + * The tip milestone is read off the chat cache, which reports every account as never having tipped |
| 31 | + * until its history has been reconciled. The wallet must not draw that: [WalletViewModel.State. |
| 32 | + * onboardingItems] stays null — holding the tab on its loading state — until the chat coordinator |
| 33 | + * commits to an answer. |
| 34 | + * |
| 35 | + * A held balance deliberately does not release the wait here, unlike for the activity feed: it is |
| 36 | + * evidence about money, not about tipping. |
| 37 | + */ |
| 38 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 39 | +class WalletMilestoneGatingTest { |
| 40 | + |
| 41 | + @get:Rule |
| 42 | + val instantExecutorRule = InstantTaskExecutorRule() |
| 43 | + |
| 44 | + @get:Rule |
| 45 | + var mainCoroutineRule = MainCoroutineRule(UnconfinedTestDispatcher()) |
| 46 | + |
| 47 | + private val hasEverTipped = MutableStateFlow<Boolean?>(null) |
| 48 | + |
| 49 | + private val chatCoordinator: ChatCoordinator = mockk(relaxed = true) { |
| 50 | + every { hasEverTipped() } returns hasEverTipped |
| 51 | + } |
| 52 | + private val feedCoordinator: ActivityFeedCoordinator = mockk(relaxed = true) { |
| 53 | + every { hasEverReceivedMoney() } returns flowOf(true) |
| 54 | + } |
| 55 | + private val tokenCoordinator: TokenCoordinator = mockk(relaxed = true) { |
| 56 | + every { hasAnyBalance } returns flowOf(true) |
| 57 | + } |
| 58 | + |
| 59 | + private lateinit var dispatchers: TestDispatchers |
| 60 | + |
| 61 | + private fun createViewModel() = WalletViewModel( |
| 62 | + userManager = mockk<UserManager>(relaxed = true), |
| 63 | + userFlags = mockk<UserFlagsCoordinator>(relaxed = true), |
| 64 | + dispatchers = dispatchers, |
| 65 | + purchaseMethodController = mockk<PurchaseMethodController>(relaxed = true), |
| 66 | + analytics = StubFlipcashAnalytics(), |
| 67 | + chatCoordinator = chatCoordinator, |
| 68 | + feedCoordinator = feedCoordinator, |
| 69 | + tokenCoordinator = tokenCoordinator, |
| 70 | + ) |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `milestones are withheld while the tip milestone is unknown`() = |
| 74 | + runTest(mainCoroutineRule.dispatcher) { |
| 75 | + dispatchers = TestDispatchers(testScheduler) |
| 76 | + |
| 77 | + val vm = createViewModel() |
| 78 | + advanceUntilIdle() |
| 79 | + |
| 80 | + assertNull( |
| 81 | + vm.stateFlow.value.onboardingItems, |
| 82 | + "an un-reconciled chat cache reports every account as never having tipped", |
| 83 | + ) |
| 84 | + assertTrue( |
| 85 | + vm.stateFlow.value.isAwaitingActivity, |
| 86 | + "the tab must keep loading rather than draw a milestone it cannot yet answer", |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `milestones are published once the tip milestone resolves`() = |
| 92 | + runTest(mainCoroutineRule.dispatcher) { |
| 93 | + dispatchers = TestDispatchers(testScheduler) |
| 94 | + |
| 95 | + val vm = createViewModel() |
| 96 | + advanceUntilIdle() |
| 97 | + hasEverTipped.value = true |
| 98 | + advanceUntilIdle() |
| 99 | + |
| 100 | + val items = vm.stateFlow.value.onboardingItems |
| 101 | + assertNotNull(items) |
| 102 | + assertEquals( |
| 103 | + listOf(true, true), |
| 104 | + items.map { it.isCompleted }, |
| 105 | + "both milestones read complete: the account holds a balance and has tipped", |
| 106 | + ) |
| 107 | + assertTrue(items.any { it is TutorialItem.ScanTipCard }) |
| 108 | + } |
| 109 | +} |
0 commit comments