|
1 | 1 | package to.bitkit.ui.screens.trezor |
2 | 2 |
|
| 3 | +import com.synonym.bitkitcore.TrezorSignedTx |
3 | 4 | import kotlinx.coroutines.CompletableDeferred |
4 | 5 | import kotlinx.coroutines.ExperimentalCoroutinesApi |
5 | 6 | import kotlinx.coroutines.flow.MutableStateFlow |
@@ -267,6 +268,46 @@ class TrezorViewModelTest : BaseUnitTest() { |
267 | 268 | assertNull(state.broadcastTxid) |
268 | 269 | } |
269 | 270 |
|
| 271 | + @Test |
| 272 | + fun `broadcastSignedTx failure should not clear newer broadcast`() = test { |
| 273 | + loadSignedTx() |
| 274 | + val firstBroadcastResult = CompletableDeferred<Result<String>>() |
| 275 | + val secondBroadcastResult = CompletableDeferred<Result<String>>() |
| 276 | + val broadcastResults = ArrayDeque( |
| 277 | + listOf(firstBroadcastResult, secondBroadcastResult) |
| 278 | + ) |
| 279 | + whenever(trezorRepo.broadcastRawTx(any(), any())) |
| 280 | + .doSuspendableAnswer { broadcastResults.removeFirst().await() } |
| 281 | + |
| 282 | + sut.broadcastSignedTx() |
| 283 | + assertTrue(sut.uiState.value.isBroadcasting) |
| 284 | + |
| 285 | + val secondSignedTx = TrezorSignedTx( |
| 286 | + signatures = listOf("30440220new"), |
| 287 | + serializedTx = "0200000001new", |
| 288 | + txid = "new-broadcast-txid", |
| 289 | + ) |
| 290 | + sut.resetSendFlow() |
| 291 | + loadSignedTx(secondSignedTx) |
| 292 | + sut.broadcastSignedTx() |
| 293 | + assertTrue(sut.uiState.value.isBroadcasting) |
| 294 | + |
| 295 | + firstBroadcastResult.complete(Result.failure(RuntimeException("first failed"))) |
| 296 | + advanceUntilIdle() |
| 297 | + |
| 298 | + val staleFailureState = sut.uiState.value |
| 299 | + assertEquals(secondSignedTx, staleFailureState.signedTxResult) |
| 300 | + assertTrue(staleFailureState.isBroadcasting) |
| 301 | + assertNull(staleFailureState.broadcastTxid) |
| 302 | + |
| 303 | + secondBroadcastResult.complete(Result.success("second-broadcast-txid")) |
| 304 | + advanceUntilIdle() |
| 305 | + |
| 306 | + val finalState = sut.uiState.value |
| 307 | + assertFalse(finalState.isBroadcasting) |
| 308 | + assertEquals("second-broadcast-txid", finalState.broadcastTxid) |
| 309 | + } |
| 310 | + |
270 | 311 | @Test |
271 | 312 | fun `composeTx should not call repo when destination address is blank`() = test { |
272 | 313 | loadAccountInfo() |
@@ -346,12 +387,14 @@ class TrezorViewModelTest : BaseUnitTest() { |
346 | 387 | advanceUntilIdle() |
347 | 388 | } |
348 | 389 |
|
349 | | - private suspend fun TestScope.loadSignedTx() { |
| 390 | + private suspend fun TestScope.loadSignedTx( |
| 391 | + signedTx: TrezorSignedTx = TrezorPreviewData.sampleSignedTx, |
| 392 | + ) { |
350 | 393 | loadAccountInfo() |
351 | 394 | whenever(trezorRepo.composeTransaction(any(), any(), any(), any(), anyOrNull(), any())) |
352 | 395 | .thenReturn(Result.success(listOf(TrezorPreviewData.sampleComposeResult))) |
353 | 396 | whenever(trezorRepo.signTxFromPsbt(any(), anyOrNull())) |
354 | | - .thenReturn(Result.success(TrezorPreviewData.sampleSignedTx)) |
| 397 | + .thenReturn(Result.success(signedTx)) |
355 | 398 |
|
356 | 399 | sut.setSendAddress("bc1qtest123") |
357 | 400 | sut.setSendAmount("1000") |
|
0 commit comments