package com.cottondb.days import com.cottondb.days.book.CoreDaysBook import com.cottondb.days.ui.PillPick import com.cottondb.days.ui.QuickAct import com.cottondb.days.ui.TodaySeen import com.cottondb.days.ui.shortDate import com.cottondb.shell.core.CoreException import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Assert.assertSame import org.junit.Test import java.util.Locale /** * Days' finds from the first-run walk on Android (25 Nov 2026), with invented items only: When kept no end, Mark not * done threw away the sheet's edits, and the sheets said dates in the phone's US order. */ class DaysFirstRunTest { /** RULE: When's Set without a change keeps the end the words gave ("7pm to 9pm" stays 19:00 – 21:00). */ @Test fun when_set_unchanged_keeps_the_end() { val read = PillPick("2026-11-27", "19:00", "21:00") assertEquals(read, read.setTo("2026-11-27", "19:00")) assertEquals("another day, the same times", PillPick("2026-11-30", "19:00", "21:00"), read.setTo("2026-11-30", "19:00")) } /** RULE: a new start keeps the length (DECISIONS "When an event ends"), past midnight too; no time, no end. */ @Test fun when_a_new_start_keeps_the_length() { val read = PillPick("2026-11-27", "19:00", "21:00") assertEquals(PillPick("2026-11-27", "20:30", "22:30"), read.setTo("2026-11-27", "20:30")) assertEquals(PillPick("2026-11-27", "23:00", "1:00"), read.setTo("2026-11-27", "23:00")) assertEquals(PillPick("2026-11-27", null, null), read.setTo("2026-11-27", null)) assertEquals(PillPick(null, null, null), read.setTo(null, "19:00")) assertEquals("no end said, none made up", PillPick("2026-11-27", "8:00", null), PillPick("2026-11-27", "7:00").setTo("2026-11-27", "8:00")) } /** RULE: the end the pill kept goes to `days.add` with its time (a time alone is the core's hour). */ @Test fun adding_sends_the_end_with_the_time() = runBlocking { val sent = mutableListOf() val book = CoreDaysBook(asked = { _, request -> sent += request; buildJsonObject { put("table", "event"); put("id", "e1") } }) book.add("Invented dinner", "days", null, "2026-11-27", "19:00", null, null, "21:00") assertEquals(JsonPrimitive("21:00"), sent.last()["end"]) book.add("Invented dinner", "days", null, "2026-11-27", null, null, null, "21:00") assertNull("no time, no end", sent.last()["end"]) book.add("Invented dinner", "days", null, null, null, null) assertNull(sent.last()["end"]) } /** RULE: a quick act saves what else the sheet holds first, then acts; nothing changed saves nothing. */ @Test fun a_quick_act_saves_the_sheet_first() = runBlocking { val order = mutableListOf() val edits = buildJsonObject { put("title", "Invented lentils and rice"); put("note", "Invented note"); put("note_base", "") } val refused = QuickAct.run(edits, { f -> order += "save ${f["title"]}"; null }) { order += "not done" } assertNull(refused) assertEquals(listOf("save \"Invented lentils and rice\"", "not done"), order) order.clear() QuickAct.run(JsonObject(emptyMap()), { order += "save"; null }) { order += "done" } assertEquals(listOf("done"), order) } /** RULE: refused (Google needs leave again), the act waits and the sheet keeps what was typed. */ @Test fun a_refused_save_holds_the_act() = runBlocking { val no = CoreException("google_needs_write", "Connect again.", JsonNull) var acted = false assertSame(no, QuickAct.run(buildJsonObject { put("title", "Invented") }, { no }) { acted = true }) assertEquals(false, acted) } /** RULE: Done, Skip and Drop on the sheet go through the saving quick act. */ @Test fun the_sheets_quick_acts_save_first() { val repo = generateSequence(java.io.File("").absoluteFile) { it.parentFile }.first { java.io.File(it, "apps/registry.json").isFile } val sheets = java.io.File(repo, "apps/android/app/src/days/kotlin/com/cottondb/days/ui/Sheets.kt").readText() listOf("savingFirst { onDone(!item.done) }", "savingFirst(onSkip)", "savingFirst { onDrop(!item.dropped) }").forEach { assertEquals(it, true, sheets.contains(it)) } } /** RULE: the sheets say a day as the pill and search do, "Wed 25 Nov", on a US phone as anywhere. */ @Test fun the_sheets_say_a_day_as_the_pill_does() { val before = Locale.getDefault() try { Locale.setDefault(Locale.US) assertEquals("Wed 25 Nov", shortDate("2026-11-25")) assertEquals("Fri 27 Nov", shortDate("2026-11-27")) Locale.setDefault(Locale.UK) assertEquals("Wed 25 Nov", shortDate("2026-11-25")) } finally { Locale.setDefault(before) } } /** RULE: the Today button stands once today's date is off screen, even while today's rows still show under it. */ @Test fun today_is_shown_by_its_date_not_its_rows() { val start = -10 // the list's top padding assertEquals("at the top", true, TodaySeen.shown(0, 220, start)) assertEquals("the date half above", true, TodaySeen.shown(-110, 220, start)) assertEquals("the date above, its rows still under the top bar", false, TodaySeen.shown(-240, 220, start)) assertEquals("not laid out", false, TodaySeen.shown(null, 220, start)) } /** RULE: any touch on the list lets a jumped-to day go, so a later read never moves the list under the person. */ @Test fun a_touch_on_the_list_lets_the_jump_go() { val repo = generateSequence(java.io.File("").absoluteFile) { it.parentFile }.first { java.io.File(it, "apps/registry.json").isFile } val list = java.io.File(repo, "apps/android/app/src/days/kotlin/com/cottondb/days/ui/DaysList.kt").readText() assertEquals(true, Regex("""awaitFirstDown\(requireUnconsumed = false, pass = [\w.]*PointerEventPass.Initial\)\s*state.jumpTo = null""").containsMatchIn(list)) } }