Cotton code
66 lines · 3.5 KB · KotlinRaw
package com.cottondb.keysimport androidx.compose.ui.graphics.Colorimport com.cottondb.keys.secrets.Paletteimport com.cottondb.keys.secrets.kindMarkimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.File/** Cotton Secrets' Plum look (assets/design/keys/DECISIONS.md, owner 25 Nov 2026: Plum with the asterisk). */class SecretsLookTest {    private val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile }    /** Until the core answers a look for Secrets, the drawing's Plum stands: its ground, ink, rule and accent, day and night. */    @Test    fun plum_stands_until_the_core_answers() {        assertEquals(Color(0xFFF1EEF3), Palette.of(null, dark = false).ground)        assertEquals(Color(0xFF7A3C9E), Palette.of(null, dark = false).accent)        assertEquals(Color(0xFF18131D), Palette.of(null, dark = true).ground)        assertEquals(Color(0xFFC39BE3), Palette.of(null, dark = true).accent)    }    /** The core's colours win where it names them; what it leaves out stays Plum's. */    @Test    fun the_cores_look_is_laid_over_plum() {        val answer = Json.parseToJsonElement("""{"app":"keys","colours":{"light":{"ground":"#FBFAF6","accent":"#B23A48"},"dark":{"ground":"#030504"}}}""") as JsonObject        val day = Palette.of(answer, dark = false)        assertEquals(Color(0xFFFBFAF6), day.ground)        assertEquals(Color(0xFFB23A48), day.accent)        assertEquals(Palette.plum.ink, day.ink)        assertEquals(Color(0xFF030504), Palette.of(answer, dark = true).ground)        assertEquals(Palette.plumNight.accent, Palette.of(answer, dark = true).accent)    }    /** Every kind the core keeps wears one of the drawing's marks. */    @Test    fun every_kind_has_a_drawn_mark() {        val kinds = Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/personal/tables/secret.json").readText()).toString()        listOf("login", "api_key", "note", "card", "identity", "document", "code", "wifi", "address", "passkey").forEach { kind ->            assertTrue("$kind is named by the table", kind == "passkey" || kinds.contains("\"$kind\""))            assertTrue(kindMark(kind).startsWith("k-"))        }        assertEquals("k-pass", kindMark("login"))        assertEquals("k-token", kindMark("api_key"))        assertEquals("k-card", kindMark("card"))        assertEquals("k-passkey", kindMark("passkey"))    }    /** The words Secrets draws are the core's: the line set ships with the core, and every key the screens ask exists. */    @Test    fun secrets_words_ship_with_the_core() {        val lines = File(repo, "crates/cotton-core/kits/shell/lines/secrets.json").readText()        assertTrue(File(repo, "crates/cotton-core/src/rules/shell_lines.rs").readText().contains("lines/secrets.json"))        val sources = File(repo, "apps/android/app/src/keys/kotlin/com/cottondb/keys/secrets").listFiles()!!.joinToString("\n") { it.readText() }        val asked = Regex("""state\.(?:w|count)\("([a-z_.]+)"""").findAll(sources).map { it.groupValues[1] }.toSet()        val flat = flatten(Json.parseToJsonElement(lines) as JsonObject)        asked.forEach { key -> assertTrue("secrets.$key is written", flat.any { it == key || it.startsWith("$key.") }) }    }    private fun flatten(o: JsonObject, prefix: String = ""): List<String> = o.flatMap { (k, v) ->        if (v is JsonObject) flatten(v, "$prefix$k.") else listOf("$prefix$k")    }}