Skip to content

Commit deb5de7

Browse files
committed
feat: new lightning repo tests
1 parent 3fc00b0 commit deb5de7

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package to.bitkit.repositories
22

33
import app.cash.turbine.test
44
import com.google.firebase.messaging.FirebaseMessaging
5+
import com.synonym.bitkitcore.FeeRates
56
import kotlinx.coroutines.flow.flowOf
67
import org.junit.Before
78
import org.junit.Test
89
import org.lightningdevkit.ldknode.ChannelDetails
910
import org.lightningdevkit.ldknode.NodeStatus
1011
import org.lightningdevkit.ldknode.PaymentDetails
12+
import org.lightningdevkit.ldknode.SpendableUtxo
1113
import org.mockito.kotlin.any
1214
import org.mockito.kotlin.anyOrNull
1315
import org.mockito.kotlin.argumentCaptor
@@ -26,18 +28,21 @@ import to.bitkit.data.SettingsStore
2628
import to.bitkit.data.dto.TransactionMetadata
2729
import to.bitkit.data.keychain.Keychain
2830
import to.bitkit.ext.createChannelDetails
31+
import to.bitkit.models.CoinSelectionPreference
2932
import to.bitkit.models.ElectrumServer
3033
import to.bitkit.models.LnPeer
3134
import to.bitkit.models.NodeLifecycleState
3235
import to.bitkit.models.TransactionSpeed
3336
import to.bitkit.services.BlocktankNotificationsService
37+
import to.bitkit.services.BlocktankService
3438
import to.bitkit.services.CoreService
3539
import to.bitkit.services.LdkNodeEventBus
3640
import to.bitkit.services.LightningService
3741
import to.bitkit.services.LnurlService
3842
import to.bitkit.test.BaseUnitTest
3943
import kotlin.test.assertEquals
4044
import kotlin.test.assertFalse
45+
import kotlin.test.assertNotNull
4146
import kotlin.test.assertNull
4247
import kotlin.test.assertTrue
4348

@@ -433,4 +438,61 @@ class LightningRepoTest : BaseUnitTest() {
433438

434439
assertTrue(result.isFailure)
435440
}
441+
442+
@Test
443+
fun `getFeeRateForSpeed should use provided feeRates`() = test {
444+
val mockFeeRates = mock<FeeRates>()
445+
whenever(mockFeeRates.mid).thenReturn(20u)
446+
447+
val result = sut.getFeeRateForSpeed(TransactionSpeed.Medium, mockFeeRates)
448+
449+
assertTrue(result.isSuccess)
450+
assertEquals(20uL, result.getOrNull())
451+
}
452+
453+
@Test
454+
fun `getFeeRateForSpeed should fetch from blocktank when feeRates is null`() = test {
455+
val mockFeeRates = mock<FeeRates>()
456+
whenever(mockFeeRates.fast).thenReturn(30u)
457+
val blocktank = mock<BlocktankService>()
458+
whenever(blocktank.getFees()).thenReturn(Result.success(mockFeeRates))
459+
whenever(coreService.blocktank).thenReturn(blocktank)
460+
461+
val result = sut.getFeeRateForSpeed(TransactionSpeed.Fast, null)
462+
463+
assertTrue(result.isSuccess)
464+
assertEquals(30uL, result.getOrNull())
465+
}
466+
467+
@Test
468+
fun `determineUtxosToSpend should return null when coinSelectAuto is false`() = test {
469+
val mockSettingsData = SettingsData(coinSelectAuto = false)
470+
whenever(settingsStore.data).thenReturn(flowOf(mockSettingsData))
471+
472+
val result = sut.determineUtxosToSpend(1000uL, 10u)
473+
474+
assertNull(result)
475+
}
476+
477+
@Test
478+
fun `determineUtxosToSpend should return all UTXOs when preference is Consolidate`() = test {
479+
val mockSettingsData = SettingsData(
480+
coinSelectAuto = true,
481+
coinSelectPreference = CoinSelectionPreference.Consolidate
482+
)
483+
whenever(settingsStore.data).thenReturn(flowOf(mockSettingsData))
484+
485+
val mockUtxos = listOf(
486+
mock<SpendableUtxo>(),
487+
mock<SpendableUtxo>(),
488+
mock<SpendableUtxo>()
489+
)
490+
whenever(lightningService.listSpendableOutputs()).thenReturn(Result.success(mockUtxos))
491+
492+
val result = sut.determineUtxosToSpend(1000uL, 10u)
493+
494+
assertNotNull(result)
495+
assertEquals(3, result.size)
496+
assertEquals(mockUtxos, result)
497+
}
436498
}

0 commit comments

Comments
 (0)