From 72cfb561c439cc3e756a63f49f74a2a018bd8375 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Thu, 8 Oct 2026 16:04:13 -0500 Subject: [PATCH] feat(proposals): ask again about everything the filter is showing The bulk control I argued against, built -- and the objection turned out to be solvable rather than fatal, so it is worth recording what happened to it. Half of it was already handled. Two resends in the same second do not collide on the outbound row id: `broadcast` hashes that id over the session's own tags and steps a second forward when it finds a collision rather than overwriting. Different sessions were never at risk. The other half was real. A resend publishes two messages, and `resend` keeps to two deliberately because "publishing in bursts is what the skipped-keys bug feeds on"; three proposals at once is six, in the same second that "two events published in the same second race, and exactly one survives" was written about. So the sessions are spaced rather than the budget quietly raised -- `resendAll` waits `RESEND_SPACING` between them, one second, which is the resolution the protocol works in because `created_at` is stored to the second. The pacing lives in the manager and not in the loop that wanted it, because everything that justifies it does: MLS generations, relay ordering, outbound ids. A caller that launched `resend` per session would put every burst into one second, and the call site is the wrong place to know why that matters. There is a test against the real manager that three sessions take longer than two spacings and claim three different seconds. What it acts on is `visible`, not the room: a bulk control reaching rows the reader cannot see is its one unforgivable move. A cooling-down row is left out of the count rather than included and skipped, because the count is what the button says out loud and offering three while able to send two is a lie told before the tap. Offered from two up: one is what the row's own button is for. Every covered row goes quiet for the whole run rather than for its own turn -- a paced run means a row whose turn has not come is still about to publish, and a row button offering to send it meanwhile would send it twice. A session already in flight on its own is left out of the run for the same reason. It is offered on any filter showing two it can act on, not only "being signed". That filter is where it earns its place -- three rows stalled for one reason -- but the rule is about what is on screen, not about which chip put it there. 1419 jvm tests pass, the Android target compiles, all audit budgets met. Co-Authored-By: Claude Opus 5 --- .../composeResources/values/strings.xml | 3 + .../DatabaseFrostSigningRepository.kt | 18 ++ .../compose/managers/FrostSigningManager.kt | 56 ++++++ .../repository/FrostSigningRepository.kt | 18 ++ .../ui/composable/ProposalListScreen.kt | 67 ++++++++ .../ui/view/model/ProposalListViewModel.kt | 44 +++++ .../ui/view/state/ProposalListUIState.kt | 25 +++ .../ui/view/state/ProposalAskAgainTest.kt | 117 +++++++++++++ .../managers/FrostSigningResendJvmTest.kt | 100 +++++++++++ .../composable/ProposalListScreenJvmTest.kt | 97 +++++++++++ .../model/ProposalListViewModelJvmTest.kt | 161 +++++++++++++++++- 11 files changed, 705 insertions(+), 1 deletion(-) diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index dc3de993..3a86794d 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -238,6 +238,7 @@ Shared key Signed Signing key + Some had already settled, so not every one was asked again. The key this room signs as, and the ceremony it came from. What the group signed This room signs as @@ -348,6 +349,8 @@ Add chapter to %1$s Add people to %1$s + Ask all %1$s again + Asked again about all %1$s proposals Chapter %1$s Chapter %1$s · %2$s words · %3$s characters Chunk %1$s diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt index 2cd42465..b098f205 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseFrostSigningRepository.kt @@ -141,6 +141,24 @@ class DatabaseFrostSigningRepository( false } + override suspend fun resendAll( + localChatRoom: LocalChatRoom, + sessionIds: List + ): Int = try { + FrostSigningManager.resendAll( + database = database, + localChatRoom = localChatRoom, + sessionIds = sessionIds + ) + } catch (e: Throwable) { + // Zero rather than rethrown, as a single resend swallows and for the + // same reason. Whatever went out before the throw is already out; what + // this loses is the count, and a repair that takes the screen down with + // it is worse than one that undercounted. + logger.e("Error asking again about ${sessionIds.size} signing session(s)", e) + 0 + } + override suspend fun membersOwing(session: FrostSigningSession): Set = try { FrostSigningManager.membersOwing(database = database, session = session) } catch (e: Throwable) { diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt index 0427f8c8..5b7b2b2c 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/managers/FrostSigningManager.kt @@ -23,7 +23,9 @@ import kotlin.time.Clock import kotlin.time.Duration import kotlin.time.Instant import kotlin.time.Duration.Companion.minutes +import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.delay import to.curare.compose.database.CurareDatabase import to.curare.compose.database.model.ChatMessage import to.curare.compose.database.model.DkgSession @@ -177,6 +179,24 @@ object FrostSigningManager { */ val RESEND_COOLDOWN: Duration = 5.minutes + /** + * How long [resendAll] waits between one session and the next. + * + * A resend publishes two messages, which is a burst the room is known to + * survive -- `MlsGroupCache` covers a burst decrypted in one sync, and + * [resend] keeps to two deliberately for that reason. Asking again about + * three proposals at once is six, outside the budget somebody set with a bug + * attached to it, so the sessions are spaced instead of the budget being + * quietly raised. + * + * One second because that is the resolution the protocol works in. + * `created_at` is stored to the second, relays offer no ordering, and "two + * events published in the same second race, and exactly one survives" is how + * a proposal was lost once already -- see `MlsGroupCache`. A second between + * sessions puts each one's pair in a second of its own. + */ + val RESEND_SPACING: Duration = 1.seconds + /** * The kinds a member authors for a session, and so the ones [republishOwn] * may repeat. Everything else on the session is the coordinator's. @@ -1940,6 +1960,42 @@ object FrostSigningManager { * have been published against an aggregate already, so the retry is a fresh * proposal and never this one again -- which is what [fail] tells the group. */ + /** + * Asks again about several sessions, one after another, and says how many + * actually published. + * + * One at a time and spaced, which is the whole reason this is here rather + * than a loop at the call site: a caller that launched [resend] per session + * would put every one of their two-message bursts into the same second, and + * the room's tolerance for that is the thing `MlsGroupCache` documents the + * limits of. See [RESEND_SPACING]. + * + * A session that publishes nothing is stepped over rather than stopping the + * run -- a cooldown that has not expired, or one that turns out to be + * waiting on nobody, is a reason to skip it and not to abandon the others. + * The count returned is of the ones that went out, so a caller can say "two + * of three" rather than claiming all of them. + */ + suspend fun resendAll( + database: CurareDatabase, + localChatRoom: LocalChatRoom, + sessionIds: List + ): Int { + var sent = 0 + + sessionIds.forEachIndexed { index, sessionId -> + // Before, not after, so the last session is not followed by a pause + // nobody is waiting through. + if (index > 0) delay(RESEND_SPACING) + + if (resend(database, localChatRoom, sessionId)) sent++ + } + + logger.i("Asked again about $sent of ${sessionIds.size} session(s)") + + return sent + } + suspend fun resend( database: CurareDatabase, localChatRoom: LocalChatRoom, diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt index 5ae62831..e34e7392 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt @@ -166,6 +166,19 @@ interface FrostSigningRepository { */ suspend fun resend(localChatRoom: LocalChatRoom, sessionId: String): Boolean + /** + * Asks again about several sessions, spaced out, and says how many published. + * + * One call rather than a loop over [resend], because the spacing is the + * point: every resend is a two-message burst, and launching several at once + * would put all of them in one second -- which is how a proposal was lost + * once already. See `FrostSigningManager.RESEND_SPACING`. + * + * A session that publishes nothing is stepped over, not a reason to abandon + * the rest, so the count returned may be less than the list given. + */ + suspend fun resendAll(localChatRoom: LocalChatRoom, sessionIds: List): Int + /** * The share holders a session is still waiting on, as the manager counts * them -- who has said nothing while it collects nonces, and which chosen @@ -262,6 +275,11 @@ interface FrostSigningRepository { sessionId: String ): Boolean = false + override suspend fun resendAll( + localChatRoom: LocalChatRoom, + sessionIds: List + ): Int = 0 + override suspend fun membersOwing(session: FrostSigningSession): Set = emptySet() override suspend fun resumeAnswered(localChatRoom: LocalChatRoom) = Unit diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/ProposalListScreen.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/ProposalListScreen.kt index 064a7d26..ad9e3ca4 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/ProposalListScreen.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/ProposalListScreen.kt @@ -65,6 +65,9 @@ import to.curare.compose.ui.theme.spacing import curare.composeapp.generated.resources.Res import org.jetbrains.compose.resources.stringResource import curare.composeapp.generated.resources.abandoned +import curare.composeapp.generated.resources.some_proposals_had_already_settled +import curare.composeapp.generated.resources.asked_again_about_all +import curare.composeapp.generated.resources.ask_all_again import curare.composeapp.generated.resources.everyone_has_signed_this_session import curare.composeapp.generated.resources.asked_again_to_sign import curare.composeapp.generated.resources.asked_again_recently @@ -190,6 +193,23 @@ fun ProposalListScreen( val askedToSign = stringResource(Res.string.asked_again_to_sign) val nobodyLeft = stringResource(Res.string.everyone_has_signed_this_session) + // Parameterised only on the count the composition already + // knows -- how many were asked about -- so both are read here + // with the rest. How many of them went out is not known until + // the run finishes, and the one message that would want it is + // for a case rare enough to say in words: a proposal settles + // between the tap and its turn only if somebody else's + // signature lands in that second. + val askedAbout = state.askableVisible.size + val askedAll = stringResource(Res.string.asked_again_about_all, askedAbout) + val someSettled = stringResource(Res.string.some_proposals_had_already_settled) + + val onAskAll: () -> Unit = { + proposalListViewModel.askAllAgain { sent, of -> + notify(if (sent == of) askedAll else someSettled) + } + } + val askAgain: (ProposalListUIState.Proposal) -> Unit = { proposal -> val signing = proposal.state == ProposalState.BEING_SIGNED @@ -236,6 +256,21 @@ fun ProposalListScreen( ) } + // Under the chips, because what it acts on is what the + // chips chose. From two up: one is what the row's own + // button is for, and a bulk control over a single + // proposal is a second way to do one thing, further from + // the row it affects. + if (state.canAskAll) { + AskAllAgainButton( + count = askedAbout, + asking = state.askableVisible.any { + proposalListViewModel.isAsking(it.session.id) + }, + onAskAll = onAskAll + ) + } + ProposalList( state = state, onOpen = open, @@ -603,6 +638,38 @@ private fun ProposalCard( } } +/** + * Asks again about every proposal the filter has in front of the reader. + * + * The bulk form of the row's own button, and it earns its place on exactly the + * screen the filters made: "being signed" with three rows in it is three + * proposals stalled for the same reason, and asking about them one at a time is + * three taps to say one thing. + * + * It says how many, because that is how many publishes the tap causes. The run + * itself is paced a second apart -- see `FrostSigningManager.RESEND_SPACING` -- + * so what the button starts is not a burst, and the rows it covers go quiet + * together for its duration rather than one at a time. + */ +@Composable +private fun AskAllAgainButton( + count: Int, + asking: Boolean, + onAskAll: () -> Unit, +) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = MaterialTheme.spacing.containerPadding), + ) { + TextButton(enabled = !asking, onClick = onAskAll) { + Icon(Icons.Default.Campaign, contentDescription = Decorative) + Spacer(modifier = Modifier.width(MaterialTheme.spacing.itemGap)) + Text(stringResource(Res.string.ask_all_again, count)) + } + } +} + /** * Asks the members a proposal is still waiting on to take another look. * diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/ProposalListViewModel.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/ProposalListViewModel.kt index b794e633..2776aa12 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/ProposalListViewModel.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/ProposalListViewModel.kt @@ -228,6 +228,50 @@ class ProposalListViewModel( } } + /** + * Asks again about every visible proposal that can be, one after another. + * + * The list of sessions comes from the state rather than from the caller, so + * what goes out is what the filter had in front of the reader when they + * tapped -- a bulk control that acted on rows somebody could not see would + * be the one thing it must never do. + * + * Each session is marked pending for the whole run, not just for its own + * turn. The run is paced -- see `FrostSigningManager.RESEND_SPACING` -- so a + * row whose turn has not come yet is still a row that is about to publish, + * and a button offering to send it separately in the meantime would send it + * twice. + * + * [onResult] is told how many went out and how many were tried. They differ + * when a session stopped being askable between the tap and its turn, which a + * paced run leaves room for: somebody else's signature can land in that + * second and settle it. + */ + fun askAllAgain(onResult: (sent: Int, of: Int) -> Unit) { + val state = proposalListUIState as? ProposalListUIState.Loaded ?: return + + val sessionIds = state.askableVisible + .map { it.session.id } + .filterNot { it in pending } + + if (sessionIds.isEmpty()) return + + pending.addAll(sessionIds) + + viewModelScope.launch(Dispatchers.IO) { + val sent = frostSigningRepository.resendAll( + localChatRoom = state.localChatRoom, + sessionIds = sessionIds + ) + + pending.removeAll(sessionIds.toSet()) + + viewModelScope.launch(Dispatchers.Main) { + onResult.invoke(sent, sessionIds.size) + } + } + } + /** Whether an ask for this session is still in flight. */ fun isAsking(sessionId: String): Boolean = sessionId in pending diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/ProposalListUIState.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/ProposalListUIState.kt index 944ec75c..4f8b0d0c 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/ProposalListUIState.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/state/ProposalListUIState.kt @@ -64,6 +64,31 @@ sealed interface ProposalListUIState { val counts: Map = ProposalState.entries.associateWith { state -> proposals.count { it.state == state } } + + /** + * The rows on screen that one tap could ask again about. + * + * Drawn from [visible] and not from [proposals], because the offer is to + * ask again about *these* -- the ones the filter put in front of the + * reader. Asking about proposals they cannot see would be the bulk + * control's one unforgivable move. + * + * A proposal inside its cooldown is left out rather than included and + * skipped. `resend` would decline it harmlessly either way, but the + * count is what the button says out loud, and a button offering three + * when it can only send two is a button that lies before it is pressed. + */ + val askableVisible: List = + visible.filter { it.canAskAgain && it.askAgainAt == null } + + /** + * Whether to offer asking about all of them at once. + * + * Two, because one is what the row's own button is for: a bulk control + * over a single proposal is a second way to do the same thing, in a + * place further from the row it affects. + */ + val canAskAll: Boolean = askableVisible.size >= 2 } /** diff --git a/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/ProposalAskAgainTest.kt b/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/ProposalAskAgainTest.kt index 64f815c6..bf1c9baf 100644 --- a/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/ProposalAskAgainTest.kt +++ b/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/ProposalAskAgainTest.kt @@ -129,4 +129,121 @@ class ProposalAskAgainTest { ) } } + + // --- asking about all of them at once ------------------------------------------- + + private fun loaded( + proposals: List, + filter: ProposalFilter = ProposalFilter.ALL, + ) = ProposalListUIState.Loaded( + localChatRoom = to.curare.compose.database.model.intermdiate.LocalChatRoom( + chatRoom = to.curare.compose.database.model.ChatRoom( + id = roomId, + userPublicKey = me, + subject = "#Ekklesia", + description = null, + mlsGroupState = null, + ) + ), + proposals = proposals, + filter = filter, + ) + + private fun askable(id: String, owing: Int? = 1, resentAt: kotlin.time.Instant? = null) = + ProposalListUIState.Proposal( + session = session(resentAt = resentAt).copy(id = id), + proposerName = "You", + owing = owing, + ) + + @Test + fun `two askable rows on screen earn the bulk offer`() { + val state = loaded(listOf(askable("one"), askable("two"))) + + assertTrue(state.canAskAll) + assertEquals(listOf("one", "two"), state.askableVisible.map { it.session.id }) + } + + /** One is what the row's own button is for. */ + @Test + fun `a single askable row does not get a second way to ask`() { + assertFalse(loaded(listOf(askable("one"))).canAskAll) + } + + /** + * The offer is over what the filter put on screen. A bulk control that acted + * on proposals the reader could not see would be the one thing it must never + * do, so it is drawn from `visible` and not from the room. + */ + @Test + fun `the bulk offer covers what the filter shows and nothing behind it`() { + val signing = askable("signing").let { + it.copy(session = it.session.copy(stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES)) + } + val alsoSigning = askable("also").let { + it.copy(session = it.session.copy(stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES)) + } + val gathering = askable("gathering").let { + it.copy(session = it.session.copy(stage = FrostSigningStage.COLLECTING_NONCES)) + } + + val filtered = loaded( + listOf(signing, alsoSigning, gathering), + filter = ProposalFilter.BEING_SIGNED, + ) + + assertEquals( + listOf("signing", "also"), + filtered.askableVisible.map { it.session.id }, + "the bulk offer reached past the filter" + ) + assertTrue(filtered.canAskAll) + + // The one left out is askable in its own right; it is simply not on screen. + assertTrue(gathering.canAskAgain) + } + + /** + * A proposal inside its cooldown is left out rather than included and + * skipped. `resend` would decline it either way, but the count is what the + * button says out loud, and a button offering three when it can send two is + * a button that lies before it is pressed. + */ + @Test + fun `a cooling-down row is left out of the count, not skipped inside it`() { + val state = loaded( + listOf( + askable("one"), + askable("two"), + askable("cooling", resentAt = Clock.System.now()), + ) + ) + + assertEquals(listOf("one", "two"), state.askableVisible.map { it.session.id }) + assertTrue(state.canAskAll) + } + + @Test + fun `cooling down to one leaves no bulk offer`() { + val state = loaded( + listOf(askable("one"), askable("cooling", resentAt = Clock.System.now())) + ) + + assertEquals(1, state.askableVisible.size) + assertFalse(state.canAskAll) + } + + @Test + fun `rows that are not ours to send are not counted`() { + val state = loaded(listOf(askable("one"), askable("theirs", owing = null))) + + assertEquals(listOf("one"), state.askableVisible.map { it.session.id }) + assertFalse(state.canAskAll) + } + + @Test + fun `a room with nothing askable offers nothing`() { + assertFalse(loaded(listOf(askable("none", owing = 0))).canAskAll) + assertTrue(loaded(emptyList()).askableVisible.isEmpty()) + } } diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt index 77df0000..8707d59b 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/FrostSigningResendJvmTest.kt @@ -1231,4 +1231,104 @@ class FrostSigningResendJvmTest { assertEquals(before, queued(FrostSigningEvents.NONCE).size) } + + // --- asking about several at once ------------------------------------------------ + + private suspend fun resendAll(vararg sessionIds: String) = + FrostSigningManager.resendAll( + database = db, + localChatRoom = room, + sessionIds = sessionIds.toList() + ) + + @Test + fun `asking about several sessions resends each of them`() = runBlocking { + openDevice() + val one = propose() + val two = propose() + + assertEquals(2, resendAll(one.id, two.id)) + + // Two proposals of their own, plus the resend of each. + assertEquals(4, queued(FrostSigningEvents.PROPOSAL).size) + assertEquals(2, transcript(ChatMessage.TYPE_FROST_RESENT).size) + assertNotNull(session(one.id).resentAt) + assertNotNull(session(two.id).resentAt) + } + + /** + * The spacing, and why it is a second. + * + * A resend publishes two messages, which is a burst the room survives -- + * `MlsGroupCache` covers a burst decrypted in one sync, and `resend` keeps to + * two deliberately for that reason. Several at once would be outside that + * budget, in the same second that "exactly one survives" was written about. + * So the sessions are spaced, and the `created_at` seconds are where that + * shows. + */ + @Test + fun `the sessions are spaced a second apart, not published together`() = runBlocking { + openDevice() + val one = propose() + val two = propose() + val three = propose() + + val before = Clock.System.now() + assertEquals(3, resendAll(one.id, two.id, three.id)) + val elapsed = Clock.System.now() - before + + assertTrue( + elapsed >= FrostSigningManager.RESEND_SPACING * 2, + "three sessions went out in $elapsed, faster than the spacing allows" + ) + + // And the seconds they claim differ, which is what a relay with no + // ordering reads: two events in one second race and one survives. + val seconds = listOf(one, two, three) + .mapNotNull { session(it.id).resentAt?.epochSeconds } + + assertEquals(3, seconds.size) + assertEquals(3, seconds.toSet().size, "two sessions were resent in the same second") + } + + /** + * One session declining is not a reason to abandon the others -- a cooldown + * that has not run out is the ordinary way for that to happen, and the count + * returned is of the ones that actually went out. + */ + @Test + fun `a session inside its cooldown is stepped over, not a reason to stop`() = runBlocking { + openDevice() + val one = propose() + val two = propose() + val three = propose() + + // The middle one has just been asked about on its own. + assertTrue(resend(two.id)) + val alreadyAsked = session(two.id).resentAt + + assertEquals(2, resendAll(one.id, two.id, three.id), "the skipped one was counted") + + assertNotNull(session(one.id).resentAt) + assertNotNull(session(three.id).resentAt) + assertEquals(alreadyAsked, session(two.id).resentAt, "the cooling session was resent") + } + + @Test + fun `a session that is not ours is stepped over too`() = runBlocking { + openDevice(owner = bob) + val theirs = proposalArrivedAt(bob) + + assertEquals(0, resendAll(theirs.id)) + assertNull(session(theirs.id).resentAt) + } + + @Test + fun `asking about nothing publishes nothing`() = runBlocking { + openDevice() + propose() + + assertEquals(0, resendAll()) + assertEquals(1, queued(FrostSigningEvents.PROPOSAL).size) + } } diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/ProposalListScreenJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/ProposalListScreenJvmTest.kt index 73da8771..7bb58e00 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/ProposalListScreenJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/ProposalListScreenJvmTest.kt @@ -509,6 +509,92 @@ class ProposalListScreenJvmTest { onAllNodesWithText("Ask again").assertCountEquals(0) } + // --- asking about all of them at once ------------------------------------------- + + private fun stalled(id: String, detail: String, owing: Int? = 1) = proposal( + id, "New chapter", detail, + stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES, + approved = true, signerIds = "1,2", signatures = 1, owing = owing, + ) + + /** + * The screen the filters made: three rows stalled for one reason, and one + * tap to say one thing about all of them. + */ + @Test + fun `the being signed screen offers to ask about all of them`() { + render(loaded(listOf(stalled("a", "Genesis 4"), stalled("b", "Genesis 5")))) { + chip("Being signed · 2").performClick() + + onNodeWithText("Ask all 2 again").assertIsDisplayed().assertIsEnabled() + } + } + + @Test + fun `tapping it asks about every row the filter shows`() { + render( + loaded( + listOf(stalled("a", "Genesis 4"), stalled("b", "Genesis 5"), open, signed) + ) + ) { + chip("Being signed · 2").performClick() + onNodeWithText("Ask all 2 again").performClick() + + waitUntil(timeoutMillis = 10_000) { resent.size == 2 } + } + + assertEquals(listOf("a", "b"), resent, "the bulk tap reached past the filter") + } + + /** One is what the row's own button is for. */ + @Test + fun `a single stalled proposal gets no bulk button`() { + render(loaded(listOf(stalled("a", "Genesis 4")))) { + chip("Being signed · 1").performClick() + + onNodeWithText("Ask again").assertIsDisplayed() + onAllNodesWithText("Ask all 1 again").assertCountEquals(0) + } + } + + @Test + fun `rows that are not ours to send leave no bulk button`() { + render(loaded(listOf(stalled("a", "Genesis 4", owing = null), stalled("b", "Genesis 5", owing = null)))) { + chip("Being signed · 2").performClick() + + onAllNodesWithText("Ask all 2 again").assertCountEquals(0) + onAllNodesWithText("Ask again").assertCountEquals(0) + } + } + + /** + * The count on the button is what it can actually send, so a cooling-down + * row lowers it rather than being skipped silently inside it. + */ + @Test + fun `a cooling-down row is not counted in the bulk offer`() { + val cooling = proposal( + "c", "New chapter", "Genesis 6", + stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES, + approved = true, signerIds = "1,2", owing = 1, + resentAt = Clock.System.now(), + ) + + render(loaded(listOf(stalled("a", "Genesis 4"), stalled("b", "Genesis 5"), cooling))) { + chip("Being signed · 3").performClick() + + onNodeWithText("Ask all 2 again").assertIsDisplayed() + } + } + + /** It is offered on any filter that shows two it can act on, not only that one. */ + @Test + fun `the unfiltered screen offers it too when two rows can take it`() { + render(loaded(listOf(stalled("a", "Genesis 4"), stalled("b", "Genesis 5")))) { + onNodeWithText("Ask all 2 again").assertIsDisplayed() + } + } + @Test fun `a pushed screen keeps its way back, chips or no chips`() { render(loaded(mixed)) { onNodeWithContentDescription("Back").assertIsDisplayed() } @@ -549,6 +635,17 @@ class ProposalListScreenJvmTest { resent += sessionId return true } + + /** + * Unpaced here on purpose. What this file asks of a bulk tap is + * which rows it covers; that the real run spaces them a second apart + * is `FrostSigningResendJvmTest`'s to say, against the manager that + * does the spacing. + */ + override suspend fun resendAll( + localChatRoom: LocalChatRoom, + sessionIds: List + ): Int = sessionIds.count { resend(localChatRoom, it) } } private fun render( diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/view/model/ProposalListViewModelJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/view/model/ProposalListViewModelJvmTest.kt index 1c1ed765..03d3d2d0 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/view/model/ProposalListViewModelJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/view/model/ProposalListViewModelJvmTest.kt @@ -112,9 +112,23 @@ class ProposalListViewModelJvmTest { owed[session.id].orEmpty() override suspend fun resend(localChatRoom: LocalChatRoom, sessionId: String): Boolean { + val gate = resendGate resent += sessionId - return resendGate?.await() ?: true + return gate?.await() ?: true } + + /** + * Unpaced on purpose. The real run spaces the sessions a second apart, + * and that is `FrostSigningResendJvmTest`'s to say against the manager + * that does it -- pacing a fake here would assert the fake and spend + * seconds of the suite's wall time doing it. What this file asks is + * which sessions the view model hands over, and what it does to the rows + * while they are in flight. + */ + override suspend fun resendAll( + localChatRoom: LocalChatRoom, + sessionIds: List + ): Int = sessionIds.count { resend(localChatRoom, it) } } private fun nonces(sessionId: String, members: Int) = @@ -714,6 +728,151 @@ class ProposalListViewModelJvmTest { model.viewModelScope.cancel() } + // --- asking about all of them at once ------------------------------------------- + + /** What the bulk offer covers, as the filter chose it. */ + private suspend fun askableModel(): ProposalListViewModel { + val sessions = MutableSharedFlow>(replay = 1) + owed["one"] = setOf(ada) + owed["two"] = setOf(ada) + owed["three"] = setOf(ada) + sessions.emit( + listOf( + local("one", approved = true, coordinator = bob), + local("two", approved = true, coordinator = bob), + local( + "three", + stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES, + approved = true, + signerIds = "1,2", + coordinator = bob, + ), + ) + ) + + val model = viewModel(sessions) + model.initiate() + model.awaitLoaded() + return model + } + + @Test + fun `asking about all of them sends one per session, in order`() = runBlocking { + val model = askableModel() + + var result: Pair? = null + model.askAllAgain { sent, of -> result = sent to of } + + withTimeout(10_000) { while (result == null) delay(10) } + + assertEquals(listOf("one", "two", "three"), resent) + assertEquals(3 to 3, result) + + model.viewModelScope.cancel() + } + + /** + * Every covered session is pending for the whole run, not just for its turn. + * A row whose turn has not come is still about to publish, and a row button + * offering to send it separately in the meantime would send it twice. + */ + @Test + fun `every covered row goes quiet for the whole run, not just for its turn`() = runBlocking { + val model = askableModel() + + // Held open on the first session, which is what a paced run looks like + // from the rows' point of view: one publishing, the rest about to. + val gate = CompletableDeferred() + resendGate = gate + + model.askAllAgain { _, _ -> } + + // Waited for the call, not for the pending mark: the mark is set before + // the coroutine starts, so the two are not the same moment. + withTimeout(5_000) { while (resent.isEmpty()) delay(10) } + + assertEquals(listOf("one"), resent, "the run did not wait for the first to finish") + assertTrue(model.isAsking("two"), "a row waiting its turn was still offering to send") + assertTrue(model.isAsking("three")) + + // And the row's own button cannot slip a second copy in alongside. + model.askAgain("three") {} + delay(50) + assertEquals(0, resent.count { it == "three" }, "a row published out of turn") + + gate.complete(true) + resendGate = null + + withTimeout(5_000) { while (model.isAsking("three")) delay(10) } + assertEquals(listOf("one", "two", "three"), resent) + + model.viewModelScope.cancel() + } + + @Test + fun `a session already being asked separately is not asked again by the bulk run`() = + runBlocking { + val model = askableModel() + + val gate = CompletableDeferred() + resendGate = gate + + model.askAgain("one") {} + withTimeout(5_000) { while (resent.isEmpty()) delay(10) } + + var result: Pair? = null + model.askAllAgain { sent, of -> result = sent to of } + + gate.complete(true) + withTimeout(5_000) { while (result == null) delay(10) } + + assertEquals(2 to 2, result, "the bulk run counted a session already in flight") + assertEquals( + listOf("one", "two", "three"), + resent, + "the session already in flight was published twice" + ) + + model.viewModelScope.cancel() + } + + /** A run where something settled in between says so rather than claiming all. */ + @Test + fun `a run that could not send them all reports the shortfall`() = runBlocking { + val model = askableModel() + + resendGate = CompletableDeferred().also { it.complete(false) } + + var result: Pair? = null + model.askAllAgain { sent, of -> result = sent to of } + + withTimeout(10_000) { while (result == null) delay(10) } + + assertEquals(0 to 3, result) + + model.viewModelScope.cancel() + } + + @Test + fun `asking about all of them when nothing is askable publishes nothing`() = runBlocking { + val sessions = MutableSharedFlow>(replay = 1) + owed["theirs"] = setOf(ada) + sessions.emit(listOf(local("theirs", approved = true, coordinator = ada))) + + val model = viewModel(sessions) + model.initiate() + model.awaitLoaded() + + var called = false + model.askAllAgain { _, _ -> called = true } + delay(100) + + assertTrue(resent.isEmpty()) + assertFalse(called, "a run with nothing in it reported a result") + + model.viewModelScope.cancel() + } + private fun ProposalListViewModel.loaded(): ProposalListUIState.Loaded = assertIs(proposalListUIState) }