🚸 Improve mail with movies from the Savoy
[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.TextNode
13 import org.junit.Test
14 import java.time.LocalDateTime
15
16 class MovieStateTest {
17
18         @Test
19         fun `summary contains date of earliest movie`() {
20                 val movieState = MovieState(
21                         setOf(
22                                 Movie("1", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 12, 18, 45), "")) },
23                                 Movie("2", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 3, 12, 15, 30), "")) },
24                                 Movie("3", "").apply { addPerformance(Performance(LocalDateTime.of(2024, 2, 11, 21, 15), "")) }
25                         ), emptySet()
26                 )
27                 assertThat(movieState.output(Reaction("", null, null, null)).summary(), containsString("2024-02-11"))
28         }
29
30         @Test
31         fun `movie state without new movies is not triggered`() {
32                 assertThat(MovieState(emptySet(), emptySet()).triggered(), equalTo(false))
33         }
34
35         @Test
36         fun `movie state with a new movie is triggered`() {
37                 assertThat(MovieState(emptySet(), setOf(Movie("1", ""))).triggered(), equalTo(true))
38         }
39
40         @Test
41         fun `html output does not contain a section for new movies if there are no new movies`() {
42                 val movieState = MovieState(emptySet(), emptySet())
43                 val output = movieState.output(Reaction("", null, null, null))
44                 val document = Jsoup.parse(output.text("text/html"))
45                 assertThat(document.select("section.new-movies"), empty())
46         }
47
48         @Test
49         fun `html output does contain a section for new movie if there are new movies`() {
50                 val movieState = MovieState(emptySet(), setOf(Movie("1", ""), Movie("2", "")))
51                 val output = movieState.output(Reaction("", null, null, null))
52                 val document = Jsoup.parse(output.text("text/html"))
53                 assertThat(document.select("section.new-movies"), not(empty()))
54         }
55
56         @Test
57         fun `new movies section contains the titles of all new movies`() {
58                 val movieState = MovieState(emptySet(), setOf(Movie("New Movie", ""), Movie("Even Newer Movie", "")))
59                 val output = movieState.output(Reaction("", null, null, null))
60                 val document = Jsoup.parse(output.text("text/html"))
61                 assertThat(document.select("section.new-movies li.movie .name").textNodes().map(TextNode::text), containsInAnyOrder("New Movie", "Even Newer Movie"))
62         }
63
64         @Test
65         fun `html output contains section for the daily programme`() {
66                 val movieState = MovieState(emptySet(), emptySet())
67                 val output = movieState.output(Reaction("", null, null, null))
68                 val document = Jsoup.parse(output.text("text/html"))
69                 assertThat(document.select("section.daily-programmes"), not(empty()))
70         }
71
72         @Test
73         fun `html output contains a section for each day with a movie`() {
74                 val movieState = MovieState(
75                         setOf(
76                                 movie("Movie 1", "", "20240212-1845", "20240213-1330", "20240214-1815"),
77                                 movie("Movie 2", "", "20240212-2030", "20240213-1745", "20240214-1430"),
78                                 movie("Movie 3", "", "20240213-2015", "20240214-1730"),
79                                 movie("Movie 4", "", "20240216-1000"),
80                         ), emptySet()
81                 )
82                 val output = movieState.output(Reaction("", null, null, null))
83                 val html = output.text("text/html")
84                 val document = Jsoup.parse(html)
85                 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"))
86         }
87
88         @Test
89         fun `html output contains the correct movies within each day`() {
90                 val movieState = MovieState(
91                         setOf(
92                                 movie("Movie 1", "https://cdn.premiumkino.de/movie/3047/81c49774d7828a898ae1d525ffd135af_w300.jpg", "20240212-1845", "20240213-1330", "20240214-1815"),
93                                 movie("Movie 2", "https://cdn.premiumkino.de/movie/1066/aba09af737677ff6a15676ae588098b1_w300.jpg", "20240212-2030", "20240213-1745", "20240214-1430"),
94                                 movie("Movie 3", "https://cdn.premiumkino.de/movie/7300/14d1b21dee51a82a7b096ca282bf01c8_w300.png", "20240213-2015", "20240214-1730"),
95                                 movie("Movie 4", "https://cdn.premiumkino.de/movie/6080/cef2b33483d2898ddb472c955c58ea20_w300.jpg", "20240216-1000"),
96                         ), setOf(
97                                 movie("Movie 1", "https://cdn.premiumkino.de/movie/3047/81c49774d7828a898ae1d525ffd135af_w300.jpg", "20240212-1845", "20240213-1330", "20240214-1815"),
98                                 movie("Movie 2", "https://cdn.premiumkino.de/movie/1066/aba09af737677ff6a15676ae588098b1_w300.jpg", "20240212-1845", "20240213-1330", "20240214-1815")
99                         )
100                 )
101                 val output = movieState.output(Reaction("", null, null, null))
102                 val html = output.text("text/html")
103                 val document = Jsoup.parse(html)
104                 assertThat(
105                         document.select("section.daily-programmes li.day[data-date='2024-02-12'] li.performance").map { it.select(".time").text() + " - " + it.select(".name").text() }, contains(
106                                 "18:45 - Movie 1", "20:30 - Movie 2"
107                         )
108                 )
109         }
110
111 }
112
113 private fun dateTime(dateTimeString: String) = LocalDateTime.of(
114         dateTimeString.substring(0..3).toInt(),
115         dateTimeString.substring(4..5).toInt(),
116         dateTimeString.substring(6..7).toInt(),
117         dateTimeString.substring(9..10).toInt(),
118         dateTimeString.substring(11..12).toInt(),
119 )
120
121 private fun movie(name: String, imageUrl: String, vararg times: String) = Movie(name, imageUrl).apply {
122         times.map(::dateTime).map { Performance(it, "https://link/$it") }.forEach(this::addPerformance)
123 }