package com.cottondb.days import com.cottondb.days.book.Added import com.cottondb.days.book.CatalogueMark import com.cottondb.days.book.Clock import com.cottondb.days.book.Days import com.cottondb.days.book.DaysBook import com.cottondb.days.book.Found import com.cottondb.days.book.Group import com.cottondb.days.book.GroupFilter import com.cottondb.days.book.Item import com.cottondb.days.book.Kept import com.cottondb.days.book.Kind 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.Row import com.cottondb.days.book.Shown import com.cottondb.days.book.Span import com.cottondb.days.book.Whose import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.JsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test import java.time.LocalDate /** * Days' state over an invented book: nothing moves at the moment a person acts (DECISIONS, 21 Nov 2026). Sixty * invented to-dos on today; ten ticked in a row each register, in order, each row's circle flipped at once, and every * read after them keeps each where it stood until the list is opened again. */ class DaysStateTest { private val today = LocalDate.of(2026, 11, 20) private val rows = (1..60).map { n -> Row("todo:t$n", Kind.Todo, null, null, Mark.Tick(false), null, "Invented chore $n", null, false, Whose.Yours, Opens.Todo("t$n")) } private inner class Book : DaysBook { val done = mutableListOf>() val keeps = mutableListOf>() override suspend fun span(from: String, to: String, group: String?, keep: List): Days { keeps += keep // As the core answers: each to-do done as its last tick left it, where it stood. val now = rows.map { r -> val d = done.lastOrNull { it.first == (r.opens as Opens.Todo).id }?.second ?: false; r.copy(done = d, mark = Mark.Tick(d)) } return Days(today.toString(), listOf(Span.Day(today.toString(), "20", "Friday", "November", true, now))) } override suspend fun sometime(group: String?, keep: List) = emptyList() override suspend fun show(view: String, keep: List, after: String?) = Shown(view, view, emptyList(), "", null) override suspend fun find(words: String, keep: List) = Found.None override suspend fun drop(id: String) {} override suspend fun bringBack(id: String) {} override suspend fun delete(table: String, id: String, occurrenceAt: String?) {} override suspend fun month(month: String, weekStart: Int, group: String?) = Month(month, "", 1, 0, 6, emptyList()) override suspend fun groups() = GroupFilter.Empty override suspend fun readLine(words: String, where: String, dayInView: String?) = ReadLine.Empty override suspend fun add(words: String, where: String, dayInView: String?, day: String?, time: String?, mark: String?, group: String?, end: String?) = Added("todo", "x", null, null) override suspend fun item(table: String, id: String, occurrenceAt: String?): Item = throw UnsupportedOperationException() override suspend fun change(table: String, id: String, occurrenceAt: String?, fields: JsonObject): String? = null override suspend fun done(id: String, done: Boolean) { synchronized(this.done) { this.done += id to done } } override suspend fun skip(id: String) {} override suspend fun marks(words: String?) = emptyList() } private val clock = object : Clock() { override fun today() = this@DaysStateTest.today } @Test fun ten_ticks_in_a_row_each_register_and_every_row_stays_where_it_stood() = runBlocking { val book = Book() val state = DaysState(book, clock) state.read() val ten = rows.take(10) ten.map { row -> async { state.tickRow(row, today.toString()) { throw it } } }.awaitAll() assertEquals(ten.map { it.opens.let { o -> (o as Opens.Todo).id } to true }, book.done) assertTrue("each circle is drawn done at once", ten.all { state.done(it) }) // A read after the ticks keeps the ten where they stood. state.read() assertEquals(ten.map { Kept((it.opens as Opens.Todo).id, today.toString()) }.toSet(), book.keeps.last().toSet()) // A tick again on the same row, as it is drawn now, brings it back, there. state.tickRow((state.days.first() as Span.Day).rows[0], today.toString()) { throw it } assertEquals("t1" to false, book.done.last()) val shown = (state.days.first() as Span.Day).rows assertTrue(!state.done(shown[0])) assertTrue("the other nine stay done, where they stood", shown.subList(1, 10).all { state.done(it) }) assertEquals(rows.map { it.key }, shown.map { it.key }) // Opened again (Sometime and back): let go, every row where it belongs. state.go(Place.Sometime) state.go(Place.Days) state.read() assertEquals(emptyList(), book.keeps.last()) } @Test fun a_quick_second_tap_on_one_circle_takes_it_back() = runBlocking { val book = Book() val state = DaysState(book, clock) state.read() val row = rows[4] listOf(async { state.tickRow(row, today.toString()) { throw it } }, async { state.tickRow(row, today.toString()) { throw it } }).awaitAll() assertEquals(listOf("t5" to true, "t5" to false), book.done) assertTrue(!state.done(row)) } }