Cotton code
114 lines · 6.1 KB · KotlinRaw
package com.cottondb.sitesimport androidx.compose.ui.graphics.Colorimport com.cottondb.shell.Groundimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport org.json.JSONObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.File/** Cotton Sites on Android (assets/design/sites/CONTRACT.md row 14). */class SitesAppTest {    private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile }    private fun json(path: String) = JSONObject(File(repo, path).readText())    private val sources by lazy { File(repo, "apps/android/app/src/sites/kotlin/com/cottondb/sites").listFiles()!!.joinToString("\n") { it.readText() } }    /** Until the core answers a look for Sites, Rose stands by day and Night by dark, with Cranberry (owner, 25 Nov 2026). */    @Test    fun rose_and_cranberry_stand_until_the_core_answers() {        assertEquals(Color(0xFFF5F1F1), Palette.of(null, dark = false).ground)        assertEquals(Color(0xFF9A2C4C), Palette.of(null, dark = false).accent)        assertEquals(Color(0xFF171213), Palette.of(null, dark = true).ground)        assertEquals(Color(0xFFE58AA6), Palette.of(null, dark = true).accent)    }    /** The core's colours win where it names them; what it leaves out stays Rose's. */    @Test    fun the_cores_look_is_laid_over_rose() {        val answer = Json.parseToJsonElement("""{"app":"sites","colours":{"light":{"ground":"#FBFAF6","accent":"#0E6372"},"dark":{"ground":"#000000"}}}""") as JsonObject        val day = Palette.of(answer, dark = false)        assertEquals(Color(0xFFFBFAF6), day.ground)        assertEquals(Color(0xFF0E6372), day.accent)        assertEquals(Palette.rose.ink, day.ink)        assertTrue(Palette.of(answer, dark = true).dark)    }    /** Every word the screens ask of the `sites` line set is written there. */    @Test    fun every_word_asked_is_in_the_sites_line_set() {        val flat = flatten(Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/sites.json").readText()) as JsonObject)        val asked = Regex("""(?<!Log\.)\bw\("([a-z_.]+)"""").findAll(sources).map { it.groupValues[1] }.toSet()        assertTrue("the scan reads the screens", asked.size > 30)        asked.forEach { key -> assertTrue("sites.$key is written", key in flat) }        assertTrue("find.count" in flat)    }    /** Arrange's rows move for TalkBack too, in Sites' own words (51b0c496ce: `arrange.up`, `arrange.down`), each naming the site. */    @Test    fun arrange_moves_for_talkback_in_sites_words() {        val sites = flatten(Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/sites.json").readText()) as JsonObject)        listOf("arrange.up", "arrange.down").forEach { key ->            assertTrue("sites.$key is written", key in sites)            assertTrue("$key is asked", sources.contains("state.w(\"$key\", mapOf(\"label\" to r.label))"))        }    }    /** Every core call the screens make is one of the core's (CONTRACT "The core calls"): never an invented one. */    @Test    fun every_call_is_the_cores() {        val rust = File(repo, "crates/cotton-ffi/src/phone/sites_app.rs").readText() + File(repo, "crates/cotton-ffi/src/phone/sites.rs").readText()        val methods = Regex("""ask\("([a-z_.]+)"""").findAll(sources).map { it.groupValues[1] }.toSet()        assertTrue(methods.size > 15)        methods.forEach { m -> assertTrue("$m is a core call", "\"$m\"" in rust) }    }    /** The guard words never stand in a screen's own code as words: the screens write no sentence of their own. */    @Test    fun no_sentence_is_written_on_the_phone() {        val drawn = Regex("""\bT\("([^"]+)"""").findAll(sources).map { it.groupValues[1] }.toList()        // The one literal drawn is "Aa", the text size's mark, and a percentage.        drawn.forEach { assertTrue("\"$it\" is a mark, not a sentence", it == "Aa" || it.startsWith("\${")) }    }    /** The registry row, the flavour and the window: com.cottondb.sites, .dev beside it, Android first, on Rose. */    @Test    fun sites_is_declared_for_android() {        val apps = json("apps/registry.json").getJSONArray("apps")        val row = (0 until apps.length()).map { apps.getJSONObject(it) }.first { it.getString("slug") == "sites" }        assertEquals("Cotton Sites", row.getString("name"))        assertEquals("Sites", row.getString("label"))        assertEquals("com.cottondb.sites", row.getJSONObject("android").getString("package"))        // The iPhone's flavour folder, the one the Android build reads for every app the iPhone builds too.        val flavour = json("apps/cotton/App/Flavours/sites/flavour.json")        assertEquals("sites", flavour.getString("kit"))        val mine = json("apps/android/app/flavours.json").getJSONObject("sites")        assertEquals(flavour.getJSONObject("flat").getString("light"), mine.getString("windowGround"))        // The scheme is the iPhone's (apps/cotton/project.yml, read by the build), written in one place only.        assertFalse(mine.has("urlScheme"))        assertTrue(File(repo, "apps/cotton/project.yml").readText().contains("urlScheme: cottonsites"))        assertEquals(Ground.blank, SitesParts().ground)        assertFalse(SitesParts().readsDeck)    }    /** The bar's way back is Sites, named "Back to sites", never Close (owner, 25 Nov 2026). */    @Test    fun the_way_back_is_sites_never_close() {        assertTrue(Regex("""Icon\("sites", state\.w\("a11y\.sites"\)""").containsMatchIn(sources))        assertFalse(Regex("""Icon\("close", state\.w\("bar\.more""").containsMatchIn(sources))    }    /** Every drawn mark parses. */    @Test    fun every_mark_parses() { assertTrue(everyMarkParses()) }    private fun flatten(o: JsonObject, prefix: String = ""): Set<String> = o.flatMap { (k, v) ->        if (v is JsonObject) {            val inner = flatten(v, "$prefix$k.")            if (v.keys.any { it in setOf("one", "other", "zero") }) inner + "$prefix$k" else inner        } else listOf("$prefix$k")    }.toSet()}