package net.pterodactylus.rhynodge.filters.webpages.savoy import net.pterodactylus.rhynodge.states.StateManagerTest.TestState import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.containsInAnyOrder import org.hamcrest.Matchers.empty import org.hamcrest.Matchers.equalTo import org.junit.Assert.assertThrows import org.junit.Test import java.time.LocalDateTime class SavoyMergerTest { @Test fun `merger throws exception if first state is not a movie state`() { assertThrows(IllegalArgumentException::class.java) { merger.mergeStates(TestState(), MovieState(emptySet())) } } @Test fun `merger throws exception if second state is not a movie state`() { assertThrows(IllegalArgumentException::class.java) { merger.mergeStates(MovieState(emptySet()), TestState()) } } @Test fun `merging two empty states results in an empty state`() { assertThat(merger.mergeStates(MovieState(emptySet()), MovieState(emptySet())).isEmpty, equalTo(true)) } @Test fun `merging a non-empty state into an empty state results in a state containing all movies`() { val oldState = MovieState(emptySet()) val newState = MovieState(setOf(Movie("1", ""))) val mergedState = merger.mergeStates(oldState, newState) as MovieState assertThat(mergedState.movies, containsInAnyOrder(Movie("1", ""))) } @Test fun `merging two states keeps only movies from current state`() { val oldState = MovieState(setOf(Movie("1", ""), Movie("3", ""))) val newState = MovieState(setOf(Movie("2", ""), Movie("4", ""))) val mergedState = merger.mergeStates(oldState, newState) as MovieState assertThat(mergedState.movies, containsInAnyOrder(Movie("2", ""), Movie("4", ""))) } @Test fun `merging a state with new movies identifies new movies`() { val oldState = MovieState(setOf(Movie("1", ""), Movie("2", ""), Movie("3", ""))) val newState = MovieState(setOf(Movie("2", ""), Movie("3", ""), Movie("4", ""))) val mergedState = merger.mergeStates(oldState, newState) as MovieState assertThat(mergedState.newMovies, containsInAnyOrder(Movie("4", ""))) } @Test fun `movies with different performances are still considered the same movie`() { val oldState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", ""))))) val newState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", ""))))) val mergedState = merger.mergeStates(oldState, newState) as MovieState assertThat(mergedState.newMovies, empty()) } @Test fun `merging states with movies starting the same day does not create a triggered state`() { val oldState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", ""))))) val newState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 14, 14, 30), "", ""))))) val mergedState = merger.mergeStates(oldState, newState) as MovieState assertThat(mergedState.triggered(), equalTo(false)) } @Test fun `merging states with movies starting on different days does create a triggered state`() { val oldState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", ""))))) val newState = MovieState(setOf(Movie("1", "", "", listOf(Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", ""))))) val mergedState = merger.mergeStates(oldState, newState) as MovieState assertThat(mergedState.triggered(), equalTo(true)) } @Test fun `merging states where movies have different performances but still on the same day does not create a triggered state`() { val oldState = MovieState(setOf(Movie("1", "", "", listOf( Performance(LocalDateTime.of(2024, 2, 14, 14, 30), "", ""), Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "") )))) val newState = MovieState(setOf(Movie("1", "", "", listOf( Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", "") )))) val mergedState = merger.mergeStates(oldState, newState) as MovieState assertThat(mergedState.triggered(), equalTo(false)) } @Test fun `merging states where movies have different performances and are not on the same day anymore does create a triggered state`() { val oldState = MovieState(setOf(Movie("1", "", "", listOf( Performance(LocalDateTime.of(2024, 2, 14, 18, 45), "", ""), Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", "") )))) val newState = MovieState(setOf(Movie("1", "", "", listOf( Performance(LocalDateTime.of(2024, 2, 15, 14, 30), "", "") )))) val mergedState = merger.mergeStates(oldState, newState) as MovieState assertThat(mergedState.triggered(), equalTo(true)) } private val merger = SavoyMerger() }