@@ -400,6 +400,66 @@ class DeriveBalanceStateUseCaseTest : BaseUnitTest() {
400400 )
401401 }
402402
403+ @Test
404+ fun `should subtract orphan coop close balance from lightning while keeping new channel balance` () = test {
405+ val closedChannelId = " closed-channel-id"
406+ val newChannelId = " new-channel-id"
407+ val closedChannelSats = 1_531_123uL
408+ val newChannelSats = 62_158uL
409+ val orphanClosingBalance = newClosingChannelBalance(closedChannelId, closedChannelSats)
410+ val newChannelBalance = newChannelBalance(newChannelId, newChannelSats)
411+
412+ val balance = newBalanceDetails().copy(
413+ totalOnchainBalanceSats = closedChannelSats,
414+ totalLightningBalanceSats = 1_593_281uL ,
415+ lightningBalances = listOf (orphanClosingBalance, newChannelBalance),
416+ )
417+ whenever(lightningRepo.getBalancesAsync()).thenReturn(Result .success(balance))
418+
419+ val newChannel = mock<ChannelDetails > {
420+ on { channelId } doReturn newChannelId
421+ on { isChannelReady } doReturn true
422+ }
423+
424+ whenever(lightningRepo.getChannels()).thenReturn(listOf (newChannel))
425+ whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
426+
427+ val result = sut()
428+
429+ assertTrue(result.isSuccess)
430+ val balanceState = result.getOrThrow()
431+ assertEquals(closedChannelSats, balanceState.totalOnchainSats)
432+ assertEquals(newChannelSats, balanceState.totalLightningSats)
433+ assertEquals(0uL , balanceState.balanceInTransferToSavings)
434+ assertEquals(0uL , balanceState.balanceInTransferToSpending)
435+ }
436+
437+ @Test
438+ fun `should not subtract orphan non coop close balance from lightning` () = test {
439+ val channelId = " force-closed-channel-id"
440+ val amountSats = 40_000uL
441+ val closingChannelBalance = newClosingChannelBalance(
442+ id = channelId,
443+ sats = amountSats,
444+ source = BalanceSource .COUNTERPARTY_FORCE_CLOSED ,
445+ )
446+
447+ val balance = newBalanceDetails().copy(
448+ lightningBalances = listOf (closingChannelBalance),
449+ totalLightningBalanceSats = amountSats,
450+ )
451+ whenever(lightningRepo.getBalancesAsync()).thenReturn(Result .success(balance))
452+
453+ whenever(lightningRepo.getChannels()).thenReturn(emptyList())
454+ whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
455+
456+ val result = sut()
457+
458+ assertTrue(result.isSuccess)
459+ val balanceState = result.getOrThrow()
460+ assertEquals(amountSats, balanceState.totalLightningSats)
461+ }
462+
403463 @Test
404464 fun `should calculate zero max send onchain when spendable balance is zero` () = test {
405465 val balance = newBalanceDetails().copy(totalOnchainBalanceSats = 50_000u )
@@ -554,12 +614,16 @@ class DeriveBalanceStateUseCaseTest : BaseUnitTest() {
554614 inboundHtlcRoundedMsat = 0u ,
555615 )
556616
557- private fun newClosingChannelBalance (id : String , sats : ULong ) = LightningBalance .ClaimableAwaitingConfirmations (
617+ private fun newClosingChannelBalance (
618+ id : String ,
619+ sats : ULong ,
620+ source : BalanceSource = BalanceSource .COOP_CLOSE ,
621+ ) = LightningBalance .ClaimableAwaitingConfirmations (
558622 channelId = id,
559623 counterpartyNodeId = " node-id" ,
560624 amountSatoshis = sats,
561625 confirmationHeight = 344u ,
562- source = BalanceSource . COOP_CLOSE ,
626+ source = source ,
563627 )
564628
565629 private fun newTransferEntity (
0 commit comments