package com.cottondb.messages.warm import android.app.Application import androidx.compose.foundation.lazy.LazyListState import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import com.cottondb.messages.chat.ChatPictures import com.cottondb.messages.chat.ThreadPage import com.cottondb.messages.chat.ThreadState import com.cottondb.messages.chat.Words import com.cottondb.shell.core.CottonCore import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertNull 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 java.util.concurrent.atomic.AtomicInteger /** * A PICTURE THAT CANNOT BE HAD (the iPhone lane's find, 2955f1a7ab): an imported picture whose file the store does * not hold was a blank grey box, asked of the core again each time its tile was drawn. The core's `gone` is drawn as * "Picture not available" and never asked; what the core cannot know is tried once at a time, then waited out. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class) class PictureGoneTest { @get:Rule val compose = createComposeRule() private val words = Words { set, key -> if (key == "app.thread.picture_gone") "Picture not available" else "$set:$key" } private val threadWords = ThreadWordsWarm("Back", "Call", "Video call", "Not sent", "Try again", "You", "Their page") private fun j(text: String) = CottonCore.json.parseToJsonElement(text).jsonObject @Test fun a_picture_the_store_does_not_hold_says_so_and_is_never_asked() { val asked = AtomicInteger() ChatPictures.open = { _, _ -> asked.incrementAndGet(); null } val photo = """{"item":"bubble","message":"m-w7","mine":false,"kind":"photo","text":"","head":true,"tail":true,"face":true,"time":"19:01","pinned":false,"actions":[], "media":[{"kind":"photo","index":"0","width":1200,"height":900,"gone":true}]}""" val t = ThreadState("t1", ThreadPage.of(j("""{"thread":"t1","header":{"name":"Hill trip","picture":{"initials":"HT","tint":1},"status":""},"items":[$photo]}"""), words)) val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope())) compose.setContent { WarmTheme(night = false) { Conversation(t, LazyListState(), acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } } compose.waitForIdle() compose.onNodeWithTag("gone-0", useUnmergedTree = true).assertExists() compose.onNodeWithText("Picture not available", useUnmergedTree = true).assertExists() assertEquals(0, asked.get()) // Its spoken words say so too. val m = t.items.single() as com.cottondb.messages.chat.Item.Message assertTrue(m.spoken, m.spoken.contains("Picture not available")) ChatPictures.open = null } @Test fun a_try_that_found_nothing_is_not_asked_again_until_its_wait_is_over() = runBlocking { var clock = 0L val tries = ChatPictures.Tries { clock } val asked = AtomicInteger() val key = "msg:m1:0" assertNull(tries.file(key) { asked.incrementAndGet(); null }) assertTrue(key in tries.gone) // Drawn again and again within 30 seconds: nothing asked. repeat(20) { assertNull(tries.file(key) { asked.incrementAndGet(); null }) } assertEquals(1, asked.get()) clock = 30_000 assertNull(tries.file(key) { asked.incrementAndGet(); null }) assertEquals(2, asked.get()) // The second wait is two minutes. clock = 30_000 + 119_000 assertNull(tries.file(key) { asked.incrementAndGet(); null }) assertEquals(2, asked.get()) clock = 30_000 + 120_000 val file = java.io.File.createTempFile("pic", ".jpg") assertEquals(file, tries.file(key) { asked.incrementAndGet(); file }) assertTrue(key !in tries.gone) file.delete() Unit } // FEEDBACK1 item 1 (22 Nov 2026): a picture just sent showed "Picture not available". The core says `not_yet` for // a picture whose message is still going or came within minutes: never drawn as not available, asked again soon. @Test fun a_picture_not_there_yet_is_never_said_gone_and_is_asked_again_soon() = runBlocking { var clock = 0L val tries = ChatPictures.Tries { clock } val asked = AtomicInteger() val key = "msg:m3:0" val notYet: suspend () -> java.io.File? = { asked.incrementAndGet(); throw com.cottondb.shell.core.CoreException("not_yet", "The picture is not in the store yet.", kotlinx.serialization.json.JsonNull) } assertNull(tries.file(key, notYet)) assertTrue("not said gone", key !in tries.gone) assertTrue(tries.notYet(key)) assertNull(tries.file(key, notYet)) assertEquals("waited a moment", 1, asked.get()) clock = ChatPictures.NOT_YET_MS val file = java.io.File.createTempFile("pic", ".jpg") assertEquals(file, tries.file(key) { asked.incrementAndGet(); file }) assertEquals(2, asked.get()) assertTrue(!tries.notYet(key) && key !in tries.gone) file.delete() Unit } @Test fun a_tile_drawn_again_while_one_try_is_out_waits_for_that_try() = runBlocking { val tries = ChatPictures.Tries { 0L } val asked = AtomicInteger() val gate = CompletableDeferred() val file = java.io.File.createTempFile("pic", ".jpg") val all = (1..5).map { async(kotlinx.coroutines.Dispatchers.IO) { tries.file("msg:m2:0") { asked.incrementAndGet(); gate.await(); file } } } kotlinx.coroutines.delay(200) gate.complete(Unit) all.forEach { assertEquals(file, it.await()) } assertEquals(1, asked.get()) file.delete() Unit } }