package com.cottondb.messages.chat import com.cottondb.shell.core.CottonCore import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertNull import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], application = android.app.Application::class) class ChatModelTest { private val words = Words { set, key -> "$set:$key" } // Seen on an emulator (21 Nov 2026): a message held in one chat, then another chat opened (an import landed); // the held menu stayed over the new chat and Back closed the chat instead of the menu. @Test fun a_held_menu_goes_when_another_chat_opens() { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) val page = ThreadPage.of(CottonCore.json.parseToJsonElement(""" {"thread":"t1","header":{"name":"Kavya Rao","picture":{"initials":"KR","tint":2},"source":"cotton","status":""}, "items":[{"item":"bubble","message":"m1","mine":false,"kind":"text","text":"Invented words","head":true,"tail":true,"time":"09:01", "pinned":false,"actions":["reply","react","copy"]}]} """).jsonObject, words) val m = page.items.filterIsInstance().single() model.held = m to MessageMenu(emptyList(), emptyList()) model.attaching = true model.open("t2", unread = false) assertNull(model.held) assertNull(model.selected) assert(!model.attaching) } // Seen on an emulator (21 Nov 2026): Message info tapped on words just sent in a group, and nothing came: the menu // went, and the question asked in the menu's time went with it. It is asked in the model's time now, so its // answer (here, the refusal of a phone with no core) arrives after the menu has gone. @Test fun message_info_is_answered_after_the_menu_goes() { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) val page = ThreadPage.of(CottonCore.json.parseToJsonElement(""" {"thread":"g1","header":{"name":"Lake Picnic","picture":{"initials":"LP","tint":2},"source":"cotton","status":"","group":true}, "items":[{"item":"bubble","message":"m1","mine":true,"kind":"text","text":"Invented words","head":true,"tail":true,"time":"09:01", "pinned":false,"actions":["reply"]}]} """).jsonObject, words) model.held = page.items.filterIsInstance().single() to MessageMenu(emptyList(), emptyList()) model.info("m1") assertNull(model.held) org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle() org.junit.Assert.assertEquals("chat: no core", model.said) } // Seen on emulator-5558 (22 Nov 2026, FEEDBACK1 follow-up): "zzqq" searched, a message landed meanwhile so the list // was read again for the search, and once the search was cleared Chats still said "No chat or message by that // name" over no chats until All was tapped. @Test fun a_cleared_search_never_leaves_its_empty_list() { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) val searched = ChatList.of(CottonCore.json.parseToJsonElement(""" {"filter":"all","search":"zzqq","rows":[],"fan":[],"filters":[],"words":{"title":"Chats","search":"Search","empty":"No chat or message by that name."}} """).jsonObject, words) org.junit.Assert.assertEquals("zzqq", searched.search) model.search("zzqq") model.list = searched model.search("") assertNull(model.list?.empty) org.junit.Assert.assertEquals("", model.query) // A plain list stands as it was. val plain = searched.copy(search = null) model.list = plain model.search("") org.junit.Assert.assertSame(plain, model.list) } // The owner (Comms build 19, 22 Nov 2026, FEEDBACK1 item 4): no way to write words with pictures he attached; // Android sent each picked picture at once, with whatever the field held. Picked pictures now wait in the writing // box, and Send takes them with what is written as their caption. @Test fun picked_pictures_wait_for_send_and_go_with_the_caption() { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) val page = ThreadPage.of(CottonCore.json.parseToJsonElement(""" {"thread":"t1","header":{"name":"Kavya Rao","picture":{"initials":"KR","tint":2},"source":"cotton","status":""},"items":[]} """).jsonObject, words) model.thread = ThreadState("t1", page) val c = model.composer("t1") val picture = CottonCore.json.parseToJsonElement("""{"kind":"photo","path":"/invented/beach.jpg","mime":"image/jpeg"}""").jsonObject model.stage(listOf(picture, picture)) org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle() org.junit.Assert.assertEquals("two pictures wait, nothing sent", 2, c.staged.size) assertNull("nothing was asked to be sent", model.said) c.field.edit { replace(0, length, "The beach at six") } model.send() org.junit.Assert.assertEquals("Send takes the waiting pictures", 0, c.staged.size) org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle() // With no core the send is refused: it was a send of the pictures (not of the words alone). org.junit.Assert.assertEquals("chat: no core", model.said) // The ✕ takes waiting pictures out, nothing sent. model.said = null model.stage(listOf(picture)) model.unstage() org.junit.Assert.assertEquals(0, c.staged.size) } // Seen on emulator-5558 (23 Nov 2026, FEEDBACK-3): a picture picked for a WhatsApp chat, a caption written, Send // refused (the server did not yet know the outbox's new columns): the words stayed but the picture was gone, to // be picked again. A refused send now puts the picked pictures back in the writing box beside the words. @Test fun a_refused_send_gives_the_picked_pictures_back() { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) val page = ThreadPage.of(CottonCore.json.parseToJsonElement(""" {"thread":"t1","header":{"name":"Noor Example","picture":{"initials":"NE","tint":1},"source":"whatsapp","status":""},"items":[]} """).jsonObject, words) model.thread = ThreadState("t1", page) val c = model.composer("t1") val picture = CottonCore.json.parseToJsonElement("""{"kind":"photo","path":"/invented/beach.jpg","mime":"image/jpeg"}""").jsonObject model.stage(listOf(picture)) c.field.edit { replace(0, length, "The beach this morning") } model.send() org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle() org.junit.Assert.assertEquals("chat: no core", model.said) org.junit.Assert.assertEquals("the picture waits again", listOf(picture), c.staged.toList()) org.junit.Assert.assertEquals("with his words", "The beach this morning", c.field.text.toString()) } // FEEDBACK-4 B2 (owner on iPhone, Comms 26, 23 Nov 2026): a chat deleted from the list stayed on it. Android took // it off only once the core had answered and the list was asked again; it leaves the list at the tap now. @Test fun a_deleted_chat_leaves_the_list_at_once() { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) model.list = ChatList.of(CottonCore.json.parseToJsonElement(""" {"filter":"all","rows":[ {"thread":"t1","name":"Asha Rao","group":false,"source":"whatsapp","picture":{"initials":"AR","tint":3},"when":"09:12","unread":0, "markedUnread":false,"pinned":false,"muted":false,"archived":false,"leading":[],"trailing":[]}, {"thread":"t2","name":"Ravi Iyer","group":false,"source":"cotton","picture":{"initials":"RI","tint":1},"when":"09:10","unread":0, "markedUnread":false,"pinned":false,"muted":false,"archived":false,"leading":[],"trailing":[]} ],"fan":[],"filters":[],"words":{"title":"Chats","search":"Search","empty":"No chats yet."}} """).jsonObject, words) val asha = model.list!!.rows.first { it.id == "t1" } model.rowAct(asha, Act("deleteChat", "Delete", confirm = "Delete your chat with Asha Rao from Cotton?"), confirmed = true) org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper()).idle() org.junit.Assert.assertEquals(listOf("t2"), model.list?.rows?.map { it.id }) } }