Cotton code
126 lines · 8.2 KB · KotlinRaw
package com.cottondb.messages.chatimport com.cottondb.shell.core.CottonCoreimport kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.runBlockingimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * Seen on two emulators (21 Nov 2026): a person blocked from their page, then Back: the conversation still offered * Call and Video (the core answers none for a blocked person) until it was closed and opened again. An act from a * person's page now reads the conversation's top again. Invented numbers. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], application = android.app.Application::class)class ActOnTest {    private val words = Words { set, key -> "$set:$key" }    private fun page(calls: String) = ThreadPage.of(CottonCore.json.parseToJsonElement("""        {"thread":"t1","header":{"name":"+15550100123","picture":{"initials":"","tint":1},"source":"cotton","status":"","calls":$calls},         "items":[{"item":"bubble","message":"m1","mine":false,"kind":"text","text":"Invented words","head":true,"tail":true,"time":"09:01",                   "pinned":false,"actions":["reply"]}]}    """).jsonObject, words)    @Test    fun blocking_from_their_page_takes_call_off_the_conversation() = runBlocking {        val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob()))        model.prefetched["t1"] = page("""[{"video":false,"label":"Call"},{"video":true,"label":"Video call"}]""")        model.open("t1", unread = false)        assertEquals(2, model.thread?.header?.calls?.size)        var blocked = false        model.actOn("t1", work = { blocked = true }, read = { page("[]") })        assert(blocked)        assertEquals(0, model.thread?.header?.calls?.size)    }    // Seen on an emulator (21 Nov 2026): a group renamed from its page, then Back: the header said the new name, but    // "You changed the group name…" stood in the conversation only once it was closed and opened again.    @Test    fun a_group_renamed_from_its_page_shows_its_line_on_going_back() = runBlocking {        val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob()))        fun group(title: String, events: String) = ThreadPage.of(CottonCore.json.parseToJsonElement("""            {"thread":"g1","header":{"name":"$title","picture":{"initials":"LP","tint":1},"source":"cotton","status":"2 members","group":true,"calls":[]},             "items":[{"item":"event","message":"e1","text":"You created the group"}$events]}        """).jsonObject, words)        model.prefetched["g1"] = group("Lake Picnic", "")        model.open("g1", unread = false)        model.actOn("g1", work = {}, read = { group("Lake Day Out", """,{"item":"event","message":"e2","text":"You changed the group name to “Lake Day Out”"}""") })        assertEquals("Lake Day Out", model.thread?.header?.name)        assertEquals(listOf("You created the group", "You changed the group name to “Lake Day Out”"), model.thread?.items?.filterIsInstance<Item.Event>()?.map { it.text })    }    // Seen on an emulator (21 Nov 2026): Leave group from the group's page, then Back: the writing box still stood    // under "You left" until the conversation was closed and opened again; the core answers a read-only line instead.    @Test    fun a_group_left_from_its_page_takes_the_writing_box_away() = runBlocking {        val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob()))        fun group(foot: String) = ThreadPage.of(CottonCore.json.parseToJsonElement("""            {"thread":"g1","header":{"name":"Lake Picnic","picture":{"initials":"LP","tint":1},"source":"cotton","status":"2 members","group":true,"calls":[]},             "items":[{"item":"event","message":"e1","text":"You created the group"}]$foot}        """).jsonObject, words)        model.prefetched["g1"] = group(""","composer":{"thread":"g1","placeholder":"Message","button":"mic"}""")        model.open("g1", unread = false)        assert(model.composer("g1").answer != null)        model.actOn("g1", work = {}, read = { group(""","readOnly":"You're no longer in this group."""") })        assertEquals(null, model.composer("g1").answer)        assertEquals("You're no longer in this group.", model.thread?.readOnly)    }    // Seen on three emulators (21 Nov 2026): Tara, open on the group, was removed by Meera: "removed you" came in, yet    // the writing box stood (its Send dead) until the conversation was closed and opened again.    @Test    fun removed_by_someone_else_while_open_the_writing_box_goes() = runBlocking {        val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob()))        fun group(foot: String, events: String = "") = ThreadPage.of(CottonCore.json.parseToJsonElement("""            {"thread":"g1","header":{"name":"Book club","picture":{"initials":"BC","tint":1},"source":"cotton","status":"3 members","group":true,"calls":[]},             "items":[{"item":"event","message":"e1","text":"+15550100001 created the group"}$events]$foot}        """).jsonObject, words)        model.prefetched["g1"] = group(""","composer":{"thread":"g1","placeholder":"Message","button":"mic"}""")        model.open("g1", unread = false)        assert(model.composer("g1").answer != null)        model.follow { group(""","readOnly":"You're no longer in this group."""", """,{"item":"event","message":"e2","text":"+15550100001 removed you"}""") }        assertEquals(null, model.composer("g1").answer)        assertEquals("You're no longer in this group.", model.thread?.readOnly)    }    // Seen on an emulator (21 Nov 2026): "novel" searched on Chats, a message tapped: the chat opened with its search    // bar, but the bar's field stood empty, so it never said "1 of 2" and read as a search not yet begun.    @Test    fun a_match_opened_from_chats_keeps_its_words_in_the_chats_own_bar() {        val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob()))        model.query = "novel"        model.openFound(Found("t1", "m1", "+15550100123", null, false, null, true, "Which novel is it", listOf(6..10), "7:28 pm"))        assertEquals("novel", model.findText)    }    // Seen on two emulators (21 Nov 2026): Meera called Kabir from their chat and hung up after 32 s (and later Tara    // declined her call): back on the conversation no call line stood, until it was closed and opened again.    @Test    fun a_call_ended_puts_its_line_in_the_conversation_behind() = runBlocking {        val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob()))        fun chat(events: String) = ThreadPage.of(CottonCore.json.parseToJsonElement("""            {"thread":"t1","header":{"name":"+15550100123","picture":{"initials":"","tint":1},"source":"cotton","status":"","calls":[]},             "items":[{"item":"bubble","message":"m1","mine":true,"kind":"text","text":"Invented words","head":true,"tail":true,"time":"20:08","pinned":false,"actions":["reply"],"tick":"delivered"}$events]}        """).jsonObject, words)        model.prefetched["t1"] = chat("")        model.open("t1", unread = false)        model.callEnded { chat(""",{"item":"event","message":"c1","text":"Voice call · 0:32"}""") }        assertEquals(listOf("Voice call · 0:32"), model.thread?.items?.filterIsInstance<Item.Event>()?.map { it.text })    }    @Test    fun a_refused_act_is_said_and_the_top_still_read() = runBlocking {        val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob()))        model.prefetched["t1"] = page("""[{"video":false,"label":"Call"}]""")        model.open("t1", unread = false)        model.actOn("t1", work = { throw IllegalStateException("Invented refusal") }, read = { page("[]") })        assertEquals("Invented refusal", model.said)        assertEquals(0, model.thread?.header?.calls?.size)    }}