Cotton code
179 lines · 6.4 KB · KotlinRaw
package com.cottondb.android.ui.activityimport androidx.compose.ui.graphics.asAndroidBitmapimport androidx.compose.ui.test.assertIsNotEnabledimport androidx.compose.ui.test.captureToImageimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performClickimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.ScreenEvidenceimport com.cottondb.android.activitybin.ActivityAuditEntryimport com.cottondb.android.activitybin.ActivityReadimport com.cottondb.android.activitybin.ActivityReplicaimport com.cottondb.android.activitybin.ActivityWorkingimport com.cottondb.android.activitybin.AssistantRunimport com.cottondb.android.design.CottonThemeimport java.time.Clockimport java.time.Instantimport java.time.ZoneOffsetimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport 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<AssistantRun>(), 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<AssistantRun>()        val openedNotes = mutableListOf<String>()    }    private companion object {        val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneOffset.UTC)    }}