+package net.pterodactylus.rhynodge.filters.webpages.savoy
+
+import net.pterodactylus.rhynodge.Reaction
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.contains
+import org.hamcrest.Matchers.containsInAnyOrder
+import org.hamcrest.Matchers.containsString
+import org.hamcrest.Matchers.empty
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.not
+import org.jsoup.Jsoup
+import org.jsoup.nodes.TextNode
+import org.junit.Test
+import java.time.LocalDateTime
+
+class MovieStateTest {
+
+ @Test
+ fun `summary contains date of earliest movie`() {
+ val movieState = MovieState(
+ setOf(
+ Movie("1", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 12, 18, 45), "")) },
+ Movie("2", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 3, 12, 15, 30), "")) },
+ Movie("3", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 11, 21, 15), "")) }
+ ), emptySet()
+ )
+ assertThat(movieState.output(Reaction("", null, null, null)).summary(), containsString("2024-02-11"))
+ }
+
+ @Test
+ fun `movie state without new movies is not triggered`() {
+ assertThat(MovieState(emptySet(), emptySet()).triggered(), equalTo(false))
+ }
+
+ @Test
+ fun `movie state with a new movie is triggered`() {
+ assertThat(MovieState(emptySet(), setOf(Movie("1", ""))).triggered(), equalTo(true))
+ }
+
+ @Test
+ fun `html output does not contain a section for new movies if there are no new movies`() {
+ val movieState = MovieState(emptySet(), emptySet())
+ val output = movieState.output(Reaction("", null, null, null))
+ val document = Jsoup.parse(output.text("text/html"))
+ assertThat(document.select("section.new-movies"), empty())
+ }
+
+ @Test
+ fun `html output does contain a section for new movie if there are new movies`() {
+ val movieState = MovieState(emptySet(), setOf(Movie("1", ""), Movie("2", "")))
+ val output = movieState.output(Reaction("", null, null, null))
+ val document = Jsoup.parse(output.text("text/html"))
+ assertThat(document.select("section.new-movies"), not(empty()))
+ }
+
+ @Test
+ fun `new movies section contains the titles of all new movies`() {
+ val movieState = MovieState(emptySet(), setOf(Movie("New Movie", ""), Movie("Even Newer Movie", "")))
+ val output = movieState.output(Reaction("", null, null, null))
+ val document = Jsoup.parse(output.text("text/html"))
+ assertThat(document.select("section.new-movies li.movie .name").textNodes().map(TextNode::text), containsInAnyOrder("New Movie", "Even Newer Movie"))
+ }
+
+ @Test
+ fun `html output contains section for the daily programme`() {
+ val movieState = MovieState(emptySet(), emptySet())
+ val output = movieState.output(Reaction("", null, null, null))
+ val document = Jsoup.parse(output.text("text/html"))
+ assertThat(document.select("section.daily-programmes"), not(empty()))
+ }
+
+ @Test
+ fun `html output contains a section for each day with a movie`() {
+ val movieState = MovieState(
+ setOf(
+ movie("Movie 1", "", "20240212-1845", "20240213-1330", "20240214-1815"),
+ movie("Movie 2", "", "20240212-2030", "20240213-1745", "20240214-1430"),
+ movie("Movie 3", "", "20240213-2015", "20240214-1730"),
+ movie("Movie 4", "", "20240216-1000"),
+ ), emptySet()
+ )
+ val output = movieState.output(Reaction("", null, null, null))
+ val html = output.text("text/html")
+ val document = Jsoup.parse(html)
+ assertThat(document.select("section.daily-programmes li.day").map { it.attr("data-date") }, contains("2024-02-12", "2024-02-13", "2024-02-14", "2024-02-16"))
+ }
+
+ @Test
+ fun `html output contains the correct movies within each day`() {
+ val movieState = MovieState(
+ setOf(
+ movie("Movie 1", "https://cdn.premiumkino.de/movie/3047/81c49774d7828a898ae1d525ffd135af_w300.jpg", "20240212-1845", "20240213-1330", "20240214-1815"),
+ movie("Movie 2", "https://cdn.premiumkino.de/movie/1066/aba09af737677ff6a15676ae588098b1_w300.jpg", "20240212-2030", "20240213-1745", "20240214-1430"),
+ movie("Movie 3", "https://cdn.premiumkino.de/movie/7300/14d1b21dee51a82a7b096ca282bf01c8_w300.png", "20240213-2015", "20240214-1730"),
+ movie("Movie 4", "https://cdn.premiumkino.de/movie/6080/cef2b33483d2898ddb472c955c58ea20_w300.jpg", "20240216-1000"),
+ ), setOf(
+ movie("Movie 1", "https://cdn.premiumkino.de/movie/3047/81c49774d7828a898ae1d525ffd135af_w300.jpg", "20240212-1845", "20240213-1330", "20240214-1815"),
+ movie("Movie 2", "https://cdn.premiumkino.de/movie/1066/aba09af737677ff6a15676ae588098b1_w300.jpg", "20240212-1845", "20240213-1330", "20240214-1815")
+ )
+ )
+ val output = movieState.output(Reaction("", null, null, null))
+ val html = output.text("text/html")
+ val document = Jsoup.parse(html)
+ assertThat(
+ document.select("section.daily-programmes li.day[data-date='2024-02-12'] li.performance").map { it.select(".time").text() + " - " + it.select(".name").text() }, contains(
+ "18:45 - Movie 1", "20:30 - Movie 2"
+ )
+ )
+ }
+
+}
+
+private fun dateTime(dateTimeString: String) = LocalDateTime.of(
+ dateTimeString.substring(0..3).toInt(),
+ dateTimeString.substring(4..5).toInt(),
+ dateTimeString.substring(6..7).toInt(),
+ dateTimeString.substring(9..10).toInt(),
+ dateTimeString.substring(11..12).toInt(),
+)
+
+private fun movie(name: String, imageUrl: String, vararg times: String) = Movie(name, imageUrl).apply {
+ times.map(::dateTime).map { Performance(it, "https://link/$it") }.forEach(this::addPerformance)
+}