package com.cottondb.messages.warm import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onAllNodesWithTag import android.app.Application import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.test.assertIsFocused import androidx.compose.ui.test.assertIsNotFocused 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 androidx.compose.ui.unit.sp import com.cottondb.messages.chat.Compose import com.cottondb.messages.chat.ComposerState import com.cottondb.messages.chat.VoiceRecorder import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.jsonObject import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** * THE WRITING BOX as a reply or an edit begins (the emulator's fault, kept): swiped to reply, or Edit chosen from the * held menu, the strip stood over the field but the field had no focus and the keyboard stayed down, so a second tap * was needed before a word could be written. A draft's strip the conversation opens with raises nothing. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h860dp-xhdpi", application = Application::class) class WritingBoxTest { @get:Rule val compose = createComposeRule() private fun answer(strip: String?) = Compose.of(CottonCore.json.parseToJsonElement("""{"thread":"t1","placeholder":"Message Rose","button":"mic", "buttonLabel":"Hold to record","attach":true,"attachLabel":"Attach"${strip?.let { ""","strip":$it""" } ?: ""}}""").jsonObject) private val reply = """{"kind":"reply","message":"m4","label":"Reply to Rose","line":"Got the corner one","cancel":"Cancel"}""" @Test fun a_reply_begun_puts_the_finger_in_the_field_and_a_kept_draft_does_not() { var answer by mutableStateOf(answer(reply)) val state = ComposerState() compose.setContent { WarmTheme(night = false) { WritingBox(state, answer, WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {}) } } compose.waitForIdle() compose.onNodeWithTag("composer-strip").assertExists() compose.onNodeWithTag("chat-field").assertIsNotFocused() // The draft sent, then a new reply begun from another message. compose.runOnIdle { answer = answer(null) } compose.waitForIdle() compose.runOnIdle { answer = answer(reply.replace("m4", "m7")) } compose.waitForIdle() compose.onNodeWithTag("chat-field").assertIsFocused() } // FEEDBACK1 item 3 (22 Nov 2026), on the emulator sideways with the keyboard up: the box grew to six lines and filled // the page (Send pushed off the screen, no message in sight). Compact, the plus, the field and Send stand on one // line, and five lines written stand in two, the field scrolling. @Test fun compact_the_box_stands_on_one_line_and_grows_to_two() { val state = ComposerState() compose.setContent { WarmTheme(night = false) { WritingBox(state, answer(null), WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {}, compact = true) } } compose.waitForIdle() val one = compose.onNodeWithTag("chat-composer").fetchSemanticsNode().boundsInRoot.height val field = compose.onNodeWithTag("chat-field").fetchSemanticsNode().boundsInRoot val plus = compose.onNodeWithTag("chat-attach").fetchSemanticsNode().boundsInRoot org.junit.Assert.assertTrue("the plus beside the field, on its line: $plus $field", plus.right <= field.left && plus.bottom > field.top && plus.top < field.bottom) compose.runOnIdle { state.field.edit { replace(0, length, "one\ntwo\nthree\nfour\nfive") } } compose.waitForIdle() val five = compose.onNodeWithTag("chat-composer").fetchSemanticsNode().boundsInRoot.height val line = with(compose.density) { 26.sp.toPx() } org.junit.Assert.assertTrue("five lines stand in two: $one -> $five", five - one < line * 1.6f) } // The box turns compact as the keyboard rises: the field keeps the finger (on the emulator a first try lost it, and // the keyboard fell straight back). @Test fun turning_compact_keeps_the_focus_in_the_field() { var compact by mutableStateOf(false) val state = ComposerState() compose.setContent { WarmTheme(night = false) { WritingBox(state, answer(null), WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {}, compact = compact) } } compose.onNodeWithTag("chat-field").performClick() compose.onNodeWithTag("chat-field").assertIsFocused() compose.runOnIdle { compact = true } compose.waitForIdle() compose.onNodeWithTag("chat-field").assertIsFocused() } // FEEDBACK1 item 4: pictures picked wait in the writing box, each named as what it is, with one ✕ that takes them out. @Test fun waiting_pictures_stand_in_the_box_with_their_cancel() { val state = ComposerState() var out = 0 val photo = CottonCore.json.parseToJsonElement("""{"kind":"photo","path":"/invented/none.jpg"}""").jsonObject val video = CottonCore.json.parseToJsonElement("""{"kind":"video","path":"/invented/none.mp4"}""").jsonObject state.staged += listOf(photo, video) compose.setContent { WarmTheme(night = false) { WritingBox(state, answer(null), WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {}, onUnstage = { out++ }, stagedWords = StagedWords("Cancel", "Photo", "Video")) } } compose.waitForIdle() org.junit.Assert.assertEquals(2, compose.onAllNodesWithTag("chat-staged-picture").fetchSemanticsNodes().size) compose.onNodeWithContentDescription("Video").assertExists() // The row stands above the field. val row = compose.onNodeWithTag("chat-staged").fetchSemanticsNode().boundsInRoot val field = compose.onNodeWithTag("chat-field").fetchSemanticsNode().boundsInRoot org.junit.Assert.assertTrue("the pictures above the field: $row $field", row.bottom <= field.top) compose.onNodeWithContentDescription("Cancel").performClick() org.junit.Assert.assertEquals(1, out) // None waiting: no row. compose.runOnIdle { state.staged.clear() } compose.waitForIdle() compose.onNodeWithTag("chat-staged").assertDoesNotExist() } // FEEDBACK-2 A (owner, 23 Nov 2026): writing to a number not on Cotton goes to their WhatsApp through his Mac, and the // box says so before Send, above the field, in the core's words; with no `place` (an unlinked copy) nothing is drawn. @Test fun the_box_says_where_the_message_goes_before_send() { fun with(place: String?) = Compose.of(CottonCore.json.parseToJsonElement("""{"thread":"t1","placeholder":"Message","button":"send","buttonLabel":"Send","attach":false,"attachLabel":"Attach","sendEnabled":true${place?.let { ""","place":$it""" } ?: ""}}""").jsonObject) var answer by mutableStateOf(with("""{"id":"whatsapp","mark":"via WhatsApp"}""")) val state = ComposerState() compose.setContent { WarmTheme(night = false) { WritingBox(state, answer, WritingWords("Send", "Attach", "Hold to record"), VoiceRecorder(LocalContext.current, kotlinx.coroutines.MainScope()), {}, {}, {}, {}, { true }, {}) } } compose.waitForIdle() compose.onNodeWithTag("composer-place").assertExists() compose.onNodeWithText("via WhatsApp").assertExists() val mark = compose.onNodeWithTag("composer-place").fetchSemanticsNode().boundsInRoot val field = compose.onNodeWithTag("chat-field").fetchSemanticsNode().boundsInRoot org.junit.Assert.assertTrue("the mark above the field: $mark $field", mark.bottom <= field.top) compose.runOnIdle { answer = with("""{"id":"cotton","mark":"on Cotton"}""") } compose.waitForIdle() compose.onNodeWithText("on Cotton").assertExists() compose.runOnIdle { answer = with(null) } compose.waitForIdle() compose.onNodeWithTag("composer-place").assertDoesNotExist() } }