package com.cottondb.cinemas import com.cottondb.cinemas.stream.Found import com.cottondb.cinemas.stream.Maker import com.cottondb.cinemas.stream.RowPage import com.cottondb.cinemas.stream.Home import com.cottondb.cinemas.stream.Item import com.cottondb.cinemas.stream.Episode import com.cottondb.cinemas.stream.Motion import com.cottondb.cinemas.stream.Season import com.cottondb.cinemas.stream.Title import com.cottondb.cinemas.stream.clock import com.cottondb.cinemas.stream.lookOf import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test /** The streaming build reads the core's answers (CONTRACT-2.md) as they come, and draws nothing the core left out. */ class StreamShapesTest { private fun j(text: String): JsonObject = Json.parseToJsonElement(text).jsonObject @Test fun home_reads_the_featured_title_and_the_shelves_in_order() { val home = Home.of(j(""" {"featured": {"item": {"table": "film", "id": "f1", "title": "Night Ferry", "year": "2019", "kind": "film", "poster": "k1", "progress": 0.42}, "line": "Carry on · 1:12 of 2:04", "play": "Resume", "tv": "Play on TV", "playable": true}, "rows": [ {"id": "carry_on", "title": "Carry on", "group": null, "count": 1, "items": [{"table": "film", "id": "f1", "title": "Night Ferry"}], "more": false}, {"id": "group:g1", "title": "Rainy Sundays", "group": "g1", "count": 0, "items": [], "more": false} ], "empty": null} """)) val featured = home.featured!! assertEquals("Night Ferry", featured.item.title) assertEquals(0.42f, featured.item.progress!!, 0.0001f) assertEquals("Resume", featured.play) assertEquals(listOf("carry_on", "group:g1"), home.rows.map { it.id }) assertFalse(home.rows[0].mine) assertTrue("his group's shelf, even empty", home.rows[1].mine) assertNull(home.empty) } @Test fun an_empty_library_says_the_cores_words() { val home = Home.of(j("""{"featured": null, "rows": [], "empty": {"title": "Your films will live here", "line": "Add films", "add": "Add films"}}""")) assertNull(home.featured) assertEquals("Your films will live here", home.empty!!.title) } @Test fun an_item_keeps_only_what_the_core_said() { val item = Item.of(j("""{"table": "film_episode", "id": "e4", "title": "Lowmarsh", "kind": "episode", "caption": "S1 E4", "poster": "", "progress": 1.7, "on_phone": "keeping", "preparing": true, "from": "Priya"}"""))!! assertNull("an empty key is no picture", item.poster) assertEquals(1f, item.progress!!, 0f) assertEquals("keeping", item.onPhone) assertTrue(item.preparing) assertEquals("Priya", item.from) assertEquals("S1 E4", item.caption) assertTrue("one title in two shelves grows from two places", item.key("carry_on") != item.key("films")) assertNull(Item.of(j("""{"title": "no id"}"""))) } @Test fun a_title_reads_play_acts_groups_and_seasons() { val title = Title.of(j(""" {"item": {"table": "film", "id": "s1", "title": "Lowmarsh", "kind": "series"}, "synopsis": " ", "facts": ["2018", "3 seasons"], "play": {"label": "Play S2 E5", "from_start": null}, "tv": "Play on TV", "acts": [{"id": "keep", "label": "Keep on this phone"}, {"id": "group", "label": "Add to a group"}], "groups": [{"id": "g1", "name": "Rainy Sundays", "in": true}, {"id": "g2", "name": "Kids", "in": false}], "subtitles": {"line": "English · Ready", "work": null}, "preparing": null, "seasons": [{"id": "z1", "number": 1, "title": null, "episodes": 8, "watched": 3}], "source": null} """))!! assertNull("a blank synopsis is not drawn", title.synopsis) assertEquals("Play S2 E5", title.play) assertNull(title.fromStart) assertEquals(listOf("keep", "group"), title.acts.map { it.id }) assertEquals(listOf(true, false), title.groups.map { it.inIt }) assertEquals("English · Ready", title.subtitles) assertEquals(1, title.seasons.single().number) assertEquals("", title.seasons.single().title) } @Test fun a_season_names_each_episode_by_its_own_title() { val season = Season.of(j(""" {"season": {"id": "z1", "title": "Season 1"}, "episodes": [{"table": "film_episode", "id": "e1", "title": "Lowmarsh", "number": 1, "episode_title": "Peat Fire", "minutes": 48, "still": "k9"}]} """))!! val first = season.episodes.single() assertEquals("Peat Fire", first.title) assertEquals(1, first.number) assertEquals("k9", first.still) } @Test fun search_reads_titles_and_groups() { val found = Found.of(j("""{"items": [{"table": "film", "id": "f1", "title": "The Salt Orchard"}], "groups": [{"id": "g1", "name": "Salt and sea"}]}""")) assertEquals("The Salt Orchard", found.items.single().title) assertEquals("Salt and sea", found.groups.single().name) } @Test fun search_reads_the_top_results_line_and_who_titles_are_by() { val found = Found.of(j("""{"items": [{"table": "film", "id": "f1", "title": "The Salt Orchard"}], "groups": [], "top_line": "Film · 2021 · 1h 12m left", "people": [{"name": "Marta Saltonstall", "kind": "person", "ids": ["f1", "f2"], "line": "2 titles"}, {"name": "Bench & Light", "kind": "channel", "ids": ["c1"], "line": "1 title"}]}""")) assertEquals("Film · 2021 · 1h 12m left", found.topLine) assertEquals(listOf(Maker("Marta Saltonstall", "person", "2 titles"), Maker("Bench & Light", "channel", "1 title")), found.people) assertEquals(null, Found.of(j("""{"items": [], "groups": []}""")).topLine) } @Test fun a_row_opened_says_how_many_it_holds() { assertEquals(286, RowPage.of(j("""{"id": "films", "title": "Films", "count": 286, "items": [], "next": null}""")).count) assertEquals(null, RowPage.of(j("""{"id": "films", "title": "Films", "items": []}""")).count) } @Test fun clocks_read_as_the_design_writes_them() { assertEquals("4:05", clock(245_000)) assertEquals("1:12:04", clock(4_324_000)) assertEquals("0:00", clock(-5)) } @Test fun springs_are_the_iphones_response_in_composes_stiffness() { // response 0.35 s: (2π / 0.35)² ≈ 322. assertEquals(322.3f, Motion.stiffness(0.35f), 0.5f) assertEquals(0.9f, Motion.snap.dampingRatio, 0f) assertEquals(0.88f, Motion.grow.dampingRatio, 0f) } @Test fun a_title_with_no_picture_always_wears_the_same_drawn_poster() { assertEquals("case and spacing aside", lookOf("Lowmarsh"), lookOf(" lowmarsh ")) val looks = listOf("Lowmarsh", "Copper Rain", "Blue Corridor", "Far Shore", "Glass Harbour", "Second Spring").map(::lookOf).toSet() assertTrue("names spread over the design's looks", looks.size >= 3) } @Test fun a_title_wears_the_art_the_core_chose_and_an_episode_says_where_it_stands() { val item = Item.of(j("""{"table": "film", "id": "f1", "title": "Night Ferry", "art": {"a": "#14302e", "b": "#d99a4e", "shape": "kite"}}"""))!! assertEquals(com.cottondb.cinemas.stream.Look(androidx.compose.ui.graphics.Color(0xFF14302E), androidx.compose.ui.graphics.Color(0xFFD99A4E), com.cottondb.cinemas.stream.Motif.Kite), item.art) assertEquals("an unknown shape: no art, the phone picks one", null, Item.of(j("""{"table": "film", "id": "f1", "art": {"a": "#14302e", "b": "#d99a4e", "shape": "comet"}}"""))!!.art) val episode = Episode.of(j("""{"table": "film_episode", "id": "e1", "title": "Lowmarsh", "number": 2, "episode_title": "The Tide", "state_line": "23 min left"}"""))!! assertEquals("23 min left", episode.stateLine) } @Test fun the_row_of_four_reads_each_acts_short_words() { val title = Title.of(j("""{"item": {"table": "film", "id": "f1", "title": "Night Ferry"}, "play": {"label": "Play"}, "acts": [{"id": "keep", "label": "Keep on this phone", "short": "Keep on phone"}, {"id": "bin", "label": "Move to the Bin"}]}"""))!! assertEquals("Keep on phone", title.acts[0].short) assertEquals("no short words: the label", "Move to the Bin", title.acts[1].short) val placed = Title.of(j("""{"item": {"table": "film", "id": "f1", "title": "Night Ferry"}, "play": {"label": "Play"}, "watched_line": "28 min watched · 1h 53m left", "acts": [{"id": "keep", "label": "Keep on this phone", "short": "Keep on phone", "place": "row", "second": "2.3 GB"}, {"id": "kind", "label": "What it is", "place": "more", "second": ""}]}"""))!! assertEquals("row", placed.acts[0].place) assertEquals("2.3 GB", placed.acts[0].second) assertNull("a blank second line is none", placed.acts[1].second) assertEquals("28 min watched · 1h 53m left", placed.watchedLine) } }