package com.cottondb.android.ui.photos import androidx.compose.ui.test.assertIsNotEnabled import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.design.CottonTheme import com.cottondb.android.media.LocalMedia import com.cottondb.android.media.LocalMediaSource import com.cottondb.android.media.MediaPathTier import com.cottondb.android.navigation.Route import com.cottondb.android.traffic.photos.albums.AddableAlbumPhoto import com.cottondb.android.traffic.photos.albums.AlbumAddState import com.cottondb.android.traffic.photos.albums.AlbumCard import com.cottondb.android.traffic.photos.albums.AlbumDecision import com.cottondb.android.traffic.photos.albums.AlbumListState 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 AlbumCollectionScreensTest { @get:Rule val compose = createComposeRule() @Test fun firstReadErrorShowsRetryBeforeLoading() { var retries = 0 list(AlbumListState(readProblem = "offline"), onRetry = { retries++ }) compose.onNodeWithText("Your albums could not be read").assertExists() compose.onNodeWithText("Try again").performClick() assertEquals(1, retries) } @Test fun cachedReadErrorKeepsRowsVisible() { list(AlbumListState(albums = listOf(card("a")), hasRead = true, readProblem = "offline")) compose.onNodeWithContentDescription("Album Album a").assertExists() compose.onNodeWithText("offline").assertExists() } @Test fun listKeepDismissAndOpenCarryExactIdAndNativeOrder() { val decisions = mutableListOf>(); val opened = mutableListOf() list(AlbumListState(albums = listOf(card("first"), card("second")), hasRead = true), onDecide = { id, decision -> decisions += id to decision }, onOpen = { opened += it }) compose.onNodeWithContentDescription("Album Album first").performClick() compose.onAllNodesWithText("Keep")[0].performClick(); compose.onAllNodesWithText("Not this")[0].performClick() assertEquals(Route.PhotosAlbum("first"), opened.single()) assertEquals(listOf("first" to AlbumDecision.Keep, "first" to AlbumDecision.Dismiss), decisions) } @Test fun unknownWriteShowsProblemAndDisablesDecisions() { list(AlbumListState(albums = listOf(card("a")), hasRead = true, outcomeUnknown = true, writeProblem = "Could not confirm")) compose.onNodeWithText("Could not confirm").assertExists() compose.onNodeWithText("Keep").assertIsNotEnabled() compose.onNodeWithText("Not this").assertIsNotEnabled() } @Test fun listLayoutCallbackIsControlled() { var choice: Boolean? = null list(AlbumListState(albums = listOf(card("a")), hasRead = true), prints = true, onPrintsChange = { choice = it }) compose.onNodeWithText("List").performClick() assertEquals(false, choice) } @Test fun addToggleAndSelectedAddAreControlled() { var toggled: String? = null; var added = 0 add(AlbumAddState(photos = listOf(photo("photo:a")), hasRead = true, selected = setOf("photo:a")), onToggle = { toggled = it }, onAdd = { added++ }) compose.onNodeWithContentDescription("Photo tile photo:a: Photo").performClick() compose.onNodeWithContentDescription("Add the selected pictures").performClick() assertEquals("photo:a", toggled); assertEquals(1, added) } @Test fun unknownAddIsDisabledAndEmptyCopyMatchesSwift() { add(AlbumAddState(photos = listOf(photo("photo:a")), hasRead = true, selected = setOf("photo:a"), outcomeUnknown = true)) compose.onNodeWithContentDescription("Add the selected pictures").assertIsNotEnabled() } @Test fun emptyAddCopyMatchesSwift() { add(AlbumAddState(hasRead = true)) compose.onNodeWithText("Nothing left to add.").assertExists() compose.onNodeWithText("Every picture that can go in this album is already in it.").assertExists() } private fun list(state: AlbumListState, prints: Boolean = true, onRetry: () -> Unit = {}, onPrintsChange: (Boolean) -> Unit = {}, onDecide: (String, AlbumDecision) -> Unit = { _, _ -> }, onOpen: (Route) -> Unit = {}) = compose.setContent { CottonTheme { AlbumListScreen(state, AbsentMedia, 0, prints, ZoneId.of("Etc/UTC"), onPrintsChange, {}, onRetry, onDecide, onOpen) } } private fun add(state: AlbumAddState, onToggle: (String) -> Unit = {}, onAdd: () -> Unit = {}) = compose.setContent { CottonTheme { AlbumAddScreen(state, AbsentMedia, 0, Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneId.of("Etc/UTC")), {}, {}, onToggle, onAdd) } } private fun card(id: String) = AlbumCard(id, "trip", "open", null, "Album $id", null, "2026-01-01", "2026-01-01", null, null, 1, emptyList(), null, null, null) private fun photo(id: String) = AddableAlbumPhoto(id, null, null, null, null, null, null, "2026-01-01", 1, null, null, null, null, null, false, emptyList(), null, null, null, null, null, emptyList()) private object AbsentMedia : LocalMediaSource { override suspend fun local(key: String, tier: MediaPathTier) = LocalMedia.Absent } }