package com.cottondb.days import com.cottondb.days.book.Added import com.cottondb.days.book.CoreDaysBook import com.cottondb.days.book.Days import com.cottondb.days.book.GroupFilter import com.cottondb.days.book.Group import com.cottondb.days.book.Item import com.cottondb.days.book.Mark import com.cottondb.days.book.Month import com.cottondb.days.book.Opens import com.cottondb.days.book.ReadLine import com.cottondb.days.book.Span import com.cottondb.days.book.Whose import com.cottondb.days.book.CatalogueMark import com.cottondb.days.book.DayChoice import com.cottondb.days.book.End import com.cottondb.days.book.Found import com.cottondb.days.book.Lands import com.cottondb.days.ui.PillPick import com.cottondb.days.ui.bringingWords import com.cottondb.days.ui.clock import com.cottondb.days.ui.wordsFor import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test import java.time.LocalDate /** Days' reading of the core's answers (CONTRACT.md, final at f2f420f65), each in the shape cotton-ffi phone::days writes. */ class DaysLogicTest { private fun json(s: String): JsonObject = Json.parseToJsonElement(s).jsonObject @Test fun an_empty_today_says_the_cores_bringing_while_the_first_pull_runs() { fun home(s: String) = Json.parseToJsonElement(s).jsonObject assertEquals("Loading your data…", bringingWords(home("""{"gate":{"stage":"open"},"bringing":{"words":"Loading your data…"}}"""))) assertNull("a copy that holds its rows says nothing", bringingWords(home("""{"gate":{"stage":"open"},"bringing":null}"""))) assertNull(bringingWords(home("""{"gate":{"stage":"open"}}"""))) assertNull("before the core has answered", bringingWords(null)) } @Test fun a_span_is_read_as_the_contract_shapes_it() { val days = Days.of(json("""{"today":"2026-11-20","days":[ {"day":"2026-11-20","number":"20","weekday":"Friday","month":"November","today":true,"rows":[ {"key":"event:a@x","kind":"event","time":"11:30","end":"12:00","mark":{"kind":"dot"},"words":"Call with the bank","suffix":"to 12:00","done":false,"whose":"google","opens":{"sheet":"event","id":"a","occurrenceAt":"2026-11-20T11:30:00.000Z"}}, {"key":"todo:b","kind":"todo","time":null,"end":null,"mark":{"kind":"tick","done":false},"drawnMark":"star","words":"Send the invoice","suffix":"from Tue","done":false,"whose":"yours","opens":{"sheet":"todo","id":"b"}}, {"key":"birthday:p@x","kind":"birthday","time":null,"end":null,"mark":{"kind":"drawn","id":"cake"},"words":"Akash's birthday","suffix":null,"done":false,"whose":"birthday","opens":{"person":"p"}}]}, {"fold":{"from":"2026-11-22","to":"2026-11-25","count":4,"words":"22 – 25 · four quiet days"}}]}""")) val today = days.days[0] as Span.Day assertTrue(today.today) val (call, invoice, birthday) = today.rows assertEquals(Mark.Dot, call.mark) assertEquals(Opens.Event("a", "2026-11-20T11:30:00.000Z"), call.opens) assertEquals(Whose.Google, call.whose) assertFalse("a Google event's day is Google's", call.movable) assertEquals(Mark.Tick(false), invoice.mark) assertEquals("star", invoice.drawnMark) assertTrue(invoice.movable) assertEquals("todo", invoice.table) assertEquals(Mark.Drawn("cake"), birthday.mark) assertEquals(Opens.Person("p"), birthday.opens) assertNull(birthday.id) assertEquals(4, (days.days[1] as Span.Fold).count) } @Test fun sometime_reads_every_group_whole_with_no_group_last() { val groups = Group.list(json("""{"groups":[{"id":"h","name":"Home","count":1,"rows":[{"key":"todo:a","kind":"todo","time":null,"end":null,"mark":{"kind":"tick","done":false},"words":"Fix the tap","suffix":null,"done":false,"whose":"yours","opens":{"sheet":"todo","id":"a"}}]},{"id":null,"name":"No group","count":41,"rows":[]}],"count":42}""")) assertEquals(listOf("h", "none"), groups.map { it.key }) assertEquals("Fix the tap", groups[0].rows.single().words) } @Test fun the_sheet_says_whose_it_is_whether_it_repeats_and_whether_it_is_done() { val item = Item.of("todo", "t", json("""{"table":"todo","id":"t","whose":"yours","says":"Yours","words":"Water the plants","day":"2026-11-20","time":null,"end":null,"mark":"watering-can","note":null,"alert":null,"done":true,"repeats":true,"canChange":{"title":true,"time":true,"mark":true,"note":true,"alert":false}}""")) assertTrue(item.repeats) assertTrue(item.done) assertFalse(item.canChange.alert) val google = Item.of("event", "e", json("""{"table":"event","id":"e","whose":"google","says":"From Google Calendar","words":"Standup","day":"2026-11-20","time":"9:00","end":"9:15","mark":null,"note":null,"alert":10,"alerts":[{"alert":"none","on":false},{"alert":0,"on":false},{"alert":10,"on":true}],"done":false,"repeats":false,"canChange":{"title":false,"time":false,"mark":true,"note":true,"alert":true}}""")) assertEquals(Whose.Google, google.whose) assertFalse(google.canChange.title) assertEquals(listOf("10"), google.alerts.filter { it.on }.map { it.alert.content }) } @Test fun adding_says_where_it_landed() { assertEquals(Added("event", "e1", "2026-11-26", "19:00"), Added.of(json("""{"table":"event","id":"e1","day":"2026-11-26","time":"19:00"}"""))) assertNull(Added.of(json("""{"table":"todo","id":"t1","day":null,"time":null}""")).day) } @Test fun the_filter_reads_all_and_each_group_with_its_count() { val filter = GroupFilter.of(json("""{"all":"All","count":41,"groups":[{"id":"h","name":"Home","count":6},{"id":"w","name":"Work","count":2}]}""")) assertEquals("All", filter.all) assertEquals(41, filter.count) assertEquals(listOf("Home" to 6, "Work" to 2), filter.groups.map { it.name to it.count }) val item = Item.of("todo", "t", json("""{"table":"todo","id":"t","whose":"yours","words":"x","done":false,"repeats":false,"group":{"id":"h","name":"Home"},"canChange":{"title":true,"time":true,"mark":true,"note":true,"alert":false,"group":true}}""")) assertEquals("h", item.group?.id) assertTrue(item.canChange.group) } @Test fun the_month_and_the_marks_read_as_answered() { val month = Month.of(json("""{"month":"2026-11","title":"November 2026","weekStart":1,"lead":6,"weeks":6,"days":[{"day":"2026-11-20","marks":["cake"],"other":true,"today":true}]}""")) assertEquals(listOf("cake"), month.days.single().marks) assertEquals(6 to 6, month.lead to month.weeks) val marks = CatalogueMark.list(Json.parseToJsonElement("""{"marks":[{"id":"cake","words":{"en":["birthday","cake"],"hi":["जन्मदिन"]},"strokes":["M4 20 C8 20 16 20 20 20"]}]}""")) assertEquals("cake", marks.single().id) assertEquals(listOf("जन्मदिन"), marks.single().words["hi"]) assertEquals(1, marks.single().strokes.size) } @Test fun a_span_longer_than_the_cores_limit_is_read_in_pieces() { val pieces = CoreDaysBook.spanPieces(LocalDate.of(2026, 10, 23), LocalDate.of(2027, 1, 15)) assertEquals(LocalDate.of(2026, 10, 23), pieces.first().first) assertEquals(LocalDate.of(2027, 1, 15), pieces.last().second) assertTrue(pieces.all { java.time.temporal.ChronoUnit.DAYS.between(it.first, it.second) < CoreDaysBook.MOST_DAYS }) pieces.zipWithNext().forEach { (a, b) -> assertEquals(a.second.plusDays(1), b.first) } } @Test fun the_end_the_beginning_and_where_a_result_lands_are_read() { val days = Days.of(json("""{"today":"2026-11-20","days":[],"end":{"after":"2026-12-02","words":"Nothing else planned."},"begins":"2026-09-01"}""")) assertEquals(End("2026-12-02", "Nothing else planned."), days.end) assertEquals("2026-09-01", days.begins) val found = Found.of(json("""{"coming":[{"key":"todo:a","kind":"todo","time":null,"end":null,"mark":{"kind":"tick","done":false},"words":"Pay the bill","suffix":null,"done":false,"whose":"yours","opens":{"sheet":"todo","id":"a"},"day":null,"dayWords":"Sometime","lands":{"place":"sometime"}}], "before":[{"key":"todo:b","kind":"todo","time":null,"end":null,"mark":{"kind":"tick","done":false},"words":"Dentist","suffix":null,"done":false,"whose":"yours","opens":{"sheet":"todo","id":"b"},"day":"2026-11-05","dayWords":"Thu 5 Nov","dropped":true,"lands":{"place":"show","view":"dropped"}}],"empty":null}""")) assertEquals(Lands.InSometime, found.coming.single().lands) assertTrue(found.before.single().dropped) assertEquals(Lands.InView("dropped"), found.before.single().lands) val item = Item.of("todo", "t", json("""{"words":"Walk","canChange":{"time":true},"choices":[{"key":"today","day":"2026-11-20"},{"key":"sometime","day":null}],"dropped":true}""")) assertEquals(listOf(DayChoice("today", "2026-11-20"), DayChoice("sometime", null)), item.choices) assertTrue(item.dropped) } @Test fun a_day_or_time_set_to_none_on_the_pill_drops_the_words_that_said_it() { val words = "Dinner with Rose Thursday 7pm" val read = ReadLine.of(json("""{"title":"Dinner with Rose","day":"2026-11-26","time":"19:00","end":null,"kind":"event","pill":"Thu 26 Nov · 19:00","marks":["fork-knife"],"said":[{"from":17,"to":25,"what":"day"},{"from":26,"to":29,"what":"time"}]}""")) assertEquals(words, wordsFor(words, read, null)) assertEquals("Dinner with Rose Thursday", wordsFor(words, read, PillPick("2026-11-26", null))) assertEquals("Dinner with Rose", wordsFor(words, read, PillPick(null, null))) assertEquals("9:05", clock("09:05")) } @Test fun the_said_stretches_count_characters_not_utf16_units() { val words = "🎂 Cake Friday" val read = ReadLine.of(json("""{"title":"🎂 Cake","day":"2026-11-27","time":null,"end":null,"kind":"todo","pill":"Fri 27 Nov","marks":[],"said":[{"from":7,"to":13,"what":"day"}]}""")) assertEquals("🎂 Cake", wordsFor(words, read, PillPick(null, null))) } }