diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 8482400..3166e86 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -87,7 +87,4 @@ dependencies { implementation(libs.compose.material3) debugImplementation(libs.compose.ui.tooling) - - testImplementation(libs.junit) - testImplementation(libs.kotlinx.coroutines.test) } diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/ui/InviteAddFlow.kt b/android/app/src/main/java/com/siriusdevops/onionwire/ui/InviteAddFlow.kt deleted file mode 100644 index 0f50a48..0000000 --- a/android/app/src/main/java/com/siriusdevops/onionwire/ui/InviteAddFlow.kt +++ /dev/null @@ -1,60 +0,0 @@ -package com.siriusdevops.onionwire.ui - -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.setValue - -/** - * JVM-testable holder for the paste-invite verify/add flow. - * - * Verify and add own independent in-flight flags. They must never share a - * single `busy` boolean — that is what made Approve & add a no-op: the - * verify path reset the flag the add path was waiting on. - */ -class InviteAddFlow( - private val onDecode: suspend (String) -> P, - private val onAdd: suspend (String) -> A, -) { - var preview: P? by mutableStateOf(null) - private set - var added: A? by mutableStateOf(null) - private set - var error: String? by mutableStateOf(null) - private set - var verifying: Boolean by mutableStateOf(false) - private set - var adding: Boolean by mutableStateOf(false) - private set - - fun clear() { - preview = null - error = null - added = null - } - - suspend fun verify(raw: String) { - if (verifying) return - verifying = true - error = null - try { - preview = onDecode(raw.trim()) - } catch (t: Throwable) { - error = t.message ?: t.toString() - } finally { - verifying = false - } - } - - suspend fun add(raw: String) { - if (adding || added != null) return - adding = true - error = null - try { - added = onAdd(raw.trim()) - } catch (t: Throwable) { - error = t.message ?: t.toString() - } finally { - adding = false - } - } -} diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/ui/InviteScreens.kt b/android/app/src/main/java/com/siriusdevops/onionwire/ui/InviteScreens.kt index 31da473..f05eb0f 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/ui/InviteScreens.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/ui/InviteScreens.kt @@ -28,13 +28,11 @@ 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.LocalClipboardManager import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.unit.dp -import kotlinx.coroutines.launch import uniffi.onionwire_sdk.FriendInfo import uniffi.onionwire_sdk.InviteInfo @@ -131,13 +129,10 @@ fun PasteInviteScreen( onBack: () -> Unit, ) { var raw by remember { mutableStateOf("") } - val flow = remember { InviteAddFlow(onDecode, onAdd) } - val scope = rememberCoroutineScope() - val preview = flow.preview - val error = flow.error - val added = flow.added - val verifying = flow.verifying - val adding = flow.adding + var preview by remember { mutableStateOf(null) } + var error by remember { mutableStateOf(null) } + var added by remember { mutableStateOf(null) } + var busy by remember { mutableStateOf(false) } Scaffold( topBar = { @@ -157,7 +152,9 @@ fun PasteInviteScreen( value = raw, onValueChange = { raw = it - flow.clear() + preview = null + error = null + added = null }, label = { Text("onionwire:v1:…") }, minLines = 4, @@ -167,14 +164,25 @@ fun PasteInviteScreen( Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { Button( - enabled = raw.isNotBlank() && !verifying && !adding, - onClick = { scope.launch { flow.verify(raw) } }, + enabled = raw.isNotBlank() && !busy, + onClick = { + busy = true + error = null + }, ) { Text("Verify") } if (added != null) { TextButton(onClick = onBack) { Text("Done") } } } + LaunchedEffect(busy) { + if (!busy) return@LaunchedEffect + runCatching { onDecode(raw.trim()) } + .onSuccess { preview = it } + .onFailure { error = it.message ?: it.toString() } + busy = false + } + Spacer(Modifier.height(16.dp)) error?.let { @@ -215,9 +223,20 @@ fun PasteInviteScreen( } Spacer(Modifier.height(12.dp)) Button( - enabled = !adding && added == null, - onClick = { scope.launch { flow.add(raw) } }, + enabled = !busy && added == null, + onClick = { + busy = true + error = null + }, ) { Text("Approve & add") } + + LaunchedEffect(busy, preview) { + if (!busy || preview == null || added != null) return@LaunchedEffect + runCatching { onAdd(raw.trim()) } + .onSuccess { added = it } + .onFailure { error = it.message ?: it.toString() } + busy = false + } } added?.let { f -> diff --git a/android/app/src/test/java/com/siriusdevops/onionwire/ui/InviteAddFlowTest.kt b/android/app/src/test/java/com/siriusdevops/onionwire/ui/InviteAddFlowTest.kt deleted file mode 100644 index d47be8e..0000000 --- a/android/app/src/test/java/com/siriusdevops/onionwire/ui/InviteAddFlowTest.kt +++ /dev/null @@ -1,131 +0,0 @@ -package com.siriusdevops.onionwire.ui - -import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.async -import kotlinx.coroutines.launch -import kotlinx.coroutines.test.runTest -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotNull -import org.junit.Assert.assertNull -import org.junit.Test - -class InviteAddFlowTest { - - private data class Preview(val id: String) - private data class Added(val id: String) - - @Test - fun add_while_verify_in_flight_completes_add_exactly_once() = runTest { - val decodeEntered = CompletableDeferred() - val decodeGate = CompletableDeferred() - var decodeCount = 0 - var addCount = 0 - val flow = InviteAddFlow( - onDecode = { - decodeCount++ - if (decodeCount == 1) { - decodeEntered.complete(Unit) - decodeGate.await() - } - Preview("p") - }, - onAdd = { - addCount++ - Added("a") - }, - ) - - val verifyJob = launch { flow.verify("invite") } - decodeEntered.await() - flow.add("invite") - decodeGate.complete(Unit) - verifyJob.join() - - assertEquals(1, addCount) - assertEquals(Added("a"), flow.added) - } - - @Test - fun onAdd_is_invoked_exactly_once_per_approve() = runTest { - var addCount = 0 - val flow = InviteAddFlow( - onDecode = { Preview("p") }, - onAdd = { - addCount++ - Added("a") - }, - ) - - flow.verify("invite") - flow.add("invite") - - assertEquals(1, addCount) - assertEquals(Added("a"), flow.added) - } - - @Test - fun failed_decode_sets_error_and_never_calls_onAdd() = runTest { - var addCount = 0 - val flow = InviteAddFlow( - onDecode = { error("bad invite") }, - onAdd = { - addCount++ - Added("a") - }, - ) - - flow.verify("invite") - - assertEquals("bad invite", flow.error) - assertNull(flow.preview) - assertNull(flow.added) - assertEquals(0, addCount) - } - - @Test - fun double_tap_approve_while_in_flight_does_not_add_twice() = runTest { - val addEntered = CompletableDeferred() - val addGate = CompletableDeferred() - var addCount = 0 - val flow = InviteAddFlow( - onDecode = { Preview("p") }, - onAdd = { - addCount++ - addEntered.complete(Unit) - addGate.await() - Added("a") - }, - ) - - flow.verify("invite") - val first = async { flow.add("invite") } - addEntered.await() - val second = async { flow.add("invite") } - addGate.complete(Unit) - first.await() - second.await() - - assertEquals(1, addCount) - assertEquals(Added("a"), flow.added) - } - - @Test - fun after_successful_add_further_taps_are_inert() = runTest { - var addCount = 0 - val flow = InviteAddFlow( - onDecode = { Preview("p") }, - onAdd = { - addCount++ - Added("a") - }, - ) - - flow.verify("invite") - flow.add("invite") - flow.add("invite") - flow.add("invite") - - assertNotNull(flow.added) - assertEquals(1, addCount) - } -} diff --git a/android/gradle/libs.versions.toml b/android/gradle/libs.versions.toml index 551b081..c100f16 100644 --- a/android/gradle/libs.versions.toml +++ b/android/gradle/libs.versions.toml @@ -8,14 +8,11 @@ navigationCompose = "2.9.8" composeBom = "2025.12.01" jna = "5.19.1" coroutines = "1.11.0" -junit = "4.13.2" [libraries] androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" } jna = { module = "net.java.dev.jna:jna", version.ref = "jna" } -junit = { module = "junit:junit", version.ref = "junit" } kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } -kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activityCompose" } androidx-lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version.ref = "lifecycle" } androidx-lifecycle-viewmodel-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "lifecycle" }