package com.cottondb.contacts import android.app.Application import androidx.activity.ComponentActivity import androidx.compose.material3.Text import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag import androidx.compose.ui.test.assertTextEquals import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import kotlinx.coroutines.CompletableDeferred import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** * CONTACTS ANDROID AT SIZE (walk on an emulator with 1,500 invented contacts, 28 Nov 2026): the home stands on its last * answer while the next is asked (it blanked for half a second under each letter typed, and on each back from a * person), and You follows a save into the account that stopped (it said "Not synced yet" with a Sync now that could * not move it, while the core asked for Sign in again). */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h860dp-mdpi", application = Application::class) class ContactsAtSizeTest { @get:Rule val compose = createAndroidComposeRule() private fun json(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject private fun home(title: String) = json("""{"title":"$title"}""") @Test fun the_home_stands_on_its_last_answer_while_a_new_search_is_asked() { val kept = mutableMapOf() val second = CompletableDeferred() var query by mutableStateOf("") compose.setContent { val (answer, _) = answerOf(0, kept, query, last = "home") { if (query.isEmpty()) home("Everyone") else second.await() } Text(answer?.let { it["title"].toString().trim('"') } ?: "(blank)", Modifier.testTag("shown")) } compose.onNodeWithTag("shown").assertTextEquals("Everyone") query = "Arlo" compose.waitForIdle() compose.onNodeWithTag("shown").assertTextEquals("Everyone") second.complete(home("Found Arlo")) compose.waitUntil(2_000) { kept["home"]?.get("title").toString().contains("Found") } compose.onNodeWithTag("shown").assertTextEquals("Found Arlo") } @Test fun a_screen_opened_again_draws_its_last_answer_at_once() { val kept = mutableMapOf("home" to home("Everyone")) val never = CompletableDeferred() compose.setContent { val (answer, _) = answerOf(0, kept, "all", last = "home") { never.await() } Text(answer?.let { it["title"].toString().trim('"') } ?: "(blank)", Modifier.testTag("shown")) } compose.onNodeWithTag("shown").assertTextEquals("Everyone") } @Test fun a_screen_without_last_still_starts_blank_for_a_new_key() { val never = CompletableDeferred() compose.setContent { val (answer, _) = answerOf(0, mutableMapOf(), "person-b") { never.await() } Text(answer?.toString() ?: "(blank)", Modifier.testTag("shown")) } compose.onNodeWithTag("shown").assertTextEquals("(blank)") } @Test fun a_stopped_save_signs_you_out_and_says_why() { val failed = json("""{"stand":"failed","backup":{"sent":20,"of":7884,"line":"Cotton Contacts cannot be reached right now. Try again when you are online."}}""") assertFalse("Sign in again, as the core's account offers", YouStand.signedIn(held = true, home = failed)) assertEquals("Cotton Contacts cannot be reached right now. Try again when you are online.", YouStand.saving(failed)) } @Test fun the_save_under_way_is_said_in_place_of_the_sync_line() { val saving = json("""{"stand":"backing_up","backup":{"sent":158,"of":7884,"line":"Syncing your data to your account… 158 of 7,884"}}""") assertTrue(YouStand.signedIn(held = true, home = saving)) assertEquals("Syncing your data to your account… 158 of 7,884", YouStand.saving(saving)) val connected = json("""{"stand":"connected","backup":{"sent":0,"of":0,"line":""}}""") assertTrue(YouStand.signedIn(held = true, home = connected)) assertNull(YouStand.saving(connected)) assertFalse("no session, not signed in", YouStand.signedIn(held = false, home = connected)) } }