package com.cottondb.messages import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test import java.io.File class ChatNoticesTest { private fun answer(text: String) = Notices.of(Json.parseToJsonElement(text).jsonObject) /** `chat.notices`' answer, as the core writes it (cotton-core messages::app::notices), read for the shade. */ @Test fun the_answer_is_read() { val n = answer( """{"notices":[{"thread":"t1","title":"Asha","picture":{"initials":"A","tint":2},"photoPath":"/p/a.jpg","group":false,"request":false, "lines":[{"id":"m1","mine":false,"text":"we met at the station","at":1700000000000},{"id":"m2","from":"You","mine":true,"text":"yes","at":1700000001000}], "unread":1,"at":1700000000000,"actions":true,"key":"chat:t1"}, {"thread":"t2","title":"New message","picture":{"initials":"","tint":0},"group":false,"request":true,"lines":[],"unread":2,"at":5,"actions":false,"key":"t2"}], "missed":[{"call":"c1","thread":"t1","title":"Missed call","text":"Asha","picture":{"initials":"A","tint":2},"video":true,"at":9,"back":"Call back"}], "words":{"channel":"Messages","missedChannel":"Missed calls","reply":"Reply","replyHint":"Message","markRead":"Mark as read","you":"You","newMessage":"New message"}}""", ) val first = n.chats[0] assertEquals("Asha", first.title) assertEquals("/p/a.jpg", first.photoPath) assertEquals(listOf(false, true), first.lines.map { it.mine }) assertEquals(1700000001000L, first.lines[1].at) assertTrue(first.actions) assertEquals("chat:t1", first.key) val request = n.chats[1] assertTrue(request.request) assertFalse(request.actions) assertEquals(MissedCall("c1", "t1", "Missed call", "Asha", null, true, 9L, "Call back"), n.missed.single()) assertEquals("Mark as read", n.words.markRead) } /** What this phone posted and the core no longer lists goes: read here, on another phone, or by Mark as read. */ @Test fun what_is_no_longer_listed_is_taken_away() { val active = listOf("chat:t1", "chat:t2", "missed:c1", "ring", "chat:t2") assertEquals(listOf("chat:t2", "missed:c1"), Notices.stale(active, listOf("chat:t1"))) assertEquals(emptyList(), Notices.stale(listOf("ongoing"), emptyList())) } /** Only a live video call shrinks to picture in picture. */ @Test fun picture_in_picture_is_for_a_live_video_call() { assertTrue(ChatPip.wanted("live", video = true)) assertTrue(ChatPip.wanted("reconnecting", video = true)) assertFalse(ChatPip.wanted("live", video = false)) assertFalse(ChatPip.wanted("ringing_in", video = true)) } /** A call ended while in picture in picture takes the small window away (it stayed, showing Chats). */ @Test fun picture_in_picture_ends_with_the_call() { assertTrue(ChatPip.leave(inPip = true, phase = "ended", video = true)) assertTrue(ChatPip.leave(inPip = true, phase = null, video = false)) assertFalse(ChatPip.leave(inPip = true, phase = "live", video = true)) assertFalse(ChatPip.leave(inPip = false, phase = "ended", video = true)) } /** The Worker's read nudge is `{kind: chat, read}`: MessagesPush lands it as any chat push (kind chat). */ @Test fun the_workers_read_nudge_is_a_chat_push() { val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile } val push = File(repo, "apps/business/src/chat/push.ts").readText() assertTrue("push.ts no longer nudges with kind chat", push.contains("data: { kind: \"chat\", read: \"1\" }")) assertEquals("chat", MessagesPush.KIND) } // A chat answered from its notification is put back by Android 15+ when cancelled at once: there the shade is matched again after. @Test fun from_android_15_the_shade_is_matched_again_after_a_reply() { assertTrue(Notices.keptByReply(35)) assertTrue(Notices.keptByReply(36)) assertFalse(Notices.keptByReply(34)) } }