package com.cottondb.messages.warm import android.app.Application import android.graphics.Bitmap import android.graphics.Color import android.util.Base64 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.Compose import com.cottondb.messages.chat.Item import com.cottondb.messages.chat.LinkFetch import com.cottondb.messages.chat.Words import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import org.robolectric.annotation.GraphicsMode import 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) } }