Cotton code
111 lines · 4.7 KB · KotlinRaw
package com.cottondb.android.ui.traffic.callsimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.sizeimport androidx.compose.ui.Modifierimport androidx.compose.ui.test.onAllNodesWithTextimport androidx.compose.ui.test.onAllNodesWithContentDescriptionimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.compose.ui.unit.dpimport androidx.test.ext.junit.runners.AndroidJUnit4import com.cottondb.android.core.FakeCoreimport com.cottondb.android.design.CottonThemeimport com.cottondb.android.traffic.calls.CallDetailModelimport com.cottondb.android.traffic.calls.CallHistoryModelimport com.cottondb.android.traffic.calls.CallsRepositoryimport java.time.Clockimport java.time.Instantimport java.time.ZoneIdimport kotlinx.coroutines.runBlockingimport kotlinx.serialization.json.Jsonimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithprivate val testAvatar: CallAvatar = { _, size -> Box(Modifier.size(size)) }@RunWith(AndroidJUnit4::class)class CallsScreensTest {    @get:Rule val compose = createComposeRule()    @Test    fun historyRowNavigatesToItsCallAndFilteredClearIsExplicit() {        val core = FakeCore().apply { answer("read.callLog", callLog()) }        val model = CallHistoryModel(CallsRepository(core), personId = "person-1")        var route: String? = null        var cleared = 0        compose.setContent {            CottonTheme {                CallsHistoryScreen(                    model = model,                    clock = CLOCK,                    zoneId = ZONE,                    dataRevision = 0,                    avatar = testAvatar,                    onBack = {},                    onClearPersonFilter = { cleared += 1 },                    onNavigate = { route = it },                )            }        }        compose.waitUntil { compose.onAllNodesWithContentDescription("Ada, incoming · 2 min").fetchSemanticsNodes().isNotEmpty() }        compose.onNodeWithContentDescription("Ada, incoming · 2 min").performClick()        compose.onNodeWithContentDescription("Show all calls").performClick()        compose.runOnIdle {            assertEquals("/calls/call/call-1", route)            assertEquals(1, cleared)        }    }    @Test    fun detailUnavailableRetriesAndCachedFailureStaysVisible(): Unit = runBlocking {        val core = FakeCore().apply { answer("read.call", kotlinx.serialization.json.JsonNull) }        val model = CallDetailModel(CallsRepository(core), "call-1")        compose.setContent {            CottonTheme {                CallDetailScreen(                    model = model,                    zoneId = ZONE,                    dataRevision = 0,                    avatar = testAvatar,                    onBack = {},                    onNavigate = {},                    onOpenUri = {},                )            }        }        compose.waitUntil { compose.onAllNodesWithText("This call could not be read").fetchSemanticsNodes().isNotEmpty() }        core.answer("read.call", callDetail())        compose.onNodeWithText("Try again").performClick()        compose.waitUntil { compose.onAllNodesWithText("Ada").fetchSemanticsNodes().isNotEmpty() }        core.fail("read.call", IllegalStateException("The local replica is unavailable."))        model.refresh()        compose.waitUntil { compose.onAllNodesWithText("The local replica is unavailable.").fetchSemanticsNodes().isNotEmpty() }        compose.onNodeWithText("Ada").assertExists()        compose.onNodeWithText("Try again").performClick()        compose.waitUntil { core.calls.count { it.method == "read.call" } >= 4 }        compose.onNodeWithText("Ada").assertExists()    }    private companion object {        val CLOCK: Clock = Clock.fixed(Instant.parse("2026-09-22T12:00:00Z"), ZoneId.of("UTC"))        val ZONE: ZoneId = ZoneId.of("UTC")        fun callLog() = Json.parseToJsonElement(            """{"calls":[{"id":"call-1","startedAt":"2026-09-22T10:15:00Z","provider":"cellular","direction":"in","outcome":"answered","durationS":120,"handle":"+491234","personId":"person-1","person":"Ada","avatarKey":null}]}""",        )        fun callDetail() = Json.parseToJsonElement(            """{"id":"call-1","startedAt":"2026-09-22T10:15:00Z","provider":"cellular","direction":"in","outcome":"answered","durationS":120,"handle":"+491234","personId":"person-1","person":"Ada","device":"Android test","avatarKey":null,"source":"android_call_log","externalId":"external-1","contactId":null,"meta":{},"recordedAt":"2026-09-22T10:16:00Z"}""",        )    }}