diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml
index 5327edad..5d08cb50 100644
--- a/composeApp/src/commonMain/composeResources/values/strings.xml
+++ b/composeApp/src/commonMain/composeResources/values/strings.xml
@@ -21,6 +21,7 @@
"XML file ... is not valid. Check the file content."
-->
Abandoned
+ Abandoned. Nothing was signed.
All
Curare
Add a dialect the group can translate into
@@ -91,6 +92,7 @@
Emergency kit
ENDED
Encrypt and back your recovery information up to your Google Drive or iCloud.
+ Enough members took part without you.
Enter the name you want to use for your group
Enter the name you want to use for your profile
Enter the translation for this chunk
@@ -155,6 +157,7 @@
Name (eg. Group Discussions)
Name of artifact
Needs you
+ Needs your review.
Network relays
No proposal is being signed.
No proposal is waiting on members.
@@ -240,6 +243,7 @@
The signed event
Copy the signed event
Copied the signed event
+ Signed by the group.
This device holds the group's reading of this, but not the signed event itself. Nothing here can be checked against a signature.
Sign
Sign in
@@ -324,9 +328,12 @@
Write down and secure the 12 word phrase that this profile and its wallet are derived from.
Who will you be passing the aux to?
You
+ You agreed.
You and 1 other
You are about to create a NOSTR profile.
+ You are signing.
You can still carry on and invite people later.
+ You declined.
You have not backed up your recovery phrase
You only live once. Lose this phone and the profile goes with it, along with anything it holds.
You said you wrote it down
@@ -400,6 +407,8 @@
Anyone in this group can run a subgroup, whether or not they administer this one.
Has no key package yet
Admin of this group
+ %1$s of %2$s members have offered to sign
+ %1$s of %2$s signatures are in
You coordinate this subgroup
Start the key ceremony
Checking who can be added…
diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/FrostSigningSessionDao.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/FrostSigningSessionDao.kt
index 57fcd390..433e83d3 100644
--- a/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/FrostSigningSessionDao.kt
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/FrostSigningSessionDao.kt
@@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.Flow
import to.curare.compose.database.model.FrostSignerMessage
import to.curare.compose.database.model.FrostSigningItem
import to.curare.compose.database.model.FrostSigningSession
+import to.curare.compose.database.model.intermdiate.FrostSignerTally
import to.curare.compose.database.model.intermdiate.LocalFrostSigningSession
@Dao
@@ -33,6 +34,48 @@ interface FrostSigningSessionDao {
@Query("SELECT * FROM FrostSigningSession WHERE chatRoomId = :chatRoomId ORDER BY createdAt DESC")
fun observeSessionsForChatRoom(chatRoomId: String): Flow>
+ /**
+ * How many members have sent each kind of message, for every session in a
+ * room, in one query.
+ *
+ * The list shows each proposal's progress -- three of the four nonces it
+ * needs, two of the three signatures -- and a query per row would be one per
+ * proposal on a screen whose whole point is that a room has several.
+ *
+ * The join to `DkgParticipantMessage` is what makes the figure honest rather
+ * than merely available. A nonce or a partial signature is recorded without
+ * any check on who sent it, unlike a refusal; `FrostSigningManager`'s
+ * `orderedNonces` then drops the ones from members who hold no share when it
+ * aggregates. Counting those here would have a row read ahead of the session
+ * it describes -- saying the group has what it needs while the coordinator
+ * is still waiting -- which is the one direction a progress figure must
+ * never be wrong in. `HOST_KEY` is the message that makes somebody a share
+ * holder, and the entity's primary key makes it at most one row per member,
+ * so the join cannot multiply a count either.
+ *
+ * @param hostKeyKind `DkgRitualEvents.HOST_KEY`; passed rather than written
+ * in, because a kind inlined into SQL is a kind nothing would renumber.
+ */
+ @Query(
+ """
+ SELECT m.sessionId AS sessionId,
+ m.kind AS kind,
+ COUNT(DISTINCT m.signerPublicKey) AS members
+ FROM FrostSignerMessage m
+ JOIN FrostSigningSession s ON s.id = m.sessionId
+ JOIN DkgParticipantMessage d ON d.sessionId = s.dkgSessionId
+ AND d.participantPublicKey = m.signerPublicKey
+ AND d.kind = :hostKeyKind
+ WHERE s.chatRoomId = :chatRoomId AND m.kind IN (:kinds)
+ GROUP BY m.sessionId, m.kind
+ """
+ )
+ fun observeSignerTalliesForChatRoom(
+ chatRoomId: String,
+ kinds: List,
+ hostKeyKind: Kind,
+ ): Flow>
+
@Query("SELECT * FROM FrostSigningSession WHERE chatRoomId = :chatRoomId ORDER BY createdAt DESC LIMIT 1")
suspend fun getLatestSessionForChatRoom(chatRoomId: String): FrostSigningSession?
diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/intermdiate/FrostSignerTally.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/intermdiate/FrostSignerTally.kt
new file mode 100644
index 00000000..cb728194
--- /dev/null
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/intermdiate/FrostSignerTally.kt
@@ -0,0 +1,26 @@
+package to.curare.compose.database.model.intermdiate
+
+import com.vitorpamplona.quartz.nip01Core.core.Kind
+
+/**
+ * How many members have sent one kind of message to one signing session.
+ *
+ * A projection rather than an entity: there is no table of these, only a count
+ * over `FrostSignerMessage` grouped by session and kind. It exists so a list of
+ * a room's proposals can say how far each has got without one query per row --
+ * the same reason `LocalFrostSigningSession` joins the items.
+ *
+ * [members] counts members and not rows, and counts only the ones who hold a
+ * share of the key being signed with. Both matter: `FrostSigningManager` records
+ * a nonce or a partial signature without checking who sent it -- unlike a
+ * refusal, which it filters -- so the table may hold messages from members who
+ * are not in the ceremony at all, and `orderedNonces` drops exactly those when
+ * it aggregates. A count that included them would read ahead of the session it
+ * describes, which is the one way a progress figure must never be wrong: it
+ * would say the group is ready while the coordinator is still waiting.
+ */
+data class FrostSignerTally(
+ val sessionId: String,
+ val kind: Kind,
+ val members: Int,
+)
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 aa9daa47..4f6697fa 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
@@ -12,10 +12,12 @@ import to.curare.compose.database.CurareDatabase
import to.curare.compose.database.model.FrostSignerMessage
import to.curare.compose.database.model.FrostSigningItem
import to.curare.compose.database.model.FrostSigningSession
+import to.curare.compose.database.model.intermdiate.FrostSignerTally
import to.curare.compose.database.model.intermdiate.LocalChatRoom
import to.curare.compose.database.model.intermdiate.LocalFrostSigningSession
import to.curare.compose.managers.FrostSigningManager
import to.curare.compose.nostr.dkg.DkgRitualEvents
+import to.curare.compose.nostr.frost.FrostSigningEvents
import to.curare.compose.repository.FrostSigningRepository
class DatabaseFrostSigningRepository(
@@ -30,6 +32,17 @@ class DatabaseFrostSigningRepository(
override fun observeSessionsForChatRoom(chatRoomId: String): Flow> =
database.frostSigningSessionDao().observeSessionsForChatRoom(chatRoomId)
+ override fun observeSignerTalliesForChatRoom(
+ chatRoomId: String
+ ): Flow> =
+ database.frostSigningSessionDao().observeSignerTalliesForChatRoom(
+ chatRoomId = chatRoomId,
+ // The two kinds a proposal's progress is counted in. Asked for by
+ // name so the query carries no opinion about which kinds exist.
+ kinds = listOf(FrostSigningEvents.NONCE, FrostSigningEvents.PARTIAL_SIGNATURE),
+ hostKeyKind = DkgRitualEvents.HOST_KEY,
+ )
+
override fun observeMessages(sessionId: String): Flow> =
database.frostSigningSessionDao().observeMessages(sessionId)
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 41ec4783..2d61cf11 100644
--- a/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt
+++ b/composeApp/src/commonMain/kotlin/to/curare/compose/repository/FrostSigningRepository.kt
@@ -10,6 +10,7 @@ import to.curare.compose.database.model.FrostSignerMessage
import to.curare.compose.database.model.FrostSigningItem
import to.curare.compose.database.model.FrostSigningSession
import to.curare.compose.database.model.intermdiate.LocalChatRoom
+import to.curare.compose.database.model.intermdiate.FrostSignerTally
import to.curare.compose.database.model.intermdiate.LocalFrostSigningSession
/**
@@ -30,6 +31,21 @@ interface FrostSigningRepository {
*/
fun observeSessionsForChatRoom(chatRoomId: String): Flow>
+ /**
+ * How far each of a room's sessions has got at gathering what it needs, as
+ * a count of members per session per kind of message.
+ *
+ * A companion to [observeSessionsForChatRoom] rather than part of it: the
+ * session rows say which stage a proposal is in, and this says how far
+ * through that stage it is. Kept apart because the counts change far more
+ * often than the rows do -- every member's nonce moves one -- and because
+ * carrying the messages themselves would carry every nonce payload the room
+ * has ever exchanged to a screen that only wants to count them.
+ *
+ * Only members holding a share of the key are counted; see the query.
+ */
+ fun observeSignerTalliesForChatRoom(chatRoomId: String): Flow>
+
fun observeMessages(sessionId: String): Flow>
/**
@@ -177,6 +193,10 @@ interface FrostSigningRepository {
override fun observeSessionsForChatRoom(chatRoomId: String): Flow> =
flowOf(emptyList())
+ override fun observeSignerTalliesForChatRoom(
+ chatRoomId: String
+ ): Flow> = flowOf(emptyList())
+
override fun observeMessages(sessionId: String): Flow> =
flowOf(emptyList())
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 ae2d738e..22f85e47 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
@@ -59,6 +59,15 @@ 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.you_declined
+import curare.composeapp.generated.resources.you_are_signing
+import curare.composeapp.generated.resources.you_agreed
+import curare.composeapp.generated.resources.signed_by_the_group
+import curare.composeapp.generated.resources.of_signatures_are_in
+import curare.composeapp.generated.resources.of_members_have_offered_to_sign
+import curare.composeapp.generated.resources.needs_your_review
+import curare.composeapp.generated.resources.enough_members_took_part_without_you
+import curare.composeapp.generated.resources.abandoned_nothing_was_signed
import curare.composeapp.generated.resources.everything_else
import curare.composeapp.generated.resources.filter_all
import curare.composeapp.generated.resources.filter_and_count
@@ -508,7 +517,11 @@ private fun ProposalCard(
)
}
- Text(text = statusOf(proposal))
+ Text(text = statusOf(proposal), color = cardContentColor)
+
+ proposal.progress?.let { progress ->
+ ProgressLine(progress = progress, color = cardContentColor)
+ }
Text(
text = "${proposal.proposerName} · " +
@@ -523,13 +536,19 @@ private fun ProposalCard(
}
/**
- * Where a proposal has got to, in a line.
+ * Who the reader is in this proposal, in a line.
*
* Shorter than the signing screen's version of the same question, and it has to
* be: this is one row of a list somebody is scanning, not the page they are
* deciding on. What it must never do is call a session finished when it is not,
* or waiting when the decision has gone.
*
+ * It no longer carries any numbers. It used to say "waiting for 2 of 3 members
+ * to take part", which is the rule rather than the progress -- a row said the
+ * same thing on the day a proposal was made and on the day before it completed.
+ * The fraction that does move is [ProgressLine]'s, and two fractions on one row
+ * meaning different things would be worse than the one that said nothing.
+ *
* Still switches on the stage rather than on `Proposal.state`, and still has to,
* though for less than it used to. What is left collapsed inside a state is who
* the reader was in it: whether they agreed or declined, under
@@ -538,39 +557,75 @@ private fun ProposalCard(
* than about the proposal, which is why they are a sentence on the row and not a
* chip above it.
*/
+@Composable
private fun statusOf(proposal: ProposalListUIState.Proposal): String {
val session = proposal.session
- if (proposal.state == ProposalState.NEEDS_YOU) return "Needs your review"
+ if (proposal.state == ProposalState.NEEDS_YOU) {
+ return stringResource(Res.string.needs_your_review)
+ }
return when (session.stage) {
// Not awaiting the reader and still collecting means they answered, one
// way or the other. A refusal leaves the session open -- a t-of-n key
// can do without this member -- so it is still a session in progress,
// and the row has to say which answer was given.
- FrostSigningStage.COLLECTING_NONCES ->
- (if (session.signDeclinedAt != null) "You declined. " else "You agreed. ") +
- "Waiting for ${session.threshold} of " +
- "${session.participantCount} members to take part."
+ FrostSigningStage.COLLECTING_NONCES -> when (session.signDeclinedAt) {
+ null -> stringResource(Res.string.you_agreed)
+ else -> stringResource(Res.string.you_declined)
+ }
- FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES ->
- if (session.isSigner()) {
- "You are signing. Waiting on the rest of the signers."
- } else {
- "Enough members took part without you. Waiting on them to sign."
- }
+ FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES -> when {
+ session.isSigner() -> stringResource(Res.string.you_are_signing)
+ else -> stringResource(Res.string.enough_members_took_part_without_you)
+ }
- FrostSigningStage.COMPLETE -> "Signed by the group."
+ FrostSigningStage.COMPLETE -> stringResource(Res.string.signed_by_the_group)
// The reason is the whole content of a failure -- "declined on this
// device" and "the group could not agree" are different things to
// have happened -- so it stands in for the word "abandoned" whenever
// there is one.
- FrostSigningStage.FAILED ->
- session.failureReason ?: "Abandoned. Nothing was signed."
+ FrostSigningStage.FAILED -> session.failureReason
+ ?: stringResource(Res.string.abandoned_nothing_was_signed)
}
}
+/**
+ * How much of what the proposal is waiting for has arrived.
+ *
+ * A line of its own rather than a clause of the status line, because it is the
+ * only thing on the row that moves while the reader is looking at it -- a
+ * member's nonce landing changes this and nothing else -- and because a reader
+ * scanning a list for the one that is nearly there should not have to read a
+ * sentence to find out.
+ *
+ * Drawn only while something is being gathered: see
+ * `ProposalListUIState.Proposal.progress`, which is null for both endings.
+ */
+@Composable
+private fun ProgressLine(
+ progress: ProposalListUIState.Progress,
+ color: Color,
+) {
+ Text(
+ text = when (progress.of) {
+ ProposalListUIState.Progress.Of.MEMBERS -> stringResource(
+ Res.string.of_members_have_offered_to_sign,
+ progress.have,
+ progress.need
+ )
+
+ ProposalListUIState.Progress.Of.SIGNATURES -> stringResource(
+ Res.string.of_signatures_are_in,
+ progress.have,
+ progress.need
+ )
+ },
+ color = color
+ )
+}
+
@ConformancePreviews
@Composable
private fun ProposalListScreenPreview() {
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 5c740e82..32e357f5 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
@@ -10,12 +10,16 @@ import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import co.touchlab.kermit.Logger
import com.vitorpamplona.quartz.nip01Core.core.Event
+import com.vitorpamplona.quartz.nip01Core.core.Kind
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
+import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
+import to.curare.compose.database.model.intermdiate.FrostSignerTally
import to.curare.compose.database.model.intermdiate.LocalFrostSigningSession
import to.curare.compose.extensions.shortened
import to.curare.compose.managers.FrostSigningManager
+import to.curare.compose.nostr.frost.FrostSigningEvents
import to.curare.compose.repository.ChatRepository
import to.curare.compose.repository.FrostSigningRepository
import to.curare.compose.text.ProposedEvent
@@ -82,12 +86,24 @@ class ProposalListViewModel(
return@launch
}
- frostSigningRepository.observeSessionsForChatRoom(chatRoomId).collect { sessions ->
+ // Two flows rather than one, combined here. The rows and the counts
+ // move on different things -- a row changes when a session advances,
+ // a count every time any member's message lands -- and the counts
+ // are a `GROUP BY` that would carry every nonce payload the room has
+ // ever exchanged if it travelled as the messages themselves.
+ combine(
+ frostSigningRepository.observeSessionsForChatRoom(chatRoomId),
+ frostSigningRepository.observeSignerTalliesForChatRoom(chatRoomId),
+ ) { sessions, tallies ->
+ sessions to tallies.groupBy { it.sessionId }
+ }.collect { (sessions, talliesBySession) ->
logger.d("${sessions.size} proposals in $chatRoomId")
proposalListUIState = ProposalListUIState.Loaded(
localChatRoom = localChatRoom,
- proposals = sessions.map { asProposal(it) },
+ proposals = sessions.map {
+ asProposal(it, talliesBySession[it.session.id].orEmpty())
+ },
filter = filter
)
}
@@ -119,10 +135,17 @@ class ProposalListViewModel(
* worked out here, so the list and the signing screen cannot disagree about
* which proposals still have a decision in them.
*/
- private fun asProposal(local: LocalFrostSigningSession): ProposalListUIState.Proposal {
+ private fun asProposal(
+ local: LocalFrostSigningSession,
+ tallies: List,
+ ): ProposalListUIState.Proposal {
val items = local.items
val events = items.mapNotNull { Event.fromJsonOrNull(it.unsignedEventJson) }
+ // A session nobody has answered has no row in the tally at all, which is
+ // a count of zero rather than a missing one.
+ fun counted(kind: Kind) = tallies.firstOrNull { it.kind == kind }?.members ?: 0
+
return ProposalListUIState.Proposal(
session = local.session,
proposerName = when {
@@ -132,7 +155,9 @@ class ProposalListViewModel(
},
signs = events.map { ProposedEvent.summarize(it) },
unreadable = items.size - events.size,
- awaitsYou = FrostSigningManager.isAwaitingApproval(local.session, items)
+ awaitsYou = FrostSigningManager.isAwaitingApproval(local.session, items),
+ noncesOffered = counted(FrostSigningEvents.NONCE),
+ partialSignatures = counted(FrostSigningEvents.PARTIAL_SIGNATURE)
)
}
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 95a13d8c..d8be898a 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
@@ -93,6 +93,17 @@ sealed interface ProposalListUIState {
/** Whether the session is still waiting on this device's owner. */
val awaitsYou: Boolean = false,
+
+ /**
+ * How many share-holding members have offered a nonce, and how many
+ * have published their part of the signature.
+ *
+ * Counts of members, not of messages, and only of members who hold a
+ * share -- see `FrostSignerTally`, which is where both restrictions are
+ * imposed and explained.
+ */
+ val noncesOffered: Int = 0,
+ val partialSignatures: Int = 0,
) {
/** The batch's size, readable or not. */
val eventCount: Int get() = signs.size + unreadable
@@ -126,6 +137,61 @@ sealed interface ProposalListUIState {
FrostSigningStage.FAILED -> ProposalState.ABANDONED
}
}
+
+ /**
+ * How far the thing it is currently gathering has got, or null when it
+ * is gathering nothing.
+ *
+ * Keyed on the stage rather than on [state], because the stage is what
+ * decides *which* count is the live one and [ProposalState.NEEDS_YOU]
+ * overlays both of the gathering stages. A proposal can need the
+ * reader's answer while the group is on either, and in both cases the
+ * figure it wants is the one for the stage it is actually in.
+ *
+ * Null once it has ended, which is both of the endings and not only the
+ * happy one. A signed session has nothing left to gather; an abandoned
+ * one has counts frozen where they stopped, and "2 of 3" beside a
+ * failure invites the reader to wait for a third that is never coming.
+ * The failure reason is what that row has to say.
+ */
+ val progress: Progress? get() = when (session.stage) {
+ // Enough to aggregate is the threshold, not the whole room --
+ // `orderedNonces` gates on exactly this and takes the first t of
+ // them, so t is both what is needed and what will be used.
+ FrostSigningStage.COLLECTING_NONCES ->
+ Progress(noncesOffered, session.threshold, Progress.Of.MEMBERS)
+
+ // The signer set once it exists, and the threshold until it does --
+ // they are the same number, since the set is the first t offers, but
+ // the set is the one this stage is actually waiting on.
+ FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES -> Progress(
+ partialSignatures,
+ session.signerIdList()?.size ?: session.threshold,
+ Progress.Of.SIGNATURES
+ )
+
+ FrostSigningStage.COMPLETE, FrostSigningStage.FAILED -> null
+ }
+ }
+
+ /**
+ * How much of what a proposal is waiting for has arrived.
+ *
+ * [have] can exceed [need] for a moment: a member's nonce lands locally
+ * before this device has run `advance` and moved the session on, so a row
+ * can read "3 of 2" between a delivery and the pass that acts on it. Clamped
+ * rather than shown, because a fraction over one reads as a bug in the app
+ * rather than as a session about to advance.
+ */
+ data class Progress(
+ private val arrived: Int,
+ val need: Int,
+ val of: Of,
+ ) {
+ val have: Int = arrived.coerceIn(0, need)
+
+ /** What is being counted, which decides the word the row uses. */
+ enum class Of { MEMBERS, SIGNATURES }
}
data class Error(
diff --git a/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/ProposalProgressTest.kt b/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/ProposalProgressTest.kt
new file mode 100644
index 00000000..1c6abdaf
--- /dev/null
+++ b/composeApp/src/commonTest/kotlin/to/curare/compose/ui/view/state/ProposalProgressTest.kt
@@ -0,0 +1,184 @@
+package to.curare.compose.ui.view.state
+
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertNull
+import kotlin.time.Clock
+import to.curare.compose.database.model.FrostSigningSession
+import to.curare.compose.database.model.types.FrostSigningStage
+
+/**
+ * How far along a proposal says it is, and when it says nothing.
+ *
+ * The denominators are the ones the protocol actually gates on, not the ones
+ * that happen to be nearby. `FrostSigningManager.orderedNonces` returns null
+ * while fewer than `threshold` nonces have arrived and then takes the first
+ * `threshold` of them, so the threshold is both what is needed and what gets
+ * used -- the room's size is a bigger number that nothing waits for. Once the
+ * signer set exists it is the set that is being waited on, which is the same
+ * count arrived at from the other side.
+ *
+ * A figure that read against the room's size would sit at "2 of 5" on a session
+ * that was about to complete, and one that read ahead of the coordinator would
+ * sit at "3 of 2" on a session that was not moving. Both are worse than no
+ * figure, which is why there is a test for each.
+ */
+class ProposalProgressTest {
+ private val roomId = "11".repeat(32)
+
+ private fun session(
+ stage: FrostSigningStage = FrostSigningStage.COLLECTING_NONCES,
+ threshold: Int = 2,
+ participantCount: Int = 5,
+ signerIds: String? = null,
+ ) = FrostSigningSession(
+ id = "session",
+ chatRoomId = roomId,
+ coordinatorPublicKey = "a".repeat(64),
+ userPublicKey = "b".repeat(64),
+ dkgSessionId = "ceremony",
+ threshold = threshold,
+ participantCount = participantCount,
+ signerId = 2,
+ stage = stage,
+ signerIds = signerIds,
+ signApprovedAt = Clock.System.now(),
+ )
+
+ private fun proposal(
+ session: FrostSigningSession,
+ nonces: Int = 0,
+ signatures: Int = 0,
+ awaitsYou: Boolean = false,
+ ) = ProposalListUIState.Proposal(
+ session = session,
+ proposerName = "Ada",
+ awaitsYou = awaitsYou,
+ noncesOffered = nonces,
+ partialSignatures = signatures,
+ )
+
+ @Test
+ fun `gathering nonces counts members against the threshold, not the room`() {
+ val progress = proposal(session(threshold = 2, participantCount = 5), nonces = 1).progress
+
+ assertEquals(ProposalListUIState.Progress.Of.MEMBERS, progress?.of)
+ assertEquals(1, progress?.have)
+ assertEquals(
+ 2,
+ progress?.need,
+ "the room has five members but only the threshold is waited for"
+ )
+ }
+
+ @Test
+ fun `gathering signatures counts against the signer set once there is one`() {
+ val progress = proposal(
+ session(
+ stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES,
+ threshold = 2,
+ participantCount = 5,
+ signerIds = "1,3,4",
+ ),
+ signatures = 2,
+ ).progress
+
+ assertEquals(ProposalListUIState.Progress.Of.SIGNATURES, progress?.of)
+ assertEquals(2, progress?.have)
+ assertEquals(3, progress?.need, "the signer set is what this stage waits on")
+ }
+
+ /**
+ * The signer set is written together with the aggregate, and this device may
+ * reach the stage before that write lands. The threshold is the same number
+ * the set will turn out to be, so it stands in rather than the row going
+ * blank for a frame.
+ */
+ @Test
+ fun `gathering signatures falls back to the threshold before the set is written`() {
+ val progress = proposal(
+ session(stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES, threshold = 2),
+ signatures = 1,
+ ).progress
+
+ assertEquals(1, progress?.have)
+ assertEquals(2, progress?.need)
+ }
+
+ @Test
+ fun `the count each stage shows is the one that stage is waiting for`() {
+ val collecting = proposal(session(), nonces = 1, signatures = 7).progress
+
+ assertEquals(ProposalListUIState.Progress.Of.MEMBERS, collecting?.of)
+ assertEquals(1, collecting?.have, "it read the signature count while gathering nonces")
+
+ val signing = proposal(
+ session(stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES, signerIds = "1,2"),
+ nonces = 2,
+ signatures = 1,
+ ).progress
+
+ assertEquals(ProposalListUIState.Progress.Of.SIGNATURES, signing?.of)
+ assertEquals(1, signing?.have, "it read the nonce count while gathering signatures")
+ }
+
+ /**
+ * A proposal can want the reader's answer on either gathering stage, so the
+ * figure follows the stage rather than the classification.
+ */
+ @Test
+ fun `a proposal waiting on you still shows the stage it is actually in`() {
+ val needsYou = proposal(session(threshold = 3), nonces = 1, awaitsYou = true)
+
+ assertEquals(ProposalState.NEEDS_YOU, needsYou.state)
+ assertEquals(ProposalListUIState.Progress.Of.MEMBERS, needsYou.progress?.of)
+ assertEquals(1, needsYou.progress?.have)
+ assertEquals(3, needsYou.progress?.need)
+ }
+
+ @Test
+ fun `a signed proposal counts nothing, because there is nothing left to gather`() {
+ assertNull(
+ proposal(
+ session(stage = FrostSigningStage.COMPLETE, signerIds = "1,2"),
+ signatures = 2,
+ ).progress
+ )
+ }
+
+ /**
+ * An abandoned one counts nothing either, and for a reason of its own: its
+ * counts are frozen where they stopped, and "1 of 2" beside a failure invites
+ * the reader to wait for a second that is never coming.
+ */
+ @Test
+ fun `an abandoned proposal counts nothing, so no row waits for a signature that is not coming`() {
+ assertNull(proposal(session(stage = FrostSigningStage.FAILED), nonces = 1).progress)
+ }
+
+ /**
+ * A nonce can land locally before this device has run the pass that acts on
+ * it, so the raw count can momentarily exceed what is needed.
+ */
+ @Test
+ fun `a count running ahead of the stage is clamped rather than shown`() {
+ val ahead = proposal(session(threshold = 2), nonces = 3).progress
+
+ assertEquals(2, ahead?.have, "a row read more than it needed")
+ assertEquals(2, ahead?.need)
+ }
+
+ @Test
+ fun `nothing gathered yet is zero rather than missing`() {
+ val fresh = proposal(session(threshold = 2)).progress
+
+ assertEquals(0, fresh?.have)
+ assertEquals(2, fresh?.need)
+ }
+
+ /** Negative is not reachable, but the clamp is two-sided so it cannot be drawn. */
+ @Test
+ fun `a count below zero is clamped too`() {
+ assertEquals(0, ProposalListUIState.Progress(-1, 2, ProposalListUIState.Progress.Of.MEMBERS).have)
+ }
+}
diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/database/dao/FrostSigningSessionDaoJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/database/dao/FrostSigningSessionDaoJvmTest.kt
index 5f1ae5d2..5433b087 100644
--- a/composeApp/src/jvmTest/kotlin/to/curare/compose/database/dao/FrostSigningSessionDaoJvmTest.kt
+++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/database/dao/FrostSigningSessionDaoJvmTest.kt
@@ -8,6 +8,8 @@ import kotlinx.coroutines.runBlocking
import to.curare.compose.database.CurareDatabase
import to.curare.compose.database.builder.getRoomDatabase
import to.curare.compose.database.model.ChatRoom
+import to.curare.compose.database.model.DkgParticipantMessage
+import to.curare.compose.database.model.DkgSession
import to.curare.compose.database.model.FrostSignerMessage
import to.curare.compose.database.model.FrostSigningItem
import to.curare.compose.database.model.FrostSigningSession
@@ -376,4 +378,144 @@ class FrostSigningSessionDaoJvmTest {
assertEquals("f2".repeat(32), db.frostSigningSessionDao().getItem("s1", 2)?.eventId)
assertNull(db.frostSigningSessionDao().getItem("s1", 3))
}
+
+ // --- the progress tallies -----------------------------------------------------
+
+ private val carol = KeyPair().pubKey.toHexKey()
+ private val hostKeyKind = 9
+
+ /** A ceremony, and who holds a share of the key it made. */
+ private suspend fun ceremony(holders: List, id: String = "dkg-1") {
+ db.dkgSessionDao().upsert(
+ DkgSession(
+ id = id,
+ chatRoomId = roomOne,
+ coordinatorPublicKey = user,
+ userPublicKey = user,
+ threshold = 2,
+ participantCount = 3,
+ hostPublicKey = user,
+ round1Random = "aa".repeat(32),
+ round2AuxRandom = "bb".repeat(32),
+ )
+ )
+
+ holders.forEach { holder ->
+ db.dkgSessionDao().upsert(
+ DkgParticipantMessage(
+ sessionId = id,
+ participantPublicKey = holder,
+ kind = hostKeyKind,
+ payload = "cc".repeat(32),
+ )
+ )
+ }
+ }
+
+ private suspend fun tallies(chatRoomId: String = roomOne) =
+ db.frostSigningSessionDao().observeSignerTalliesForChatRoom(
+ chatRoomId = chatRoomId,
+ kinds = listOf(nonceKind, partialSignatureKind),
+ hostKeyKind = hostKeyKind,
+ ).first()
+
+ @Test
+ fun `a tally counts the members who answered, per session and per kind`() = runBlocking {
+ seedRooms()
+ ceremony(holders = listOf(user, alice, bob))
+ session("one")
+ session("two")
+
+ message("one", alice, nonceKind)
+ message("one", bob, nonceKind)
+ message("one", alice, partialSignatureKind)
+ message("two", alice, nonceKind)
+
+ val byKey = tallies().associate { (it.sessionId to it.kind) to it.members }
+
+ assertEquals(2, byKey[("one" to nonceKind)])
+ assertEquals(1, byKey[("one" to partialSignatureKind)])
+ assertEquals(1, byKey[("two" to nonceKind)])
+ assertNull(byKey[("two" to partialSignatureKind)], "a kind nobody sent has no row")
+ }
+
+ /**
+ * The restriction the figure rests on.
+ *
+ * `FrostSigningManager` records a nonce or a partial signature without
+ * checking who sent it -- unlike a refusal, which it filters -- and
+ * `orderedNonces` then drops the ones from members holding no share when it
+ * aggregates. A tally that counted those would have the list read ahead of
+ * the coordinator: a row saying the group has the two offers it needs while
+ * the session sits still, which is the one direction this figure must never
+ * be wrong in.
+ */
+ @Test
+ fun `a nonce from somebody holding no share is not counted`() = runBlocking {
+ seedRooms()
+ ceremony(holders = listOf(alice, bob))
+ session("one")
+
+ message("one", alice, nonceKind)
+ message("one", carol, nonceKind)
+
+ assertEquals(
+ 1,
+ tallies().single { it.kind == nonceKind }.members,
+ "a stranger's nonce was counted towards the threshold"
+ )
+ }
+
+ /** A session nobody has answered has no row at all, which a reader takes as zero. */
+ @Test
+ fun `a session with no messages has no tally`() = runBlocking {
+ seedRooms()
+ ceremony(holders = listOf(alice, bob))
+ session("quiet")
+
+ assertEquals(emptyList(), tallies())
+ }
+
+ /**
+ * The composite key already makes a redelivered message replace its
+ * predecessor, so a member cannot be counted twice by sending twice. The
+ * count is of members either way, which is what the threshold is counted in.
+ */
+ @Test
+ fun `a member who answers twice is still one member`() = runBlocking {
+ seedRooms()
+ ceremony(holders = listOf(alice, bob))
+ session("one")
+
+ message("one", alice, nonceKind, payload = "aa".repeat(32))
+ message("one", alice, nonceKind, payload = "bb".repeat(32))
+
+ assertEquals(1, tallies().single().members)
+ }
+
+ @Test
+ fun `a tally is scoped to its room`() = runBlocking {
+ seedRooms()
+ ceremony(holders = listOf(alice, bob))
+ session("here", chatRoomId = roomOne)
+ session("elsewhere", chatRoomId = roomTwo)
+
+ message("here", alice, nonceKind)
+ message("elsewhere", alice, nonceKind)
+
+ assertEquals(listOf("here"), tallies(roomOne).map { it.sessionId })
+ assertEquals(listOf("elsewhere"), tallies(roomTwo).map { it.sessionId })
+ }
+
+ /** Kinds the caller did not ask for stay out, so a refusal is not read as an offer. */
+ @Test
+ fun `a kind outside the asked-for set is not tallied`() = runBlocking {
+ seedRooms()
+ ceremony(holders = listOf(alice, bob))
+ session("one")
+
+ message("one", alice, kind = 99)
+
+ assertEquals(emptyList(), tallies())
+ }
}
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 fe2db71e..2c9ff1a3 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
@@ -72,6 +72,8 @@ class ProposalListScreenJvmTest {
awaitsYou: Boolean = false,
approved: Boolean = false,
signerIds: String? = null,
+ nonces: Int = 0,
+ signatures: Int = 0,
) = ProposalListUIState.Proposal(
session = session.copy(
id = id,
@@ -82,16 +84,19 @@ class ProposalListScreenJvmTest {
proposerName = "Ada",
signs = listOf(ProposedEvent.Summary(label, detail)),
awaitsYou = awaitsYou,
+ noncesOffered = nonces,
+ partialSignatures = signatures,
)
private val needsYou = proposal("needs", "New chapter", "Genesis 1", awaitsYou = true)
private val alsoNeedsYou = proposal("also", "New chapter", "Genesis 2", awaitsYou = true)
- private val open = proposal("open", "New translation", "isiZulu", approved = true)
+ private val open = proposal("open", "New translation", "isiZulu", approved = true, nonces = 1)
private val beingSigned = proposal(
"signing", "New chapter", "Genesis 4",
stage = FrostSigningStage.COLLECTING_PARTIAL_SIGNATURES,
approved = true,
signerIds = "1,2",
+ signatures = 1,
)
private val signed = proposal("signed", "New post", "A note", stage = FrostSigningStage.COMPLETE)
@@ -285,6 +290,78 @@ class ProposalListScreenJvmTest {
onAllNodesWithText("Waiting for you").assertCountEquals(0)
}
+ // --- the progress line --------------------------------------------------------
+
+ /**
+ * The fraction each row shows is its own stage's, in that stage's words.
+ *
+ * The session gathering nonces counts members against the threshold -- two
+ * of the three in the room, because the threshold is what the coordinator
+ * gates on. The one being signed counts signatures against the signer set.
+ */
+ @Test
+ fun `a gathering proposal says how far it has got, in its stage's terms`() =
+ render(loaded(mixed)) {
+ onNodeWithText("1 of 2 members have offered to sign").assertIsDisplayed()
+ onNodeWithText("1 of 2 signatures are in").assertIsDisplayed()
+ }
+
+ /**
+ * The one the reader has to answer shows it too: whether they are the
+ * deciding vote is the most useful thing the row can tell them, and it is
+ * the row they are most likely to act on.
+ */
+ @Test
+ fun `a proposal waiting on you shows its count as well`() =
+ render(loaded(listOf(proposal("a", "New chapter", "Genesis 9", awaitsYou = true, nonces = 1)))) {
+ onNodeWithText("Needs your review.").assertIsDisplayed()
+ onNodeWithText("1 of 2 members have offered to sign").assertIsDisplayed()
+ }
+
+ /** Nothing left to gather, so nothing counted. */
+ @Test
+ fun `a signed proposal shows no count`() = render(loaded(listOf(signed))) {
+ onNodeWithText("Signed by the group.").assertIsDisplayed()
+ onAllNodesWithText("signatures are in", substring = true).assertCountEquals(0)
+ onAllNodesWithText("have offered to sign", substring = true).assertCountEquals(0)
+ }
+
+ /**
+ * An abandoned one shows none either: its counts are frozen where they
+ * stopped, and a fraction beside a failure invites the reader to wait for a
+ * part that is never coming. The reason is what that row has to say.
+ */
+ @Test
+ fun `an abandoned proposal shows its reason and no count`() {
+ val gone = proposal(
+ "gone", "New chapter", "Genesis 8",
+ stage = FrostSigningStage.FAILED,
+ nonces = 1,
+ ).let { it.copy(session = it.session.copy(failureReason = "The group could not agree.")) }
+
+ render(loaded(listOf(gone))) {
+ onNodeWithText("The group could not agree.").assertIsDisplayed()
+ onAllNodesWithText("have offered to sign", substring = true).assertCountEquals(0)
+ }
+ }
+
+ /**
+ * The status line carries no numbers of its own any more. It used to say
+ * "waiting for 2 of 3 members to take part", which is the rule rather than
+ * the progress -- a row said it on the day a proposal was made and on the
+ * day before it completed. Two fractions meaning different things on one row
+ * would be worse than the one that never moved.
+ */
+ @Test
+ fun `the status line says who you are in it, and leaves the counting alone`() =
+ render(loaded(mixed)) {
+ onNodeWithText("You agreed.").assertIsDisplayed()
+ onNodeWithText("You are signing.").assertIsDisplayed()
+
+ onAllNodesWithText("to take part", substring = true).assertCountEquals(0)
+ onAllNodesWithText("Waiting on the rest", substring = true).assertCountEquals(0)
+ }
+
@Test
fun `a pushed screen keeps its way back, chips or no chips`() {
render(loaded(mixed)) { onNodeWithContentDescription("Back").assertIsDisplayed() }
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 ceea09c6..37b8cad1 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
@@ -9,6 +9,7 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
+import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
@@ -17,9 +18,11 @@ import kotlinx.coroutines.withTimeout
import to.curare.compose.database.model.ChatRoom
import to.curare.compose.database.model.FrostSigningItem
import to.curare.compose.database.model.FrostSigningSession
+import to.curare.compose.database.model.intermdiate.FrostSignerTally
import to.curare.compose.database.model.intermdiate.LocalChatRoom
import to.curare.compose.database.model.intermdiate.LocalFrostSigningSession
import to.curare.compose.database.model.types.FrostSigningStage
+import to.curare.compose.nostr.frost.FrostSigningEvents
import to.curare.compose.repository.ChatRepository
import to.curare.compose.repository.FrostSigningRepository
import to.curare.compose.ui.view.state.ProposalFilter
@@ -83,12 +86,24 @@ class ProposalListViewModelJvmTest {
room.takeIf { id == chatRoomId }
}
- private fun frostRepository(sessions: Flow>) =
- object : FrostSigningRepository by FrostSigningRepository.NO_OP_FROST_SIGNING_REPOSITORY {
- override fun observeSessionsForChatRoom(
- chatRoomId: String
- ): Flow> = sessions
- }
+ private fun frostRepository(
+ sessions: Flow>,
+ tallies: Flow> = flowOf(emptyList()),
+ ) = object : FrostSigningRepository by FrostSigningRepository.NO_OP_FROST_SIGNING_REPOSITORY {
+ override fun observeSessionsForChatRoom(
+ chatRoomId: String
+ ): Flow> = sessions
+
+ override fun observeSignerTalliesForChatRoom(
+ chatRoomId: String
+ ): Flow> = tallies
+ }
+
+ private fun nonces(sessionId: String, members: Int) =
+ FrostSignerTally(sessionId, FrostSigningEvents.NONCE, members)
+
+ private fun signatures(sessionId: String, members: Int) =
+ FrostSignerTally(sessionId, FrostSigningEvents.PARTIAL_SIGNATURE, members)
private fun session(
id: String,
@@ -149,11 +164,12 @@ class ProposalListViewModelJvmTest {
sessions: Flow>,
room: LocalChatRoom? = localChatRoom,
initialFilter: ProposalFilter = ProposalFilter.ALL,
+ tallies: Flow> = flowOf(emptyList()),
) = ProposalListViewModel(
chatRoomId = chatRoomId,
initialProposalListUIState = ProposalListUIState.Loading,
chatRepository = chatRepository(room),
- frostSigningRepository = frostRepository(sessions),
+ frostSigningRepository = frostRepository(sessions, tallies),
initialFilter = initialFilter,
)
@@ -374,6 +390,123 @@ class ProposalListViewModelJvmTest {
model.viewModelScope.cancel()
}
+ // --- the progress counts ------------------------------------------------------
+
+ /**
+ * The counts arrive on a flow of their own and have to land on the right
+ * rows. A tally is keyed by session, and the rooms this screen is for have
+ * several open at once -- putting one session's nonces on another's row
+ * would be the quietest possible way to be wrong.
+ */
+ @Test
+ fun `each session gets its own counts, by session and by kind`() = runBlocking {
+ val sessions = MutableSharedFlow>(replay = 1)
+ val tallies = MutableSharedFlow>(replay = 1)
+ sessions.emit(mixed)
+ tallies.emit(
+ listOf(
+ nonces("open", 1),
+ signatures("signing", 2),
+ // A kind the other session has too, to catch a row that took the
+ // first tally it found rather than its own.
+ nonces("signing", 3),
+ )
+ )
+
+ val model = viewModel(sessions, tallies = tallies)
+ model.initiate()
+
+ val loaded = model.awaitLoaded()
+ val byId = loaded.proposals.associateBy { it.session.id }
+
+ assertEquals(1, byId.getValue("open").noncesOffered)
+ assertEquals(0, byId.getValue("open").partialSignatures)
+ assertEquals(3, byId.getValue("signing").noncesOffered)
+ assertEquals(2, byId.getValue("signing").partialSignatures)
+
+ // And the figure each row actually shows is its stage's.
+ assertEquals(
+ ProposalListUIState.Progress.Of.MEMBERS,
+ byId.getValue("open").progress?.of
+ )
+ assertEquals(2, byId.getValue("signing").progress?.have)
+ assertEquals(
+ ProposalListUIState.Progress.Of.SIGNATURES,
+ byId.getValue("signing").progress?.of
+ )
+
+ model.viewModelScope.cancel()
+ }
+
+ @Test
+ fun `a session nobody has answered counts zero rather than nothing`() = runBlocking {
+ val sessions = MutableSharedFlow>(replay = 1)
+ sessions.emit(listOf(local("open", approved = true)))
+
+ val model = viewModel(sessions)
+ model.initiate()
+
+ val proposal = model.awaitLoaded().proposals.single()
+
+ assertEquals(0, proposal.noncesOffered)
+ assertEquals(0, proposal.progress?.have)
+
+ model.viewModelScope.cancel()
+ }
+
+ /**
+ * The counts move on their own flow, and far more often than the rows do --
+ * every member's nonce moves one. A count arriving must reach the screen
+ * without the session rows changing at all.
+ */
+ @Test
+ fun `a nonce landing updates the row without the session rows moving`() = runBlocking {
+ val sessions = MutableSharedFlow>(replay = 1)
+ val tallies = MutableSharedFlow>(replay = 1)
+ sessions.emit(listOf(local("open", approved = true)))
+ tallies.emit(listOf(nonces("open", 1)))
+
+ val model = viewModel(sessions, tallies = tallies)
+ model.initiate()
+
+ assertEquals(1, model.awaitLoaded().proposals.single().progress?.have)
+
+ tallies.emit(listOf(nonces("open", 2)))
+
+ model.await("the second nonce") {
+ it is ProposalListUIState.Loaded &&
+ it.proposals.single().noncesOffered == 2
+ }
+
+ model.viewModelScope.cancel()
+ }
+
+ /** A selection survives a count arriving, the same as it survives a row moving. */
+ @Test
+ fun `a count arriving does not reset the filter either`() = runBlocking {
+ val sessions = MutableSharedFlow>(replay = 1)
+ val tallies = MutableSharedFlow>(replay = 1)
+ sessions.emit(mixed)
+ tallies.emit(emptyList())
+
+ val model = viewModel(sessions, tallies = tallies)
+ model.initiate()
+ model.awaitLoaded()
+
+ model.select(ProposalFilter.WAITING_ON_MEMBERS)
+ tallies.emit(listOf(nonces("open", 1)))
+
+ model.await("the count") {
+ it is ProposalListUIState.Loaded &&
+ it.proposals.any { proposal -> proposal.noncesOffered == 1 }
+ }
+
+ assertEquals(ProposalFilter.WAITING_ON_MEMBERS, model.loaded().filter)
+ assertEquals(listOf("open"), model.loaded().visible.map { it.session.id })
+
+ model.viewModelScope.cancel()
+ }
+
private fun ProposalListViewModel.loaded(): ProposalListUIState.Loaded =
assertIs(proposalListUIState)
}