Cotton code
98 lines · 5.7 KB · KotlinRaw
package com.cottondb.contactsimport 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.assertNullimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.Fileclass ContactsFlavourTest {    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())    /** Contacts draws its own plain ground: no material, and the window shows the flavour's `flat.light` before its first frame. */    @Test    fun contacts_stands_on_its_own_flat_ground() {        assertEquals(Ground.blank, ContactsParts().ground)        assertFalse(ContactsParts().readsDeck)        val window = json("apps/android/app/flavours.json").getJSONObject("contacts").getString("windowGround")        assertEquals(json("apps/cotton/App/Flavours/contacts/flavour.json").getJSONObject("flat").getString("light"), window)    }    /** The registry row: com.cottondb.contacts (.dev beside it), labelled Contacts, the people kit, on Android. */    @Test    fun the_registry_row_is_contacts_on_android() {        val apps = json("apps/registry.json").getJSONArray("apps")        val row = (0 until apps.length()).map { apps.getJSONObject(it) }.first { it.getString("slug") == "contacts" }        assertEquals("Contacts", row.getString("label"))        assertEquals("people", row.getString("kit"))        assertTrue(File(repo, "crates/cotton-core/kits/people/kit.json").isFile)        val android = row.getJSONObject("android")        assertEquals("com.cottondb.contacts", android.getString("package"))        assertEquals("com.cottondb.contacts.dev", android.getString("devPackage"))    }    /** Every line key the screens and the stand-in name is a line of the kit's `contacts` set (a missing one draws as its key). */    @Test    fun every_word_the_screens_say_is_in_the_contacts_lines() {        val lines = json("crates/cotton-core/kits/shell/lines/contacts.json")        fun has(key: String): Boolean {            var at: Any = lines            for (part in key.split('.')) at = (at as? JSONObject)?.opt(part) ?: return false            return at is String        }        val source = File(repo, "apps/android/app/src/contacts/kotlin/com/cottondb/contacts").listFiles()!!.joinToString("\n") { it.readText() }        val keys = Regex(""""((?:tabs|home|import|person|edit|groups|same|circle|you|ask)\.[a-z_]+)"""").findAll(source).map { it.groupValues[1] }.toSet()        assertTrue(keys.size > 60)        keys.forEach { assertTrue("contacts.$it is no line", has(it)) }        // The keys built from parts: the tabs and the kinds.        ContactsState.TABS.forEach { assertTrue(has("tabs.$it")) }        listOf("kind_all", "kind_people", "kind_organisations", "kind_assistants").forEach { assertTrue(has("home.$it")) }        // The app bundles a byte copy for before the core answers.        assertEquals(File(repo, "crates/cotton-core/kits/shell/lines/contacts.json").readText(), File(repo, "apps/loom/Sources/CottonValues/Resources/lines/contacts.json").readText())    }    /** Every `contacts.*` method of the screens' sections of CONTACTS-ANSWERS.md has a stand-in, for a core that does not answer it. */    @Test    fun the_stand_in_answers_every_published_method() {        // Sections 1 to 8: the app's own screens (later sections, the Circle, Share and files, are the core's alone).        val doc = File(repo, "assets/design/shared/CONTACTS-ANSWERS.md").readText().substringBefore("\n## 9.")        val published = Regex("""`(contacts\.[a-z.]+)""").findAll(doc).map { it.groupValues[1].trimEnd('.') }.toSet()        val local = File(repo, "apps/android/app/src/contacts/kotlin/com/cottondb/contacts/Local.kt").readText()        val answered = Regex(""""(contacts\.[a-z.]+)" ->""").findAll(local).map { it.groupValues[1] }.toSet()        assertTrue(published.size >= 15)        published.forEach { assertTrue("$it has no stand-in", it in answered) }    }    /** READ_CONTACTS is declared, and asked only from Bring in from contacts, never at launch. */    @Test    fun the_address_book_is_asked_for_only_in_import() {        assertTrue(File(repo, "apps/android/app/src/contacts/AndroidManifest.xml").readText().contains("android.permission.READ_CONTACTS"))        val dir = File(repo, "apps/android/app/src/contacts/kotlin/com/cottondb/contacts")        val asking = dir.listFiles()!!.filter { it.readText().contains("RequestPermission") }.map { it.name }        assertEquals(listOf("Import.kt"), asking)    }    /** A row from `list_people` (name) or a record's data (full_name), its mark and its search. */    @Test    fun a_contact_reads_either_shape_and_is_found_by_name_number_or_address() {        val listed = Contact.of(Json.parseToJsonElement("""{"id":"p1","name":"Asha Menon","phones":["+91 90000 64411"],"emails":["asha@example.com"],"org":"Acme"}""").jsonObject)!!        assertEquals("AM", listed.initials)        assertEquals(Contact.KIND_PERSON, listed.kind)        assertTrue(listed.matches("asha"))        assertTrue(listed.matches("64411"))        assertTrue(listed.matches("example.com"))        assertTrue(listed.matches("acme"))        assertFalse(listed.matches("ravi"))        assertFalse(listed.matches("91"))        val record = Contact.of(Json.parseToJsonElement("""{"id":"p2","full_name":"+91 98765","role_title":"Lead"}""").jsonObject)!!        assertEquals("#", record.initials)        assertEquals("Lead", record.role)        assertNull(Contact.of(Json.parseToJsonElement("""{"id":"x"}""").jsonObject))    }}