package com.cottondb.days import android.app.Application import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.test.assertIsSelected import androidx.compose.ui.test.assertIsNotSelected 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.test.performTextInput import com.cottondb.days.book.Item import com.cottondb.days.book.Person import com.cottondb.days.pen.DaysCardLook import com.cottondb.days.pen.penFonts import com.cottondb.days.ui.EndPick import com.cottondb.days.ui.EndsRow import com.cottondb.days.ui.PeopleRow import com.cottondb.days.ui.sent import com.cottondb.shell.LocalCardLook import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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() 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() } }