package com.cottondb.messages.chatimport android.os.Looperimport 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.Shadows.shadowOfimport 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)classDataChangedTest {privateval words = Words { set, key -> "$set:$key" }privatefun 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) @Testfun 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<Item.Message>()?.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) }privatefun 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}"""privatefun 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. */ @Testfun 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<Item.Message>()?.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<Item.Message>()?.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). */ @Testfun 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<Item.Message>()?.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) }}