package com.cottondb.messages.warm import android.app.Application import androidx.activity.ComponentActivity import androidx.compose.foundation.lazy.LazyListState import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import com.cottondb.messages.chat.ChatList import com.cottondb.messages.chat.Found import com.cottondb.messages.chat.InChat import com.cottondb.messages.chat.SearchResults import com.cottondb.messages.chat.Words import com.cottondb.messages.chat.marksOf import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** Chat's search on Android: the core's answers read and drawn (CONTRACT.md "Search"). Invented chats and words. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], application = Application::class) class ChatSearchTest { @get:Rule val compose = createAndroidComposeRule() private val words = Words { set, key -> "$set:$key" } private fun json(s: String) = CottonCore.json.parseToJsonElement(s).jsonObject private val answer = """{"text":"मिलते", "chats":[{"thread":"t-group","name":"मिलते रहो friends","group":true,"source":"cotton","picture":{"initials":"MF","tint":2}, "unread":0,"markedUnread":false,"pinned":false,"muted":false,"archived":false,"leading":[],"trailing":[]}], "messages":[{"thread":"t-asha","message":"m-2","name":"Asha Example","picture":{"initials":"AE","tint":1},"group":false, "from":"You","mine":true,"line":"ठीक है, मिलते हैं","marks":[[8,13]],"when":"10:42"}, {"thread":"t-asha","message":"m-1","name":"Asha Example","picture":{"initials":"AE","tint":1},"group":false, "mine":false,"line":"कल शाम को मिलते हैं","marks":[[10,15]],"when":"10:40"}], "after":"77","older":"Still reading older messages. More may appear.", "words":{"hint":"Search chats and messages","chats":"Chats","messages":"Messages","more":"More messages"}}""" // RULE: A search's answer reads into chats, then messages with their marks in UTF-16; a tap on a message hands it back. @Test fun the_results_draw_chats_then_marked_messages() { val r = SearchResults.of(json(answer), words) assertEquals(listOf("m-2", "m-1"), r.found.map { it.message }) assertEquals(8 until 13, r.found[0].marks.single()) assertEquals("मिलते", r.found[0].line.substring(8, 13)) val list = ChatList.of(json("""{"words":{"title":"Cotton Comms","search":"Search"},"rows":[]}"""), words) var tapped: Found? = null compose.setContent { WarmTheme(night = false) { ChatsPlace( list, LazyListState(), "मिलते", ChatsWords("New chat", "You", "Muted", "Pinned", "Unread", "More"), {}, {}, {}, { _, _ -> }, {}, {}, {}, results = r, onFound = { tapped = it }, ) } } compose.waitForIdle() compose.onNodeWithTag("chat-found").assertIsDisplayed() compose.onNodeWithText("Chats").assertIsDisplayed() compose.onNodeWithTag("chat-row-t-group").assertIsDisplayed() compose.onNodeWithText("Messages").assertIsDisplayed() compose.onNodeWithText("Still reading older messages. More may appear.").assertExists() compose.onNodeWithTag("chat-found-m-1").performClick() assertEquals("m-1", tapped?.message) } // RULE: Inside a chat the matches are newest first; Earlier moves to an older one and says where it stands, and a bubble's marks are read. @Test fun inside_a_chat_the_bar_says_where_the_match_stands() { val f = InChat.of(json("""{"text":"मिलते","thread":"t-asha","hits":["m-2","m-1"],"count":"2","places":["1 of 2","2 of 2"], "words":{"hint":"Search this chat","earlier":"Earlier","later":"Later"}}""")) assertEquals("m-2", f.open) assertEquals("1 of 2", f.place) val older = f.copy(at = 1) assertEquals("m-1", older.open) assertEquals("2 of 2", older.place) assertEquals(listOf(0 until 4), marksOf(json("""{"marks":[[0,4],[3,3],[9]]}"""))) val none = InChat.of(json("""{"text":"x","thread":"t","hits":[],"count":"0","places":[],"empty":"Not in this chat.","words":{}}""")) assertEquals("Not in this chat.", none.place) } }