Cotton code
177 lines · 7.0 KB · KotlinRaw
package com.cottondb.android.ui.meetingsimport androidx.compose.runtime.Composableimport androidx.compose.runtime.MutableStateimport androidx.compose.runtime.mutableStateOfimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.hasSetTextActionimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTextInputimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.design.CottonThemeimport com.cottondb.android.work.meetings.MeetingListRowimport java.time.ZoneOffsetimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport com.cottondb.android.ScreenEvidenceimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.graphics.asAndroidBitmapimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class MeetingsScreenTest {    @get:Rule    val compose = createComposeRule()    @Test    fun addMenuDelegatesRecordAndImportWithoutOwningEitherFlow() {        val rows = mutableStateOf<List<MeetingListRow>?>(listOf(row("meeting-1", "Planning")))        var records = 0        var imports = 0        show(rows = rows, onRecord = { records += 1 }, onImport = { imports += 1 })        compose.onNodeWithContentDescription("Add a meeting").performClick()        compose.onNodeWithText("Record a meeting").performClick()        compose.onNodeWithContentDescription("Add a meeting").performClick()        compose.onNodeWithText("Import a recording").performClick()        assertEquals(1, records)        assertEquals(1, imports)    }    @Test    fun searchFiltersTheVisiblePoolAndClosingClearsTheControlledQuery() {        val rows = mutableStateOf<List<MeetingListRow>?>(listOf(row("meeting-1", "Planning", summary = "Shipping date"), row("meeting-2", "Standup")))        val query = mutableStateOf("")        show(rows = rows, query = { query.value }, onQuery = { query.value = it })        compose.onNodeWithContentDescription("Search meetings").performClick()        compose.onAllNodes(hasSetTextAction())[0].performTextInput("shipping")        compose.waitForIdle()        compose.onNodeWithContentDescription("Open Planning").assertIsDisplayed()        compose.onNodeWithText("Close").performClick()        assertEquals("", query.value)    }    @Test    fun cachedRowsRemainOpenableWithAReadFailureAndRetry() {        val rows = mutableStateOf<List<MeetingListRow>?>(listOf(row("meeting-1", "Planning")))        var opened: String? = null        var refreshes = 0        show(rows = rows, readProblem = "Synthetic meetings read failure", onOpenId = { opened = it }, onRefresh = { refreshes += 1 })        compose.onNodeWithText("Synthetic meetings read failure").assertIsDisplayed()        compose.onNodeWithContentDescription("Open Planning").performClick()        compose.onNodeWithText("Try again").performClick()        assertEquals("meeting-1", opened)        ScreenEvidence.capture("meetings-list-leaf") { compose.onRoot().captureToImage().asAndroidBitmap() }        assertEquals(1, refreshes)    }    @Test    fun emptyListOffersTheControlledRecordAction() {        val empty = mutableStateOf<List<MeetingListRow>?>(emptyList())        var records = 0        show(rows = empty, onRecord = { records += 1 })        compose.onNodeWithText("No meetings yet.").assertIsDisplayed()        compose.onNodeWithContentDescription("Record a meeting").performClick()        assertEquals(1, records)    }    @Test    fun searchMissUsesTheDistinctSwiftSentence() {        val rows = mutableStateOf<List<MeetingListRow>?>(listOf(row("meeting-1", "Planning")))        val query = mutableStateOf("")        show(rows = rows, query = { query.value }, onQuery = { query.value = it })        compose.onNodeWithContentDescription("Search meetings").performClick()        compose.onAllNodes(hasSetTextAction())[0].performTextInput("absent")        compose.waitForIdle()        compose.onNodeWithText("No meeting is named, summarised or spoken in by that.").assertIsDisplayed()    }    @Test    fun recoverySlotStaysVisibleAboveEmptyAndUnreadableReadStates() {        val empty = mutableStateOf<List<MeetingListRow>?>(emptyList())        show(rows = empty, recovery = { androidx.compose.material3.Text("A recording wasn't saved.") })        compose.onNodeWithText("A recording wasn't saved.").assertIsDisplayed()        compose.onNodeWithText("No meetings yet.").assertIsDisplayed()    }    @Test    fun recoverySlotStaysVisibleAboveReadFailure() {        val unreadable = mutableStateOf<List<MeetingListRow>?>(null)        show(rows = unreadable, readProblem = "Synthetic meetings read failure", recovery = { androidx.compose.material3.Text("A recording wasn't saved.") })        compose.onNodeWithText("A recording wasn't saved.").assertIsDisplayed()        compose.onNodeWithText("Synthetic meetings read failure").assertIsDisplayed()        compose.onNodeWithText("This could not be read.").assertIsDisplayed()    }    @Test    fun recoverySlotAppearsBeforeRowsWithoutGivingTheLeafRecoveryWrites() {        val rows = mutableStateOf<List<MeetingListRow>?>(listOf(row("meeting-1", "Planning", status = "transcribing")))        show(rows = rows, recovery = { androidx.compose.material3.Text("A recording wasn't saved.") })        compose.onNodeWithText("A recording wasn't saved.").assertIsDisplayed()        compose.onNodeWithText("transcribing…").assertIsDisplayed()    }    private fun show(        rows: MutableState<List<MeetingListRow>?>,        loading: Boolean = false,        readProblem: String? = null,        query: () -> String = { "" },        onQuery: (String) -> Unit = {},        onOpenId: (String) -> Unit = {},        onRecord: () -> Unit = {},        onImport: () -> Unit = {},        onRefresh: () -> Unit = {},        recovery: (@Composable () -> Unit)? = null,    ) {        compose.setContent {            CottonTheme {                MeetingsScreen(                    rows = rows.value,                    loading = loading,                    readProblem = readProblem,                    query = query(),                    onQuery = onQuery,                    onOpenId = onOpenId,                    onRecord = onRecord,                    onImport = onImport,                    onBack = {},                    onRefresh = onRefresh,                    zone = ZoneOffset.UTC,                    recovery = recovery,                )            }        }    }    private fun row(        id: String,        title: String,        status: String = "ready",        summary: String? = null,    ) = MeetingListRow(        id = id,        title = title,        status = status,        startedAt = "2026-08-17T10:30:00Z",        durationSeconds = 18.0,        rawTitle = title,        summaryMd = summary,        speakerNames = listOf("Asha"),        waitingToUpload = false,    )}