Skip to content

Commit 8dea165

Browse files
committed
test: bip21 wallet state management fix
1 parent 9450857 commit 8dea165

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

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

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,159 @@ class WalletRepoTest : BaseUnitTest() {
445445
assertTrue(result.isSuccess)
446446
assertFalse(result.getOrThrow())
447447
}
448+
449+
@Test
450+
fun `clearBip21State should clear all bip21 related state`() = test {
451+
sut.addTagToSelected("tag1")
452+
sut.updateBip21Invoice(amountSats = 1000uL, description = "test")
453+
454+
sut.clearBip21State()
455+
456+
assertEquals("", sut.walletState.value.bip21)
457+
assertEquals(null, sut.walletState.value.bip21AmountSats)
458+
assertEquals("", sut.walletState.value.bip21Description)
459+
assertTrue(sut.walletState.value.selectedTags.isEmpty())
460+
}
461+
462+
@Test
463+
fun `setBip21AmountSats should update state`() = test {
464+
val testAmount = 5000uL
465+
466+
sut.setBip21AmountSats(testAmount)
467+
468+
assertEquals(testAmount, sut.walletState.value.bip21AmountSats)
469+
}
470+
471+
@Test
472+
fun `setBip21Description should update state`() = test {
473+
val testDescription = "test description"
474+
475+
sut.setBip21Description(testDescription)
476+
477+
assertEquals(testDescription, sut.walletState.value.bip21Description)
478+
}
479+
480+
@Test
481+
fun `refreshBip21ForEvent ChannelReady should update bolt11 and preserve amount`() = test {
482+
val testAmount = 1000uL
483+
val testDescription = "test"
484+
val testInvoice = "newInvoice"
485+
sut.setBip21AmountSats(testAmount)
486+
sut.setBip21Description(testDescription)
487+
whenever(lightningRepo.canReceive()).thenReturn(true)
488+
whenever(lightningRepo.createInvoice(anyOrNull(), any(), any())).thenReturn(Result.success(testInvoice))
489+
490+
sut.refreshBip21ForEvent(
491+
Event.ChannelReady(
492+
channelId = "testChannelId",
493+
userChannelId = "testUserChannelId",
494+
counterpartyNodeId = null
495+
)
496+
)
497+
498+
assertEquals(testInvoice, sut.walletState.value.bolt11)
499+
assertEquals(testAmount, sut.walletState.value.bip21AmountSats)
500+
assertEquals(testDescription, sut.walletState.value.bip21Description)
501+
}
502+
503+
@Test
504+
fun `refreshBip21ForEvent ChannelReady should not create invoice when cannot receive`() = test {
505+
sut.setBip21AmountSats(1000uL)
506+
whenever(lightningRepo.canReceive()).thenReturn(false)
507+
508+
sut.refreshBip21ForEvent(
509+
Event.ChannelReady(
510+
channelId = "testChannelId",
511+
userChannelId = "testUserChannelId",
512+
counterpartyNodeId = null
513+
)
514+
)
515+
516+
verify(lightningRepo, never()).createInvoice(anyOrNull(), any(), any())
517+
}
518+
519+
@Test
520+
fun `refreshBip21ForEvent ChannelClosed should clear bolt11 when cannot receive`() = test {
521+
val testAddress = "testAddress"
522+
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(onchainAddress = testAddress)))
523+
sut = createSut()
524+
sut.setBolt11("existingInvoice")
525+
whenever(lightningRepo.canReceive()).thenReturn(false)
526+
527+
sut.refreshBip21ForEvent(
528+
Event.ChannelClosed(
529+
channelId = "testChannelId",
530+
userChannelId = "testUserChannelId",
531+
counterpartyNodeId = null,
532+
reason = null
533+
)
534+
)
535+
536+
assertEquals("", sut.walletState.value.bolt11)
537+
}
538+
539+
@Test
540+
fun `refreshBip21ForEvent ChannelClosed should not clear bolt11 when can still receive`() = test {
541+
val testInvoice = "existingInvoice"
542+
sut.setBolt11(testInvoice)
543+
whenever(lightningRepo.canReceive()).thenReturn(true)
544+
545+
sut.refreshBip21ForEvent(
546+
Event.ChannelClosed(
547+
channelId = "testChannelId",
548+
userChannelId = "testUserChannelId",
549+
counterpartyNodeId = null,
550+
reason = null
551+
)
552+
)
553+
554+
assertEquals(testInvoice, sut.walletState.value.bolt11)
555+
}
556+
557+
@Test
558+
fun `refreshBip21ForEvent PaymentReceived should refresh address if used`() = test {
559+
val testAddress = "testAddress"
560+
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(onchainAddress = testAddress)))
561+
whenever(addressChecker.getAddressInfo(any())).thenReturn(
562+
mockAddressInfo().let { addressInfo ->
563+
addressInfo.copy(
564+
chain_stats = addressInfo.chain_stats.copy(tx_count = 1)
565+
)
566+
}
567+
)
568+
whenever(lightningRepo.newAddress()).thenReturn(Result.success("newAddress"))
569+
sut = createSut()
570+
571+
sut.refreshBip21ForEvent(
572+
Event.PaymentReceived(
573+
paymentId = "testPaymentId",
574+
paymentHash = "testPaymentHash",
575+
amountMsat = 1000uL,
576+
customRecords = emptyList()
577+
)
578+
)
579+
580+
verify(lightningRepo).newAddress()
581+
}
582+
583+
@Test
584+
fun `refreshBip21ForEvent PaymentReceived should not refresh address if not used`() = test {
585+
val testAddress = "testAddress"
586+
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(onchainAddress = testAddress)))
587+
whenever(addressChecker.getAddressInfo(any())).thenReturn(mockAddressInfo())
588+
sut = createSut()
589+
590+
sut.refreshBip21ForEvent(
591+
Event.PaymentReceived(
592+
paymentId = "testPaymentId",
593+
paymentHash = "testPaymentHash",
594+
amountMsat = 1000uL,
595+
customRecords = emptyList()
596+
)
597+
)
598+
599+
verify(lightningRepo, never()).newAddress()
600+
}
448601
}
449602

450603
private fun mockAddressInfo() = AddressInfo(

0 commit comments

Comments
 (0)