package com.cottondb.android.ui.traffic.calls import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.size import androidx.compose.ui.Modifier import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onAllNodesWithContentDescription import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.unit.dp import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.core.FakeCore import com.cottondb.android.design.CottonTheme import com.cottondb.android.traffic.calls.CallDetailModel import com.cottondb.android.traffic.calls.CallHistoryModel import com.cottondb.android.traffic.calls.CallsRepository import java.time.Clock import java.time.Instant import java.time.ZoneId import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith private 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"}""", ) } }