package com.cottondb.data import androidx.compose.ui.graphics.Color import com.cottondb.shell.Ground import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import org.json.JSONObject import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test import java.io.File /** Cotton Data on Android (assets/design/data/CONTRACT.md rows 8 and 9). */ class DataAppTest { 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()) /** Until the core answers a look for Data, Stone stands, with Days' blue as the accent (owner, 25 Nov 2026). */ @Test fun stone_and_days_blue_stand_until_the_core_answers() { assertEquals(Color(0xFFEFF1EA), Palette.of(null, dark = false).ground) assertEquals(Color(0xFF1D3FD8), Palette.of(null, dark = false).accent) assertEquals(Color(0xFF141812), Palette.of(null, dark = true).ground) assertEquals(Color(0xFF8EA2FF), Palette.of(null, dark = true).accent) } /** The core's colours win where it names them; what it leaves out stays Stone's. */ @Test fun the_cores_look_is_laid_over_stone() { val answer = Json.parseToJsonElement("""{"app":"data","colours":{"light":{"ground":"#FBFAF6","accent":"#3B6B2E"},"dark":{"ground":"#050605"}}}""") as JsonObject val day = Palette.of(answer, dark = false) assertEquals(Color(0xFFFBFAF6), day.ground) assertEquals(Color(0xFF3B6B2E), day.accent) assertEquals(Palette.stone.ink, day.ink) assertEquals(Color(0xFF050605), Palette.of(answer, dark = true).ground) assertTrue(Palette.of(answer, dark = true).dark) } /** Every word the screens ask of the `data` line set is written there. */ @Test fun every_word_asked_is_in_the_data_line_set() { val flat = flatten(Json.parseToJsonElement(File(repo, "crates/cotton-core/kits/shell/lines/data.json").readText()) as JsonObject) val sources = File(repo, "apps/android/app/src/data/kotlin/com/cottondb/data").listFiles()!!.joinToString("\n") { it.readText() } val asked = Regex("""(? 10) asked.forEach { key -> assertTrue("data.$key is written", key in flat) } // The tabs' words and TalkBack's names for them. DataState.TABS.forEach { tab -> assertTrue("tab.$tab" in flat); assertTrue("a11y.${tab}_tab" in flat) } } /** The registry row, the flavour and the window: com.cottondb.data, .dev beside it, on both phones, on Stone. */ @Test fun data_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") == "data" } assertEquals("Cotton Data", row.getString("name")) assertEquals("Data", row.getString("label")) assertEquals("data", row.getString("kit")) assertEquals("com.cottondb.data", row.getJSONObject("android").getString("package")) assertEquals("com.cottondb.data.dev", row.getJSONObject("android").getString("devPackage")) // The iPhone came with DATA iPhone 1 (a3d7626983): the same bundle ids. assertEquals("com.cottondb.data", row.getJSONObject("ios").getJSONObject("live").getString("bundleId")) assertEquals("com.cottondb.data.dev", row.getJSONObject("ios").getJSONObject("dev").getString("bundleId")) val flavour = json("apps/cotton/App/Flavours/data/flavour.json") assertEquals("data", flavour.getString("kit")) val window = json("apps/android/app/flavours.json").getJSONObject("data").getString("windowGround") assertEquals(flavour.getJSONObject("flat").getString("light"), window) assertEquals(Ground.blank, DataParts().ground) } /** Every drawn mark parses. */ @Test fun every_mark_parses() { assertTrue(everyMarkParses()) } /** Read only: no screen asks the core to change a row; the one write is this phone's Keep everything choice. */ @Test fun the_screens_only_read() { val sources = File(repo, "apps/android/app/src/data/kotlin/com/cottondb/data").listFiles()!!.joinToString("\n") { it.readText() } val methods = Regex("""(?:read|readWithFree|core\.obj)\("([a-z_.]+)"""").findAll(sources).map { it.groupValues[1] }.toSet() assertTrue(methods.isNotEmpty()) methods.forEach { m -> assertTrue("$m is a read", m.startsWith("data.") || m == "media.ensure") } } private fun flatten(o: JsonObject, prefix: String = ""): Set = o.flatMap { (k, v) -> if (v is JsonObject) { // A counted word (`.one`, `.other`) is asked by its stem. 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() }