Cotton code
85 lines · 4.6 KB · KotlinRaw
package com.cottondb.messagesimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport 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.File/** Messages on Android draws the core's answers and decides nothing: these read them as the face does. Every row is invented. */class MessagesFlavourTest {    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 fun answer(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject    /**     * Chat stands on its own Warm ground (DECISIONS "Look"), never the pegboard: the launch is the same warm ground,     * light and night, with no painted object, and the app reads no deck (it draws every page itself).     */    @Test    fun the_chat_launches_on_its_own_warm_ground() {        val mine = json("apps/android/app/flavours.json").getJSONObject("messages")        assertEquals("#FAF7F2", mine.getString("windowGround"))        assertEquals("#FAF7F2", mine.getJSONObject("flatLaunch").getString("light"))        assertEquals("#1A1714", mine.getJSONObject("flatLaunch").getString("dark"))        val parts = Class.forName("com.cottondb.messages.MessagesParts")        assertTrue("the chat's own fonts are carried", File(repo, "apps/cotton/App/Flavours/messages/ChatFonts/HankenGrotesk.ttf").isFile)        assertTrue(File(repo, "apps/cotton/App/Flavours/messages/ChatFonts/NotoSansDevanagari.ttf").isFile)        assertTrue(parts.name.isNotEmpty())    }    /** A socket frame: an envelope's item is handed on whole; a pong, a send or nonsense is not chat's. */    @Test    fun only_an_envelope_is_handed_on() {        val item = ChatFrames.envelope("""{"t":"envelope","item":{"id":"abc","kind":"message","peer":"+15550100001","out":false,"request":false,"box":"c1:x","at":5}}""")        assertEquals("abc", (item as JsonObject)["id"]!!.toString().trim('"'))        assertNull(ChatFrames.envelope("""{"t":"pong"}"""))        assertNull(ChatFrames.envelope("""{"t":"send"}"""))        assertNull(ChatFrames.envelope("not json"))    }    /** `{t:"reach"}`: someone waited for can be written to, so the outbox runs (chat-people); it is never an envelope. */    @Test    fun a_reach_frame_runs_the_outbox() {        assertEquals(true, ChatFrames.reach("""{"t":"reach","number":"+15550100001"}"""))        assertEquals(false, ChatFrames.reach("""{"t":"envelope","item":{}}"""))        assertEquals(false, ChatFrames.reach("not json"))        assertNull(ChatFrames.envelope("""{"t":"reach","number":"+15550100001"}"""))    }    /** `{t:"ask"}`: someone asks to chat, so the inbox is read and the ask lands as a request; never an envelope or a reach. */    @Test    fun an_ask_frame_reads_the_inbox() {        assertEquals(true, ChatFrames.ask("""{"t":"ask"}"""))        assertEquals(false, ChatFrames.ask("""{"t":"reach","number":"+15550100001"}"""))        assertEquals(false, ChatFrames.ask("not json"))        assertEquals(false, ChatFrames.reach("""{"t":"ask"}"""))        assertNull(ChatFrames.envelope("""{"t":"ask"}"""))    }    /** `{t:"rows"}`: another device of his moved rows, so the core is asked for a pass now; nothing else is one. */    @Test    fun a_rows_frame_asks_for_a_pass() {        assertEquals(true, ChatFrames.rows("""{"t":"rows","v":42}"""))        assertEquals(false, ChatFrames.rows("""{"t":"ask"}"""))        assertEquals(false, ChatFrames.rows("not json"))        assertEquals(false, ChatFrames.ask("""{"t":"rows","v":42}"""))        assertEquals(false, ChatFrames.reach("""{"t":"rows","v":42}"""))        assertNull(ChatFrames.envelope("""{"t":"rows","v":42}"""))    }    /** `{t:"ring"}`: a call rung over the socket reads as the FCM ring does; its stop too; nothing else is a ring. */    @Test    fun a_ring_frame_is_the_pushes_ring() {        assertEquals(com.cottondb.calling.CallPush.Ring("r1", true, "c1:abc"), ChatFrames.ring("""{"t":"ring","kind":"ring","room":"r1","video":"1","box":"c1:abc"}"""))        assertEquals(com.cottondb.calling.CallPush.Stop("r1", "cancelled"), ChatFrames.ring("""{"t":"ring","kind":"stop","room":"r1","stop":"cancelled"}"""))        assertNull(ChatFrames.ring("""{"t":"envelope","item":{}}"""))        assertNull(ChatFrames.ring("""{"t":"ring","kind":"ring"}"""))        assertNull(ChatFrames.envelope("""{"t":"ring","kind":"ring","room":"r1","video":"0"}"""))    }}