102 lines · 5.1 KB · KotlinRaw
package com.cottondb.messages.warmimport android.app.Applicationimport android.graphics.Bitmapimport android.graphics.Colorimport android.util.Base64import androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport com.cottondb.messages.chat.Composeimport com.cottondb.messages.chat.Itemimport com.cottondb.messages.chat.LinkFetchimport com.cottondb.messages.chat.Wordsimport com.cottondb.shell.core.CottonCoreimport kotlinx.serialization.json.JsonPrimitiveimport kotlinx.serialization.json.buildJsonObjectimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNotNullimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Configimport org.robolectric.annotation.GraphicsModeimport java.io.ByteArrayOutputStream/** * FEEDBACK-4 B4 (owner on iPhone, Comms 26, 23 Nov 2026: a link sent shows no preview): this phone fetches the card * for a link the core offers, shows it above the writing box with a close mark, and the bubble's card draws its * picture. The picture is made small enough to travel. Invented page and words. */@RunWith(RobolectricTestRunner::class)@GraphicsMode(GraphicsMode.Mode.NATIVE)@Config(sdk = [35], application = Application::class)class LinkPreviewTest { @get:Rule val compose = createComposeRule() private val words = Words { set, key -> "$set:$key" } private fun jpeg(side: Int): ByteArray { val b = Bitmap.createBitmap(side, side * 2 / 3, Bitmap.Config.ARGB_8888) for (x in 0 until b.width step 7) for (y in 0 until b.height step 5) b.setPixel(x, y, Color.rgb(x % 255, y % 255, (x * y) % 255)) return ByteArrayOutputStream().also { b.compress(Bitmap.CompressFormat.JPEG, 95, it) }.toByteArray() } @Test fun the_composer_names_the_link_its_card_is_fetched_for() { fun of(json: String) = Compose.of(CottonCore.json.parseToJsonElement(json).jsonObject) assertEquals("https://example.org/walks", of("""{"thread":"t1","preview":{"url":"https://example.org/walks"}}""").preview) assertEquals(null, of("""{"thread":"t1"}""").preview) } @Test fun a_large_picture_is_made_a_small_jpeg_that_travels() { val thumb = LinkFetch.thumbOf(jpeg(1600)) assertNotNull(thumb) assertTrue("under 2,800 characters: ${thumb!!.length}", thumb.length <= LinkFetch.THUMB_MOST) val bytes = Base64.decode(thumb, Base64.DEFAULT) assertTrue("a JPEG", bytes[0] == 0xFF.toByte() && bytes[1] == 0xD8.toByte()) assertEquals(null, LinkFetch.thumbOf("not a picture".toByteArray())) } @Test fun never_this_phone_or_its_network() { listOf("localhost", "127.0.0.1", "10.1.2.3", "192.168.0.1", "172.16.4.4", "169.254.169.254", "100.64.0.9").forEach { assertFalse(it, LinkFetch.public(it)) } assertTrue(LinkFetch.public("93.184.216.34")) } @Test fun the_card_stands_above_the_box_and_closes() { var closed = false val thumb = LinkFetch.thumbOf(jpeg(400))!! val card = buildJsonObject { put("url", JsonPrimitive("https://example.org/walks")); put("site", JsonPrimitive("example.org")); put("title", JsonPrimitive("Hill walks")); put("thumb", JsonPrimitive(thumb)) } compose.setContent { WarmTheme(night = false) { LinkCardStrip(card, "Remove link preview") { closed = true } } } compose.onNodeWithText("Hill walks").assertIsDisplayed() compose.onNodeWithTag("chat-preview-picture", useUnmergedTree = true).assertExists() compose.onNodeWithTag("chat-preview-close").performClick() assertTrue(closed) } @Test fun a_sent_card_draws_its_picture() { val thumb = LinkFetch.thumbOf(jpeg(400))!! val m = Item.of(CottonCore.json.parseToJsonElement("""{"item":"bubble","message":"m1","mine":true,"kind":"text","text":"the route https://example.org/walks","head":true,"tail":true, "time":"19:00","link":{"url":"https://example.org/walks","site":"example.org","title":"Hill walks","thumb":"$thumb"},"actions":[]}""").jsonObject, words) as Item.Message assertEquals(thumb, m.link?.thumb) } @Test fun privacy_says_this_phone_fetches_previews_and_turns_it_off() { var turned: Boolean? = null val w = PrivacyWords("Back", "Privacy", "Blocked", "Nobody", "Seen by others", "Read receipts", "r", "Typing", "t", "Last seen", "Safety", "s", "Claude", "c", "Link previews", "This phone fetches a link's title and picture.") compose.setContent { WarmTheme(night = false) { PrivacyPage(w, 0, receipts = false, typing = false, signals = true, onBlocked = {}, onReceipts = {}, onTyping = {}, onClaude = {}, onBack = {}, previews = true, onPreviews = { turned = it }) } } compose.onNodeWithTag("you-link-previews").performClick() assertEquals(false, turned) }}