Cotton code
179 lines · 13.7 KB · KotlinRaw
package com.cottondb.messages.warmimport android.app.Applicationimport androidx.compose.foundation.lazy.LazyListStateimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onAllNodesWithTagimport androidx.compose.ui.test.onAllNodesWithTextimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport com.cottondb.messages.chat.Itemimport com.cottondb.messages.chat.ThreadPageimport com.cottondb.messages.chat.ThreadStateimport com.cottondb.messages.chat.Wordsimport com.cottondb.messages.chat.toNewestimport com.cottondb.shell.core.CottonCoreimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * THE CONVERSATION'S FOOT as messages land (the emulator's fault, kept): a message sent or arriving while the * conversation stands at its newest is drawn in full above the writing box, not left below the screen. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class)class ConversationFootTest {    @get:Rule val compose = createComposeRule()    private val words = Words { set, key -> "$set:$key" }    private val threadWords = ThreadWordsWarm("Back", "Call", "Video call", "Not sent", "Try again", "You", "Their page")    private fun j(text: String) = CottonCore.json.parseToJsonElement(text).jsonObject    private fun msg(id: String, mine: Boolean, text: String) =        """{"item":"bubble","message":"$id","mine":$mine,"kind":"text","text":"$text","head":true,"tail":true,"time":"19:00","pinned":false,"actions":["reply"]${if (mine) ""","tick":"sent"""" else ""}}"""    @Test    fun a_message_landing_at_the_foot_is_drawn_whole() {        val items = (1..40).joinToString(",") { msg("m$it", it % 2 == 0, "Message number $it") }        val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Rose Martin","picture":{"initials":"RM","tint":1},"status":""},"items":[$items]}"""), words))        val list = LazyListState()        val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        compose.setContent { WarmTheme(night = false) { Conversation(t, list, acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } }        compose.waitForIdle()        compose.onNodeWithTag("bubble-m40").assertIsDisplayed()        // Sent (as ChatModel.landed does it), then an arrival merged after it (as onChanged does).        for (id in listOf("s1", "s2")) {            compose.runOnIdle {                t.sent(Item.of(j(msg(id, true, "Just sent $id")), words))                list.toNewest()            }            compose.waitForIdle()            compose.onNodeWithTag("bubble-$id").assertIsDisplayed()            assertEquals(0, list.firstVisibleItemIndex)            assertEquals(0, list.firstVisibleItemScrollOffset)        }    }    /** Sideways with the keyboard up the page above the writing box is a sliver: the header gives way to the field. */    @Test    @Config(qualifiers = "w800dp-h240dp")    fun where_the_page_is_a_sliver_the_header_steps_aside_for_the_field() {        val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Rose Martin","picture":{"initials":"RM","tint":1},"status":"",            "calls":[{"video":false,"label":"Call"}]},"items":[${msg("m1", false, "Are we still on?")}]}"""), words))        val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        compose.setContent {            WarmTheme(night = false) {                Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {                    WritingBox(com.cottondb.messages.chat.ComposerState(), com.cottondb.messages.chat.Compose.of(j("""{"thread":"t1","placeholder":"Message Rose","button":"mic","buttonLabel":"Hold","attach":true,"attachLabel":"Attach"}""")),                        WritingWords("Send", "Attach", "Hold"), com.cottondb.messages.chat.VoiceRecorder(androidx.compose.ui.platform.LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {})                }            }        }        compose.waitForIdle()        compose.onNodeWithTag("chat-field").assertIsDisplayed()        compose.onNodeWithTag("chat-back").assertDoesNotExist()    }    /**     * FEEDBACK-2 A (owner, 23 Nov 2026): writing to a number not on Cotton goes to their WhatsApp through his Mac. The     * header says "via WhatsApp" under the name before anything is sent; a text on its way says where it stands in the     * core's words ("Waiting to send"; Android drew only the tick's "Sending…"), a failed one its reason with Retry; and     * in a chat holding both kinds each message says which way it went.     */    @Test    fun a_chat_to_whatsapp_says_where_its_words_go_and_where_each_stands() {        val pending = """{"item":"bubble","message":"outbox:o1","mine":true,"kind":"text","text":"On my way","head":true,"tail":true,"time":"19:01","pinned":false,"tick":"sending","state":"Waiting to send","actions":["copy"]}"""        val failed = """{"item":"bubble","message":"outbox:o2","mine":true,"kind":"text","text":"Are you there?","head":true,"tail":true,"time":"19:02","pinned":false,"tick":"failed","state":"Not sent: This number is not on WhatsApp","actions":["retry","copy"]}"""        val old = """{"item":"bubble","message":"m1","mine":false,"kind":"text","text":"Leaving now","head":true,"tail":true,"time":"18:00","pinned":false,"actions":["copy"],"place":{"id":"whatsapp","mark":"via WhatsApp"}}"""        val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Noor Example","picture":{"initials":"NE","tint":1},"status":"","place":{"id":"whatsapp","mark":"via WhatsApp"}},"items":[$old,$pending,$failed]}"""), words))        var retried = ""        val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, { retried = it.id }, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        compose.setContent { WarmTheme(night = false) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } }        compose.waitForIdle()        compose.onNodeWithTag("thread-place", useUnmergedTree = true).assertIsDisplayed()        assertEquals("the header and the old message", 2, compose.onAllNodesWithText("via WhatsApp", useUnmergedTree = true).fetchSemanticsNodes().size)        compose.onNodeWithTag("state-outbox:o1", useUnmergedTree = true).assertIsDisplayed()        compose.onNodeWithText("Waiting to send", useUnmergedTree = true).assertIsDisplayed()        compose.onNodeWithText("Not sent: This number is not on WhatsApp · Try again", useUnmergedTree = true).assertIsDisplayed()        compose.onNodeWithTag("place-m1", useUnmergedTree = true).assertExists()        compose.onNodeWithTag("retry-outbox:o2").performClick()        assertEquals("outbox:o2", retried)    }    /**     * FEEDBACK-3 (owner, 23 Nov 2026): a picture, a video or a voice note sent to WhatsApp through the Mac is drawn at     * once as its own kind of bubble from the phone's copy (core 2dcdf0d411: `outbox:<id>` bubbles of kind photo,     * video and voice), and says where it stands exactly as a text does: "Waiting to send" under a captioned picture     * and under a voice note, and a failed video its reason with Try again, which retries that one.     */    @Test    fun pictures_videos_and_voice_notes_to_whatsapp_say_where_they_stand() {        val preview = "/9j/4AAQSkZJRgABAQAAAQABAAD/2Q=="        val photo = """{"item":"bubble","message":"outbox:p1","mine":true,"kind":"photo","text":"The beach this morning","head":true,"tail":true,"time":"17:01","pinned":false,"tick":"sending","state":"Waiting to send","actions":["copy"],            "media":[{"index":0,"kind":"photo","width":1200.0,"height":900.0,"preview":"$preview"}]}"""        val voice = """{"item":"bubble","message":"outbox:v1","mine":true,"kind":"voice","head":true,"tail":true,"time":"17:02","pinned":false,"tick":"sending","state":"Waiting to send","actions":[],            "media":[{"index":0,"kind":"voice","length":"0:04","durationMs":4200,"bars":[0.2,0.6,1.0,0.4]}]}"""        val video = """{"item":"bubble","message":"outbox:c1","mine":true,"kind":"video","head":true,"tail":true,"time":"17:03","pinned":false,"tick":"failed","state":"Not sent: This file is too large for WhatsApp.","actions":["retry"],            "media":[{"index":0,"kind":"video","width":1280.0,"height":720.0,"length":"0:09","durationMs":9000,"preview":"$preview"}]}"""        val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Noor Example","picture":{"initials":"NE","tint":1},"status":"","place":{"id":"whatsapp","mark":"via WhatsApp"}},"items":[$photo,$voice,$video]}"""), words))        var retried = ""        val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, { retried = it.id }, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        compose.setContent { WarmTheme(night = false) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } }        compose.waitForIdle()        assertEquals("the picture and the video, each drawn as its tile", 2, compose.onAllNodesWithTag("album-0", useUnmergedTree = true).fetchSemanticsNodes().size)        compose.onNodeWithText("The beach this morning", useUnmergedTree = true).assertIsDisplayed()        compose.onNodeWithTag("state-outbox:p1", useUnmergedTree = true).assertIsDisplayed()        compose.onNodeWithTag("voice-outbox:v1", useUnmergedTree = true).assertExists()        compose.onNodeWithTag("state-outbox:v1", useUnmergedTree = true).assertExists()        assertEquals("under the picture and under the voice note", 2, compose.onAllNodesWithText("Waiting to send", useUnmergedTree = true).fetchSemanticsNodes().size)        compose.onNodeWithText("Not sent: This file is too large for WhatsApp. · Try again", useUnmergedTree = true).assertExists()        compose.onNodeWithTag("retry-outbox:c1").performClick()        assertEquals("outbox:c1", retried)    }    /**     * FEEDBACK-3 (owner, 23 Nov 2026; CONTRACT "Replies and reactions"): his reaction on their message, on its way to     * WhatsApp through the Mac, says where it stands under their message as his own texts do: "Reaction waiting to     * send", and a failed one its reason with Try again, which retries that message's reaction. Before, a state was     * drawn only under his own bubbles.     */    @Test    fun a_reaction_on_its_way_to_whatsapp_says_where_it_stands_under_their_message() {        val waiting = """{"item":"bubble","message":"m1","mine":false,"kind":"text","text":"Leaving now","head":true,"tail":true,"time":"18:00","pinned":false,            "actions":["reply","react","copy"],"reactions":[{"emoji":"👍","count":1,"mine":true}],"state":"Reaction waiting to send"}"""        val failed = """{"item":"bubble","message":"m2","mine":false,"kind":"text","text":"See you there","head":true,"tail":true,"time":"18:01","pinned":false,            "actions":["retry","reply","react","copy"],"reactions":[{"emoji":"❤️","count":1,"mine":true}],"state":"Reaction not sent: WhatsApp did not take it: offline"}"""        val reply = """{"item":"bubble","message":"outbox:r1","mine":true,"kind":"text","text":"On my way","head":true,"tail":true,"time":"18:02","pinned":false,"tick":"sending",            "state":"Waiting to send","actions":["copy"],"reply":{"message":"m1","sender":"Noor Example","line":"Leaving now"}}"""        val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Noor Example","picture":{"initials":"NE","tint":1},"status":"","place":{"id":"whatsapp","mark":"via WhatsApp"}},"items":[$waiting,$failed,$reply]}"""), words))        var retried = ""        val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, { retried = it.id }, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        compose.setContent { WarmTheme(night = false) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } }        compose.waitForIdle()        compose.onNodeWithTag("state-m1", useUnmergedTree = true).assertIsDisplayed()        compose.onNodeWithText("Reaction waiting to send", useUnmergedTree = true).assertIsDisplayed()        compose.onNodeWithText("Reaction not sent: WhatsApp did not take it: offline · Try again", useUnmergedTree = true).assertIsDisplayed()        compose.onNodeWithTag("retry-m2").performClick()        assertEquals("m2", retried)        // His reply on its way quotes the original at once, from the core's `reply`.        compose.onNodeWithText("Noor Example · Leaving now", useUnmergedTree = true).assertExists()        compose.onNodeWithTag("state-outbox:r1", useUnmergedTree = true).assertExists()    }    /** Upright, the header stands as drawn. */    @Test    fun upright_the_header_stands() {        val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Rose Martin","picture":{"initials":"RM","tint":1},"status":""},"items":[${msg("m1", false, "Are we still on?")}]}"""), words))        val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope()))        compose.setContent { WarmTheme(night = false) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } }        compose.waitForIdle()        compose.onNodeWithTag("chat-back").assertIsDisplayed()    }}