package com.cottondb.android.ui.search import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.captureToImage import androidx.compose.ui.graphics.asAndroidBitmap import com.cottondb.android.ScreenEvidence import androidx.compose.ui.test.performScrollTo 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.home.HomeTool import com.cottondb.android.search.SearchHit import com.cottondb.android.search.SearchKind import com.cottondb.android.search.SearchRead import com.cottondb.android.search.SearchSection import com.cottondb.android.search.SearchState import java.time.Clock import java.time.Instant import java.time.ZoneId import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class SearchScreenTest { @get:Rule val compose = createComposeRule() @Test fun restShowsOnlyTheSearchHint() { show(SearchState()) compose.onNodeWithContentDescription("Search hint").assertExists() compose.onNodeWithText("Todos").assertDoesNotExist() ScreenEvidence.capture("search-rest") { compose.onRoot().captureToImage().asAndroidBitmap() } } @Test fun oneCharacterSettledQueryIsStillAQuestionNotANoMatchVerdict() { show(SearchState(query = "x", settled = "x", failed = true)) compose.onNodeWithContentDescription("Search hint").assertExists() compose.onNodeWithContentDescription("search-failed").assertDoesNotExist() compose.onNodeWithContentDescription("search-empty").assertDoesNotExist() } @Test fun recentSearchReusesTheTypedCallbackAndClearStaysDeviceLocal() { var query = "" var clears = 0 show(SearchState(recents = listOf("synthetic query")), onQuery = { query = it }, onClear = { clears++ }) compose.onNodeWithContentDescription("Recent search synthetic query").performClick() compose.onNodeWithContentDescription("Clear recent searches").performClick() assertEquals("synthetic query", query) assertEquals(1, clears) } @Test fun matchingToolUsesTheExistingHomeDirectoryPath() { var opened: String? = null show(SearchState(query = "cal"), onPlace = { opened = it }) compose.onNodeWithContentDescription("Open Calendar").performClick() assertEquals("/calendar", opened) } @Test fun sectionsUseEditorialOrderCollapseAndExpandInPlace() { var expanded: SearchSection? = null val todos = (1..4).map { syntheticHit("todo-$it", "Synthetic todo $it") } val note = syntheticHit("note-1", "Synthetic note", SearchKind.Note) show(SearchState(query = "synthetic", settled = "synthetic", read = SearchRead(todos + note, mapOf(SearchSection.Todo to 4, SearchSection.Note to 1))), onExpand = { expanded = it }) compose.onNodeWithText("Todos").assertExists() compose.onNodeWithText("Notes").assertExists() ScreenEvidence.capture("search-results") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithText("see 1 more").performScrollTo().performClick() assertEquals(SearchSection.Todo, expanded) } @Test fun failedRefreshKeepsPriorResultsAndRoutesTheTypedHit() { var opened: SearchHit? = null val hit = syntheticHit("todo-1", "Synthetic todo") show(SearchState(query = "synthetic", settled = "synthetic", read = SearchRead(listOf(hit), emptyMap()), failed = true), onHit = { opened = it }) compose.onNodeWithContentDescription("Open Synthetic todo").performClick() compose.onNodeWithContentDescription("search-failed").assertDoesNotExist() assertEquals(hit, opened) } @Test fun inputIsControlledAndTheCloseCallbackLeavesSearch() { var query by mutableStateOf("") var closed = 0 compose.setContent { CottonTheme { SearchScreen( state = SearchState(query = query), places = places(), onQueryChange = { query = it }, onBack = { closed++ }, onClearRecents = {}, onOpenPlace = {}, onOpenHit = {}, onExpand = {}, clock = clock(), zoneId = ZoneId.of("UTC"), ) } } compose.onNodeWithContentDescription("Search everything").performTextInput("synthetic") compose.onNodeWithContentDescription("Close search").performClick() assertEquals("synthetic", query) assertEquals(1, closed) } @Test fun hitMetadataUsesTheInjectedClockAndZoneInsteadOfRawIso() { val clock = clock() val relative = syntheticHit("todo-1", "Synthetic todo").copy(whenAt = "2026-09-22T11:30:00Z") val old = syntheticHit("todo-2", "Synthetic old").copy(whenAt = "2025-08-17T00:00:00Z") val document = syntheticHit("document-1", "Synthetic document", SearchKind.Document).copy(page = 7L, whenAt = "2020-01-01T00:00:00Z") assertEquals("30m ago", searchHitMeta(relative, clock, ZoneId.of("UTC"))) assertEquals("17 Aug 2025", searchHitMeta(old, clock, ZoneId.of("UTC"))) assertEquals("Page 7", searchHitMeta(document, clock, ZoneId.of("UTC"))) } private fun show( state: SearchState, onQuery: (String) -> Unit = {}, onClear: () -> Unit = {}, onPlace: (String) -> Unit = {}, onHit: (SearchHit) -> Unit = {}, onExpand: (SearchSection) -> Unit = {}, ) { compose.setContent { CottonTheme { SearchScreen(state, places(), onQuery, {}, onClear, onPlace, onHit, onExpand, clock(), ZoneId.of("UTC")) } } } private fun places() = listOf(HomeTool("calendar", "Calendar", null, "/calendar"), HomeTool("todos", "Todos", null, "/todos")) private fun syntheticHit(id: String, title: String, kind: SearchKind = SearchKind.Todo) = SearchHit( kind = kind, id = id, title = title, snippet = null, whenAt = null, struck = false, journal = false, threadId = null, sentAt = null, page = null, repoId = null, number = null, tag = null, ) private fun clock(): Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneId.of("UTC")) }