package com.cottondb.android.ui.locations import androidx.compose.runtime.key import androidx.compose.runtime.mutableLongStateOf import androidx.compose.ui.graphics.asAndroidBitmap import androidx.compose.ui.test.captureToImage import androidx.compose.ui.test.onRoot import com.cottondb.android.ScreenEvidence 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.test.performTextReplacement import androidx.compose.ui.test.performTextClearance import androidx.compose.ui.test.performTextInput import androidx.test.ext.junit.runners.AndroidJUnit4 import com.cottondb.android.LocationsModels import com.cottondb.android.core.CottonCalling import com.cottondb.android.core.CottonEvent import com.cottondb.android.core.MalformedCoreEventException import com.cottondb.android.design.CottonTheme import com.cottondb.android.navigation.Route import java.time.Clock import java.time.Instant import java.time.ZoneOffset import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive import kotlinx.serialization.json.put import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class LocationsRouteTest { @get:Rule val compose = createComposeRule() private var fixture: Fixture? = null @After fun close() { fixture?.let { current -> compose.runOnIdle { current.models.retain(emptyList()); current.scope.cancel() }; fixture = null } } @Test fun verifiedCreateNavigatesOnceAndDoesNotReplayOnRemount() { val f = show(Route.Locations) compose.waitUntil(5_000) { f.core.locationReads > 0 } compose.waitForIdle() ScreenEvidence.capture("locations-postcards-route") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Add a place").performClick() compose.onNodeWithContentDescription("Place name").performTextInput(" Library ") compose.onNodeWithContentDescription("Address").performTextInput(" 1 Test Way ") compose.onNodeWithText("Add place").performClick() compose.waitUntil(5_000) { f.opened.size == 1 } compose.runOnIdle { f.remount.longValue++ } compose.waitForIdle() assertEquals(listOf(Route.LocationsLocation(ID)), f.opened) assertEquals("create_location", f.core.writes.single()["tool"]!!.jsonPrimitive.content) assertEquals("Library", f.core.writes.single()["args"]!!.jsonObject["name"]!!.jsonPrimitive.content) assertEquals("1 Test Way", f.core.writes.single()["args"]!!.jsonObject["address"]!!.jsonPrimitive.content) } @Test fun detailRenameReceiptSurvivesRemountAndArchivePopsOnce() { val f = show(Route.LocationsLocation(ID)) compose.waitUntil(5_000) { f.core.detailReads > 0 } compose.waitForIdle() ScreenEvidence.capture("location-detail-route") { compose.onRoot().captureToImage().asAndroidBitmap() } compose.onNodeWithContentDescription("Place name").performTextReplacement("After") compose.onNodeWithContentDescription("Save place name").performClick() compose.waitUntil(5_000) { f.core.writes.any { it["tool"]!!.jsonPrimitive.content == "update_location" } } compose.runOnIdle { f.remount.longValue++ } compose.waitForIdle() assertEquals("After", f.models.detail(Route.LocationsLocation(ID)).state.value.name.value) compose.onNodeWithContentDescription("More actions").performClick() compose.onNodeWithText("Archive").performClick() compose.onNodeWithText("Archive").performClick() compose.waitUntil(5_000) { f.back == 1 } compose.runOnIdle { f.remount.longValue++ } compose.waitForIdle() assertEquals(1, f.back) } @Test fun retainClosesDepartingDetailRoute() { val f = show(Route.LocationsLocation(ID)) compose.waitUntil(5_000) { f.core.detailReads > 0 } lateinit var retired: com.cottondb.android.day.locations.LocationDetailModel compose.runOnIdle { retired = f.models.detail(Route.LocationsLocation(ID)); f.models.retain(emptyList()) } compose.runOnIdle { assertNull(retired.refresh()) } } private fun show(route: Route): Fixture { val fresh = Fixture(); fixture = fresh compose.setContent { CottonTheme { key(fresh.remount.longValue) { LocationsRoute(route, fresh.models, CLOCK, 0, { fresh.back += 1 }, { route -> fresh.opened += route }) } } } return fresh } private class Fixture { val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) val core = Core() val models = LocationsModels(core, scope) val remount = mutableLongStateOf(0) val opened = mutableListOf() var back = 0 } private class Core : CottonCalling { override val events: SharedFlow = MutableSharedFlow() override val eventErrors: SharedFlow = MutableSharedFlow() var locationReads = 0 var detailReads = 0 var name = "Before" val writes = mutableListOf() override suspend fun call(method: String, request: JsonObject): JsonElement = when (method) { "read.locations" -> { locationReads++; locations() } "read.location" -> { detailReads++; detail(name) } "write" -> { writes += request val tool = request["tool"]!!.jsonPrimitive.content val args = request["args"]!!.jsonObject when (tool) { "create_location" -> answer(row("Library", "1 Test Way")) "update_location" -> { args["name"]?.jsonPrimitive?.content?.let { name = it }; answer(row(name, "Address")) } "archive_location" -> answer(buildJsonObject { put("id", ID); put("name", name); put("archived", true) }) else -> error(tool) } } else -> error(method) } override suspend fun close() = Unit } } private const val ID = "01234567-89ab-cdef-0123-456789abcdef" private val CLOCK = Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC) private fun answer(value: JsonObject) = buildJsonObject { put("answer", value) } private fun row(name: String, address: String?) = buildJsonObject { put("id", ID); put("name", name); put("address", address); put("lat", null); put("lng", null); put("note", null) } private fun locations() = Json.parseToJsonElement("""{"locations":[{"id":"$ID","name":"Before","address":"Address","lat":null,"lng":null,"note":null,"updatedAt":null,"linkCount":0}]}""") private fun detail(name: String) = Json.parseToJsonElement("""{"location":{"id":"$ID","name":"$name","address":"Address","note":null,"lat":null,"lng":null,"created_at":null},"projects":[],"projectLinks":[]}""")