package com.cottondb.android.ui.take import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.test.captureToImage import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.assertIsNotEnabled import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performScrollTo import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.ScreenEvidence import com.cottondb.android.design.CottonTheme import com.cottondb.android.take.TakeDestination import com.cottondb.android.take.TakePhase import com.cottondb.android.take.TakeRead import com.cottondb.android.take.TakeState import com.cottondb.android.take.TakeWriteOutcome import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class TakeScreenTest { @get:Rule val compose = createComposeRule() @Test fun listeningShowsOneElapsedCaptionHighlightsVocabularyAndFinishesReadyOnce() { var finished = emptyList() setContent( TakeState( phase = TakePhase.Listening, text = "Meet Aster after lunch", elapsedMillis = 7_000, read = TakeRead(3, listOf("Aster"), listOf("bias only")), ), onFinish = { finished += it }, ) compose.onNodeWithContentDescription("Listening for 0:07").assertExists() compose.onNodeWithText("Meet Aster after lunch").assertExists() compose.onNodeWithContentDescription("Done").performClick() assertEquals(listOf(TakeDestination.Ready), finished) ScreenEvidence.capture("take-listening") { compose.onRoot().captureToImage().asAndroidBitmap() } } @Test fun cachedReadFailureLeavesTheLiveTranscriptAndControlsUsable() { var cotton = 0 setContent( TakeState( phase = TakePhase.Listening, text = "Words are still here", elapsedMillis = 1_000, read = TakeRead(1, listOf("Words"), emptyList()), readProblem = "Vocabulary could not be refreshed.", ), onFinish = { if (it == TakeDestination.Cotton) cotton += 1 }, ) compose.onNodeWithText("Words are still here").assertExists() compose.onNodeWithText("Vocabulary could not be refreshed.").assertExists() compose.onNodeWithContentDescription("→ Cotton").performClick() assertEquals(1, cotton) } @Test fun finishingDisablesEveryLiveAction() { setContent(TakeState(phase = TakePhase.Finishing, text = "Settling words")) compose.onNodeWithContentDescription("Finishing…").assertExists() compose.onNodeWithContentDescription("Discard").assertIsNotEnabled() compose.onNodeWithContentDescription("Finishing…").assertIsNotEnabled() compose.onNodeWithContentDescription("→ Cotton").assertIsNotEnabled() } @Test fun settledCopyAndSpeakAgainEachCallTheirHostOnce() { var copied = emptyList() var retries = 0 setContent( TakeState( phase = TakePhase.Settled, text = "Copy this exact take", problem = "Dictation ended before the final words arrived.", corpusOutcome = TakeWriteOutcome.Unconfirmed( "Copy this exact take", "The corpus write could not be confirmed.", ), ), onCopy = { copied += it }, onRetry = { retries += 1 }, ) ScreenEvidence.capture("take-settled") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithText("Copy this exact take").assertExists() compose.onNodeWithText("Dictation ended before the final words arrived.").assertExists() compose.onNodeWithText("Ready to copy. The database could not confirm this take.").assertExists() compose.onNodeWithContentDescription("Copy").performClick() compose.onNodeWithContentDescription("Speak again").performClick() assertEquals(listOf("Copy this exact take"), copied) assertEquals(1, retries) } @Test fun backAndDiscardStayOwnedByTheHost() { var backs = 0 var discards = 0 setContent( TakeState(phase = TakePhase.Listening, elapsedMillis = 1_000), onBack = { backs += 1 }, onDiscard = { discards += 1 }, ) compose.onNodeWithContentDescription("Back").performClick() compose.onNodeWithContentDescription("Discard").performClick() assertEquals(1, backs) assertEquals(1, discards) } @Test fun longSettledTakeKeepsItsLastLineReachableInTheSelectableSlip() { val text = buildString { repeat(50) { index -> append("Synthetic line $index remains part of this take.\n") } append("The final synthetic line survives.") } setContent(TakeState(phase = TakePhase.Settled, text = text)) compose.onNodeWithTag("take-settled-text").assertExists() compose.onNodeWithText("The final synthetic line survives.", substring = true) .performScrollTo() .assertExists() } private fun setContent( initial: TakeState, onBack: () -> Unit = {}, onDiscard: () -> Unit = {}, onFinish: (TakeDestination) -> Unit = {}, onRetry: () -> Unit = {}, onCopy: (String) -> Unit = {}, ) { compose.setContent { CottonTheme { TakeScreen( state = initial, onBack = onBack, onDiscard = onDiscard, onFinish = onFinish, onRetry = onRetry, onCopy = onCopy, ) } } } }