Cotton code
104 lines · 5.6 KB · KotlinRaw
package com.cottondb.daysimport com.cottondb.days.book.Addedimport com.cottondb.days.book.CatalogueMarkimport com.cottondb.days.book.Clockimport com.cottondb.days.book.Daysimport com.cottondb.days.book.DaysBookimport com.cottondb.days.book.Foundimport com.cottondb.days.book.Groupimport com.cottondb.days.book.GroupFilterimport com.cottondb.days.book.Itemimport com.cottondb.days.book.Keptimport com.cottondb.days.book.Kindimport com.cottondb.days.book.Markimport com.cottondb.days.book.Monthimport com.cottondb.days.book.Opensimport com.cottondb.days.book.ReadLineimport com.cottondb.days.book.Rowimport com.cottondb.days.book.Shownimport com.cottondb.days.book.Spanimport com.cottondb.days.book.Whoseimport kotlinx.coroutines.asyncimport kotlinx.coroutines.awaitAllimport kotlinx.coroutines.runBlockingimport kotlinx.serialization.json.JsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Testimport 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<Pair<String, Boolean>>()        val keeps = mutableListOf<List<Kept>>()        override suspend fun span(from: String, to: String, group: String?, keep: List<Kept>): 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<Kept>) = emptyList<Group>()        override suspend fun show(view: String, keep: List<Kept>, after: String?) = Shown(view, view, emptyList(), "", null)        override suspend fun find(words: String, keep: List<Kept>) = 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<CatalogueMark>()    }    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<Kept>(), 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))    }}