@@ -312,6 +312,66 @@ class DeriveBalanceStateUseCaseTest : BaseUnitTest() {
312312 )
313313 }
314314
315+ @Test
316+ fun `should subtract orphan coop close balance from lightning while keeping new channel balance` () = test {
317+ val closedChannelId = " closed-channel-id"
318+ val newChannelId = " new-channel-id"
319+ val closedChannelSats = 1_531_123uL
320+ val newChannelSats = 62_158uL
321+ val orphanClosingBalance = newClosingChannelBalance(closedChannelId, closedChannelSats)
322+ val newChannelBalance = newChannelBalance(newChannelId, newChannelSats)
323+
324+ val balance = newBalanceDetails().copy(
325+ totalOnchainBalanceSats = closedChannelSats,
326+ totalLightningBalanceSats = 1_593_281uL ,
327+ lightningBalances = listOf (orphanClosingBalance, newChannelBalance),
328+ )
329+ whenever(lightningRepo.getBalancesAsync()).thenReturn(Result .success(balance))
330+
331+ val newChannel = mock<ChannelDetails > {
332+ on { channelId } doReturn newChannelId
333+ on { isChannelReady } doReturn true
334+ }
335+
336+ whenever(lightningRepo.getChannels()).thenReturn(listOf (newChannel))
337+ whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
338+
339+ val result = sut()
340+
341+ assertTrue(result.isSuccess)
342+ val balanceState = result.getOrThrow()
343+ assertEquals(closedChannelSats, balanceState.totalOnchainSats)
344+ assertEquals(newChannelSats, balanceState.totalLightningSats)
345+ assertEquals(0uL , balanceState.balanceInTransferToSavings)
346+ assertEquals(0uL , balanceState.balanceInTransferToSpending)
347+ }
348+
349+ @Test
350+ fun `should not subtract orphan non coop close balance from lightning` () = test {
351+ val channelId = " force-closed-channel-id"
352+ val amountSats = 40_000uL
353+ val closingChannelBalance = newClosingChannelBalance(
354+ id = channelId,
355+ sats = amountSats,
356+ source = BalanceSource .COUNTERPARTY_FORCE_CLOSED ,
357+ )
358+
359+ val balance = newBalanceDetails().copy(
360+ lightningBalances = listOf (closingChannelBalance),
361+ totalLightningBalanceSats = amountSats,
362+ )
363+ whenever(lightningRepo.getBalancesAsync()).thenReturn(Result .success(balance))
364+
365+ whenever(lightningRepo.getChannels()).thenReturn(emptyList())
366+ whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
367+
368+ val result = sut()
369+
370+ assertTrue(result.isSuccess)
371+ val balanceState = result.getOrThrow()
372+ assertEquals(amountSats, balanceState.totalLightningSats)
373+ }
374+
315375 @Test
316376 fun `should calculate zero max send onchain when spendable balance is zero` () = test {
317377 val balance = newBalanceDetails().copy(totalOnchainBalanceSats = 50_000u )
@@ -466,12 +526,16 @@ class DeriveBalanceStateUseCaseTest : BaseUnitTest() {
466526 inboundHtlcRoundedMsat = 0u ,
467527 )
468528
469- private fun newClosingChannelBalance (id : String , sats : ULong ) = LightningBalance .ClaimableAwaitingConfirmations (
529+ private fun newClosingChannelBalance (
530+ id : String ,
531+ sats : ULong ,
532+ source : BalanceSource = BalanceSource .COOP_CLOSE ,
533+ ) = LightningBalance .ClaimableAwaitingConfirmations (
470534 channelId = id,
471535 counterpartyNodeId = " node-id" ,
472536 amountSatoshis = sats,
473537 confirmationHeight = 344u ,
474- source = BalanceSource . COOP_CLOSE ,
538+ source = source ,
475539 )
476540
477541 private fun newTransferEntity (
0 commit comments