238 lines · 9.3 KB · KotlinRaw
package com.cottondb.android.ui.passwordsimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.compose.ui.test.assertIsNotEnabledimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onAllNodesWithTextimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performScrollToimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.ScreenEvidenceimport com.cottondb.android.design.CottonThemeimport com.cottondb.android.generated.SecretKindimport com.cottondb.android.passwords.PasswordFactsimport com.cottondb.android.passwords.PasswordPlacementimport com.cottondb.android.passwords.PasswordProjectimport com.cottondb.android.passwords.PasswordSecretimport com.cottondb.android.passwords.PasswordSecretReadimport com.cottondb.android.passwords.PasswordVaultStatusimport com.cottondb.android.passwords.SecretDetailStateimport com.cottondb.android.passwords.SecretValueStateimport java.time.Clockimport java.time.Instantimport java.time.ZoneIdimport java.util.concurrent.atomic.AtomicIntegerimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWith@RunWith(AndroidJUnit4::class)class SecretDetailScreenTest { @get:Rule val compose = createComposeRule() @Test fun lockedSecretRefusesRevealAndNeverRendersPayloadPlaintext() { var reveals = 0 var locks = 0 show(state = data(wrapped = false), onReveal = { reveals++ }, onLock = { locks++ }) compose.onNodeWithTag("secret-locked").assertExists() compose.onNodeWithTag("secret-reveal").assertDoesNotExist() assertTrue(compose.onAllNodesWithText("synthetic plaintext", substring = true).fetchSemanticsNodes().isEmpty()) compose.onNodeWithTag("secret-lock").performScrollTo().performClick() assertEquals(0, reveals) assertEquals(1, locks) } @Test fun unsetSecretCannotRevealOrCopyEvenWhenTheVaultIsAvailable() { var reveals = 0 show(state = data(isSet = false), onReveal = { reveals++ }) compose.onNodeWithTag("secret-reveal").assertDoesNotExist() compose.onNodeWithTag("secret-copy-password").assertDoesNotExist() compose.onNodeWithText("•••• last4").assertExists() assertEquals(0, reveals) } @Test fun uncachedReadErrorIsNotPresentedAsAMissingPassword() { show(state = SecretDetailState.Error("Synthetic uncached failure", null)) compose.onNodeWithTag("secret-read-failed").assertExists() compose.onNodeWithTag("secret-missing").assertDoesNotExist() compose.onNodeWithTag("secret-retry").assertExists() } @Test fun sealedPrimarySendsRevealAndCopyCallbacks() { val reveals = AtomicInteger() val copied = mutableListOf<String>() show(onReveal = { reveals.incrementAndGet() }, onCopy = { copied += it }) compose.onNodeWithTag("secret-field-password").performScrollTo().assertExists() compose.onNodeWithTag("secret-field-notes").assertDoesNotExist() compose.onNodeWithTag("secret-reveal").performScrollTo().performClick() compose.onNodeWithTag("secret-copy-password").performScrollTo().performClick() assertEquals(1, reveals.get()) assertEquals(listOf("password"), copied) } @Test fun openedPayloadShowsOptionalFieldsAndHideCallback() { val hides = AtomicInteger() show( values = SecretValueState(PasswordFacts.payload(SecretKind.Login, mapOf( "password" to "synthetic plaintext", "notes" to "synthetic optional note", ))), onHide = { hides.incrementAndGet() }, ) compose.onNodeWithTag("secret-field-notes").performScrollTo().assertExists() compose.onNodeWithText("synthetic optional note").performScrollTo().assertExists() compose.onNodeWithTag("secret-reveal").performScrollTo().performClick() assertEquals(1, hides.get()) } @Test fun binCallsItsCallbackOnlyAfterConfirmation() { val trash = AtomicInteger() show(onTrash = { trash.incrementAndGet() }) compose.onNodeWithTag("secret-trash").performScrollTo().performClick() compose.onNodeWithText("Cancel").performClick() assertEquals(0, trash.get()) compose.onNodeWithTag("secret-trash").performScrollTo().performClick() compose.onNodeWithTag("secret-confirm-trash").performClick() assertEquals(1, trash.get()) } @Test fun restoreCallsItsCallbackOnlyAfterConfirmation() { val restore = AtomicInteger() show(state = data(trashed = true), onRestore = { restore.incrementAndGet() }) compose.onNodeWithTag("secret-restore").performScrollTo().performClick() compose.onNodeWithText("Cancel").performClick() assertEquals(0, restore.get()) compose.onNodeWithTag("secret-restore").performScrollTo().performClick() compose.onNodeWithTag("secret-confirm-restore").performClick() assertEquals(1, restore.get()) } @Test fun confirmationCannotCommitWhenTheModelBecomesWorking() { var working by mutableStateOf(false) compose.setContent { CottonTheme { SecretDetailScreen( state = data().copy(working = working), values = SecretValueState(), clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneId.of("Europe/Berlin")), zoneId = ZoneId.of("Europe/Berlin"), onBack = {}, onReveal = {}, onHide = {}, onCopy = {}, onEdit = {}, onTrash = {}, onRestore = {}, onRetry = {}, onProject = {}, onLock = {}, ) } } compose.onNodeWithTag("secret-trash").performScrollTo().performClick() compose.runOnIdle { working = true } compose.onNodeWithTag("secret-confirm-trash").assertIsNotEnabled() } @Test fun cachedReadErrorKeepsMetadataAndRetries() { val retries = AtomicInteger() show(state = SecretDetailState.Error("Synthetic cached failure", data().read), onRetry = { retries.incrementAndGet() }) compose.onNodeWithText("Synthetic secret").assertExists() compose.onNodeWithTag("secret-error").assertExists() compose.onNodeWithTag("secret-retry").performScrollTo().performClick() assertEquals(1, retries.get()) } @Test fun metadataOnlyDetailHasReviewablePaperEvidence() { show() ScreenEvidence.capture("secret-detail-metadata") { compose.onRoot().captureToImage().asAndroidBitmap() } } private fun show( state: SecretDetailState = data(), values: SecretValueState = SecretValueState(), onReveal: () -> Unit = {}, onHide: () -> Unit = {}, onCopy: (String) -> Unit = {}, onTrash: () -> Unit = {}, onRestore: () -> Unit = {}, onRetry: () -> Unit = {}, onLock: () -> Unit = {}, ) { compose.setContent { CottonTheme { SecretDetailScreen( state = state, values = values, clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneId.of("Europe/Berlin")), zoneId = ZoneId.of("Europe/Berlin"), onBack = {}, onReveal = onReveal, onHide = onHide, onCopy = onCopy, onEdit = {}, onTrash = onTrash, onRestore = onRestore, onRetry = onRetry, onProject = {}, onLock = onLock, ) } } } private companion object { fun data(wrapped: Boolean = true, trashed: Boolean = false, isSet: Boolean = true): SecretDetailState.Data { val secret = PasswordSecret( id = "secret-synthetic", kind = "login", name = "Synthetic secret", site = "https://example.invalid", username = "synthetic-user", service = null, hint = "last4", isSet = isSet, legacy = false, lastUsedAt = null, expiresAt = null, createdAt = "2026-09-20T12:00:00Z", updatedAt = "2026-09-21T12:00:00Z", trashedAt = if (trashed) "2026-09-22T12:00:00Z" else null, ) return SecretDetailState.Data( PasswordSecretRead( secret = secret, placements = listOf(PasswordPlacement("placement-synthetic", "device", "/synthetic/path", 0, "env", null, null, null)), projects = listOf(PasswordProject("project-synthetic", "synthetic-project", "Synthetic project")), status = PasswordVaultStatus(true, wrapped, false, null, 0, 1), ), ) } }}