package com.cottondb.android.ui.locations import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.test.assertIsNotEnabled import androidx.compose.ui.test.isToggleable import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performScrollTo import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTextReplacement import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.day.locations.LocationDetail import com.cottondb.android.day.locations.LocationDetailState import com.cottondb.android.day.locations.LocationDraft import com.cottondb.android.day.locations.LocationField import com.cottondb.android.day.locations.LocationProject import com.cottondb.android.day.locations.LocationProjectLink import com.cottondb.android.day.locations.LocationRead 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.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class LocationDetailScreenTest { @get:Rule val compose = createComposeRule() @Test fun titleAndNoteEditsUseTheirExactControlledFields() { val state = mutableStateOf(detailState()) val edits = mutableListOf>() show( state = state, callbacks = callbacks( edit = { field, value -> edits += field to value state.value = state.value.withDraft( field, state.value.draft(field).copy(value = value), ) }, ), ) compose.onNodeWithContentDescription("Place name") .performTextReplacement("North Library") compose.onNodeWithContentDescription("Place notes") .performScrollTo().performTextReplacement("Ask about the archives") assertEquals( listOf( LocationField.Name to "North Library", LocationField.Note to "Ask about the archives", ), edits, ) } @Test fun addressSaveUsesTheAddressFieldAfterItsControlledDraftChanges() { val state = mutableStateOf(detailState()) val saves = mutableListOf() show( state = state, callbacks = callbacks( edit = { field, value -> state.value = state.value.withDraft( field, state.value.draft(field).copy(value = value), ) }, save = saves::add, ), ) compose.onNodeWithContentDescription("More actions").performClick() compose.onNodeWithText("Edit address").performClick() compose.onNodeWithContentDescription("Address") .performTextReplacement("2 Test Way") compose.onNodeWithText("Save").performClick() assertEquals(listOf(LocationField.Address), saves) } @Test fun projectSelectionUsesTheExactProjectId() { val state = mutableStateOf( detailState( projects = listOf( LocationProject( id = "project-archive", slug = "archive", name = "Archive project", status = "active", createdAt = null, ), ), ), ) var selected: Pair? = null show(state, callbacks = callbacks(setProject = { id, linked -> selected = id to linked })) compose.onNodeWithContentDescription("More actions").performClick() compose.onNodeWithText("Projects…").performClick() compose.onNode(isToggleable()).performClick() assertEquals("project-archive" to true, selected) } @Test fun archiveNeedsConfirmationAndKeepDoesNotWrite() { val state = mutableStateOf(detailState()) var archives = 0 show(state, callbacks = callbacks(archive = { archives += 1 })) compose.onNodeWithContentDescription("More actions").performClick() compose.onNodeWithText("Archive").performClick() compose.onNodeWithText("Keep").performClick() assertEquals(0, archives) compose.onNodeWithContentDescription("More actions").performClick() compose.onNodeWithText("Archive").performClick() compose.onNodeWithText("Archive").performClick() assertEquals(1, archives) } @Test fun unknownOutcomeDisablesArchiveInsteadOfAdmittingAnotherWrite() { val state = mutableStateOf(detailState(outcomeUnknown = true)) show(state) compose.onNodeWithContentDescription("More actions").performClick() compose.onNodeWithText("Archive").assertIsNotEnabled() } @Test fun missingLocationAndReadFailureRemainDifferentStates() { val state = mutableStateOf( LocationDetailState( read = LocationRead( location = null, projects = emptyList(), projectLinks = emptyList(), ), ), ) show(state) compose.onNodeWithText("This place isn't here any more.").fetchSemanticsNode() state.value = LocationDetailState(readProblem = "Synthetic location read failure") compose.waitForIdle() compose.onNodeWithText("Synthetic location read failure").fetchSemanticsNode() compose.onNodeWithText("Refresh").fetchSemanticsNode() } private fun show( state: MutableState, callbacks: LocationDetailCallbacks = callbacks(), ) { compose.setContent { CottonTheme { LocationDetailScreen( state = state.value, clock = CLOCK, zone = ZoneOffset.UTC, callbacks = callbacks, ) } } } private fun callbacks( refresh: () -> Unit = {}, edit: (LocationField, String) -> Unit = { _, _ -> }, save: (LocationField) -> Unit = {}, resolve: (LocationField, Boolean) -> Unit = { _, _ -> }, setProject: (String, Boolean) -> Unit = { _, _ -> }, archive: () -> Unit = {}, ): LocationDetailCallbacks = LocationDetailCallbacks( refresh = refresh, edit = edit, save = save, resolve = resolve, setPin = {}, setProject = setProject, archive = archive, back = {}, ) private fun detailState( projects: List = emptyList(), projectLinks: List = emptyList(), outcomeUnknown: Boolean = false, ): LocationDetailState = LocationDetailState( read = LocationRead( location = LocationDetail( id = "location-1", name = "Library", address = "1 Test Way", note = "Original note", lat = 28.6139, lng = 77.2090, createdAt = "2026-09-22T10:00:00Z", ), projects = projects, projectLinks = projectLinks, ), name = LocationDraft(value = "Library", base = "Library"), address = LocationDraft(value = "1 Test Way", base = "1 Test Way"), note = LocationDraft(value = "Original note", base = "Original note"), outcomeUnknown = outcomeUnknown, ) private companion object { val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneOffset.UTC) } }