♻️ Remove “addPerformance” method
[rhynodge.git] / src / test / kotlin / net / pterodactylus / rhynodge / filters / webpages / savoy / MovieStateTest.kt
1 package net.pterodactylus.rhynodge.filters.webpages.savoy
2
3 import net.pterodactylus.rhynodge.Reaction
4 import org.hamcrest.MatcherAssert.assertThat
5 import org.hamcrest.Matchers.contains
6 import org.hamcrest.Matchers.containsInAnyOrder
7 import org.hamcrest.Matchers.containsString
8 import org.hamcrest.Matchers.empty
9 import org.hamcrest.Matchers.equalTo
10 import org.hamcrest.Matchers.not
11 import org.jsoup.Jsoup
12 import org.jsoup.nodes.Element
13 import org.jsoup.nodes.TextNode
14 import org.junit.Test
15 import java.time.LocalDateTime
16
17 class MovieStateTest {
18
19         @Test
20         fun `summary contains date of earliest movie`() {
21                 val movieState = MovieState(
22                         setOf(
23                                 Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 12, 18, 45), "", ""))),
24                                 Movie("2", "", "", listOf(Performance(LocalDateTime.of(2024, 3, 12, 15, 30), "", ""))),
25                                 Movie("3", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 11, 21, 15), "", "")))
26                         ), emptySet()
27                 )
28                 assertThat(movieState.output(Reaction("", null, null, null)).summary(), containsString("2024-02-11"))
29         }
30
31         @Test
32         fun `movie state without new movies is not triggered`() {
33                 assertThat(MovieState(emptySet(), emptySet()).triggered(), equalTo(false))
34         }
35
36         @Test
37         fun `movie state with a new movie is triggered`() {
38                 assertThat(MovieState(emptySet(), setOf(Movie("1", "", ""))).triggered(), equalTo(true))
39         }
40
41         @Test
42         fun `html output does not contain a section for new movies if there are no new movies`() {
43                 val movieState = MovieState(emptySet(), emptySet())
44                 val output = movieState.output(Reaction("", null, null, null))
45                 val document = Jsoup.parse(output.text("text/html"))
46                 assertThat(document.select("section.new-movies"), empty())
47         }
48
49         @Test
50         fun `html output does contain a section for new movie if there are new movies`() {
51                 val movieState = MovieState(emptySet(), setOf(Movie("1", "", ""), Movie("2", "", "")))
52                 val output = movieState.output(Reaction("", null, null, null))
53                 val document = Jsoup.parse(output.text("text/html"))
54                 assertThat(document.select("section.new-movies"), not(empty()))
55         }
56
57         @Test
58         fun `new movies section contains description of movies`() {
59                 val movieState = MovieState(emptySet(), setOf(Movie("1", "", "Movie 1 is cool."), Movie("2", "", "Movie 2 is bad.")))
60                 val output = movieState.output(Reaction("", null, null, null))
61                 val document = Jsoup.parse(output.text("text/html"))
62                 assertThat(document.select("section.new-movies .description").map(Element::text), contains("Movie 1 is cool.", "Movie 2 is bad."))
63         }
64
65         @Test
66         fun `new movies section contains the titles of all new movies`() {
67                 val movieState = MovieState(emptySet(), setOf(Movie("New Movie", "", ""), Movie("Even Newer Movie", "", "")))
68                 val output = movieState.output(Reaction("", null, null, null))
69                 val document = Jsoup.parse(output.text("text/html"))
70                 assertThat(document.select("section.new-movies li.movie .name").textNodes().map(TextNode::text), containsInAnyOrder("New Movie", "Even Newer Movie"))
71         }
72
73         @Test
74         fun `html output contains section for the daily programme`() {
75                 val movieState = MovieState(emptySet(), emptySet())
76                 val output = movieState.output(Reaction("", null, null, null))
77                 val document = Jsoup.parse(output.text("text/html"))
78                 assertThat(document.select("section.daily-programmes"), not(empty()))
79         }
80
81         @Test
82         fun `html output contains a section for each day with a movie`() {
83                 val movieState = MovieState(
84                         setOf(
85                                 movie("Movie 1", "", "", "20240212-1845", "20240213-1330", "20240214-1815"),
86                                 movie("Movie 2", "", "", "20240212-2030", "20240213-1745", "20240214-1430"),
87                                 movie("Movie 3", "", "", "20240213-2015", "20240214-1730"),
88                                 movie("Movie 4", "", "", "20240216-1000"),
89                         ), emptySet()
90                 )
91                 val output = movieState.output(Reaction("", null, null, null))
92                 val html = output.text("text/html")
93                 val document = Jsoup.parse(html)
94                 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"))
95         }
96
97         @Test
98         fun `html output contains the correct movies within each day`() {
99                 val movieState = MovieState(
100                         setOf(
101                                 movie("Movie 1", "https://cdn.premiumkino.de/movie/3047/81c49774d7828a898ae1d525ffd135af_w300.jpg", "", "20240212-1845", "20240213-1330", "20240214-1815"),
102                                 movie("Movie 2", "https://cdn.premiumkino.de/movie/1066/aba09af737677ff6a15676ae588098b1_w300.jpg", "", "20240212-2030", "20240213-1745", "20240214-1430"),
103                                 movie("Movie 3", "https://cdn.premiumkino.de/movie/7300/14d1b21dee51a82a7b096ca282bf01c8_w300.png", "", "20240213-2015", "20240214-1730"),
104                                 movie("Movie 4", "https://cdn.premiumkino.de/movie/6080/cef2b33483d2898ddb472c955c58ea20_w300.jpg", "", "20240216-1000"),
105                         ), setOf(
106                                 movie("Movie 1", "https://cdn.premiumkino.de/movie/3047/81c49774d7828a898ae1d525ffd135af_w300.jpg", "", "20240212-1845", "20240213-1330", "20240214-1815"),
107                                 movie("Movie 2", "https://cdn.premiumkino.de/movie/1066/aba09af737677ff6a15676ae588098b1_w300.jpg", "", "20240212-1845", "20240213-1330", "20240214-1815")
108                         )
109                 )
110                 val output = movieState.output(Reaction("", null, null, null))
111                 val html = output.text("text/html")
112                 val document = Jsoup.parse(html)
113                 assertThat(
114                         document.select("section.daily-programmes li.day[data-date='2024-02-12'] li.performance").map { it.select(".time").text() + " - " + it.select(".name").text() }, contains(
115                                 "18:45 - Movie 1", "20:30 - Movie 2"
116                         )
117                 )
118         }
119
120         @Test
121         fun `html output contains the correct release type for movies`() {
122                 val movieState = MovieState(
123                         setOf(
124                                 movie("Movie 1", "https://cdn.premiumkino.de/movie/3047/81c49774d7828a898ae1d525ffd135af_w300.jpg", "", "20240212-1845" to "2D OV", "20240213-1330" to "2D", "20240214-1815" to "2D OmeU"),
125                         ), setOf()
126                 )
127                 val output = movieState.output(Reaction("", null, null, null))
128                 val html = output.text("text/html")
129                 val document = Jsoup.parse(html)
130                 assertThat(
131                         document.select("section.daily-programmes li.performance")
132                                 .map { performance -> listOf(".name", ".time", ".type").map { performance.select(it).text() }.joinToString(" - ") }, contains(
133                                 "Movie 1 - 18:45 - 2D OV", "Movie 1 - 13:30 - 2D", "Movie 1 - 18:15 - 2D OmeU"
134                         )
135                 )
136         }
137
138 }
139
140 private fun dateTime(dateTimeString: String) = LocalDateTime.of(
141         dateTimeString.substring(0..3).toInt(),
142         dateTimeString.substring(4..5).toInt(),
143         dateTimeString.substring(6..7).toInt(),
144         dateTimeString.substring(9..10).toInt(),
145         dateTimeString.substring(11..12).toInt(),
146 )
147
148 private fun movie(name: String, imageUrl: String, description: String = "", vararg times: String) =
149         times.map(::dateTime)
150                 .map { Performance(it, "", "https://link/$it") }
151                 .let { Movie(name, imageUrl, description, it) }
152
153 private fun movie(name: String, imageUrl: String, description: String = "", vararg timesAndTypes: Pair<String, String>) =
154         timesAndTypes.map { Performance(it.first.let(::dateTime), it.second, "https://link/${it.first}") }
155                 .let { Movie(name, imageUrl, description, it) }