Cotton code
84 lines · 5.4 KB · KotlinRaw
package com.cottondb.sitesimport com.cottondb.shell.Loadingimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.jsonObjectimport kotlinx.serialization.json.jsonPrimitiveimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.File/** * FIRST SYNC in Sites (owner, 26 Nov 2026, from the iPhone): while his copy comes down the list says it is loading, * never the first run; "0 sites" is the loading words; and You's Sync says the one seen line. */class SitesFirstSyncTest {    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/sites/kotlin/com/cottondb/sites/$name").readText()    private val words by lazy { Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/sites.json").readText()).jsonObject }    /** While the core says loading, never the first run, and no Add alone: the core's `loading` only, no shell stand-in. */    @Test    fun the_list_says_loading_not_the_first_run() {        val list = src("ListScreen.kt")        assertTrue(list.contains("val loading = listLoading(home, state.w(\"a11y.loading\"))"))        assertTrue(list.contains("if (first != null && loading == null) { FirstRun(state, first); return@Column }"))        assertTrue("Add only when the core sends it", list.contains("if (home != null && add != null && (home.obj(\"first\") == null || loading != null))"))    }    /** Purely the core's `loading` ({line}): no `caught_up` read, no timer, no first-sync guess. */    @Test    fun the_list_is_loading_while_the_core_says_so() {        fun home(text: String) = Json.parseToJsonElement(text) as JsonObject        assertNull(listLoading(home("{}"), "Loading"))        assertNull(listLoading(null, "Loading"))        assertNull("caught_up is not the core's", listLoading(home("""{"caught_up":false}"""), "Loading"))        assertEquals(Loading("Loading your list…", null), listLoading(home("""{"loading":{"line":"Loading your list…"}}"""), "x"))        assertEquals(Loading("Loading", null), listLoading(home("""{"loading":{}}"""), "Loading"))        assertEquals("Loading your list…", words["loading"]!!.jsonObject["line"]!!.jsonPrimitive.content)    }    /** The sites this copy held, drawn greyed under the loading line (`waiting_rows`), their address as the second line. */    @Test    fun waiting_rows_are_drawn_greyed_while_loading() {        val w = com.cottondb.sites.SiteRow.of(Json.parseToJsonElement("""{"id":"k","label":"Work mail","site":"mail.example","letter":"W","waiting":true}""") as JsonObject)!!        assertTrue(w.waiting)        assertEquals("mail.example", w.line)        val list = src("ListScreen.kt")        assertTrue(list.contains("if (loading != null) items(waiting, key = { \"w-\" + it.id }) { r -> Row(state, r, last = r == waiting.last(), loading) }"))        assertTrue(src("SitesState.kt").contains("home?.arr(\"waiting_rows\")"))        // A hidden site's name never outside the lock: today's core marks none, so locked, none is drawn.        val h = Json.parseToJsonElement("""{"waiting_rows":[{"id":"a","label":"Quill"},{"id":"b","label":"Club","hidden":true},{"id":"c","label":"Mail","hidden":false}]}""") as JsonObject        assertEquals(listOf("Mail"), waitingShown(h, unlocked = false).map { it.label })        assertEquals(listOf("Quill", "Club", "Mail"), waitingShown(h, unlocked = true).map { it.label })    }    /** While loading every row is greyed, does nothing, and tells TalkBack it is not available yet and why. */    @Test    fun greyed_rows_do_nothing_and_say_so() {        val list = src("ListScreen.kt")        val row = list.substring(list.indexOf("private fun Row("), list.indexOf("/** The ids of one twins group"))        assertTrue(row.indexOf("if (loading != null) { Greyed(") < row.indexOf("SiteLine("))        val greyed = list.substring(list.indexOf("private fun Greyed("), list.indexOf("internal const val GREYED"))        assertTrue("TalkBack: not yet available, and why", greyed.contains("clearAndSetSemantics { contentDescription = spoken; stateDescription = \"\${Placeholder.NOT_YET}. \$why\"; disabled() }"))        assertTrue("no tap, no hold", greyed.contains("onTap = null") && !greyed.contains("onHold"))        assertTrue("faint, not gone", GREYED in 0.3f..0.6f)        // Nothing acts on a row while loading: no Keep one; the search's matches greyed too.        assertTrue(list.contains("if (loading == null) {\n                // The same site twice"))        assertTrue(list.contains("if (loading != null) item(\"loading\") { ListLoadingLine(loading) }"))        assertTrue(list.contains("if (loading != null) { Greyed(r, r == matches.last()"))        // Every hidden row passes the same loading.        assertTrue(list.contains("Row(state, r, last = r == hrows.last(), loading)"))    }    @Test    fun you_says_the_one_line_and_no_count_of_nothing() {        val you = src("You.kt")        assertTrue(you.contains("val line = seen?.line?.takeIf { id == \"sync\" } ?: row.str(\"line\")"))        assertTrue(you.contains("if (first != null && it == state.w(\"you.card.other\", mapOf(\"n\" to \"0\"))) first.words else it"))        assertEquals("{n} sites", words["you"]!!.jsonObject["card"]!!.jsonObject["other"]!!.jsonPrimitive.content)    }}