Cotton code
130 lines · 6.6 KB · KotlinRaw
package com.cottondb.daysimport android.app.Applicationimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport androidx.compose.ui.platform.LocalContextimport androidx.compose.ui.test.assertIsSelectedimport androidx.compose.ui.test.assertIsNotSelectedimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTextInputimport com.cottondb.days.book.Itemimport com.cottondb.days.book.Personimport com.cottondb.days.pen.DaysCardLookimport com.cottondb.days.pen.penFontsimport com.cottondb.days.ui.EndPickimport com.cottondb.days.ui.EndsRowimport com.cottondb.days.ui.PeopleRowimport com.cottondb.days.ui.sentimport com.cottondb.shell.LocalCardLookimport com.cottondb.shell.core.CottonCoreimport kotlinx.serialization.json.JsonPrimitiveimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * ENDS and PEOPLE on an event's sheet (contract §14, §15): the sheet draws what `days.item` answers, a chip writes the * length the core takes, and a person's chip takes them off. Invented people and addresses only. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w411dp-h914dp-xhdpi", application = Application::class)class DaysPeopleTest {    @get:Rule val compose = createComposeRule()    private val answer = """{"table":"event","id":"e","whose":"yours","words":"Invented planning","day":"2026-11-24","time":"15:30","end":"16:30",        "alerts":[],"canChange":{"title":true,"time":true,"mark":true,"note":true,"alert":true,"people":true},        "ends":{"title":"Ends","says":"16:30 · 1 hour","choices":[          {"key":"30m","minutes":30,"label":"30 min","on":false},{"key":"1h","minutes":60,"label":"1 hour","on":true},          {"key":"2h","minutes":120,"label":"2 hours","on":false},{"key":"pick","label":"Pick a time","on":false},          {"key":"none","label":"No end","on":false}]},        "people":{"title":"People","add":"Add people","canChange":true,"rows":[          {"key":"number:+15550100","name":"Rose","detail":"On Cotton","via":"cotton","answer":"accepted","answerWords":"Coming","organizer":false},          {"key":"email:ivy@example.test","name":"ivy@example.test","detail":"ivy@example.test","via":"none","answer":null,"answerWords":null,"organizer":false}],          "blocked":{"line":"Connect Google Calendar to send invitations by email.","connect":{"id":"google_connect"}}},        "deleteTells":"Rose and ivy@example.test are told it is off.",        "invitation":null}"""    private fun item() = Item.of("event", "e", CottonCore.json.parseToJsonElement(answer).jsonObject)    private fun look(content: @androidx.compose.runtime.Composable () -> Unit) = compose.setContent {        CompositionLocalProvider(LocalCardLook provides DaysCardLook(penFonts(LocalContext.current))) { androidx.compose.foundation.layout.Column { content() } }    }    @Test fun the_sheets_answer_reads_its_ends_and_people() {        val it = item()        assertEquals("16:30 · 1 hour", it.ends?.says)        assertEquals(listOf("30m", "1h", "2h", "pick", "none"), it.ends?.choices?.map { c -> c.key })        assertEquals(listOf("Rose", "ivy@example.test"), it.people?.rows?.map { p -> p.name })        assertEquals("Connect Google Calendar to send invitations by email.", it.people?.blocked?.line)        assertTrue(it.canChange.people)        assertEquals("Rose and ivy@example.test are told it is off.", it.deleteTells)        assertNull(it.invitation)    }    @Test fun a_person_goes_back_to_the_core_by_their_key() {        val sent = item().people!!.rows.map(Person::sent)        assertEquals(JsonPrimitive("+15550100"), sent[0]["number"])        assertEquals(JsonPrimitive("ivy@example.test"), sent[1]["email"])    }    @Test fun an_end_chip_writes_its_length_and_no_end_writes_none() {        val ends = item().ends!!        var pick: EndPick by mutableStateOf(EndPick.Unchanged)        look { EndsRow(ends, pick) { pick = it } }        compose.onNodeWithTag("days-change-ends-1h").assertIsSelected()        compose.onNodeWithText("16:30 · 1 hour").assertExists()        compose.onNodeWithTag("days-change-ends-2h").performClick()        compose.waitForIdle()        assertEquals("length" to JsonPrimitive(120), pick.sent())        compose.onNodeWithTag("days-change-ends-2h").assertIsSelected()        compose.onNodeWithTag("days-change-ends-1h").assertIsNotSelected()        compose.onNodeWithTag("days-change-ends-none").performClick()        compose.waitForIdle()        assertEquals("length" to JsonPrimitive("none"), pick.sent())    }    @Test fun pick_a_time_writes_the_clock_typed() {        val ends = item().ends!!        var pick: EndPick by mutableStateOf(EndPick.Unchanged)        look { EndsRow(ends, pick) { pick = it } }        compose.onNodeWithTag("days-change-ends-pick").performClick()        compose.waitForIdle()        compose.onNodeWithTag("days-change-ends-clock").performTextInput("17:15")        compose.waitForIdle()        assertEquals("end" to JsonPrimitive("17:15"), pick.sent())    }    @Test fun a_persons_chip_takes_them_off_and_the_blocked_line_offers_connect() {        val p = item().people!!        var connected = 0        val removed = mutableListOf<String>()        look {            var people by remember { mutableStateOf(p.rows) }            PeopleRow(p.title, p.add, p.canChange, people, p.blocked?.line, "Remove",                onRemove = { gone -> removed += gone.key; people = people.filter { it.key != gone.key } }, onAdd = {},                onConnect = { connected++ }, connectWords = "Connect Google Calendar")        }        compose.onNodeWithText("Rose · Coming").assertExists()        compose.onNodeWithTag("days-change-people-blocked").assertExists()        compose.onNodeWithTag("days-change-person-number:+15550100").performClick()        compose.waitForIdle()        assertEquals(listOf("number:+15550100"), removed)        compose.onNodeWithText("Rose · Coming").assertDoesNotExist()        compose.onNodeWithTag("days-change-people-connect").performClick()        assertEquals(1, connected)        compose.onNodeWithTag("days-change-people-add").assertExists()    }}