package com.cottondb.android.ui.meetings import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.hasSetTextAction import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTextInput import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.design.CottonTheme import com.cottondb.android.work.meetings.MeetingListRow import java.time.ZoneOffset import org.junit.Assert.assertEquals import org.junit.Rule import com.cottondb.android.ScreenEvidence import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.captureToImage import androidx.compose.ui.graphics.asAndroidBitmap import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class MeetingsScreenTest { @get:Rule val compose = createComposeRule() @Test fun addMenuDelegatesRecordAndImportWithoutOwningEitherFlow() { val rows = mutableStateOf?>(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?>(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?>(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?>(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?>(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?>(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?>(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?>(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?>, 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, ) }