package com.cottondb.android.ui.activity import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.test.assertIsNotEnabled 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.activitybin.BinEntry import com.cottondb.android.activitybin.BinRead import com.cottondb.android.design.CottonTheme import java.time.Clock import java.time.Instant import java.time.ZoneOffset import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class BinScreenTest { @get:Rule val compose = createComposeRule() @Test fun emptyConfirmationCancelsThenRechecksWriteAdmissionBeforeEmptying() { val calls = Calls() val canWrite = mutableStateOf(true) show(read(), canWrite = canWrite, calls = calls) ScreenEvidence.capture("bin-list") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Empty the bin").performClick() compose.onNodeWithText("Cancel").performClick() assertEquals(0, calls.empty) compose.onNodeWithContentDescription("Empty the bin").performClick() compose.runOnIdle { canWrite.value = false } compose.onNodeWithContentDescription("Confirm empty bin").assertIsNotEnabled() assertEquals(0, calls.empty) compose.runOnIdle { canWrite.value = true } compose.onNodeWithContentDescription("Confirm empty bin").performClick() assertEquals(1, calls.empty) } @Test fun restoreIsExplicitAndCachedReadFailureRetainsRowsButBlocksWritesAndRetries() { val calls = Calls() show(read(), readProblem = "Replica unavailable", canWrite = mutableStateOf(false), calls = calls) compose.onNodeWithText("Replica unavailable").assertExists() compose.onNodeWithContentDescription("Restore Invoice 1").assertIsNotEnabled() compose.onNodeWithContentDescription("Empty the bin").assertIsNotEnabled() compose.onNodeWithContentDescription("Try again").performClick() assertEquals(1, calls.retry) assertEquals(emptyList(), calls.restored) } @Test fun restoreAndLayoutToggleDelegateWithoutOpeningRows() { val calls = Calls() show(read(), isGrid = false, calls = calls) compose.onNodeWithContentDescription("Change bin layout").performClick() compose.onNodeWithContentDescription("Restore Invoice 1").performClick() assertEquals(true, calls.layout) assertEquals(listOf(BinEntry("email", "email-1", "Invoice 1", "2026-09-22T11:00:00Z")), calls.restored) } @Test fun initialFailureOffersRetry() { val calls = Calls() show(read = null, loading = false, readProblem = "Offline", calls = calls) compose.onNodeWithText("Couldn't read the bin").assertExists() compose.onNodeWithContentDescription("Try again").performClick() assertEquals(1, calls.retry) } @Test fun gridKeepsAnExplicitRestoreForEachKind() { val calls = Calls() show(read(), isGrid = true, calls = calls) ScreenEvidence.capture("bin-grid") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Restore Invoice 1").performClick() assertEquals(listOf("email-1"), calls.restored.map { it.id }) } @Test fun oneItemBinHasNoLayoutToggle() { val one = Calls() show(BinRead(listOf(BinEntry("email", "one", "One message", "2026-09-22T11:00:00Z")), mapOf("email" to 1L), null), calls = one) compose.onNodeWithContentDescription("Change bin layout").assertDoesNotExist() } private fun show( read: BinRead?, loading: Boolean = false, readProblem: String? = null, writeProblem: String? = null, canWrite: androidx.compose.runtime.MutableState = mutableStateOf(true), isGrid: Boolean = false, calls: Calls, ) { compose.setContent { CottonTheme { BinScreen( read = read, loading = loading, readProblem = readProblem, writeProblem = writeProblem, canWrite = canWrite.value, isGrid = isGrid, onLayoutChange = { calls.layout = it }, onRetry = { calls.retry += 1 }, onRestore = { calls.restored += it }, onEmpty = { calls.empty += 1 }, onBack = { calls.back += 1 }, clock = CLOCK, ) } } } private fun read() = BinRead( entries = listOf( BinEntry("email", "email-1", "Invoice 1", "2026-09-22T11:00:00Z"), BinEntry("secret", "secret-1", "Synthetic password", "2026-09-21T11:00:00Z"), ), counts = mapOf("email" to 1L, "secret" to 1L), note = null, ) private class Calls { var retry = 0 var empty = 0 var back = 0 var layout: Boolean? = null val restored = mutableListOf() } private companion object { val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneOffset.UTC) } }