Cotton code
163 lines · 6.0 KB · KotlinRaw
package com.cottondb.android.ui.takeimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.assertIsNotEnabledimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performScrollToimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.ScreenEvidenceimport com.cottondb.android.design.CottonThemeimport com.cottondb.android.take.TakeDestinationimport com.cottondb.android.take.TakePhaseimport com.cottondb.android.take.TakeReadimport com.cottondb.android.take.TakeStateimport com.cottondb.android.take.TakeWriteOutcomeimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class TakeScreenTest {    @get:Rule val compose = createComposeRule()    @Test    fun listeningShowsOneElapsedCaptionHighlightsVocabularyAndFinishesReadyOnce() {        var finished = emptyList<TakeDestination>()        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<String>()        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,                )            }        }    }}