android: profile screen — nickname + Monero receiving address #148
5 changed files with 736 additions and 0 deletions
|
|
@ -25,6 +25,7 @@ import androidx.navigation.compose.rememberNavController
|
|||
import androidx.navigation.navArgument
|
||||
import com.siriusdevops.onionwire.ui.ChatScreen
|
||||
import com.siriusdevops.onionwire.ui.PasteInviteScreen
|
||||
import com.siriusdevops.onionwire.ui.ProfileScreen
|
||||
import com.siriusdevops.onionwire.ui.RosterScreen
|
||||
import com.siriusdevops.onionwire.ui.RotateScreen
|
||||
import com.siriusdevops.onionwire.ui.ShareInviteScreen
|
||||
|
|
@ -105,6 +106,7 @@ private fun ReadyGraph(vm: WireViewModel, ready: Phase.Ready, keepAliveNote: Str
|
|||
onPaste = { nav.navigate("paste") },
|
||||
onRotate = { nav.navigate("rotate") },
|
||||
onWipe = { scope.launch { vm.wipeMessages() } },
|
||||
onOpenProfile = { nav.navigate("profile") },
|
||||
onLock = vm::lock,
|
||||
onOpenWallet = { nav.navigate("wallet") },
|
||||
)
|
||||
|
|
@ -145,6 +147,10 @@ private fun ReadyGraph(vm: WireViewModel, ready: Phase.Ready, keepAliveNote: Str
|
|||
WalletManagerScreen(vm = vm, onBack = { nav.popBackStack() })
|
||||
}
|
||||
|
||||
composable("profile") {
|
||||
ProfileScreen(vm = vm, onBack = { nav.popBackStack() })
|
||||
}
|
||||
|
||||
composable(
|
||||
route = "chat/{pk}",
|
||||
arguments = listOf(navArgument("pk") { type = NavType.StringType }),
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ import uniffi.onionwire_sdk.FriendInfo
|
|||
import uniffi.onionwire_sdk.InviteInfo
|
||||
import uniffi.onionwire_sdk.OutboxRow
|
||||
import uniffi.onionwire_sdk.RotateOutcome
|
||||
import uniffi.onionwire_sdk.SelfProfile
|
||||
import uniffi.onionwire_sdk.SendState
|
||||
import uniffi.onionwire_sdk.TransferRow
|
||||
import uniffi.onionwire_sdk.WalletChildStatus
|
||||
|
|
@ -572,6 +573,33 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
|||
|
||||
suspend fun decode(invite: String): InviteInfo = decodeInvite(invite)
|
||||
|
||||
// --- This identity's own profile ---------------------------------------
|
||||
// The phone's half of the TUI's `/profile`. `setSelfProfile` only writes
|
||||
// locally; friends keep showing the old profile until `pushSelfProfile`
|
||||
// reaches each of them, which is why the screen reports the two separately.
|
||||
|
||||
/** The stored profile (nickname, bio, address). A store read; never dials. */
|
||||
suspend fun selfProfile(): Result<SelfProfile> =
|
||||
runCatching { requireWire().selfProfile() }
|
||||
|
||||
/**
|
||||
* Replace the stored profile. Validated in the core before it is written:
|
||||
* a rejected edit leaves the previous profile untouched.
|
||||
*/
|
||||
suspend fun setSelfProfile(
|
||||
displayName: String,
|
||||
bio: String,
|
||||
xmrAddr: String,
|
||||
): Result<Unit> = runCatching { requireWire().setSelfProfile(displayName, bio, xmrAddr) }
|
||||
|
||||
/**
|
||||
* Send the signed profile to one friend so their view of this identity is
|
||||
* current. One-shot and fail-closed: an unreachable peer is a failure for
|
||||
* that peer, and nothing is queued for them.
|
||||
*/
|
||||
suspend fun pushSelfProfile(pubkeyHex: String): Result<Unit> =
|
||||
runCatching { requireWire().pushSelfProfile(pubkeyHex) }
|
||||
|
||||
// --- Wallet & payments -------------------------------------------------
|
||||
// These mirror the SDK surface added for the wallet screen. Payment sends
|
||||
// are one-shot: if the peer's onion is unreachable, `send_*` fails now.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,448 @@
|
|||
package com.siriusdevops.onionwire.ui
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.WindowInsets
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import com.siriusdevops.onionwire.WireViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import uniffi.onionwire_sdk.checkAddress
|
||||
|
||||
/**
|
||||
* This identity's own profile — nickname, bio, Monero receiving address — the
|
||||
* phone's half of the TUI's `/profile`.
|
||||
*
|
||||
* The screen is a thin shell over three SDK calls (`self_profile`,
|
||||
* `set_self_profile`, `push_self_profile`) and every decision it makes is a
|
||||
* pure function at the bottom of this file, covered by `ProfileDraftTest` on
|
||||
* the JVM. Nothing here invents policy the core does not have:
|
||||
*
|
||||
* - The write goes through the core's own validation (nickname ≤ 64 bytes,
|
||||
* bio ≤ 512, no newlines, a non-empty address must pass `check_address`).
|
||||
* The screen pre-checks the address so a typo is refused *before* the write,
|
||||
* with the typed text still in the field, instead of being reported as a
|
||||
* store error after it.
|
||||
* - An **empty** address is a real, allowed value: it clears the profile
|
||||
* address. It is labelled with its consequence, because with no profile
|
||||
* address and no running wallet child the core has nothing to hand a payer
|
||||
* (`Node::wallet_receive_address`) and the invoice path fails.
|
||||
* - The bio is carried, not owned: a save that did not touch the bio field
|
||||
* writes back the value read from the store immediately before the write, so
|
||||
* a bio set in the TUI cannot be blanked from the phone.
|
||||
* - The push is per friend and one-shot, exactly like the TUI's `save_profile`
|
||||
* (which pushes to the selected friend): a peer whose onion is down fails
|
||||
* and nothing is queued. A push failure is *not* a save failure — the local
|
||||
* write already happened — so the two are reported separately.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ProfileScreen(
|
||||
vm: WireViewModel,
|
||||
onBack: () -> Unit,
|
||||
) {
|
||||
val friends by vm.friends.collectAsStateWithLifecycle()
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
// What the store held when this screen opened, and the live edits. Both
|
||||
// null until the first read lands; the save button is dead until then.
|
||||
var loaded by remember { mutableStateOf<ProfileDraft?>(null) }
|
||||
var draft by remember { mutableStateOf<ProfileDraft?>(null) }
|
||||
var loadError by remember { mutableStateOf<String?>(null) }
|
||||
var addressError by remember { mutableStateOf<String?>(null) }
|
||||
var saveError by remember { mutableStateOf<String?>(null) }
|
||||
var status by remember { mutableStateOf<String?>(null) }
|
||||
var busy by remember { mutableStateOf(false) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
vm.selfProfile().fold(
|
||||
onSuccess = { p ->
|
||||
val d = ProfileDraft(p.displayName, p.bio, p.xmrAddr)
|
||||
loaded = d
|
||||
draft = d
|
||||
},
|
||||
onFailure = { loadError = it.message ?: it.toString() },
|
||||
)
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
contentWindowInsets = WindowInsets(0, 0, 0, 0),
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = { Text("Profile") },
|
||||
navigationIcon = {
|
||||
TextButton(onClick = onBack) { Text("Back") }
|
||||
},
|
||||
colors = TopAppBarDefaults.topAppBarColors(
|
||||
containerColor = MaterialTheme.colorScheme.background,
|
||||
),
|
||||
)
|
||||
},
|
||||
) { pad ->
|
||||
Column(
|
||||
Modifier
|
||||
.padding(pad)
|
||||
.fillMaxSize()
|
||||
.screenContentInsets()
|
||||
.verticalScroll(rememberScrollState()),
|
||||
) {
|
||||
val form = draft
|
||||
if (form == null) {
|
||||
Text(
|
||||
loadError ?: "loading…",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = if (loadError != null) {
|
||||
MaterialTheme.colorScheme.error
|
||||
} else {
|
||||
MaterialTheme.colorScheme.onSurfaceVariant
|
||||
},
|
||||
modifier = Modifier
|
||||
.padding(12.dp)
|
||||
.testTag("profile-load-error"),
|
||||
)
|
||||
return@Column
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 12.dp, vertical = 8.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceVariant,
|
||||
),
|
||||
) {
|
||||
Column(Modifier.padding(12.dp)) {
|
||||
Text(
|
||||
"Friends see this nickname and pay to this address.",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
Spacer(Modifier.height(4.dp))
|
||||
Text(
|
||||
"Nothing is published anywhere: each friend gets it only when " +
|
||||
"this is saved and sent to them.",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
OutlinedTextField(
|
||||
value = form.nickname,
|
||||
onValueChange = { draft = form.copy(nickname = it) },
|
||||
singleLine = true,
|
||||
label = { Text("nickname") },
|
||||
supportingText = {
|
||||
Text(
|
||||
"What friends call you. Empty is allowed — they fall back to " +
|
||||
"your fingerprint.",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
)
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 12.dp)
|
||||
.testTag("profile-nickname"),
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = form.bio,
|
||||
onValueChange = { draft = form.copy(bio = it) },
|
||||
singleLine = true,
|
||||
label = { Text("bio") },
|
||||
supportingText = {
|
||||
Text(
|
||||
"Kept when you save without touching it, including one set in " +
|
||||
"the TUI.",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
)
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 12.dp)
|
||||
.testTag("profile-bio"),
|
||||
)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
|
||||
OutlinedTextField(
|
||||
value = form.address,
|
||||
onValueChange = {
|
||||
draft = form.copy(address = it)
|
||||
addressError = null
|
||||
},
|
||||
singleLine = true,
|
||||
isError = addressError != null,
|
||||
label = { Text("Monero address") },
|
||||
supportingText = {
|
||||
Text(
|
||||
// The consequence, on the screen, before a payment fails:
|
||||
// `Node::wallet_receive_address` uses the wallet child's
|
||||
// subaddress while it is up and this address otherwise,
|
||||
// and with neither there is nothing to be paid to.
|
||||
profileAddressNotice(form.address),
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
)
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 12.dp)
|
||||
.testTag("profile-address"),
|
||||
)
|
||||
addressError?.let {
|
||||
Text(
|
||||
it,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 12.dp, vertical = 4.dp)
|
||||
.testTag("profile-address-error"),
|
||||
)
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(12.dp),
|
||||
horizontalArrangement = Arrangement.End,
|
||||
) {
|
||||
Button(
|
||||
onClick = {
|
||||
addressError = null
|
||||
saveError = null
|
||||
status = null
|
||||
val clean = form.normalized()
|
||||
// The address check first: a typo must be refused before
|
||||
// the write, and the field keeps exactly what was typed.
|
||||
val problem = profileAddressProblem(clean.address) { addr ->
|
||||
runCatching { checkAddress(addr) }
|
||||
}
|
||||
if (problem != null) {
|
||||
addressError = problem
|
||||
return@Button
|
||||
}
|
||||
scope.launch {
|
||||
busy = true
|
||||
try {
|
||||
// Read the stored profile again for the bio
|
||||
// alone: a bio edited elsewhere since this
|
||||
// screen loaded must not be blanked by a save
|
||||
// that never touched the field.
|
||||
val storedBio = vm.selfProfile().getOrNull()?.bio
|
||||
val bio = bioToWrite(
|
||||
draftBio = clean.bio,
|
||||
loadedBio = loaded?.bio.orEmpty(),
|
||||
storedBio = storedBio ?: clean.bio,
|
||||
)
|
||||
vm.setSelfProfile(clean.nickname, bio, clean.address).fold(
|
||||
onSuccess = {
|
||||
// Every friend in the roster, like the
|
||||
// TUI pushes on save. One-shot each: a
|
||||
// peer that is down is a failure for
|
||||
// that peer, never a save failure.
|
||||
val outcomes = friends.map { f ->
|
||||
ProfilePushOutcome(
|
||||
label = friendLabel(f.petname, f.fingerprint),
|
||||
error = vm.pushSelfProfile(f.pubkeyHex)
|
||||
.exceptionOrNull()
|
||||
?.let { it.message ?: it.toString() },
|
||||
)
|
||||
}
|
||||
status = profileStatusLine(outcomes)
|
||||
// Re-read so the form matches the store
|
||||
// and the save button goes back to dead.
|
||||
vm.selfProfile().onSuccess { p ->
|
||||
val d = ProfileDraft(p.displayName, p.bio, p.xmrAddr)
|
||||
loaded = d
|
||||
draft = d
|
||||
}
|
||||
},
|
||||
onFailure = { saveError = it.message ?: it.toString() },
|
||||
)
|
||||
} finally {
|
||||
busy = false
|
||||
}
|
||||
}
|
||||
},
|
||||
enabled = !busy && profileSaveEnabled(loaded, form),
|
||||
modifier = Modifier.testTag("profile-save"),
|
||||
) {
|
||||
Text(if (busy) "Saving…" else "Save")
|
||||
}
|
||||
}
|
||||
|
||||
status?.let {
|
||||
Text(
|
||||
it,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 12.dp, vertical = 4.dp)
|
||||
.testTag("profile-status"),
|
||||
)
|
||||
}
|
||||
saveError?.let {
|
||||
Text(
|
||||
it,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 12.dp, vertical = 4.dp)
|
||||
.testTag("profile-save-error"),
|
||||
)
|
||||
}
|
||||
Spacer(Modifier.height(12.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- the whole decision surface, pure ---------------------------------------
|
||||
|
||||
/**
|
||||
* The three fields this screen edits, as they sit on screen.
|
||||
*
|
||||
* A local mirror of the SDK's `SelfProfile`, deliberately: the rules below are
|
||||
* JVM-testable without UniFFI, and a `data class` gives the dirty check an
|
||||
* equality that is the fields themselves rather than an identity.
|
||||
*/
|
||||
internal data class ProfileDraft(
|
||||
val nickname: String = "",
|
||||
val bio: String = "",
|
||||
val address: String = "",
|
||||
)
|
||||
|
||||
/** One friend's push outcome: the label the roster shows, and the failure if any. */
|
||||
internal data class ProfilePushOutcome(
|
||||
val label: String,
|
||||
val error: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* Edge whitespace out of every field.
|
||||
*
|
||||
* A paste from a clipboard or a soft keyboard's autocorrect habitually carries
|
||||
* leading/trailing spaces, and none of the three fields can mean anything with
|
||||
* them: the core refuses a nickname or address carrying a newline, and
|
||||
* `check_address` is a base58 decode that fails on a stray space. Interior
|
||||
* whitespace is untouched — the user may well want it in a bio.
|
||||
*/
|
||||
internal fun ProfileDraft.normalized(): ProfileDraft = copy(
|
||||
nickname = nickname.trim(),
|
||||
bio = bio.trim(),
|
||||
address = address.trim(),
|
||||
)
|
||||
|
||||
/**
|
||||
* The save button's rule.
|
||||
*
|
||||
* Dead until the first read lands (`loaded == null`), dead while the form is
|
||||
* byte-identical to what was loaded, live as soon as it differs. Comparison is
|
||||
* on the normalised forms, so typing a trailing space into a field does not
|
||||
* offer a save that would write the same value back.
|
||||
*
|
||||
* An **empty nickname is allowed**: the core accepts it (it only bounds the
|
||||
* length and forbids newlines), the TUI's `/profile` accepts it, and the roster
|
||||
* already renders a friend with no nickname as their fingerprint. Clearing a
|
||||
* nickname is therefore a real edit that must be saveable.
|
||||
*/
|
||||
internal fun profileSaveEnabled(loaded: ProfileDraft?, draft: ProfileDraft): Boolean =
|
||||
loaded != null && draft.normalized() != loaded.normalized()
|
||||
|
||||
/**
|
||||
* Why the typed address cannot be saved, or `null` when it can.
|
||||
*
|
||||
* Empty is *allowed* and returns `null` without calling [check] at all — it
|
||||
* clears the profile address, which the core permits. A non-empty address is
|
||||
* handed to [check] (`uniffi.onionwire_sdk.checkAddress` in production) and its
|
||||
* message is the reason shown, so the words match the core's own.
|
||||
*/
|
||||
internal fun profileAddressProblem(address: String, check: (String) -> Result<Unit>): String? {
|
||||
val trimmed = address.trim()
|
||||
if (trimmed.isEmpty()) return null
|
||||
return check(trimmed).exceptionOrNull()?.let { it.message ?: it.toString() }
|
||||
}
|
||||
|
||||
/**
|
||||
* The bio that actually gets written.
|
||||
*
|
||||
* The field wins only when it differs from what was loaded — i.e. the user
|
||||
* edited it. Otherwise the value read from the store immediately before the
|
||||
* write is passed through unchanged, which is what keeps a bio set in the TUI
|
||||
* alive through a phone save that only changed the nickname.
|
||||
*/
|
||||
internal fun bioToWrite(draftBio: String, loadedBio: String, storedBio: String): String =
|
||||
if (draftBio.trim() != loadedBio.trim()) draftBio.trim() else storedBio
|
||||
|
||||
/** The label the roster would show for a friend: petname, else the short id. */
|
||||
internal fun friendLabel(petname: String?, fingerprint: String): String =
|
||||
petname?.takeIf { it.isNotBlank() } ?: shortId(fingerprint)
|
||||
|
||||
/**
|
||||
* The one line under the Save button, in the TUI's shape.
|
||||
*
|
||||
* `src/tui.rs` `save_profile` reports `profile saved · sent` on a clean push
|
||||
* and `profile saved` with no friends; here the count and the per-friend
|
||||
* failures are spelled out, because the phone pushes to **every** friend and
|
||||
* "some of them failed" has to name which.
|
||||
*/
|
||||
internal fun profileStatusLine(outcomes: List<ProfilePushOutcome>): String {
|
||||
if (outcomes.isEmpty()) return PROFILE_SAVED
|
||||
val failed = outcomes.filter { it.error != null }
|
||||
if (failed.isEmpty()) {
|
||||
return "$PROFILE_SAVED · sent to ${friendCount(outcomes.size)}"
|
||||
}
|
||||
val sent = outcomes.size - failed.size
|
||||
val detail = failed.joinToString("; ") { "${it.label}: ${it.error}" }
|
||||
return "$PROFILE_SAVED · sent to $sent of ${friendCount(outcomes.size)} · failed: $detail"
|
||||
}
|
||||
|
||||
private fun friendCount(n: Int): String = if (n == 1) "1 friend" else "$n friends"
|
||||
|
||||
/** The TUI's word for a local write that reached nobody. */
|
||||
internal const val PROFILE_SAVED: String = "profile saved"
|
||||
|
||||
/**
|
||||
* What an empty address means, stated where the address is typed.
|
||||
*
|
||||
* `Node::wallet_receive_address` answers with the wallet child's own subaddress
|
||||
* while it is up, and this address otherwise; with neither, the invoice path
|
||||
* fails outright. The user should read that here, not after a friend's payment
|
||||
* bounces.
|
||||
*/
|
||||
internal fun profileAddressNotice(address: String): String =
|
||||
if (address.trim().isEmpty()) {
|
||||
"Empty is allowed, and it clears the profile address. Your receive address " +
|
||||
"then comes from the wallet child while it is running — with no address " +
|
||||
"here and no wallet running, nobody can send you an invoice."
|
||||
} else {
|
||||
"What friends pay to. Validated before it is saved; an invalid address is " +
|
||||
"refused and nothing is written."
|
||||
}
|
||||
|
|
@ -49,6 +49,7 @@ fun RosterScreen(
|
|||
onPaste: () -> Unit,
|
||||
onRotate: () -> Unit,
|
||||
onWipe: () -> Unit,
|
||||
onOpenProfile: () -> Unit,
|
||||
onLock: () -> Unit,
|
||||
onOpenWallet: () -> Unit,
|
||||
) {
|
||||
|
|
@ -68,6 +69,7 @@ fun RosterScreen(
|
|||
TopAppBar(
|
||||
title = { Text("OnionWire") },
|
||||
actions = {
|
||||
TextButton(onClick = onOpenProfile) { Text("Profile") }
|
||||
TextButton(onClick = onLock) { Text("Lock") }
|
||||
},
|
||||
colors = TopAppBarDefaults.topAppBarColors(
|
||||
|
|
|
|||
|
|
@ -0,0 +1,252 @@
|
|||
package com.siriusdevops.onionwire.ui
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* The profile screen's rules, on the JVM.
|
||||
*
|
||||
* These are the decisions `ProfileScreen` renders from: whether Save is live,
|
||||
* whether a typed address can be written at all, which bio a save actually
|
||||
* persists, and what the status line says after pushing to the roster. Pure
|
||||
* logic — no Robolectric, no Compose test dependency, no device — same shape as
|
||||
* `SeedBackupGateTest` and `RosterIdTest`.
|
||||
*
|
||||
* The address check is injected (`profileAddressProblem` takes the predicate),
|
||||
* so the contract under test is the screen's, not UniFFI's: `check_address`'s
|
||||
* own behaviour is covered by the SDK's Rust tests.
|
||||
*/
|
||||
class ProfileDraftTest {
|
||||
|
||||
/** A loaded profile, i.e. what the store held when the screen opened. */
|
||||
private val loaded = ProfileDraft(
|
||||
nickname = "lance",
|
||||
bio = "gardener, mostly drip irrigation",
|
||||
address = "44AFFq5kSiGBoZ4NMDwYtN18obc8AemS33DBLWs3H7otXft3XjrpDtQGv7SqSsaBYBb98uNbr2VBBEt7f2wfn3RVGQBEP3A",
|
||||
)
|
||||
|
||||
// --- the save button's rule ---------------------------------------------
|
||||
|
||||
@Test
|
||||
fun save_is_dead_until_the_first_read_lands() {
|
||||
// `loaded == null` is "the screen has not read the store yet", not "the
|
||||
// form is unchanged" — writing before the read would fight the store.
|
||||
assertFalse(profileSaveEnabled(null, loaded))
|
||||
assertFalse(profileSaveEnabled(null, ProfileDraft()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun save_is_dead_while_the_form_is_unchanged() {
|
||||
assertFalse(profileSaveEnabled(loaded, loaded.copy()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun save_is_live_as_soon_as_any_field_differs() {
|
||||
assertTrue(profileSaveEnabled(loaded, loaded.copy(nickname = "lance2")))
|
||||
assertTrue(profileSaveEnabled(loaded, loaded.copy(bio = "something else")))
|
||||
assertTrue(profileSaveEnabled(loaded, loaded.copy(address = "")))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun an_empty_nickname_is_a_real_edit_and_is_allowed() {
|
||||
// Picked deliberately: the core accepts an empty `display_name` (it only
|
||||
// bounds the length and forbids newlines), the TUI's `/profile` accepts
|
||||
// it, and the roster already falls back to the fingerprint. So clearing
|
||||
// a nickname must be saveable rather than hidden behind a disabled
|
||||
// button the user cannot explain.
|
||||
val cleared = loaded.copy(nickname = "")
|
||||
assertTrue(profileSaveEnabled(loaded, cleared))
|
||||
assertEquals("", cleared.normalized().nickname)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whitespace_around_a_field_is_not_a_change() {
|
||||
// A trailing space from a paste or an autocorrect habit must not offer a
|
||||
// save that would write the same value back.
|
||||
val padded = loaded.copy(nickname = " lance ", address = "${loaded.address}\n")
|
||||
assertFalse(profileSaveEnabled(loaded, padded))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun normalising_strips_only_the_edges() {
|
||||
// Interior whitespace is the user's: a bio may well want it.
|
||||
val n = ProfileDraft(" a b ", " x y ", " addr ").normalized()
|
||||
assertEquals("a b", n.nickname)
|
||||
assertEquals("x y", n.bio)
|
||||
assertEquals("addr", n.address)
|
||||
}
|
||||
|
||||
// --- the address check ---------------------------------------------------
|
||||
|
||||
/** Stands in for `uniffi.onionwire_sdk.checkAddress`: anything but "" ok. */
|
||||
private val accepts: (String) -> Result<Unit> = { Result.success(Unit) }
|
||||
|
||||
/** The core's own words, copied from `pay::check_address`. */
|
||||
private val rejects: (String) -> Result<Unit> =
|
||||
{ Result.failure(IllegalStateException("invalid Monero address")) }
|
||||
|
||||
@Test
|
||||
fun an_empty_address_is_allowed_and_is_never_checked() {
|
||||
var calls = 0
|
||||
val counted: (String) -> Result<Unit> = { calls++; Result.success(Unit) }
|
||||
// Empty clears the profile address, which the core permits — and
|
||||
// `check_address("")` would fail, so the predicate must not even run.
|
||||
assertNull(profileAddressProblem("", counted))
|
||||
assertNull(profileAddressProblem(" ", counted))
|
||||
assertEquals(0, calls)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun a_valid_address_passes() {
|
||||
assertNull(profileAddressProblem(loaded.address, accepts))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun an_invalid_address_is_refused_with_the_cores_own_words() {
|
||||
assertEquals(
|
||||
"invalid Monero address",
|
||||
profileAddressProblem("not-an-address", rejects),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun the_check_sees_the_value_that_would_be_written() {
|
||||
// The screen saves `normalized()`, so the check must see the trimmed
|
||||
// address, not the raw field — otherwise a paste with a trailing space
|
||||
// passes the pre-check and then fails inside the core.
|
||||
var seen: String? = null
|
||||
val spy: (String) -> Result<Unit> = { seen = it; Result.success(Unit) }
|
||||
assertNull(profileAddressProblem(" ${loaded.address} ", spy))
|
||||
assertEquals(loaded.address, seen)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun a_failure_without_a_message_still_reports_something() {
|
||||
val blank: (String) -> Result<Unit> =
|
||||
{ Result.failure(RuntimeException()) }
|
||||
val problem = profileAddressProblem("x", blank)
|
||||
assertTrue(problem != null && problem.isNotEmpty())
|
||||
}
|
||||
|
||||
// --- the bio is carried, not owned --------------------------------------
|
||||
|
||||
@Test
|
||||
fun an_untouched_bio_writes_back_what_the_store_holds_now() {
|
||||
// The TUI edited the bio since this screen loaded. The phone save did
|
||||
// not touch the field, so the store's value must win — blanking it here
|
||||
// is exactly the regression this rule exists to prevent.
|
||||
assertEquals(
|
||||
"set in the TUI after this screen opened",
|
||||
bioToWrite(
|
||||
draftBio = loaded.bio,
|
||||
loadedBio = loaded.bio,
|
||||
storedBio = "set in the TUI after this screen opened",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun an_edited_bio_writes_what_the_user_typed() {
|
||||
assertEquals(
|
||||
"typed on the phone",
|
||||
bioToWrite(
|
||||
draftBio = "typed on the phone",
|
||||
loadedBio = loaded.bio,
|
||||
storedBio = "set in the TUI after this screen opened",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun padding_a_bio_is_not_an_edit() {
|
||||
// Same normalisation as the dirty check: a trailing space is not a
|
||||
// reason to override a bio someone set elsewhere.
|
||||
assertEquals(
|
||||
loaded.bio,
|
||||
bioToWrite("${loaded.bio} ", loaded.bio, loaded.bio),
|
||||
)
|
||||
}
|
||||
|
||||
// --- labels ---------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
fun a_friend_is_labelled_by_petname_else_by_short_id() {
|
||||
val fp = "654c4a382299b364d1345cf69961e1d529659122fc1550c7c75c537b2a30aa99"
|
||||
assertEquals("Bob", friendLabel("Bob", fp))
|
||||
assertEquals(shortId(fp), friendLabel(null, fp))
|
||||
// A roster row with a blank petname renders the fingerprint too.
|
||||
assertEquals(shortId(fp), friendLabel(" ", fp))
|
||||
}
|
||||
|
||||
// --- the status line ------------------------------------------------------
|
||||
|
||||
@Test
|
||||
fun an_empty_roster_reports_a_plain_save() {
|
||||
// The TUI's word for "written locally, sent to nobody".
|
||||
assertEquals("profile saved", profileStatusLine(emptyList()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun a_clean_push_names_the_count() {
|
||||
val one = profileStatusLine(listOf(ProfilePushOutcome("Bob")))
|
||||
assertEquals("profile saved · sent to 1 friend", one)
|
||||
|
||||
val three = profileStatusLine(
|
||||
listOf(
|
||||
ProfilePushOutcome("Bob"),
|
||||
ProfilePushOutcome("Ann"),
|
||||
ProfilePushOutcome("654c4a…aa99"),
|
||||
),
|
||||
)
|
||||
assertEquals("profile saved · sent to 3 friends", three)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun a_failed_push_still_starts_with_the_local_write() {
|
||||
// The split that matters: the store was written, one peer was not
|
||||
// reached. A status line that reads as a failed save sends the user
|
||||
// back to re-do a write that already happened.
|
||||
val line = profileStatusLine(
|
||||
listOf(
|
||||
ProfilePushOutcome("Bob"),
|
||||
ProfilePushOutcome("Ann", "connect failed"),
|
||||
),
|
||||
)
|
||||
assertTrue(line.startsWith("profile saved"))
|
||||
assertTrue(line.contains("sent to 1 of 2 friends"))
|
||||
assertTrue(line.contains("failed: Ann: connect failed"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun every_failure_is_listed_by_name() {
|
||||
val line = profileStatusLine(
|
||||
listOf(
|
||||
ProfilePushOutcome("Bob", "connect failed"),
|
||||
ProfilePushOutcome("654c4a…aa99", "friend missing prekey"),
|
||||
),
|
||||
)
|
||||
assertTrue(line.contains("sent to 0 of 2 friends"))
|
||||
assertTrue(line.contains("Bob: connect failed"))
|
||||
assertTrue(line.contains("654c4a…aa99: friend missing prekey"))
|
||||
}
|
||||
|
||||
// --- the empty-address consequence, on screen -----------------------------
|
||||
|
||||
@Test
|
||||
fun an_empty_address_says_nobody_can_pay() {
|
||||
val text = profileAddressNotice("")
|
||||
assertTrue(text.contains("clears the profile address"))
|
||||
assertTrue(text.contains("wallet child"))
|
||||
assertTrue(text.contains("nobody can send you an invoice"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun a_filled_address_says_it_is_validated_before_the_write() {
|
||||
val text = profileAddressNotice(loaded.address)
|
||||
assertTrue(text.contains("Validated"))
|
||||
assertTrue(text.contains("refused"))
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue