package com.cottondb.android.ui.day.people import androidx.compose.foundation.background import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.aspectRatio import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.cottondb.android.generated.CottonPalette import com.cottondb.android.ui.media.MediaImage import com.cottondb.android.media.LocalMediaSource import com.cottondb.android.media.LocalMedia import com.cottondb.android.media.MediaPathTier import androidx.compose.ui.test.performScrollTo import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.test.captureToImage 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.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.ScreenEvidence import com.cottondb.android.day.people.PersonFactWhenKind import com.cottondb.android.day.people.PersonLinkedFact import com.cottondb.android.day.people.PersonLink import com.cottondb.android.day.people.PersonSections import com.cottondb.android.day.people.PersonTodo import com.cottondb.android.design.CottonTheme import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import java.time.Clock import java.time.Instant import java.time.ZoneId @RunWith(AndroidJUnit4::class) class PersonSectionsScreenTest { @get:Rule val compose = createComposeRule() @Test fun previewRoutesLinkedRecordsToTheirCanonicalDestinations() { val routes = mutableListOf() 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(), ) } }