package com.cottondb.messages.chat import android.os.Looper import com.cottondb.shell.core.CottonCore import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config /** * Seen on emulator-5554 (23 Nov 2026, the polls walk): three votes reached the phone through the Mac's link, and the * open poll moved only once the chat was closed and opened again; a data event asked the list again and not the open * chat. Now the open chat merges its newest page on a data event, so the bars move where he is looking. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], application = android.app.Application::class) class DataChangedTest { private val words = Words { set, key -> "$set:$key" } private fun page(said: String, thai: String) = ThreadPage.of(CottonCore.json.parseToJsonElement(""" {"thread":"t1","header":{"name":"+1 555-019-0901","picture":{"initials":"","tint":1},"place":"whatsapp","status":""}, "items":[{"item":"bubble","message":"p1","mine":false,"kind":"poll","head":true,"tail":true,"time":"08:00","pinned":false,"actions":["reply"], "poll":{"question":"Dinner on Friday?","choose":"Select one or more","said":"$said","votesTitle":"Votes","spoken":"Poll: Dinner on Friday?", "options":[{"text":"Thai","votes":1,"said":"$thai","percent":100,"voters":["+1 555-019-0901"]}]}}]} """).jsonObject, words) @Test fun a_vote_from_the_link_moves_the_open_poll() = runBlocking { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) model.prefetched["t1"] = page("1 vote", "1 vote") model.open("t1", unread = false) fun poll() = model.thread?.items?.filterIsInstance()?.first()?.poll assertEquals("1 vote", poll()?.said) model.dataChanged(read = { page("3 votes", "2 votes") }) shadowOf(Looper.getMainLooper()).idle() assertEquals("3 votes", poll()?.said) assertEquals("2 votes", poll()?.options?.first()?.said) } private fun bubble(id: String, extra: String = "", text: String = "Invented words", kind: String = "text") = """{"item":"bubble","message":"$id","mine":false,"kind":"$kind","text":"$text","head":true,"tail":true,"time":"09:0${id.last()}","pinned":false,"actions":["reply","react"]$extra}""" private fun chat(vararg items: String) = ThreadPage.of(CottonCore.json.parseToJsonElement(""" {"thread":"t1","header":{"name":"+1 555-019-0901","picture":{"initials":"","tint":1},"place":"whatsapp","status":""}, "items":[${items.joinToString(",")}]} """).jsonObject, words) /** * Each change the Mac's link (or another phone, or this phone's own sweep) brings to a message in the open chat * redraws it there on the data event, without reopening: a reaction, an edit, deleted for everyone, a message * erased by its timer (gone from the answer, nothing new with it), and a file arriving as a new message. */ @Test fun every_change_from_the_link_redraws_the_open_chat() = runBlocking { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) val m1 = bubble("m1") val m2 = bubble("m2", text = "Leaving at seven") val m3 = bubble("m3", text = "This goes in a day") model.prefetched["t1"] = chat(m1, m2, m3) model.open("t1", unread = false) fun message(id: String) = model.thread?.items?.filterIsInstance()?.firstOrNull { it.id == id } fun changed(vararg items: String) { model.dataChanged(read = { chat(*items) }) shadowOf(Looper.getMainLooper()).idle() } val reacted = bubble("m1", ""","reactions":[{"emoji":"👍","count":"1","mine":false}]""") changed(reacted, m2, m3) assertEquals("a reaction", listOf("👍"), message("m1")?.reactions?.map { it.emoji }) val edited = bubble("m2", ""","edited":"Edited"""", text = "Leaving at eight") changed(reacted, edited, m3) assertEquals("an edit", "Leaving at eight" to "Edited", message("m2")?.body to message("m2")?.edited) val revoked = bubble("m1", "", text = "This message was deleted", kind = "deleted") changed(revoked, edited, m3) assertEquals("deleted for everyone", "This message was deleted", message("m1")?.deleted) changed(revoked, edited) assertEquals("erased by its timer: gone, nothing new with it", null, message("m3")) assertEquals(listOf("m1", "m2"), model.thread?.items?.filterIsInstance()?.map { it.id }) val file = bubble("m4", ""","media":[{"index":0,"kind":"file","name":"Tickets.pdf","size":"84 KB"}]""", text = "") changed(revoked, edited, file) assertEquals("a file arriving", "Tickets.pdf", message("m4")?.file?.name) } /** * A message whose time comes while its chat stays open, with nothing else changing, leaves the screen then: the * core's `erasesIn` sets one timer, and when it fires the chat is asked again (the core sweeps it first). */ @Test fun a_disappearing_message_leaves_the_open_chat_when_its_time_comes() = runBlocking { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) val m1 = bubble("m1") val m2 = bubble("m2", text = "This goes in a second") var asked = 0 model.readThread = { asked++; chat(m1) } val page = chat(m1, m2) model.prefetched["t1"] = page.copy(erasesIn = 1_000, askedAt = android.os.SystemClock.elapsedRealtime()) model.open("t1", unread = false) fun ids() = model.thread?.items?.filterIsInstance()?.map { it.id } shadowOf(Looper.getMainLooper()).idleFor(java.time.Duration.ofMillis(900)) assertEquals("before its time it stands, nothing asked", listOf("m1", "m2") to 0, ids() to asked) shadowOf(Looper.getMainLooper()).idleFor(java.time.Duration.ofMillis(1_000)) assertEquals("its time came: asked again and gone", listOf("m1") to 1, ids() to asked) shadowOf(Looper.getMainLooper()).idleFor(java.time.Duration.ofMillis(5_000)) assertEquals("the answer named no next moment: asked no more", 1, asked) } }