diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 449eecf8..15583caf 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -473,8 +473,11 @@ Spend from %1$s's guarded wallet Wallets of groups below this one. This group can spend a coin in them once it has %1$d confirmations. %1$s sats within this group's reach + Fund Fund %1$s's guarded wallet + %1$s sats arriving To + %1$s's guarded wallet This group can spend these coins again once they have %1$d confirmations. Finding an address in %1$s's wallet… Wait for this group's open payments to %1$s to confirm. diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt index d089fed7..fdea3ee7 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt @@ -9,6 +9,7 @@ import bennu.composeapp.generated.resources.fund_s_s_guarded_wallet import bennu.composeapp.generated.resources.sends_s_sats_to_s_s_guarded_wallet_with_a_fee_of_s_sats import bennu.composeapp.generated.resources.this_group_can_spend_these_coins_again import bennu.composeapp.generated.resources.to +import bennu.composeapp.generated.resources.s_s_guarded_wallet import bennu.composeapp.generated.resources.wait_for_this_group_s_open_payments_to_s import com.machankura.bennu.compose.managers.GuardianDerivation import com.machankura.bennu.compose.ui.view.state.FundingTarget @@ -320,7 +321,7 @@ private fun FundingDestination(funding: FundingTarget) { verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap), ) { Text(text = stringResource(Res.string.to), style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.onSurfaceVariant) - Text(text = stringResource(Res.string.fund_s_s_guarded_wallet, name), style = MaterialTheme.typography.titleMedium) + Text(text = stringResource(Res.string.s_s_guarded_wallet, name), style = MaterialTheme.typography.titleMedium) val address = funding.address when { address != null -> { diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletScreen.kt index 65b06227..aae6c5be 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletScreen.kt @@ -4,6 +4,14 @@ import bennu.composeapp.generated.resources.the_group_above_decided_this_wallet_ import bennu.composeapp.generated.resources.the_group_above_decided_this_wallet_is_guarded import com.machankura.bennu.compose.nostr.subgroup.WalletPolicyReading import com.machankura.bennu.compose.nostr.subgroup.SubgroupWalletPolicy +import androidx.compose.foundation.layout.FlowRow +import androidx.compose.material3.OutlinedCard +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.semantics +import bennu.composeapp.generated.resources.fund +import bennu.composeapp.generated.resources.fund_s_s_guarded_wallet +import bennu.composeapp.generated.resources.s_sats_arriving +import bennu.composeapp.generated.resources.spend_from_s_s_guarded_wallet import androidx.compose.foundation.layout.Row import com.machankura.bennu.compose.text.ProposedSpend import com.machankura.bennu.compose.ui.view.state.SpendRow @@ -197,6 +205,18 @@ fun GroupWalletScreen( ) ) } + // Behind the room's own send's gates, since a funding is one: a share to + // propose with, and a node to see this group's coins with. + val toGuardianFund = { row: GuardedWalletRow -> + onNavigateToRoute( + GroupSendRoute( + activeUserPublicKey = activeUserPublicKey, + chatRoomId = chatRoomId, + relayHint = null, + fundChatRoomId = row.wallet.descendantChatRoomId, + ) + ) + } val onShare = { shareable: GuardianManager.Shareable -> groupWalletViewModel.share(shareable, onShared = { notify(shared) }, onFailure = { notify(couldNotShare) }) } @@ -297,6 +317,8 @@ fun GroupWalletScreen( isPending = groupWalletViewModel.isActionPending.value, onShare = onShare, onSpend = if (state.canSign && wallets != null) toGuardianSpend else null, + // A group with no wallet of its own has nothing to fund with. + onFund = null, ) } } @@ -340,6 +362,7 @@ fun GroupWalletScreen( isPending = groupWalletViewModel.isActionPending.value, onShare = onShare, onSpend = if (state.canSign && state.canWatch) toGuardianSpend else null, + onFund = if (state.canSign && state.canWatch) toGuardianFund else null, ) } } @@ -440,6 +463,7 @@ private fun OpenWallet( isPending: Boolean, onShare: (GuardianManager.Shareable) -> Unit, onSpend: ((GuardedWalletRow) -> Unit)?, + onFund: ((GuardedWalletRow) -> Unit)?, ) { val clipboardManager = LocalClipboardManager.current val addressCopied = stringResource(Res.string.address_copied) @@ -605,6 +629,7 @@ private fun OpenWallet( isPending = isPending, onShare = onShare, onSpend = onSpend, + onFund = onFund, ) } } @@ -662,6 +687,7 @@ private fun LazyListScope.guardianItems( isPending: Boolean, onShare: (GuardianManager.Shareable) -> Unit, onSpend: ((GuardedWalletRow) -> Unit)?, + onFund: ((GuardedWalletRow) -> Unit)?, ) { if (guardedWallets.isNotEmpty()) { item { @@ -675,23 +701,7 @@ private fun LazyListScope.guardianItems( ) } items(guardedWallets, key = { "guarded-" + it.wallet.statement.id }) { row -> - ListItem( - leadingContent = { Icon(Icons.Default.AccountBalanceWallet, contentDescription = Decorative) }, - headlineContent = { Text(row.wallet.name ?: stringResource(Res.string.a_subgroup), maxLines = 1, overflow = TextOverflow.Ellipsis) }, - supportingContent = { - Text( - row.withinReach?.let { stringResource(Res.string.s_sats_within_this_group_s_reach, it.grouped()) } - ?: stringResource(Res.string.balance_not_known_yet) - ) - }, - trailingContent = onSpend?.let { spend -> - { - TextButton(onClick = { spend(row) }) { - Text(stringResource(Res.string.spend)) - } - } - }, - ) + GuardedWalletCard(row = row, onSpend = onSpend, onFund = onFund) } } @@ -713,6 +723,66 @@ private fun LazyListScope.guardianItems( } } +/** + * One descendant's guarded wallet: its name and what is within reach, and under + * them the actions, in the card's own row rather than the list item's trailing + * slot. Two buttons there would leave the name -- the one thing that says which + * wallet this is -- a few characters at large text sizes, and a flow row wraps + * them instead. Each button names its wallet, since a screen reader hears every + * row's *Fund* and *Spend* alike. See `docs/guarded-wallet-funding.md`, Phase 5. + */ +@Composable +private fun GuardedWalletCard( + row: GuardedWalletRow, + onSpend: ((GuardedWalletRow) -> Unit)?, + onFund: ((GuardedWalletRow) -> Unit)?, +) { + val name = row.wallet.name ?: stringResource(Res.string.a_subgroup) + OutlinedCard(modifier = Modifier.fillMaxWidth()) { + ListItem( + leadingContent = { Icon(Icons.Default.AccountBalanceWallet, contentDescription = Decorative) }, + // Two lines, since the name is what says which wallet the buttons act on. + headlineContent = { Text(name, maxLines = 2, overflow = TextOverflow.Ellipsis) }, + supportingContent = { + val reach = row.withinReach?.let { stringResource(Res.string.s_sats_within_this_group_s_reach, it.grouped()) } + val arriving = row.arriving?.takeIf { it.sat > 0 }?.let { stringResource(Res.string.s_sats_arriving, it.grouped()) } + Text( + when { + reach == null -> stringResource(Res.string.balance_not_known_yet) + arriving == null -> reach + else -> stringResource(Res.string.s_dot_s, reach, arriving) + } + ) + }, + ) + if (onFund != null || onSpend != null) { + val fundLabel = stringResource(Res.string.fund_s_s_guarded_wallet, name) + val spendLabel = stringResource(Res.string.spend_from_s_s_guarded_wallet, name) + FlowRow( + modifier = Modifier.fillMaxWidth().padding( + start = MaterialTheme.spacing.containerPadding, + end = MaterialTheme.spacing.containerPadding, + bottom = MaterialTheme.spacing.compactPadding, + ), + horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.targetGap, Alignment.End), + ) { + // Money in before money out, at equal weight: neither is the one + // every member wants, and a filled button would say it was. + onFund?.let { fund -> + TextButton(onClick = { fund(row) }, modifier = Modifier.semantics { contentDescription = fundLabel }) { + Text(stringResource(Res.string.fund)) + } + } + onSpend?.let { spend -> + TextButton(onClick = { spend(row) }, modifier = Modifier.semantics { contentDescription = spendLabel }) { + Text(stringResource(Res.string.spend)) + } + } + } + } + } +} + @Composable private fun BalanceCard(walletState: GroupWalletState?) { Card(modifier = Modifier.fillMaxWidth()) { diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt index baf38de0..1ed598cf 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletUIState.kt @@ -88,6 +88,18 @@ data class GuardedWalletRow( get() = walletState?.let { state -> state.utxos.filter { state.confirmations(it) >= GuardianDerivation.ANCESTOR_DELAY }.map { it.amount }.sum() } + + /** + * Coins in the wallet not yet within reach: in the mempool, or confirmed + * less deeply than the leaf's lock. What a funding looks like for the two + * hours after it lands -- without it, [withinReach] would make the money + * look gone. Every such coin, whoever paid it, which is all this group can + * know. Null while the chain is not known. + */ + val arriving: Satoshi? + get() = walletState?.let { state -> + state.utxos.filter { state.confirmations(it) < GuardianDerivation.ANCESTOR_DELAY }.map { it.amount }.sum() + } } /** Where one of the room's spends has got to, as one fact a member reads. */ diff --git a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/ui/view/state/GuardedWalletRowTest.kt b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/ui/view/state/GuardedWalletRowTest.kt new file mode 100644 index 00000000..0136d3eb --- /dev/null +++ b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/ui/view/state/GuardedWalletRowTest.kt @@ -0,0 +1,80 @@ +package com.machankura.bennu.compose.ui.view.state + +import com.machankura.bennu.compose.managers.GroupWalletDerivation +import com.machankura.bennu.compose.managers.GuardianDerivation +import com.machankura.bennu.compose.managers.GuardianManager +import com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent +import com.machankura.bennu.compose.wallet.GroupWalletState +import com.vitorpamplona.quartz.nip01Core.core.Event +import fr.acinq.bitcoin.ByteVector +import fr.acinq.bitcoin.ByteVector32 +import fr.acinq.bitcoin.Chain +import fr.acinq.bitcoin.OutPoint +import fr.acinq.bitcoin.PrivateKey +import fr.acinq.bitcoin.Satoshi +import fr.acinq.bitcoin.Transaction +import fr.acinq.bitcoin.TxId +import fr.acinq.bitcoin.TxIn +import fr.acinq.bitcoin.TxOut +import fr.acinq.lightning.blockchain.electrum.WalletState +import fr.acinq.secp256k1.Hex +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +/** + * What an ancestor's wallet screen says about a descendant's guarded wallet: a + * funding is *arriving* until the leaf's lock lets it go, and then *within + * reach* -- never neither, which would make the money look gone. + */ +class GuardedWalletRowTest { + private val descendantRoomId = "c".repeat(64) + private val account = GroupWalletDerivation.account( + PrivateKey(ByteVector32(Hex.decode("decaf".padStart(64, '0')))).publicKey().toString(), Chain.Regtest, + ) + private val wallet = GuardianManager.GuardedWallet( + ancestorChatRoomId = "d".repeat(64), + descendantChatRoomId = descendantRoomId, + statement = Event("1".repeat(64), descendantRoomId, 1_700_000_000, GroupWalletStatementEvent.KIND, emptyArray(), "", ""), + account = account, + certificate = Event("2".repeat(64), "d".repeat(64), 1_700_000_000, 0, emptyArray(), "", ""), + lineage = listOf(descendantRoomId), + ) + + /** One coin of 50,000 sats at the first receive address, confirmed at [height] (0 in the mempool), with the tip at [tip]. */ + private fun holding(height: Long, tip: Int): GroupWalletState { + val derived = GroupWalletDerivation.derive(account, GroupWalletDerivation.RECEIVE, 0) + val tx = Transaction(2, listOf(TxIn(OutPoint(TxId(ByteVector32(ByteArray(32) { 7 })), 0), ByteVector.empty, 0xffffffffL)), listOf(TxOut(Satoshi(50_000), derived.pubkeyScript)), 0) + val meta = WalletState.AddressMeta.Derived(0) + return GroupWalletState( + account, + WalletState(mapOf(derived.address(Chain.Regtest) to WalletState.AddressState(meta, true, listOf(WalletState.Utxo(tx.txid, 0, height, tx, meta))))), + WalletState.empty, + tipHeight = tip, + ) + } + + @Test + fun `a coin is arriving until the lock lets it go, then within reach`() { + val lock = GuardianDerivation.ANCESTOR_DELAY.toInt() + + val mempool = GuardedWalletRow(wallet, holding(height = 0, tip = 100)) + assertEquals(Satoshi(50_000), mempool.arriving) + assertEquals(Satoshi(0), mempool.withinReach) + + val young = GuardedWalletRow(wallet, holding(height = 100, tip = 100 + lock - 2)) + assertEquals(Satoshi(50_000), young.arriving, "${lock - 1} confirmations") + assertEquals(Satoshi(0), young.withinReach) + + val old = GuardedWalletRow(wallet, holding(height = 100, tip = 100 + lock - 1)) + assertEquals(Satoshi(0), old.arriving, "$lock confirmations") + assertEquals(Satoshi(50_000), old.withinReach) + } + + @Test + fun `nothing is said before the chain is known`() { + val row = GuardedWalletRow(wallet, walletState = null) + assertNull(row.arriving) + assertNull(row.withinReach) + } +} diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreenJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreenJvmTest.kt index 197071e3..4d54fd35 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreenJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreenJvmTest.kt @@ -19,6 +19,9 @@ import com.machankura.bennu.compose.database.model.ChatRoom import com.machankura.bennu.compose.database.model.GroupSignedEvent import com.machankura.bennu.compose.database.model.intermdiate.LocalChatRoom import com.machankura.bennu.compose.managers.GroupWalletDerivation +import com.machankura.bennu.compose.managers.GuardianManager +import com.machankura.bennu.compose.ui.view.state.FundingTarget +import com.vitorpamplona.quartz.nip01Core.core.Event import com.machankura.bennu.compose.nostr.wallet.GroupWallet import com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent import com.machankura.bennu.compose.repository.ChatRepository @@ -106,6 +109,40 @@ class GroupSendScreenJvmTest { onNodeWithText("Ask the group to send").assertIsNotEnabled() } + @Test + fun `funding a guarded wallet names it, shows where it pays, and takes no address`() = render(Chain.Regtest, funded = true) { + onNodeWithText("Fund Savings's guarded wallet").assertIsDisplayed() + onNodeWithText("Savings's guarded wallet").assertIsDisplayed() + onNodeWithText("Address").assertDoesNotExist() + onNodeWithText("Address 2").assertIsDisplayed() + onNodeWithText("This group can spend these coins again once they have 12 confirmations.").assertIsDisplayed() + + onNodeWithText("Fee rate in sats per byte").performTextInput("4") + onNodeWithText("Amount in sats").performTextInput("40000") + onNodeWithText("Sends 40,000 sats to Savings's guarded wallet", substring = true).assertIsDisplayed() + onNodeWithText("Ask the group to send").assertIsEnabled() + } + + private fun funding(chain: Chain): FundingTarget { + val descendantRoomId = "c".repeat(64) + val descendant = GroupWalletDerivation.account( + PrivateKey(ByteVector32(Hex.decode("decaf".padStart(64, '0')))).publicKey().toString(), chain, + ) + val guarded = GuardianManager.GuardedWallet( + ancestorChatRoomId = chatRoomId, + descendantChatRoomId = descendantRoomId, + statement = Event("1".repeat(64), descendantRoomId, 1_700_000_000, GroupWalletStatementEvent.KIND, emptyArray(), "", ""), + account = descendant, + certificate = Event("2".repeat(64), chatRoomId, 1_700_000_000, 0, arrayOf(arrayOf("name", "Savings")), "", ""), + lineage = listOf(descendantRoomId), + ) + val receive = (0 until 10).associate { index -> + GroupWalletDerivation.derive(descendant, GroupWalletDerivation.RECEIVE, index.toLong()).address(chain) to + WalletState.AddressState(WalletState.AddressMeta.Derived(index), alreadyUsed = false, utxos = emptyList()) + } + return FundingTarget(guarded, GroupWalletState(descendant, WalletState(receive), WalletState.empty, tipHeight = 110)) + } + private fun ComposeUiTest.fill(address: String, amount: String) { onNodeWithText("Address").performTextInput(address) onNodeWithText("Fee rate in sats per byte").performTextInput("4") @@ -146,7 +183,7 @@ class GroupSendScreenJvmTest { return GroupSendUIState.Ready(room, wallet, walletState, reserved = emptySet()) } - private fun render(chain: Chain, assertions: ComposeUiTest.() -> Unit) = + private fun render(chain: Chain, funded: Boolean = false, assertions: ComposeUiTest.() -> Unit) = runDesktopComposeUiTest(width = 400, height = 1200) { setContent { BennuTheme { @@ -156,7 +193,7 @@ class GroupSendScreenJvmTest { activeUserPublicKey = "a".repeat(64), chatRoomId = chatRoomId, chain = chain, - initialGroupSendUIState = ready(chain), + initialGroupSendUIState = ready(chain).let { if (funded) it.copy(funding = funding(chain)) else it }, chatRepository = ChatRepository.NO_OP_CHAT_REPOSITORY, frostSigningRepository = FrostSigningRepository.NO_OP_FROST_SIGNING_REPOSITORY, wallets = null, diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/GuardedWalletCardJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/GuardedWalletCardJvmTest.kt new file mode 100644 index 00000000..58831b5b --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/composable/GuardedWalletCardJvmTest.kt @@ -0,0 +1,167 @@ +package com.machankura.bennu.compose.ui.composable + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.test.ComposeUiTest +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.onNodeWithContentDescription +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.runDesktopComposeUiTest +import androidx.compose.ui.unit.Density +import com.machankura.bennu.compose.database.model.ChatRoom +import com.machankura.bennu.compose.database.model.GroupSignedEvent +import com.machankura.bennu.compose.database.model.intermdiate.LocalChatRoom +import com.machankura.bennu.compose.managers.GroupWalletDerivation +import com.machankura.bennu.compose.managers.GuardianManager +import com.machankura.bennu.compose.nostr.wallet.GroupWallet +import com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent +import com.machankura.bennu.compose.repository.ChatRepository +import com.machankura.bennu.compose.repository.FrostSigningRepository +import com.machankura.bennu.compose.ui.composable.navigation.routes.GroupSendRoute +import com.machankura.bennu.compose.ui.composable.navigation.routes.Route +import com.machankura.bennu.compose.ui.composable.widgets.ProvideSnackbarHost +import com.machankura.bennu.compose.ui.theme.BennuTheme +import com.machankura.bennu.compose.ui.view.state.GroupWalletUIState +import com.machankura.bennu.compose.ui.view.state.GuardedWalletRow +import com.machankura.bennu.compose.wallet.GroupWalletState +import com.vitorpamplona.quartz.nip01Core.core.Event +import fr.acinq.bitcoin.ByteVector +import fr.acinq.bitcoin.ByteVector32 +import fr.acinq.bitcoin.Chain +import fr.acinq.bitcoin.OutPoint +import fr.acinq.bitcoin.PrivateKey +import fr.acinq.bitcoin.Satoshi +import fr.acinq.bitcoin.Transaction +import fr.acinq.bitcoin.TxId +import fr.acinq.bitcoin.TxIn +import fr.acinq.bitcoin.TxOut +import fr.acinq.lightning.blockchain.electrum.WalletState +import fr.acinq.secp256k1.Hex +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.time.Instant + +/** + * A descendant's guarded wallet on an ancestor's wallet screen: a card whose + * actions sit under the name rather than squeezing it, each one telling a + * screen reader which wallet it acts on. `docs/guarded-wallet-funding.md`, Phase 5. + */ +@OptIn(ExperimentalTestApi::class) +class GuardedWalletCardJvmTest { + private val chatRoomId = "d4c3b2a1".repeat(8) + private val descendantRoomId = "c".repeat(64) + + private fun account(seed: String) = GroupWalletDerivation.account( + PrivateKey(ByteVector32(Hex.decode(seed.padStart(64, '0')))).publicKey().toString(), Chain.Regtest, + ) + + private fun holding(account: GroupWalletDerivation.Account, sats: Long, height: Long, tip: Int): GroupWalletState { + val derived = GroupWalletDerivation.derive(account, GroupWalletDerivation.RECEIVE, 0) + val next = GroupWalletDerivation.derive(account, GroupWalletDerivation.RECEIVE, 1) + val tx = Transaction(2, listOf(TxIn(OutPoint(TxId(ByteVector32(ByteArray(32) { 3 })), 0), ByteVector.empty, 0xffffffffL)), listOf(TxOut(Satoshi(sats), derived.pubkeyScript)), 0) + val meta = WalletState.AddressMeta.Derived(0) + return GroupWalletState( + account, + WalletState(mapOf( + derived.address(Chain.Regtest) to WalletState.AddressState(meta, true, listOf(WalletState.Utxo(tx.txid, 0, height, tx, meta))), + next.address(Chain.Regtest) to WalletState.AddressState(WalletState.AddressMeta.Derived(1), false, emptyList()), + )), + WalletState.empty, + tipHeight = tip, + ) + } + + private fun loaded(name: String): GroupWalletUIState.Loaded { + val own = account("1c0ffee") + val descendant = account("decaf") + val guarded = GuardianManager.GuardedWallet( + ancestorChatRoomId = chatRoomId, + descendantChatRoomId = descendantRoomId, + statement = Event("1".repeat(64), descendantRoomId, 1_700_000_000, GroupWalletStatementEvent.KIND, emptyArray(), "", ""), + account = descendant, + certificate = Event("2".repeat(64), chatRoomId, 1_700_000_000, 0, arrayOf(arrayOf("name", name)), "", ""), + lineage = listOf(descendantRoomId), + ) + val wallet = GroupWallet( + GroupSignedEvent( + id = "e".repeat(64), chatRoomId = chatRoomId, publicKey = chatRoomId, kind = GroupWalletStatementEvent.KIND, + tags = GroupWalletStatementEvent.template(own).tags, content = own.xpub, signature = "f".repeat(128), + createdAt = Instant.fromEpochSeconds(1_700_000_000), + ), + own, + ) + val room = LocalChatRoom(ChatRoom(id = chatRoomId, userPublicKey = "a".repeat(64), subject = "Treasury", description = null, initialGiftWrapPayloadId = "p", mlsGroupState = "state")) + return GroupWalletUIState.Loaded( + localChatRoom = room, + wallet = wallet, + walletState = holding(own, 100_000, height = 100, tip = 200), + canWatch = true, + canSign = true, + guardedWallets = listOf(GuardedWalletRow(guarded, holding(descendant, 50_000, height = 0, tip = 200))), + ) + } + + private fun render(state: GroupWalletUIState, fontScale: Float = 1f, width: Int = 400, onRoute: (Route) -> Unit = {}, assertions: ComposeUiTest.() -> Unit) = + runDesktopComposeUiTest(width = width, height = 2400) { + setContent { + BennuTheme { + ProvideSnackbarHost { + CompositionLocalProvider(LocalDensity provides Density(LocalDensity.current.density, fontScale)) { + Box(modifier = Modifier.fillMaxSize()) { + GroupWalletScreen( + activeUserPublicKey = "a".repeat(64), + chatRoomId = chatRoomId, + chain = Chain.Regtest, + initialGroupWalletUIState = state, + chatRepository = ChatRepository.NO_OP_CHAT_REPOSITORY, + frostSigningRepository = FrostSigningRepository.NO_OP_FROST_SIGNING_REPOSITORY, + wallets = null, + onNavigateToRoute = onRoute, + onNavigateBack = {}, + ) + } + } + } + } + } + assertions() + } + + @Test + fun `each guarded wallet offers fund beside spend, each naming its wallet`() = render(loaded("Savings")) { + onNodeWithText("Savings").assertIsDisplayed() + onNodeWithText("0 sats within this group's reach · 50,000 sats arriving").assertIsDisplayed() + onNodeWithContentDescription("Fund Savings's guarded wallet").assertIsDisplayed() + onNodeWithContentDescription("Spend from Savings's guarded wallet").assertIsDisplayed() + } + + @Test + fun `fund opens the send form paying the descendant, from the room's own wallet`() { + var opened: Route? = null + render(loaded("Savings"), onRoute = { opened = it }) { + onNodeWithContentDescription("Fund Savings's guarded wallet").performClick() + } + assertEquals( + GroupSendRoute(activeUserPublicKey = "a".repeat(64), chatRoomId = chatRoomId, relayHint = null, fundChatRoomId = descendantRoomId), + opened, + ) + } + + @Test + fun `a device that cannot sign is offered neither`() = render(loaded("Savings").copy(canSign = false)) { + onNodeWithText("Savings").assertIsDisplayed() + onNodeWithText("Fund").assertDoesNotExist() + onNodeWithText("Spend").assertDoesNotExist() + } + + @Test + fun `at twice the text size the buttons stay whole`() = render(loaded("Savings"), fontScale = 2f) { + onNodeWithContentDescription("Fund Savings's guarded wallet").assertIsDisplayed() + onNodeWithContentDescription("Spend from Savings's guarded wallet").assertIsDisplayed() + } +}