package com.cottondb.android.ui.activity 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.ActivityAuditEntry import com.cottondb.android.activitybin.ActivityRead import com.cottondb.android.activitybin.ActivityReplica import com.cottondb.android.activitybin.ActivityWorking import com.cottondb.android.activitybin.AssistantRun 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 ActivityScreenTest { @get:Rule val compose = createComposeRule() @Test fun queuedRunCancelsAndResultNoteNavigatesWhileFailedRunNeverOffersRetry() { val calls = Calls() show(read = read(), calls = calls) ScreenEvidence.capture("activity") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Cancel Queue synthetic work").performClick() compose.onNodeWithContentDescription("Open the note").performClick() compose.onNodeWithText("A server-written failed reason.").assertExists() compose.onNodeWithContentDescription("Cancel Failed synthetic work").assertDoesNotExist() compose.onNodeWithText("Retry").assertDoesNotExist() assertEquals(listOf("queued"), calls.cancelled.map { it.id }) assertEquals(listOf("note-1"), calls.openedNotes) } @Test fun cachedReadFailureStaysVisibleAndRetryDelegatesToHost() { val calls = Calls() show(read = read(), readProblem = "Synthetic replica failure", calls = calls) compose.onNodeWithText("Synthetic replica failure").assertExists() compose.onNodeWithText("Queue synthetic work").assertExists() compose.onNodeWithContentDescription("Try again").performClick() assertEquals(1, calls.retries) } @Test fun initialReadUsesLoadingPaperInsteadOfAnUnreadableFailure() { val calls = Calls() show(read = null, calls = calls) compose.onNodeWithText("Activity could not be read.").assertDoesNotExist() compose.onNodeWithContentDescription("Try again").assertDoesNotExist() } @Test fun unreadableActivityExplainsTheReadFailureAndRetries() { val calls = Calls() show(read = null, readProblem = "Synthetic native failure", calls = calls) compose.onNodeWithText("Activity could not be read.").assertExists() compose.onNodeWithText("This phone could not read its own database just now.").assertExists() compose.onNodeWithText("Synthetic native failure").assertExists() compose.onNodeWithContentDescription("Try again").performClick() assertEquals(1, calls.retries) } @Test fun cancellationStaysVisibleButDisabledWhenHostCannotAdmitAWrite() { val calls = Calls() show(read = read(), canWrite = false, calls = calls) compose.onNodeWithContentDescription("Cancel Queue synthetic work").assertIsNotEnabled() compose.onNodeWithContentDescription("Open the note").performClick() assertEquals(emptyList(), calls.cancelled) assertEquals(listOf("note-1"), calls.openedNotes) } private fun show( read: ActivityRead?, loading: Boolean = false, readProblem: String? = null, canWrite: Boolean = true, calls: Calls, ) { compose.setContent { CottonTheme { ActivityScreen( read = read, loading = loading, readProblem = readProblem, writeProblem = null, canWrite = canWrite, onRetry = { calls.retries += 1 }, onCancel = { calls.cancelled += it }, onOpenNote = { calls.openedNotes += it }, onBack = { calls.backs += 1 }, clock = CLOCK, ) } } } private fun read() = ActivityRead( replica = ActivityReplica("workspace", "device", "replica", 1L), runs = listOf( run(id = "queued", state = "queued", task = "Queue synthetic work", resultNoteId = "note-1"), run(id = "failed", state = "failed", task = "Failed synthetic work", error = "A server-written failed reason."), ), working = ActivityWorking(1L, "Queue synthetic work", "2026-09-22T10:00:00Z"), audit = listOf( ActivityAuditEntry( id = "audit-1", tokenId = "token-1", tokenName = "Synthetic connection", tool = "read.activity", ok = true, argsSummary = "server-written args", createdAt = "2026-09-22T10:00:00Z", ), ), auditTruncated = false, ) private fun run( id: String, state: String, task: String, error: String? = null, resultNoteId: String? = null, ) = AssistantRun( id = id, sourceKind = "assistant", sourceId = "source-$id", recordKind = null, recordId = null, sessionId = null, runClass = "quick", state = state, task = task, receipt = "A server-written receipt.", error = error, turns = 1L, inputTokens = 0L, outputTokens = 0L, costUsd = null, resultNoteId = resultNoteId, stoppedReason = "answered", queuedAt = "2026-09-22T10:00:00Z", startedAt = null, finishedAt = null, ) private class Calls { var retries = 0 var backs = 0 val cancelled = mutableListOf() val openedNotes = mutableListOf() } private companion object { val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneOffset.UTC) } }