Cotton code
99 lines · 4.9 KB · KotlinRaw
package com.cottondb.messages.warmimport android.app.Applicationimport androidx.activity.ComponentActivityimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport com.cottondb.messages.chat.Avatarimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * A group imported from WhatsApp, its name tapped: there is no Cotton group behind it, so `chat.group.info` refuses. * The page stood blank with no Back of its own (Back left Chat). Now it stands on what the thread says (its picture, * name and status) with Back, and no empty Members heading. Invented names. * The members' count is said as the conversation's header says it (one_member_is_said_as_one). */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-mdpi", application = Application::class)class GroupPageTest {    @get:Rule val compose = createAndroidComposeRule<ComponentActivity>()    @Test    fun a_group_with_no_cotton_answer_stands_with_back() {        var back = 0        compose.setContent {            WarmTheme(night = false) {                GroupPage(                    GroupView("Hill Walkers", 0, 64, "member", emptySet(), emptyList(), emptyList()), Avatar("HW", 1, null),                    { key -> if (key.startsWith("members:")) "${key.removePrefix("members:")} members" else key }, "Back", "Close",                    onBack = { back++ }, onAdd = {}, onRename = {}, onPicture = {}, onLeave = {}, onMember = {}, status = "65 members",                )            }        }        compose.waitForIdle()        compose.onNodeWithTag("group-title").assertIsDisplayed()        compose.onNodeWithText("65 members").assertIsDisplayed()        assertEquals(0, compose.onAllNodesWithTextCount("app.group.members"))        compose.onNodeWithTag("group-back").performClick()        compose.runOnUiThread { compose.activity.onBackPressedDispatcher.onBackPressed() }        compose.waitForIdle()        assertEquals(2, back)    }    // Seen on an emulator (21 Nov 2026): the group's page said "1 members" once the other person had left, while the    // conversation's header said "1 member". The page now counts in the header's words, one and more.    @Test    fun one_member_is_said_as_one() {        val lines = com.cottondb.shell.core.Lines.of(            com.cottondb.shell.core.CottonCore.json.parseToJsonElement(                """{"sets":{"chat":{"app":{"person":{"members":"{count} members"},"thread":{"members":{"one":"{n} member","other":"{n} members"}}}}}}""",            ).let { it as kotlinx.serialization.json.JsonObject },            "Chat",        )        val say = { n: Int -> membersLine(n) { k, fills -> lines.line("chat", k, fills) } }        assertEquals("1 member", say(1))        assertEquals("2 members", say(2))        assertEquals("0 members", say(0))    }    // An agent in a chat (CONTRACT "An agent in a chat"): the agent stands among the group's people under its own    // heading, with its mark, its name and whose agent it is; Add an agent is offered to a member; its person taps it.    @Test    fun an_agent_stands_in_the_member_list_marked_as_an_agent() {        var added = 0        var tapped: AgentSeat? = null        val g = GroupView(            "Feedback", 2, 64, "member", setOf("send", "leave"),            listOf(Member("+15550100001", "Asha", you = true, admin = true, out = null), Member("+15550100002", "Ben", you = false, admin = false, out = null)), emptyList(),            agents = listOf(AgentSeat("mem-1", "Claude", "Your agent", yours = true, canRemove = true)), canAgents = true,        )        compose.setContent {            WarmTheme(night = false) {                GroupPage(                    g, Avatar("F", 1, null), { it }, "Back", "Close",                    onBack = {}, onAdd = {}, onRename = {}, onPicture = {}, onLeave = {}, onMember = {},                    onAddAgent = { added++ }, onAgent = { tapped = it },                )            }        }        compose.waitForIdle()        compose.onNodeWithText("app.group.agents", ignoreCase = true).assertExists()        compose.onNodeWithText("Your agent", useUnmergedTree = true).assertExists()        compose.onNodeWithTag("group-agent-mem-1").performClick()        compose.onNodeWithTag("group-add-agent").performClick()        compose.waitForIdle()        assertEquals("mem-1", tapped?.agent)        assertEquals(1, added)    }    private fun androidx.compose.ui.test.junit4.AndroidComposeTestRule<*, *>.onAllNodesWithTextCount(text: String) =        onAllNodes(androidx.compose.ui.test.hasText(text, ignoreCase = true)).fetchSemanticsNodes().size}