Cotton code
139 lines · 5.8 KB · KotlinRaw
package com.cottondb.mailimport kotlinx.coroutines.CompletableDeferredimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.asyncimport kotlinx.coroutines.runBlockingimport kotlinx.coroutines.yieldimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.buildJsonObjectimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertNullimport org.junit.Assert.assertSameimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.File/** * POST DRAWER AT ONCE (the owner's bug on Post build 2, core b417610653): the side page draws its rows on the first * frame it opens, from the answer Post holds since launch; the counts fill in behind; a refusal never takes rows away. */class PostSideTest {    private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile }    private fun src(name: String) = File(repo, "apps/android/app/src/mail/kotlin/com/cottondb/mail/$name").readText()    private fun json(text: String) = Json.parseToJsonElement(text).jsonObject    private val rows = json("""{"inboxes":{"title":"Inboxes","rows":[{"id":"all","title":"All inboxes"}]},"places":{"title":"Places","rows":[{"id":"bin","title":"Bin"}]}}""")    private val whole = json("""{"inboxes":{"title":"Inboxes","rows":[{"id":"all","title":"All inboxes","count":12}]},"places":{"title":"Places","rows":[{"id":"bin","title":"Bin","count":3}]},"labels":{"title":"Labels","rows":[]}}""")    /** Each request the side page made, and the answers it is given in turn. */    private class Core(vararg answers: Asked) {        val asked = mutableListOf<JsonObject>()        private val left = ArrayDeque(answers.toList())        val fetch: Fetch = { method, build ->            assertEquals("mail.side", method)            asked += buildJsonObject(build)            left.removeFirst()        }    }    @Test    fun the_rows_alone_are_asked_first_then_the_whole_answer() = runBlocking {        val core = Core(Asked(rows), Asked(whole))        val side = SideHeld(core.fetch)        side.read()        assertEquals("counts: false first, nothing counted", listOf(json("""{"counts":false}"""), json("{}")), core.asked)        assertSame(whole, side.answer)        assertNull(side.refused)    }    @Test    fun the_rows_stand_while_the_counts_are_counted() = runBlocking(Dispatchers.Unconfined) {        val counted = CompletableDeferred<Asked>()        var calls = 0        val side = SideHeld { _, _ -> if (calls++ == 0) Asked(rows) else counted.await() }        val reading = async { side.read() }        yield()        assertSame("the rows are drawn before the counts come", rows, side.answer)        counted.complete(Asked(whole))        reading.await()        assertSame(whole, side.answer)    }    @Test    fun a_held_answer_is_drawn_at_once_and_only_the_whole_one_is_asked_again() = runBlocking {        val core = Core(Asked(rows), Asked(whole), Asked(whole))        val side = SideHeld(core.fetch)        side.read()        core.asked.clear()        side.read()        assertEquals("rows held: no rows-only ask", listOf(json("{}")), core.asked)        // The page draws what Post holds on its first frame, and asks only behind it.        val page = src("Side.kt")        assertTrue(page.contains("val side = state.side.answer"))        assertTrue(page.contains("LaunchedEffect(Unit) { state.readSide() }"))        assertFalse("the page asks nothing itself on open", page.contains("state.ask(\"mail.side\")"))        assertTrue(src("PostApp.kt").contains("LaunchedEffect(shell.revision, state.generation) { state.readSide() }"))    }    @Test    fun a_refusal_never_takes_the_rows_away() = runBlocking {        val refused = Asked(null, "busy", "Post is busy")        val core = Core(Asked(rows), refused)        val side = SideHeld(core.fetch)        side.read()        assertSame("the rows-only answer stands", rows, side.answer)        assertNull("nothing new is said while rows stand", side.refused)        val later = SideHeld(Core(Asked(rows), Asked(whole), refused).fetch)        later.read(); later.read()        assertSame("the last whole answer stands", whole, later.answer)        assertNull(later.refused)    }    @Test    fun an_error_keeps_the_rows_too() = runBlocking {        var calls = 0        val side = SideHeld { _, _ ->            calls++            when (calls) { 1 -> Asked(rows); 2 -> Asked(whole); else -> error("the core fell over") }        }        side.read()        assertTrue(runCatching { side.read() }.isFailure)        assertSame(whole, side.answer)        // Not left reading: the next read asks again.        assertTrue(runCatching { side.read() }.isFailure)        assertEquals(4, calls)        assertSame(whole, side.answer)    }    @Test    fun with_nothing_held_the_refusal_is_said() = runBlocking {        val refused = Asked(null, "busy", "Post is busy")        val side = SideHeld(Core(refused, refused).fetch)        runCatching { side.read() }        assertNull(side.answer)        assertEquals("Post is busy", side.refused)    }    @Test    fun reads_asked_while_one_is_out_are_one_more_never_stacked() = runBlocking(Dispatchers.Unconfined) {        val gate = CompletableDeferred<Unit>()        var calls = 0        val side = SideHeld { _, build ->            calls++            if (buildJsonObject(build).isEmpty() && calls == 2) gate.await()            Asked(if (calls == 1) rows else whole)        }        val first = async { side.read() }        yield()        repeat(5) { side.read() }        gate.complete(Unit)        first.await()        assertEquals("rows, whole, then one whole more for the five asked meanwhile", 3, calls)        assertSame(whole, side.answer)    }}