Add reviewed suggestions for bigger pools (0.44.0 in testing)
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 1m36s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m26s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-02 07:03:16 +00:00
co-authored by Claude Opus 5.5
parent 99b1d8a478
commit 64c2604f03
10 changed files with 334 additions and 23 deletions
@@ -0,0 +1,88 @@
package tv.orbit.classic
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class ChannelSuggestionsTest {
@get:Rule val folder = TemporaryFolder()
private val user = "fixture-user"
private val rule = SuggestionRule("day", "Daytime", "Movie", listOf("Horror", "Comedy"), 1975..2010, 6f, setOf("NC-17"), 40)
private fun row(id: String, name: String, genres: List<String>, year: Int = 1985, rating: Double = 7.2, official: String = "R", container: String = "mkv") = JSONObject().put("Id", id).put("Type", "Movie").put("Name", name)
.put("ProductionYear", year).put("CommunityRating", rating).put("OfficialRating", official).put("RunTimeTicks", 60_000_000_000L).put("Genres", JSONArray(genres))
.put("MediaSources", JSONArray().put(JSONObject().put("Id", "s$id").put("Container", container)))
private fun reply(vararg rows: JSONObject) = EmbyTransport { EmbyReply(200, JSONObject().put("Items", JSONArray(rows.toList())).toString()) }
@Test fun onlyTitlesMeetingEveryPartOfTheRuleAreSuggestedWithAReason() {
val read = findSuggestions(reply(row("a", "Good", listOf("Horror", "Comedy")), row("b", "Half", listOf("Horror")), row("c", "Banned", listOf("Horror", "Comedy"), official = "NC-17"), row("d", "Unplayable", listOf("Horror", "Comedy"), container = "iso")),
user, { "t" }, listOf(rule), emptySet(), emptySet())
assertEquals(listOf("a"), read.suggestions.map { it.id })
assertEquals("Daytime: Horror and Comedy, 1985, rated 7.2", read.suggestions.single().reason)
assertEquals("sa", read.suggestions.single().sourceId)
}
@Test fun rejectedPooledAndDuplicateTitlesAreLeftOut() {
val rows = arrayOf(row("a", "Rejected", listOf("Horror", "Comedy")), row("b", "Pooled", listOf("Horror", "Comedy")), row("c", "Copy", listOf("Horror", "Comedy")), row("d", "Copy", listOf("Horror", "Comedy")))
assertEquals(listOf("c"), findSuggestions(reply(*rows), user, { "t" }, listOf(rule), setOf("b"), setOf("a")).suggestions.map { it.id })
}
@Test fun requestsAreBoundedAndOnePageEndsTheRule() {
val requests = mutableListOf<EmbyRequest>()
val full = JSONArray((1..SUGGESTION_PAGE).map { row("x$it", "Film $it", listOf("Other")) })
val read = findSuggestions(EmbyTransport { requests += it; EmbyReply(200, JSONObject().put("Items", full).toString()) }, user, { "tok" }, listOf(rule), emptySet(), emptySet())
assertEquals(SUGGESTION_MAX_PAGES, read.requests)
assertTrue(requests.all { it.path.contains("Limit=$SUGGESTION_PAGE") && it.path.contains("SortBy=CommunityRating") && !it.path.contains("tok") })
assertEquals(1, findSuggestions(reply(row("a", "One", listOf("Horror", "Comedy"))), user, { "t" }, listOf(rule), emptySet(), emptySet()).requests)
}
@Test fun aRuleStopsAtItsLimit() {
val rows = (1..20).map { row("f$it", "Film $it", listOf("Horror", "Comedy")) }.toTypedArray()
assertEquals(5, findSuggestions(reply(*rows), user, { "t" }, listOf(rule.copy(limit = 5)), emptySet(), emptySet()).suggestions.size)
}
@Test fun choicesSurviveASaveAndAReplan() {
val store = ScheduleStore(folder.newFolder())
val accepted = Suggestion("a", "Film", 1985, "Movie", "day", "reason", "sa", 60_000)
store.saveChoices(user, 102, ChannelChoices(listOf(accepted), setOf("b")))
store.deletePlan(102)
val read = store.loadChoices(user, 102)
assertEquals(listOf(accepted), read.accepted)
assertEquals(setOf("b"), read.rejected)
}
@Test fun noChoicesFileMeansNoChoices() {
val read = ScheduleStore(folder.newFolder()).loadChoices(user, 102)
assertTrue(read.accepted.isEmpty() && read.rejected.isEmpty())
}
@Test fun changingTheAcceptedTitlesChangesTheCacheTag() {
val a = Suggestion("a", "A", 1985, "Movie", "day", "", "s", 1_000)
assertNotEquals(ChannelChoices().tag, ChannelChoices(listOf(a)).tag)
assertEquals(ChannelChoices(listOf(a), setOf("x")).tag, ChannelChoices(listOf(a), setOf("y")).tag)
}
@Test fun acceptedTitlesJoinTheirBlockByGroup() {
val london = java.time.ZoneId.of("Europe/London")
val at = { h: Int -> java.time.ZonedDateTime.of(2026, 11, 2, h, 0, 0, 0, london).toInstant().toEpochMilli() }
fun film(id: String, group: String) = PoolSeries(id, id, listOf(PoolEpisode(id, "s$id", EpisodeMark(1, 1), id, 60_000)), group)
val pool = listOf(film("named", ""), film("dayFilm", "day"), film("lateFilm", "late"))
val parts = listOf(DayPart(21, 5), DayPart(5, 21, listOf("named"), "day"))
assertEquals(listOf("named", "dayFilm"), dayPartPool(pool, parts, at(12), london).map { it.id })
assertEquals(3, dayPartPool(pool, parts, at(23), london).size)
}
@Test fun thePoolFileKeepsEachTitlesGroup() {
val store = ScheduleStore(folder.newFolder())
val film = PoolSeries("a", "A", listOf(PoolEpisode("a", "sa", EpisodeMark(1, 1), "A", 60_000)), "day")
store.savePool(user, 102, ScheduleStore.SavedPool(listOf(film), 5L, "sig"))
assertEquals("day", store.loadPool(user, 102)!!.series.single().group)
}
@Test fun afterDarkAndVortexBlocksAcceptTheirGroups() {
assertEquals("day", SmartChannels.first { it.number == 102 }.dayParts[1].group)
assertEquals(listOf("day", "night"), SmartChannels.first { it.number == 103 }.dayParts.map { it.group })
}
}