Modern Apps git

← Modern-Apps.git

games/contacts: fix the final five (one real production bug)

Commit
d9c9fd590ff537c10b76a02862877e16262a988e
Author
Vayun Mathur <[email protected]>
When
2026-11-23 12:54 UTC (12 days ago)
Parents
82ce531
Arrows casualRestart timed out on a real app bug: winning
writes recordClearedOnce to the shared DataStore, the
emission re-fires the combine, and a reload sets
generating=true - nextLevel()'s generating guard then
silently dropped the player's tap (fast boxes win the
race, CI doesn't). The guard is removed: the next level
is computed from the on-screen game, so repeat taps are
idempotent. Its timeout also poisoned the next method's
board (the second arrows failure). Fork isolation was a
red herring - this was intra-class.

Contacts: MoveDialog flaked on three concurrent
loadAccounts overwriting the injected accounts (row label
also timing-dependent) - the test reseats accounts while
awaiting any SIM label; the search and SIM-delete tests
carry the same straggler-sync shape (inferred from CI
signatures, not locally reproduced - stated in the code)
and await their fixtures reaching the provider.

Verified under taskset with 5 CPU hogs (MoveDialog
reproduced on original code): arrows 140, contacts 571
green. One production change (arrows guard), no assertion
weakened anywhere.

Diff

contacts/src/test/java/com/vayunmathur/contacts/ui/MoveDialogsPass2Test.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/ui/MoveDialogsPass2Test.kt b/contacts/src/test/java/com/vayunmathur/contacts/ui/MoveDialogsPass2Test.ktindex 8730a36d99..613c42a2f2 100644--- a/contacts/src/test/java/com/vayunmathur/contacts/ui/MoveDialogsPass2Test.kt+++ b/contacts/src/test/java/com/vayunmathur/contacts/ui/MoveDialogsPass2Test.kt@@ -149,18 +149,33 @@ class MoveDialogsPass2Test {                 MoveDialogHost(vm, listOf(7L), onDismiss = { dismissed++ }, onTransferred = { transferred++ })             }         }-        rule.awaitSemantics("account rows listed") {-            rule.onAllNodesWithText("[email protected]", substring = true).fetchSemanticsNodes().isNotEmpty()+        // A straggler loadAccountsInternal from the ViewModel's init+        // (init launches a sync and a separate loadAccounts, and this+        // test's runBlocking sync is a third) can land after the+        // injection above and overwrite accountsState with the+        // provider-derived list โ€” which contains the google account+        // (fake provider) but not the SIM account, so the SIM row+        // vanishes while the google row stays (CI run 598beb90f:+        // no node containing '5' at the click). Re-seat the injected+        // accounts inside the wait, and wait for the SIM row itself,+        // whose label depends on whether the SIM display labels have+        // landed: the raw account name "5", a "SIM n" label, or the+        // on-device string ("On-Device", hyphenated).+        val rowLabels = listOf("On-Device", "SIM", "5")+        fun simRow() = rowLabels.firstNotNullOfOrNull { label ->+            rule.onAllNodesWithText(label, substring = true)+                .fetchSemanticsNodes().takeIf { it.isNotEmpty() }?.let { label }+        }+        rule.awaitSemantics("SIM account row listed") {+            if (vm.accountsState.value != listOf(google, sim)) {+                vm.accountsState.value = listOf(google, sim)+            }+            simRow() != null         }         // Aisha's own (google) account is the disabled source; pick the SIM         // account instead, which switches the dialog to its loss-warning page-        val simRow = rule.onAllNodesWithText("On device", substring = true)-        if (simRow.fetchSemanticsNodes().isNotEmpty()) {-            simRow[0].performClick()-        } else {-            // SIM row may be labelled by its account name "5"-            rule.onAllNodesWithText("5", substring = true)[0].performClick()-        }+        val label = simRow()!!+        rule.onAllNodesWithText(label, substring = true)[0].performClick()         // The SIM destination switches to the loss-warning page ("Move         // anyway"); that switch is a recomposition on the main looper, so         // wait on the page itself rather than idling once.
contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.kt b/contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.ktindex dfe378e3f1..bc49f33ff0 100644--- a/contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.kt+++ b/contacts/src/test/java/com/vayunmathur/contacts/util/ContactViewModelPass2Test.kt@@ -84,7 +84,11 @@ class ContactViewModelPass2Test {      * Waits until [ContactViewModel.contacts] equals [expected], idling Robolectric's      * main looper so the WhileSubscribed stateIn collectors can run.      */-    private fun awaitContacts(vm: ContactViewModel, expected: List<Contact>) {+    private fun awaitContacts(+        vm: ContactViewModel,+        expected: List<Contact>,+        source: List<Contact> = expected,+    ) {         // subscribing is what starts the WhileSubscribed upstreams         val job = kotlinx.coroutines.CoroutineScope(Dispatchers.Default).launch {             vm.contacts.collect {}@@ -92,6 +96,18 @@ class ContactViewModelPass2Test {         try {             val deadline = System.nanoTime() + 15_000_000_000L             while (System.nanoTime() < deadline) {+                // The ViewModel's init sync (and the runBlocking sync+                // below) publish the provider's list whenever their IO+                // lands; a straggler publish can land after the fixture+                // was injected and erase it, leaving contacts == [] for+                // the rest of the wait (CI run 598beb90f: expected the+                // fixture, was [], after the full 15s). Re-seat the+                // fixture whenever it has been overwritten: the number+                // of in-flight syncs is finite and none is triggered+                // after this point, so the fixture then sticks.+                if (vm.allContactsState.value != source) {+                    vm.allContactsState.value = source+                }                 org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle()                 if (vm.contacts.value == expected) return                 Thread.sleep(10)@@ -111,18 +127,19 @@ class ContactViewModelPass2Test {         val bob = contact(2, "Bob")         vm.allContactsState.value = listOf(aisha, bob) -        awaitContacts(vm, listOf(aisha, bob))+        val fixture = listOf(aisha, bob)+        awaitContacts(vm, fixture, source = fixture)         vm.setSearchQuery("aisha")         assertEquals("aisha", vm.searchQuery.value)-        awaitContacts(vm, listOf(aisha))+        awaitContacts(vm, listOf(aisha), source = fixture)         // multi-token: name token + phone fragment must both match         vm.setSearchQuery("bob 555")-        awaitContacts(vm, listOf(bob))+        awaitContacts(vm, listOf(bob), source = fixture)         // searching by email domain fragment works too         vm.setSearchQuery("aisha@x")-        awaitContacts(vm, listOf(aisha))+        awaitContacts(vm, listOf(aisha), source = fixture)         vm.setSearchQuery("no-such-person")-        awaitContacts(vm, emptyList())+        awaitContacts(vm, emptyList(), source = fixture)         assertEquals(aisha, vm.getContact(1))         assertEquals(bob, vm.getContact(2))         assertNull(vm.getContact(999))
contacts/src/test/java/com/vayunmathur/contacts/util/ViewModelDeleteCalendarW5Test.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/util/ViewModelDeleteCalendarW5Test.kt b/contacts/src/test/java/com/vayunmathur/contacts/util/ViewModelDeleteCalendarW5Test.ktindex 345561afb8..b5a3a0dd1a 100644--- a/contacts/src/test/java/com/vayunmathur/contacts/util/ViewModelDeleteCalendarW5Test.kt+++ b/contacts/src/test/java/com/vayunmathur/contacts/util/ViewModelDeleteCalendarW5Test.kt@@ -18,6 +18,7 @@ import com.vayunmathur.contacts.data.Name import com.vayunmathur.contacts.data.PhoneNumber import com.vayunmathur.contacts.data.SIM_ACCOUNT_TYPE import com.vayunmathur.contacts.data.SimProviderPass2Test+import com.vayunmathur.testsupport.awaitWithMainIdle import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals@@ -46,6 +47,12 @@ class ViewModelDeleteCalendarW5Test {             ContactsContract.AUTHORITY, FakeProviderPass2Test.FakeContactsProvider()         )         ShadowContentResolver.registerProviderInternal("icc", SimProviderPass2Test.FakeIccProvider())+        // The fake's delete record is a class-wide static: a previous+        // method's (or class's) delete would satisfy the assert below+        // without this method deleting anything, and a stale record+        // cannot distinguish "not yet" from "never".+        SimProviderPass2Test.FakeIccProvider.lastDeleteUri = null+        SimProviderPass2Test.FakeIccProvider.lastDeleteSelection = null         try {             androidx.work.WorkManager.initialize(app, androidx.work.Configuration.Builder().build())         } catch (_: IllegalStateException) {@@ -83,11 +90,16 @@ class ViewModelDeleteCalendarW5Test {     }      private fun assertNotNullDelete() {-        assertTrue(+        // deleteContact launches the delete on viewModelScope/IO;+        // settle()'s syncFromSystem is a concurrent IO call, not a+        // barrier for that launch, so under load the sync can finish+        // before the delete coroutine is even scheduled (CI run+        // 598beb90f failed in 0.024s with no delete recorded). Wait+        // on the delete reaching the provider itself.+        awaitWithMainIdle("SIM delete should reach the ICC provider", maxSteps = 30_000) {             SimProviderPass2Test.FakeIccProvider.lastDeleteUri != null ||-                SimProviderPass2Test.FakeIccProvider.lastDeleteSelection != null,-            "SIM delete should reach the ICC provider",-        )+                SimProviderPass2Test.FakeIccProvider.lastDeleteSelection != null+        }     }      @Test
games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.ktindex ddc8047537..116235672b 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/platform/ArrowsViewModel.kt@@ -352,12 +352,14 @@ class ArrowsViewModel(application: Application) : AndroidViewModel(application),      * from [ArrowsUiState.level]: [load] bumps the latter as soon as the new level is requested,      * while the cleared board โ€” and its button โ€” are still showing, so a quick second tap used to      * read the already-advanced level and skip one. Counting from the loaded board makes a repeat-     * tap write the same level again, and the guard ignores taps once loading has started or the-     * board on screen is not a cleared one.+     * tap write the same level again, so there is deliberately no `generating` guard: a tap that+     * lands while the won board is being reloaded (a store edit re-emits the session and+     * [load] runs again over the same board) must not be dropped, and once the next board+     * replaces this one its not-won state is what stops further taps.      */     override fun nextLevel() {         val state = _uiState.value-        if (state.mode == GameMode.DAILY || state.generating) return+        if (state.mode == GameMode.DAILY) return         val game = state.game ?: return         if (!game.isWon) return         lastBlockedId = -1