Cotton code
152 lines · 6.5 KB · KotlinRaw
package com.cottondb.android.ui.searchimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.graphics.asAndroidBitmapimport com.cottondb.android.ScreenEvidenceimport androidx.compose.ui.test.performScrollToimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTextInputimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.design.CottonThemeimport com.cottondb.android.home.HomeToolimport com.cottondb.android.search.SearchHitimport com.cottondb.android.search.SearchKindimport com.cottondb.android.search.SearchReadimport com.cottondb.android.search.SearchSectionimport com.cottondb.android.search.SearchStateimport java.time.Clockimport java.time.Instantimport java.time.ZoneIdimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport 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"))}