package com.cottondb.cinemas import android.app.Application import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.unit.dp import com.cottondb.cinemas.stream.CoreAct import com.cottondb.cinemas.stream.Home import com.cottondb.cinemas.stream.Loaded import com.cottondb.cinemas.stream.ReceivedLine import com.cottondb.cinemas.stream.ReceivedPage import com.cottondb.cinemas.stream.ReceivedRow import com.cottondb.cinemas.stream.SharedList import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import java.io.File /** * A CINEMAS LIST SHARED WITH HIM (CONTACTS-ANSWERS 10.7, 4c95802070): Home's "Shared with you" line read from * `cinemas.home`'s `received` and drawn from it, a row opening with the call it carries (`cinemas.shared`), the page drawn * whole from `cinemas.shared`'s answer, and Leave asking its confirm before calling `cinemas.shared.leave`. Words are the * kit's `cinemas.json` `shared.*`; every name and number is invented. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h1400dp-mdpi", application = Application::class) class ReceivedListTest { @get:Rule val compose = createComposeRule() private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile } private val kit = Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/cinemas.json").readText()).jsonObject private fun w(path: String, vararg fills: Pair): String = fills.fold(path.split(".").fold(kit) { at, k -> at.jsonObject[k]!! }.jsonPrimitive.content) { s, (k, v) -> s.replace("{$k}", v) } private fun q(text: String) = Json.encodeToString(JsonPrimitive.serializer(), JsonPrimitive(text)) private fun j(text: String) = Json.parseToJsonElement(text).jsonObject private val owner = "+15550100007" private val detail = w("shared.detail", "name" to "Mira Example", "count" to "2") private val home = j( """{"featured":null,"rows":[],"empty":null, "received":{"title":${q(w("shared.title"))},"rows":[{"owner":"$owner","id":"cinemas-g-1","words":"Rainy Sundays","detail":${q(detail)},"initials":"ME", "call":{"method":"cinemas.shared","request":{"owner":"$owner","id":"cinemas-g-1"}}}]}}""", ) private val opened = j( """{"owner":"$owner","id":"cinemas-g-1","title":"Rainy Sundays","from":{"number":"$owner","name":"Mira Example"},"detail":${q(detail)}, "items":[{"id":"f-1","title":"An Invented Film","year":1999,"kind":"film"},{"id":"f-2","title":"Another Invented Film","year":2004,"kind":"film"}],"unopened":0, "leave":{"words":${q(w("shared.leave"))},"red":true,"confirm":${q(w("shared.leave_confirm", "name" to "Mira Example"))}, "call":{"method":"cinemas.shared.leave","request":{"owner":"$owner","id":"cinemas-g-1"}}}}""", ) @Test fun homeReadsReceivedAndNoneIsNull() { val received = Home.of(home).received!! assertEquals(w("shared.title"), received.title) val row = received.rows.single() assertEquals("cinemas.shared", row.call.method) assertEquals(j("""{"owner":"$owner","id":"cinemas-g-1"}"""), row.call.request) assertNull(Home.of(j("""{"featured":null,"rows":[],"empty":null,"received":null}""")).received) } @Test fun theLineIsDrawnFromReceivedAndARowOpensWithItsCall() { var tapped: ReceivedRow? = null compose.setContent { ReceivedLine(Home.of(home).received!!) { tapped = it } } compose.onNodeWithText(w("shared.title")).assertIsDisplayed() compose.onNodeWithText("Rainy Sundays").assertIsDisplayed() compose.onNodeWithText(detail).assertIsDisplayed() compose.onNodeWithTag("received-$owner-cinemas-g-1").performClick() assertEquals(CoreAct("cinemas.shared", j("""{"owner":"$owner","id":"cinemas-g-1"}""")), tapped?.call) } @Test fun thePageIsDrawnFromSharedAndLeaveAsksFirst() { var left: CoreAct? = null compose.setContent { ReceivedPage(Loaded.Ready(SharedList.of(opened)), back = "Back", cancel = "Cancel", bottom = 0.dp, onBack = {}, onRetry = {}, onLeave = { left = it }) } compose.onNodeWithText("RAINY SUNDAYS").assertIsDisplayed() compose.onNodeWithText(detail).assertIsDisplayed() compose.onNodeWithText("An Invented Film").assertIsDisplayed() compose.onNodeWithText("2004").assertIsDisplayed() compose.onNodeWithTag("received-leave").performClick() assertNull(left) compose.onNodeWithText(w("shared.leave_confirm", "name" to "Mira Example")).assertIsDisplayed() compose.onNodeWithTag("received-leave-no").performClick() assertNull(left) compose.onNodeWithTag("received-leave").performClick() compose.onNodeWithTag("received-leave-yes").performClick() assertEquals(CoreAct("cinemas.shared.leave", j("""{"owner":"$owner","id":"cinemas-g-1"}""")), left) } @Test fun aRefusalIsTheCoresLine() { compose.setContent { ReceivedPage(Loaded.Failed("The list could not be read."), back = "Back", cancel = "Cancel", bottom = 0.dp, onBack = {}, onRetry = {}, onLeave = {}) } compose.onNodeWithTag("received-refused").assertIsDisplayed() } }