Cotton code
63 lines · 3.5 KB · KotlinRaw
package com.cottondb.developerimport com.cottondb.developer.faces.ArtifactTitleimport com.cottondb.developer.faces.MachineReadingimport com.cottondb.developer.faces.SessionMarkimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertEqualsimport org.junit.Testclass DeveloperReadingTest {    private fun card(json: String): JsonObject = Json.parseToJsonElement(json).jsonObject    /** The dials are the core's numbers in the face's order; a dial with no total, or a missing one, is not drawn; a share stays within the scale. */    @Test    fun machine_dials_read_the_numbers() {        val c = card("""{"face":"machine","slots":{"load_dial":{"used":28,"total":14},"memory_dial":{"used":22,"total":48},"disk_dial":{"used":5,"total":0}}}""")        val dials = MachineReading.dials(c)        assertEquals(listOf("memory_dial", "load_dial"), dials.map { it.slot })        assertEquals(22f / 48f, dials[0].share, 1e-6f)        assertEquals(1f, dials[1].share, 0f)    }    /** The facts are the core's label and words; a card from before the facts says its chip and its body's lines. */    @Test    fun machine_facts_fall_back_to_the_lines() {        val listed = card("""{"slots":{"facts":[{"label":"Chip","said":"M4 Pro"},{"label":"Coding sessions","said":""},{"said":"3"}]}}""")        assertEquals(listOf(MachineReading.Fact("Chip", "M4 Pro"), MachineReading.Fact(null, "3")), MachineReading.facts(listed))        val old = card("""{"slots":{"subtitle":"M2","body":"macOS 26\n2 sessions"}}""")        assertEquals(listOf("M2", "macOS 26", "2 sessions"), MachineReading.facts(old).map { it.said })        assert(MachineReading.spans("Coding sessions")); assert(!MachineReading.spans("Memory"))    }    /** The Mac drawn is the one the model names, else the desk Mac. */    @Test    fun machine_art_follows_the_model() {        assertEquals("face-art-mac-mini", MachineReading.art(card("""{"slots":{"model":"Mac mini (M4)","title":"box"}}""")))        assertEquals("face-art-laptop", MachineReading.art(card("""{"slots":{"title":"MacBook Pro"}}""")))        assertEquals(MachineReading.deskMac, MachineReading.art(card("""{"slots":{"title":"mac-c"}}""")))    }    /** A session's ink is its stored state's: running green, waiting or failed red, anything else pencil; only waiting asks. */    @Test    fun session_marks_by_stored_state() {        assertEquals(SessionMark.Ink.Green, SessionMark.ink("running"))        assertEquals(SessionMark.Ink.Red, SessionMark.ink("waiting"))        assertEquals(SessionMark.Ink.Red, SessionMark.ink("failed"))        assertEquals(SessionMark.Ink.Pencil, SessionMark.ink("queued"))        assertEquals(SessionMark.Ink.Pencil, SessionMark.ink(null))        assert(SessionMark.asks("waiting")); assert(!SessionMark.asks("running"))        assertEquals(listOf("mac-c", "lane/x", "opus"), SessionMark.chips("mac-c · lane/x ·  opus", " · "))        assertEquals(emptyList<String>(), SessionMark.chips(null, " · "))    }    /** A page titled by its address reads by its last part with the host apart; any other title as it is. */    @Test    fun artifact_title_reads_an_address() {        assertEquals(ArtifactTitle("Trip planner", "example.com"), ArtifactTitle.of("https://example.com/pages/trip-planner.html"))        assertEquals(ArtifactTitle("example.com", null), ArtifactTitle.of("https://example.com/"))        assertEquals(ArtifactTitle("Trip budget planner", null), ArtifactTitle.of("Trip budget planner"))    }}