Cotton code
213 lines · 8.0 KB · KotlinRaw
package com.cottondb.android.ui.day.peopleimport androidx.compose.foundation.backgroundimport androidx.compose.foundation.verticalScrollimport androidx.compose.foundation.rememberScrollStateimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.aspectRatioimport androidx.compose.ui.Modifierimport androidx.compose.ui.unit.dpimport com.cottondb.android.generated.CottonPaletteimport com.cottondb.android.ui.media.MediaImageimport com.cottondb.android.media.LocalMediaSourceimport com.cottondb.android.media.LocalMediaimport com.cottondb.android.media.MediaPathTierimport androidx.compose.ui.test.performScrollToimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.captureToImageimport 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.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.ScreenEvidenceimport com.cottondb.android.day.people.PersonFactWhenKindimport com.cottondb.android.day.people.PersonLinkedFactimport com.cottondb.android.day.people.PersonLinkimport com.cottondb.android.day.people.PersonSectionsimport com.cottondb.android.day.people.PersonTodoimport com.cottondb.android.design.CottonThemeimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport java.time.Clockimport java.time.Instantimport java.time.ZoneId@RunWith(AndroidJUnit4::class)class PersonSectionsScreenTest {    @get:Rule val compose = createComposeRule()    @Test    fun previewRoutesLinkedRecordsToTheirCanonicalDestinations() {        val routes = mutableListOf<String>()        var localMediaRenders = 0        val sections = PersonSections(            notes = emptyList(),            links = listOf(                link("note-link", "note", "note-1"),                link("event-link", "event", "event-1"),                link("document-link", "document", "document-1"),                link("photo-link", "photo", "photo-1"),            ),            linkedFacts = mapOf(                "note-link" to fact("Note route"),                "event-link" to fact("Event route"),                "document-link" to fact("Document route", docType = "Legal", pageCount = 2),                "photo-link" to fact("Photo route", key = "photos/photo-1"),            ),            todos = listOf(PersonTodo("todo-1", "Todo route", "open", null, null, null)),            calls = emptyList(),            faceSuggestions = emptyList(),            faceSuggestionPhotos = emptyList(),            money = emptyList(),        )        compose.setContent {            CottonTheme {                Column(Modifier.fillMaxSize().background(CottonPalette.groundMuted).verticalScroll(rememberScrollState()).padding(24.dp)) {                PersonSectionsPreview(                    personId = "person-1",                    personName = "Ada",                    sections = sections,                    onNavigate = { routes += it },                    clock = clock,                    zoneId = zone,                    media = { key, description, modifier ->                        localMediaRenders += 1                        MediaImage(key, object : LocalMediaSource {                            override suspend fun local(key: String, tier: MediaPathTier) = LocalMedia.Absent                        }, 0, 120.dp, modifier.aspectRatio(1f), description)                    },                )                }            }        }        ScreenEvidence.capture("person-sections-preview") { compose.onRoot().captureToImage().asAndroidBitmap() }        compose.onNodeWithContentDescription("Open Todo route").performScrollTo().performClick()        compose.onNodeWithContentDescription("Open Note route").performScrollTo().performClick()        compose.onNodeWithContentDescription("Open Event route").performScrollTo().performClick()        compose.onNodeWithContentDescription("Open Document route").performScrollTo().performClick()        compose.onNodeWithContentDescription("Open Photo route").performScrollTo().performClick()        compose.runOnIdle {            assertEquals(                listOf(                    "/todos/todo/todo-1",                    "/notes/note/note-1",                    "/calendar/event/event-1",                    "/documents/document/document-1",                    "/photos/photo/photo-1",                ),                routes,            )            // The renderer has only the local render slot. It cannot call media.ensure.            assertTrue(localMediaRenders > 0)        }    }    @Test    fun linkedRecordWithoutFactsRemainsReadableAndEmptySectionsStayEmpty() {        var route: String? = null        val incomplete = PersonSections(            notes = emptyList(),            links = listOf(link("thought-link", "thought", "thought-1")),            linkedFacts = emptyMap(),            todos = emptyList(),            calls = emptyList(),            faceSuggestions = emptyList(),            faceSuggestionPhotos = emptyList(),            money = emptyList(),        )        compose.setContent {            CottonTheme {                PersonSectionScreen(                    personId = "person-1",                    personName = "Ada",                    sectionRoute = "linked",                    sections = incomplete,                    onBack = {},                    onNavigate = { route = it },                    clock = clock,                    zoneId = zone,                )            }        }        compose.onNodeWithContentDescription("Open Thought").assertExists().performScrollTo().performClick()        compose.runOnIdle { assertEquals("/thoughts/thought/thought-1", route) }    }    @Test    fun emptySectionExplainsThereIsNothingToShow() {        compose.setContent {            CottonTheme {                PersonSectionScreen(                    personId = "person-1",                    personName = "Ada",                    sectionRoute = "notes",                    sections = emptySections(),                    onBack = {},                    onNavigate = {},                    clock = clock,                    zoneId = zone,                )            }        }        compose.onNodeWithText("Nothing here yet.").assertExists()        compose.onNodeWithContentDescription("Open all Todos").assertDoesNotExist()    }    private companion object {        val zone: ZoneId = ZoneId.of("Europe/Berlin")        val clock: Clock = Clock.fixed(Instant.parse("2026-09-22T10:00:00Z"), zone)        fun link(id: String, kind: String, recordId: String): PersonLink = PersonLink(            id = id,            workspaceId = null,            fromKind = "person",            fromId = "person-1",            toKind = kind,            toId = recordId,            createdAt = null,        )        fun fact(            title: String,            docType: String? = null,            pageCount: Long? = null,            key: String? = null,        ): PersonLinkedFact = PersonLinkedFact(            title = title,            body = null,            rawText = null,            container = null,            whenIso = "2026-09-21",            whenKind = PersonFactWhenKind.Made,            filedAway = null,            docType = docType,            pageCount = pageCount,            key = key,            posterKey = null,        )        fun emptySections(): PersonSections = PersonSections(            notes = emptyList(),            links = emptyList(),            linkedFacts = emptyMap(),            todos = emptyList(),            calls = emptyList(),            faceSuggestions = emptyList(),            faceSuggestionPhotos = emptyList(),            money = emptyList(),        )    }}