Add the shared scheduling engine, on Paradox only (0.39.0, Phase A in testing)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
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 ChannelScheduleTest {
|
||||
@get:Rule val folder = TemporaryFolder()
|
||||
private val minute = 60_000L
|
||||
private val day = 24 * 60 * minute
|
||||
private val now = 1_800_000_000_000L
|
||||
private val user = "fixture-user"
|
||||
private fun show(id: String, seasons: Int, perSeason: Int, minutes: Long = 45) = PoolSeries(id, "Show $id", (1..seasons).flatMap { season ->
|
||||
(1..perSeason).map { number -> PoolEpisode("$id-$season-$number", "src-$id-$season-$number", EpisodeMark(season, number), "E$number", minutes * minute) }
|
||||
})
|
||||
private val pool = listOf(show("a", 2, 3), show("b", 1, 2), show("c", 3, 10, 22))
|
||||
private val loop = SampleChannel(listOf(media("x"), media("y")), "Paradox", 152)
|
||||
private fun media(id: String) = ScheduledMedia(SampleItem(id, "Episode", 1, "", id, minute), "s-$id", minute)
|
||||
private fun store() = ScheduleStore(folder.newFolder())
|
||||
|
||||
@Test fun showsTakeTurnsOneEpisodeEach() {
|
||||
val entries = extendSchedule(pool, ScheduleCursor(), now, now + 8 * 60 * minute).entries
|
||||
assertEquals(listOf("a", "b", "c", "a", "b", "c"), entries.take(6).map { it.seriesId })
|
||||
}
|
||||
|
||||
@Test fun episodesRunInOrderAcrossSeasonsThenStartAgain() {
|
||||
val only = listOf(show("a", 2, 2))
|
||||
val entries = extendSchedule(only, ScheduleCursor(), now, now + 6 * 45 * minute).entries
|
||||
assertEquals(listOf(EpisodeMark(1, 1), EpisodeMark(1, 2), EpisodeMark(2, 1), EpisodeMark(2, 2), EpisodeMark(1, 1), EpisodeMark(1, 2)), entries.map { it.mark })
|
||||
}
|
||||
|
||||
@Test fun programmesRunBackToBackWithExactTimes() {
|
||||
val entries = extendSchedule(pool, ScheduleCursor(), now, now + day).entries
|
||||
assertEquals(now, entries.first().startMs)
|
||||
assertTrue(entries.zipWithNext().all { (first, second) -> first.endMs == second.startMs })
|
||||
}
|
||||
|
||||
@Test fun aSevenDayPlanCoversTheWeek() {
|
||||
val plan = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
assertTrue(plan.endMs >= now + SCHEDULE_AHEAD_MS)
|
||||
}
|
||||
|
||||
@Test fun extendingNeverChangesWhatIsCommitted() {
|
||||
val first = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
val later = now + 2 * day
|
||||
val second = topUpPlan(first, ScheduleCursor(), pool, later)!!
|
||||
val stillThere = first.entries.filter { it.endMs >= later - SCHEDULE_KEEP_PAST_MS }
|
||||
assertEquals(stillThere, second.entries.take(stillThere.size))
|
||||
assertTrue(second.endMs >= later + SCHEDULE_AHEAD_MS)
|
||||
assertTrue(second.entries.zipWithNext().all { (a, b) -> a.endMs == b.startMs })
|
||||
}
|
||||
|
||||
@Test fun extendingCarriesOnFromTheTailNotTheStart() {
|
||||
val first = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
val second = topUpPlan(first, ScheduleCursor(), pool, now + 2 * day)!!
|
||||
val added = second.entries[second.entries.indexOf(first.entries.last()) + 1]
|
||||
val expected = extendSchedule(pool, first.tail, first.endMs, first.endMs + 1).entries.single()
|
||||
assertEquals(expected, added)
|
||||
}
|
||||
|
||||
@Test fun aFreshPlanNeedsNoWorkWithinTheTopUpWindow() {
|
||||
val first = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
assertEquals(first.entries, topUpPlan(first, ScheduleCursor(), null, now + 60 * minute)!!.entries)
|
||||
}
|
||||
|
||||
@Test fun aProviderOutageKeepsThePlanButAddsNothing() {
|
||||
val first = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
val later = now + 3 * day
|
||||
assertEquals(first.entries.last(), topUpPlan(first, ScheduleCursor(), null, later)!!.entries.last())
|
||||
assertNull(topUpPlan(null, ScheduleCursor(), null, now))
|
||||
}
|
||||
|
||||
@Test fun guideAndPlayerAgreeAndJoinPartWayThrough() {
|
||||
val plan = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
val channel = ScheduledChannel(loop, plan)
|
||||
val entry = plan.entries[5]
|
||||
val joined = channel.at(entry.startMs + 18 * minute)
|
||||
assertEquals(entry.media, joined.media)
|
||||
assertEquals(18 * minute, joined.offsetAt(entry.startMs + 18 * minute))
|
||||
val cells = guideCells(channel, entry.startMs, entry.startMs + 2 * 60 * minute)
|
||||
assertEquals(entry.media, cells.first().show.media)
|
||||
}
|
||||
|
||||
@Test fun outsideThePlanTheLoopCarriesOn() {
|
||||
val plan = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
val channel = ScheduledChannel(loop, plan)
|
||||
assertEquals(loop.at(plan.endMs + day), channel.at(plan.endMs + day))
|
||||
assertEquals(loop.at(now - day), channel.at(now - day))
|
||||
}
|
||||
|
||||
@Test fun aTopUpKeepsTheSameChannelObject() {
|
||||
val channel = ScheduledChannel(loop, topUpPlan(null, ScheduleCursor(), pool, now)!!)
|
||||
val probe = now + 3 * day
|
||||
val before = channel.at(probe)
|
||||
channel.plan = topUpPlan(channel.plan, ScheduleCursor(), pool, now + 2 * day)!!
|
||||
assertEquals(before, channel.at(probe))
|
||||
}
|
||||
|
||||
@Test fun airedPositionOnlyCountsWhatHasStarted() {
|
||||
val plan = extendSchedule(pool, ScheduleCursor(), now, now + day)
|
||||
val aired = airedCursor(ScheduleCursor(), plan.entries, plan.entries[3].startMs)
|
||||
assertEquals("a", aired.lastSeriesId)
|
||||
assertEquals(EpisodeMark(1, 2), aired.marks["a"])
|
||||
assertEquals(EpisodeMark(1, 1), aired.marks["b"])
|
||||
}
|
||||
|
||||
@Test fun aRestartReadsBackTheSameSchedule() {
|
||||
val store = store()
|
||||
val plan = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
store.savePlan(user, 152, plan)
|
||||
assertEquals(plan, store.loadPlan(user, 152))
|
||||
}
|
||||
|
||||
@Test fun losingTheScheduleCarriesOnFromTheAiredPlace() {
|
||||
val store = store()
|
||||
val plan = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
val aired = airedCursor(ScheduleCursor(), plan.entries, plan.entries[6].startMs)
|
||||
store.saveProgress(user, 152, ScheduleStore.SavedProgress(true, aired))
|
||||
val rebuilt = topUpPlan(store.loadPlan(user, 152), store.loadProgress(user, 152)!!.aired, pool, plan.entries[6].startMs + minute)!!
|
||||
assertEquals(extendSchedule(pool, aired, 0, 1).entries.single().media, rebuilt.entries.first().media)
|
||||
}
|
||||
|
||||
@Test fun aDamagedScheduleFallsBackToItsLastGoodCopy() {
|
||||
val dir = folder.newFolder()
|
||||
val store = ScheduleStore(dir)
|
||||
val plan = topUpPlan(null, ScheduleCursor(), pool, now)!!
|
||||
store.savePlan(user, 152, plan)
|
||||
store.savePlan(user, 152, plan)
|
||||
java.io.File(dir, "schedule-152.json").writeText("{ not json")
|
||||
assertEquals(plan, store.loadPlan(user, 152))
|
||||
}
|
||||
|
||||
@Test fun aScheduleDamagedEverywhereIsAbsentAndProgressSurvives() {
|
||||
val dir = folder.newFolder()
|
||||
val store = ScheduleStore(dir)
|
||||
store.savePlan(user, 152, topUpPlan(null, ScheduleCursor(), pool, now)!!)
|
||||
store.saveProgress(user, 152, ScheduleStore.SavedProgress(true, ScheduleCursor("a", mapOf("a" to EpisodeMark(2, 1)))))
|
||||
dir.listFiles()!!.filter { it.name.startsWith("schedule") }.forEach { it.writeText("") }
|
||||
assertNull(store.loadPlan(user, 152))
|
||||
assertEquals(EpisodeMark(2, 1), store.loadProgress(user, 152)!!.aired.marks["a"])
|
||||
}
|
||||
|
||||
@Test fun anotherAccountOrChannelOrFormatIsIgnored() {
|
||||
val dir = folder.newFolder()
|
||||
val store = ScheduleStore(dir)
|
||||
store.savePlan(user, 152, topUpPlan(null, ScheduleCursor(), pool, now)!!)
|
||||
assertNull(store.loadPlan("other-user", 152))
|
||||
assertNull(store.loadPlan(user, 140))
|
||||
val file = java.io.File(dir, "schedule-152.json")
|
||||
file.writeText(JSONObject(file.readText()).put("schema", 999).toString())
|
||||
assertNull(store.loadPlan(user, 152))
|
||||
}
|
||||
|
||||
@Test fun overlappingEntriesAreRejected() {
|
||||
val dir = folder.newFolder()
|
||||
val store = ScheduleStore(dir)
|
||||
store.savePlan(user, 152, topUpPlan(null, ScheduleCursor(), pool, now)!!)
|
||||
val file = java.io.File(dir, "schedule-152.json")
|
||||
val root = JSONObject(file.readText())
|
||||
root.getJSONArray("entries").getJSONObject(1).put("a", now)
|
||||
file.writeText(root.toString())
|
||||
assertNull(store.loadPlan(user, 152))
|
||||
}
|
||||
|
||||
@Test fun theEpisodeCacheRoundTrips() {
|
||||
val store = store()
|
||||
store.savePool(user, 152, ScheduleStore.SavedPool(pool, now))
|
||||
val read = store.loadPool(user, 152)!!
|
||||
assertEquals(pool, read.series); assertEquals(now, read.fetchedAtMs)
|
||||
}
|
||||
|
||||
@Test fun loopChoiceKeepsTheSavedPlace() {
|
||||
val store = store()
|
||||
val scheduler = ChannelScheduler(store) { now }
|
||||
store.saveProgress(user, 152, ScheduleStore.SavedProgress(true, ScheduleCursor("a", mapOf("a" to EpisodeMark(1, 2)))))
|
||||
scheduler.setEnabled(user, 152, false)
|
||||
assertFalse(scheduler.enabled(user, 152))
|
||||
assertEquals(EpisodeMark(1, 2), store.loadProgress(user, 152)!!.aired.marks["a"])
|
||||
store.savePlan(user, 152, topUpPlan(null, ScheduleCursor(), pool, now)!!)
|
||||
assertNull(scheduler.load(user, loop))
|
||||
scheduler.setEnabled(user, 152, true)
|
||||
assertNotNull(scheduler.load(user, loop))
|
||||
}
|
||||
|
||||
private fun episode(id: String, season: Int, number: Int, container: String = "mkv", sourceId: String = "src-$id") = JSONObject().put("Id", id).put("Type", "Episode")
|
||||
.put("Name", "Episode").put("ParentIndexNumber", season).put("IndexNumber", number).put("RunTimeTicks", 13_200_000_000L)
|
||||
.put("MediaSources", JSONArray().put(JSONObject().put("Id", sourceId).put("Container", container).put("RunTimeTicks", 13_300_000_000L)))
|
||||
private val series = SeriesCandidate("fixture-series", "Fixture", 1996, "")
|
||||
|
||||
@Test fun aWholeSeriesIsReadInBoundedPages() {
|
||||
val requests = mutableListOf<EmbyRequest>()
|
||||
val all = (1..250).map { episode("e$it", 1, it) }
|
||||
val transport = EmbyTransport { request ->
|
||||
requests += request
|
||||
val start = Regex("StartIndex=(\\d+)").find(request.path)!!.groupValues[1].toInt()
|
||||
val rows = JSONArray(); all.drop(start).take(WHOLE_SERIES_PAGE).forEach { rows.put(it) }
|
||||
EmbyReply(200, JSONObject().put("Items", rows).put("TotalRecordCount", all.size).toString())
|
||||
}
|
||||
val result = readWholeSeries(transport, user, { "fixture-token" }, series)
|
||||
assertEquals(250, result.episodes.size)
|
||||
assertEquals(3, requests.size)
|
||||
assertTrue(requests.all { it.path.contains("Limit=$WHOLE_SERIES_PAGE") && !it.path.contains("fixture-token") })
|
||||
}
|
||||
|
||||
@Test fun onlyRegularPlayableEpisodesAreKeptInOrderOnce() {
|
||||
val rows = JSONArray().put(episode("late", 2, 1)).put(episode("special", 0, 1)).put(episode("early", 1, 1))
|
||||
.put(episode("copy", 1, 1)).put(episode("bad", 1, 2, container = "iso"))
|
||||
.put(JSONObject().put("Id", "nosource").put("Type", "Episode").put("ParentIndexNumber", 1).put("IndexNumber", 3).put("RunTimeTicks", 13_200_000_000L))
|
||||
val transport = EmbyTransport { EmbyReply(200, JSONObject().put("Items", rows).put("TotalRecordCount", rows.length()).toString()) }
|
||||
val result = readWholeSeries(transport, user, { "t" }, series)
|
||||
assertEquals(listOf("early", "late"), result.episodes.map { it.id })
|
||||
assertEquals(1_330_000L, result.episodes.first().durationMs)
|
||||
assertEquals("src-early", result.episodes.first().sourceId)
|
||||
}
|
||||
|
||||
@Test fun guideDayLabelOnlyAppearsAwayFromToday() {
|
||||
assertEquals("", guideDayPrefix(now, now))
|
||||
assertTrue(guideDayPrefix(now + 3 * day, now).isNotEmpty())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user