|
| 1 | +package to.bitkit.ui.sheets.hardware |
| 2 | + |
| 3 | +import app.cash.turbine.test |
| 4 | +import com.synonym.bitkitcore.TrezorDeviceInfo |
| 5 | +import com.synonym.bitkitcore.TrezorFeatures |
| 6 | +import com.synonym.bitkitcore.TrezorTransportType |
| 7 | +import kotlinx.collections.immutable.ImmutableList |
| 8 | +import kotlinx.collections.immutable.persistentListOf |
| 9 | +import kotlinx.collections.immutable.persistentSetOf |
| 10 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 11 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 12 | +import org.junit.Before |
| 13 | +import org.junit.Test |
| 14 | +import org.mockito.kotlin.mock |
| 15 | +import org.mockito.kotlin.verify |
| 16 | +import org.mockito.kotlin.whenever |
| 17 | +import to.bitkit.models.HwWallet |
| 18 | +import to.bitkit.models.TransportType |
| 19 | +import to.bitkit.repositories.HwWalletRepo |
| 20 | +import to.bitkit.repositories.TrezorState |
| 21 | +import to.bitkit.test.BaseUnitTest |
| 22 | +import kotlin.test.assertEquals |
| 23 | + |
| 24 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 25 | +class HwConnectViewModelTest : BaseUnitTest() { |
| 26 | + |
| 27 | + private val hwWalletRepo = mock<HwWalletRepo>() |
| 28 | + private val needsPairingCode = MutableStateFlow(false) |
| 29 | + private val wallets = MutableStateFlow<ImmutableList<HwWallet>>(persistentListOf()) |
| 30 | + private val deviceState = MutableStateFlow(TrezorState()) |
| 31 | + |
| 32 | + private lateinit var sut: HwConnectViewModel |
| 33 | + |
| 34 | + @Before |
| 35 | + fun setUp() { |
| 36 | + whenever(hwWalletRepo.needsPairingCode).thenReturn(needsPairingCode) |
| 37 | + whenever(hwWalletRepo.wallets).thenReturn(wallets) |
| 38 | + whenever(hwWalletRepo.deviceState).thenReturn(deviceState) |
| 39 | + sut = HwConnectViewModel(hwWalletRepo) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `onIntroContinue searches then advances to found with the first discovered device`() = test { |
| 44 | + deviceState.value = TrezorState(nearbyDevices = persistentListOf(deviceInfo("dev1", model = "Safe 3"))) |
| 45 | + whenever(hwWalletRepo.scan()).thenReturn(Result.success(emptyList())) |
| 46 | + |
| 47 | + sut.effects.test { |
| 48 | + sut.onIntroContinue() |
| 49 | + assertEquals(HwConnectEffect.NavigateToSearching, awaitItem()) |
| 50 | + assertEquals(HwConnectEffect.NavigateToFound, awaitItem()) |
| 51 | + cancelAndIgnoreRemainingEvents() |
| 52 | + } |
| 53 | + |
| 54 | + verify(hwWalletRepo).scan() |
| 55 | + assertEquals("dev1", sut.uiState.value.foundDeviceId) |
| 56 | + assertEquals("Trezor Safe 3", sut.uiState.value.deviceModel) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `onConnectClick connects the found device and advances to paired`() = test { |
| 61 | + givenDeviceFound() |
| 62 | + val connectedFeatures = features(model = "Safe 3") |
| 63 | + whenever(hwWalletRepo.connect("dev1")).thenReturn(Result.success(connectedFeatures)) |
| 64 | + |
| 65 | + sut.effects.test { |
| 66 | + sut.onConnectClick() |
| 67 | + assertEquals(HwConnectEffect.NavigateToPaired, awaitItem()) |
| 68 | + cancelAndIgnoreRemainingEvents() |
| 69 | + } |
| 70 | + |
| 71 | + verify(hwWalletRepo).connect("dev1") |
| 72 | + assertEquals("dev1", sut.uiState.value.pairedDeviceId) |
| 73 | + assertEquals("Trezor Safe 3", sut.uiState.value.labelInput) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `pairing code request surfaces the inline pair code step`() = test { |
| 78 | + sut.effects.test { |
| 79 | + needsPairingCode.value = true |
| 80 | + assertEquals(HwConnectEffect.NavigateToPairCode, awaitItem()) |
| 81 | + cancelAndIgnoreRemainingEvents() |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `connected wallet updates the balance shown on the paired step`() = test { |
| 87 | + givenDeviceFound() |
| 88 | + val connectedFeatures = features(model = "Safe 3") |
| 89 | + whenever(hwWalletRepo.connect("dev1")).thenReturn(Result.success(connectedFeatures)) |
| 90 | + sut.onConnectClick() |
| 91 | + |
| 92 | + wallets.value = persistentListOf(hwWallet("dev1", name = "Trezor Safe 3", balance = 10_562_411uL)) |
| 93 | + |
| 94 | + assertEquals(10_562_411uL, sut.uiState.value.balanceSats) |
| 95 | + assertEquals("Trezor Safe 3", sut.uiState.value.deviceName) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun `onFinishClick persists the edited label and dismisses`() = test { |
| 100 | + givenDeviceFound() |
| 101 | + val connectedFeatures = features(model = "Safe 3") |
| 102 | + whenever(hwWalletRepo.connect("dev1")).thenReturn(Result.success(connectedFeatures)) |
| 103 | + sut.onConnectClick() |
| 104 | + sut.onLabelChange("My Cold Wallet") |
| 105 | + whenever(hwWalletRepo.setDeviceLabel("dev1", "My Cold Wallet")).thenReturn(Result.success(Unit)) |
| 106 | + |
| 107 | + sut.effects.test { |
| 108 | + sut.onFinishClick() |
| 109 | + assertEquals(HwConnectEffect.Dismiss, awaitItem()) |
| 110 | + cancelAndIgnoreRemainingEvents() |
| 111 | + } |
| 112 | + |
| 113 | + verify(hwWalletRepo).setDeviceLabel("dev1", "My Cold Wallet") |
| 114 | + } |
| 115 | + |
| 116 | + private suspend fun givenDeviceFound() { |
| 117 | + deviceState.value = TrezorState(nearbyDevices = persistentListOf(deviceInfo("dev1", model = "Safe 3"))) |
| 118 | + whenever(hwWalletRepo.scan()).thenReturn(Result.success(emptyList())) |
| 119 | + sut.onIntroContinue() |
| 120 | + } |
| 121 | + |
| 122 | + private fun deviceInfo(id: String, model: String?) = TrezorDeviceInfo( |
| 123 | + id = id, |
| 124 | + transportType = TrezorTransportType.BLUETOOTH, |
| 125 | + name = null, |
| 126 | + path = "ble:$id", |
| 127 | + label = null, |
| 128 | + model = model, |
| 129 | + isBootloader = false, |
| 130 | + ) |
| 131 | + |
| 132 | + private fun features(model: String?): TrezorFeatures { |
| 133 | + val features = mock<TrezorFeatures>() |
| 134 | + whenever(features.label).thenReturn(null) |
| 135 | + whenever(features.model).thenReturn(model) |
| 136 | + return features |
| 137 | + } |
| 138 | + |
| 139 | + private fun hwWallet(id: String, name: String, balance: ULong) = HwWallet( |
| 140 | + id = id, |
| 141 | + name = name, |
| 142 | + model = null, |
| 143 | + transportType = TransportType.BLUETOOTH, |
| 144 | + isConnected = true, |
| 145 | + balanceSats = balance, |
| 146 | + activities = persistentListOf(), |
| 147 | + deviceIds = persistentSetOf(id), |
| 148 | + ) |
| 149 | +} |
0 commit comments