package com.cottondb.messages.warm import android.app.Application import androidx.compose.ui.test.junit4.createComposeRule import com.cottondb.messages.chat.ChatData import com.cottondb.messages.chat.ChatModel import com.cottondb.messages.chat.ThreadPage import com.cottondb.messages.chat.Words import com.cottondb.shell.core.CottonCore import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Ignore 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.shadows.ShadowLooper import java.util.concurrent.TimeUnit /** * Seen on the emulator (21 Nov 2026): in an imported chat scrolled a little way up, Jump to latest closed Comms. The * glide down ran in the model's own scope, which has no frame clock ("A MonotonicFrameClock is not available"). Invented * words. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class) class JumpToLatestTest { @get:Rule val compose = createComposeRule() private val words = Words { set, key -> "$set:$key" } private val threadWords = ThreadWordsWarm("Back", "Call", "Video call", "Not sent", "Try again", "You", "Their page") private val acts = MessageActs({}, {}, { _, _ -> }, {}, { _, _ -> }, {}, { _, _ -> }, {}, {}, VoicePlayer(kotlinx.coroutines.MainScope())) private fun page() = ThreadPage.of(CottonCore.json.parseToJsonElement(""" {"thread":"t1","header":{"name":"+15550100123","picture":{"initials":"","tint":1},"source":"cotton","status":"","calls":[]}, "items":[${(1..30).joinToString(",") { """{"item":"bubble","message":"m$it","mine":${it % 2 == 0},"kind":"text","text":"Invented words $it","head":true,"tail":true,"time":"20:0${it % 10}","pinned":false,"actions":["reply"]}""" }}]} """).jsonObject, words) // Marked and filed (coordinator, 23 Nov 2026: "make it not depend on timing, or mark and file it"): // passes alone, fails in the 195-test fork (android 16196, :59). ChatModel.jump glides on // AndroidUiDispatcher.Main, a process-wide singleton bound to the Choreographer of the first test that // made it; Robolectric resets that looper between tests, so its frames never come. The fix is jump's, // in the app: glide on the composition's own frame clock (a scope from rememberCoroutineScope). @Ignore("jump glides on a process-wide frame clock; filed in main-green-19's report, 23 Nov 2026") @Test fun jump_to_latest_glides_to_the_foot() { val model = ChatModel(ChatData(null, null, words, CoroutineScope(SupervisorJob()), null), CoroutineScope(SupervisorJob())) model.prefetched["t1"] = page() model.open("t1", unread = false) compose.setContent { WarmTheme(night = false) { Conversation(model.thread!!, model.threadList, acts, threadWords, {}, {}, {}, {}, {}, {}, {}, {}, null, {}, {}) {} } } compose.waitForIdle() compose.runOnIdle { model.threadList.dispatchRawDelta(1500f) } compose.waitForIdle() assert(model.threadList.firstVisibleItemIndex in 4..40) { "scrolled up: ${model.threadList.firstVisibleItemIndex}" } // The glide runs on the frames' clock (AndroidUiDispatcher), which waitForIdle does not wait on: the test // steps the main looper a frame at a time until the jump has ended, so no run depends on how fast the box is. val jump = compose.runOnIdle { model.jump() } for (frame in 0 until 600) { if (jump.isCompleted) break ShadowLooper.idleMainLooper(16, TimeUnit.MILLISECONDS) compose.waitForIdle() } assert(jump.isCompleted) { "the jump never ended" } compose.waitForIdle() assertEquals(0, model.threadList.firstVisibleItemIndex) } }