package com.cottondb.messages.warm import android.app.Application import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import com.cottondb.messages.chat.Document import com.cottondb.messages.chat.readWords import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** * A file a message or a mail carries opens in Comms' own viewer (owner, 24 Nov 2026: "a document in a chat cannot be * opened and read"): words drawn as words, what Comms cannot draw named with Open in another app. Invented files. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w360dp-h800dp-mdpi", application = Application::class) class DocumentViewerTest { @get:Rule val compose = createComposeRule() @get:Rule val folder = TemporaryFolder() private val words = ViewerWords("Close", "Open in another app", "Comms can't show this kind of file.", "This is the start of the file.", "This file didn't open.") { n, c -> "Page $n of $c" } @Test fun the_cores_answer_is_a_document() { val o = CottonCore.json.parseToJsonElement("""{"path":"/x/Invented plan.pdf","mime":"application/pdf","name":"Invented plan.pdf","kept":true,"view":"pdf"}""").jsonObject assertEquals(Document("/x/Invented plan.pdf", "application/pdf", "Invented plan.pdf", "pdf"), Document.of(o)) val bare = CottonCore.json.parseToJsonElement("""{"path":"/x/a.bin"}""").jsonObject assertEquals(Document("/x/a.bin", "*/*", "a.bin", "other"), Document.of(bare)) } @Test fun words_are_read_in_pieces_and_say_when_there_is_more() { val file = folder.newFile("invented.csv").apply { writeText("name,day\r\n" + (1..100).joinToString("\n") { "Asha Example,$it" }) } val all = readWords(file) assertFalse(all.more) assertTrue(all.pieces.first().startsWith("name,day\nAsha Example,1")) assertEquals(3, all.pieces.size) assertTrue(readWords(file, most = 64).more) } @Test fun a_words_file_is_drawn_as_words() { val file = folder.newFile("Invented notes.txt").apply { writeText("Bring the tent.\nAnd the poles.") } compose.setContent { WarmTheme(night = false) { DocumentViewer(Document(file.path, "text/plain", file.name, "text"), words, onOpenWith = {}, onClose = {}) } } compose.waitUntil(5_000) { compose.onAllNodes(androidx.compose.ui.test.hasText("Bring the tent.", substring = true)).fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithTag("doc-name").assertIsDisplayed() compose.onNodeWithTag("doc-open-with").assertIsDisplayed() } @Test fun what_comms_cannot_draw_offers_another_app() { val file = folder.newFile("Invented.docx").apply { writeBytes(byteArrayOf(0x50, 0x4b, 3, 4)) } var opened = 0 var closed = 0 compose.setContent { WarmTheme(night = false) { DocumentViewer(Document(file.path, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", file.name, "other"), words, onOpenWith = { opened++ }, onClose = { closed++ }) } } compose.onNodeWithTag("doc-unshown").assertIsDisplayed() compose.onNodeWithText("Comms can't show this kind of file.").assertIsDisplayed() compose.onNodeWithTag("doc-open-with").performClick() compose.onNodeWithTag("doc-close").performClick() assertEquals(1 to 1, opened to closed) } @Test fun a_pdf_that_does_not_open_says_so() { val file = folder.newFile("broken.pdf").apply { writeText("not a pdf") } compose.setContent { WarmTheme(night = false) { DocumentViewer(Document(file.path, "application/pdf", file.name, "pdf"), words, onOpenWith = {}, onClose = {}) } } compose.waitUntil(5_000) { compose.onAllNodes(androidx.compose.ui.test.hasTestTag("doc-unshown")).fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithText("This file didn't open.").assertIsDisplayed() } }