package com.cottondb.days import android.app.Application import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import com.cottondb.days.book.CoreDaysBook import com.cottondb.days.pen.DaysCardLook import com.cottondb.days.pen.penFonts import com.cottondb.days.ui.LeaveNotice import com.cottondb.shell.LocalCardLook import com.cottondb.shell.core.CottonCore import com.cottondb.shell.str import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** * Leave to notify is asked with its reason shown first (LAUNCH.md section 4; the launch walk of 22 Nov 2026): the first * timed event put Android's "Allow Days to send you notifications?" straight over the list, with nothing in Days * saying why. The core's answer now carries `leave` (title, body, go, notNow); Days shows it, and only Continue goes on * to the OS's ask. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w411dp-h914dp-xhdpi", application = Application::class) class DaysLeaveNoticeTest { @get:Rule val compose = createComposeRule() private fun answer(json: String): JsonObject = CottonCore.json.parseToJsonElement(json).jsonObject private val why = """{"title":"Reminders","body":"Days can remind you shortly before this.","go":"Continue","notNow":"Not now"}""" /** RULE: the book hands the core's reason on with the ask, so the phone can say why before the OS asks. */ @Test fun the_book_hands_on_why() = runBlocking { val handed = mutableListOf() CoreDaysBook(asked = { _, _ -> answer("""{"table":"event","id":"e1","platform_action":"request_notifications","leave":$why}""") }, leave = { handed += it }) .add("Invented dentist", "days", null, "2026-11-24", "10:00", null) assertEquals("Reminders", handed.single()?.str("title")) } /** RULE: the reason stands first; Not now asks nothing; Continue goes on to the OS. */ @Test fun why_stands_before_the_os_asks() { var went = 0 var not = 0 compose.setContent { CompositionLocalProvider(LocalCardLook provides DaysCardLook(penFonts(LocalContext.current))) { LeaveNotice(answer(why), onGo = { went++ }, onNotNow = { not++ }) } } compose.onNodeWithText("Days can remind you shortly before this.").assertIsDisplayed() compose.onNodeWithText("Not now").performClick() assertEquals(0 to 1, went to not) compose.onNodeWithText("Continue").performClick() assertEquals(1, went) } /** RULE: Days' surface draws the reason, and its parts point the core's answer at it. */ @Test fun days_draws_why_before_the_shells_ask() { val repo = generateSequence(java.io.File("").absoluteFile) { it.parentFile }.first { java.io.File(it, "apps/registry.json").isFile } val surface = java.io.File(repo, "apps/android/app/src/days/kotlin/com/cottondb/days/DaysSurface.kt").readText() val parts = java.io.File(repo, "apps/android/app/src/days/kotlin/com/cottondb/days/DaysParts.kt").readText() assertEquals(true, surface.contains("LeaveNotice(")) assertEquals(true, parts.contains("state.leaving = why")) } }