package com.cottondb.cinemas import com.cottondb.cinemas.faces.Card import com.cottondb.cinemas.faces.EpisodeLobby import com.cottondb.cinemas.faces.FilmPoster import com.cottondb.cinemas.faces.Measure import com.cottondb.cinemas.faces.dayStamp import com.cottondb.cinemas.faces.lines import com.cottondb.shell.player.Heard import com.cottondb.shell.core.Lines import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.double import kotlinx.serialization.json.jsonPrimitive import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Test /** The Cinemas faces' setting rules (ported from FilmPosterFace / EpisodeLobbyFace) and the player's Heard clock, on invented cards. */ class CinemaSettingTest { private val words = Lines( mapOf( "cards.film.length_minutes" to "{minutes} min", "cards.film.length_hours" to "{hours} h {minutes}", "cards.film.position" to "{at} of {of}", "cards.film.stub" to "Watched", "cards.film.stub_converting" to "Converting", "cards.date.add" to "Add date", ), "Cotton Cinemas", ) /** Every letter a unit wide. */ private val mono = Measure { text, _, size -> text.length * size } private fun card(json: String) = Card(Json.parseToJsonElement(json) as JsonObject) @Test fun aFilmSaysItsLengthInHoursOrMinutes() { assertEquals("1 h 52", FilmPoster.length(card("""{"slots":{"limit":112}}"""), words)) assertEquals("1 h 05", FilmPoster.length(card("""{"slots":{"limit":65}}"""), words)) assertEquals("48 min", FilmPoster.length(card("""{"slots":{"limit":48}}"""), words)) assertNull(FilmPoster.length(card("""{"slots":{}}"""), words)) } @Test fun theStripFillsOnlyPartWayAndNeverForASeriesOrAWatchedFilm() { assertEquals(0.5, FilmPoster.share(card("""{"slots":{"limit":100,"used":3000}}"""))!!, 1e-9) assertNull(FilmPoster.share(card("""{"slots":{"limit":100,"used":3000},"stored":{"state":"watched"}}"""))) assertNull(FilmPoster.share(card("""{"slots":{"limit":100,"used":3000},"stored":{"facts":"series"}}"""))) } @Test fun aSeriesLaysItsNewestSeasonsNewestAtTheBack() { val series = card("""{"slots":{},"stored":{"facts":"series"},"related":[{"id":"film_season","said":"5 seasons","count":5}]}""") assertEquals(listOf(5, 3, 4), FilmPoster.seasons(series)) assertEquals(emptyList(), FilmPoster.seasons(card("""{"slots":{}}"""))) } @Test fun theStubSaysWatchedWithItsDayOrConverting() { assertEquals("WATCHED" to "12 NOV 2026", FilmPoster.stub(card("""{"slots":{"date":"Thu 12 Nov 2026"},"stored":{"state":"watched"}}"""), words)) assertEquals("CONVERTING" to null, FilmPoster.stub(card("""{"slots":{},"stored":{"state":"needs_mac"}}"""), words)) assertEquals("WATCHED" to null, FilmPoster.stub(card("""{"slots":{"date":"Add date"},"stored":{"state":"watched"}}"""), words)) assertEquals("12 NOV", dayStamp("Thu 12 Nov")) } @Test fun anEpisodeSaysWhereItStandsOnAPlayersClock() { assertEquals("24:00 of 40:00", EpisodeLobby.place(card("""{"slots":{"limit":40,"used":1440}}"""), words)) assertEquals("40:00 of 40:00", EpisodeLobby.place(card("""{"slots":{"limit":40,"used":5},"stored":{"state":"watched"}}"""), words)) assertEquals("1:52:00", EpisodeLobby.clock(6720.0)) assertEquals("5:30", EpisodeLobby.clock(330.0)) } @Test fun wordsBreakAtSpacesAndThePastTheLimitIsCutByLetters() { assertEquals(listOf("The Fog", "Bell"), lines("The Fog Bell", mono, Measure.Face.Type, 1f, 7f, 2)) assertEquals(listOf("The Fog", "Bell R…"), lines("The Fog Bell Rings", mono, Measure.Face.Type, 1f, 7f, 2)) } @Test fun heardCountsForwardPlayButNotAScrub() { val heard = Heard("film", "f1", from = 10.0) heard.tick(11.0); heard.tick(12.0); heard.tick(300.0); heard.tick(301.0) val report = heard.report("stopped", 301.0, 600.0, "An invented phone")!! assertEquals(3.0, report["seconds"]!!.jsonPrimitive.double, 1e-9) assertEquals(301.0, report["at_s"]!!.jsonPrimitive.double, 1e-9) assertEquals("stopped", report["ended"]!!.jsonPrimitive.content) assertNull("a play ends once", heard.report("stopped", 301.0, 600.0, null)) } }