package com.cottondb.android.ui.locations import androidx.compose.runtime.mutableStateOf 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.day.locations.LocationRow import com.cottondb.android.day.locations.LocationsState import com.cottondb.android.design.CottonTheme import java.time.Clock import java.time.Instant import java.time.ZoneOffset import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class LocationsScreenTest { @get:Rule val compose = createComposeRule() @Test fun emptyStateOpensTheControlledNewPlaceDialog() { val state = mutableStateOf(LocationsState(rows = emptyList())) var opens = 0 show(state, onCreateOpen = { opens += 1; state.value = state.value.copy(composing = true) }) compose.onNodeWithText("Add a place").performClick() compose.onAllNodes(hasSetTextAction())[0].fetchSemanticsNode() assertEquals(1, opens) } @Test fun newPlaceFieldsAndAdmissionAreControlledBySuppliedState() { val state = mutableStateOf(LocationsState(rows = emptyList(), composing = true)) var creates = 0 show( state = state, onName = { state.value = state.value.copy(name = it) }, onAddress = { state.value = state.value.copy(address = it) }, onCreate = { creates += 1 }, ) compose.onAllNodes(hasSetTextAction())[0].performTextInput("Library") compose.onAllNodes(hasSetTextAction())[1].performTextInput("1 Test Way") compose.onNodeWithText("Add place").performClick() assertEquals("Library", state.value.name) assertEquals("1 Test Way", state.value.address) assertEquals(1, creates) } @Test fun cachedRowsStayVisibleWithNamedReadFailureAndRetry() { val state = mutableStateOf( LocationsState( rows = listOf(row("location-1", "Library")), readProblem = "Synthetic direct read failure", ), ) var refreshes = 0 var opened: String? = null show(state, onRefresh = { refreshes += 1 }, onOpen = { opened = it }) compose.onNodeWithText("Synthetic direct read failure").fetchSemanticsNode() compose.onNodeWithContentDescription("Open Library").performClick() compose.onNodeWithText("Try again").performClick() assertEquals("location-1", opened) assertEquals(1, refreshes) } @Test fun recentAppearsOnlyAboveTheThresholdAndLayoutIsControlled() { val state = mutableStateOf( LocationsState( rows = listOf( row("one", "One", "2026-09-22T10:00:00Z"), row("two", "Two", "2026-09-21T10:00:00Z"), row("three", "Three", "2026-09-20T10:00:00Z"), row("four", "Four", "2026-09-19T10:00:00Z"), ), ), ) val layouts = mutableListOf() show(state, onLayout = layouts::add) compose.onNodeWithText("Recent").fetchSemanticsNode() compose.onNodeWithContentDescription("Change locations layout").performClick() assertEquals(listOf(LocationLayout.List), layouts) assertTrue(state.value.rows!!.size > 3) } private fun show( state: androidx.compose.runtime.MutableState, onRefresh: () -> Unit = {}, onCreateOpen: () -> Unit = {}, onName: (String) -> Unit = {}, onAddress: (String) -> Unit = {}, onCreate: () -> Unit = {}, onOpen: (String) -> Unit = {}, onLayout: (LocationLayout) -> Unit = {}, ) { compose.setContent { CottonTheme { LocationsScreen( state = state.value, layout = LocationLayout.Postcards, clock = CLOCK, zone = ZoneOffset.UTC, onRefresh = onRefresh, onCreateOpen = onCreateOpen, onCreateCancel = {}, onName = onName, onAddress = onAddress, onCreate = onCreate, onOpen = onOpen, onLayout = onLayout, ) } } } private fun row(id: String, name: String, updatedAt: String? = null) = LocationRow( id = id, name = name, address = "Test address", lat = null, lng = null, note = null, updatedAt = updatedAt, linkCount = 0L, ) private companion object { val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneOffset.UTC) } }