0.82.0: Phase 1, a reason with each planned programme, with planning proven unchanged
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 3m14s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m49s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-03 09:01:18 +00:00
co-authored by Claude Opus 5.5
parent 99ffebfe47
commit e1d2b77da5
8 changed files with 172 additions and 25 deletions
@@ -0,0 +1,81 @@
package tv.orbit.classic
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.time.ZoneId
/**
* Plans made before selection reasons existed, reduced to a fingerprint. Reasons must never change what gets planned,
* so these stay fixed: if one changes, scheduling behaviour has changed.
*/
class PlanBaselineTest {
private val minute = 60_000L
private val hour = 60 * minute
private val start = 1_793_700_000_000L
private val zone = ZoneId.of("Europe/London")
private fun show(id: String, seasons: Int, perSeason: Int, minutes: Long, group: String = "") = PoolSeries(id, "Show $id", (1..seasons).flatMap { season ->
(1..perSeason).map { n -> PoolEpisode("$id-$season-$n", "src-$id-$season-$n", EpisodeMark(season, n), "E$n", (minutes + (n % 3)) * minute) }
}, group)
private fun film(id: String, minutes: Long) = PoolSeries(id, "Film $id", listOf(PoolEpisode(id, "src-$id", EpisodeMark(1, 1), "Film $id", minutes * minute)))
private val shows = listOf(show("a", 2, 7, 22, "day"), show("b", 1, 5, 44, "day"), show("c", 3, 9, 11, "night"), show("d", 2, 4, 30, "night"), show("e", 1, 3, 25))
private val films = (1..18).map { film("f$it", 85L + it * 3) }
private val parts = listOf(DayPart(20, 22, group = "movie", days = setOf(java.time.DayOfWeek.FRIDAY)), DayPart(6, 18, group = "day"), DayPart(18, 6, group = "night"))
// Another channel's plan, so the clash check has something to avoid.
private val other = extendSchedule(listOf(show("a", 2, 7, 22), show("c", 3, 9, 11)), ScheduleCursor(), start + 3 * hour, start + 2 * 24 * hour)
private val avoid = clashesWith(listOf(other))
private fun fingerprint(plan: ChannelPlan?): String {
val text = plan!!.entries.joinToString("|") { "${it.seriesId}:${it.mark.season}.${it.mark.episode}@${(it.startMs - start) / 1000}" } + "#" + plan.tail.lastSeriesId + plan.tail.marks.toSortedMap() + plan.tail.filmShown.toSortedMap()
return java.security.MessageDigest.getInstance("SHA-256").digest(text.toByteArray()).joinToString("") { "%02x".format(it) }.take(16)
}
private val day = 24 * hour
private fun plans() = linkedMapOf(
"turns" to extendSchedule(shows, ScheduleCursor(), start, start + 3 * day),
"finishTogether" to extendSchedule(shows, ScheduleCursor(), start, start + 4 * day, finishTogether = true),
"dayParts" to extendSchedule(shows, ScheduleCursor(), start, start + 4 * day, finishTogether = true, dayParts = parts, zone = zone),
"clash" to extendSchedule(shows, ScheduleCursor(), start, start + 3 * day, finishTogether = true, dayParts = parts, zone = zone, avoid = avoid),
"everythingClashes" to extendSchedule(listOf(show("a", 2, 7, 22)), ScheduleCursor(), start, start + day, avoid = avoid),
"shuffle" to extendSchedule(shows, ScheduleCursor(), start, start + 3 * day, finishTogether = true, dayParts = parts, zone = zone, avoid = avoid, shuffle = true),
"films" to extendSchedule(films, ScheduleCursor(), start, start + 5 * day, filmRestMs = 2 * day),
"filmsClash" to extendSchedule(films + listOf(PoolSeries("a", "Show a", listOf(PoolEpisode("a", "src-a", EpisodeMark(1, 1), "Show a", 90 * minute)))), ScheduleCursor(), start, start + 3 * day,
filmRestMs = day, dayParts = parts, zone = zone, avoid = avoid),
"topUp" to topUpPlan(extendSchedule(shows, ScheduleCursor(), start, start + 2 * day), ScheduleCursor(), shows, start + day, finishTogether = true, dayParts = parts, zone = zone, avoid = avoid),
)
@Test fun everyNewProgrammeSaysWhy() {
plans().values.forEach { plan -> assertTrue(plan!!.entries.all { it.why.isNotBlank() }) }
val shows = plans().getValue("clash")!!.entries
assertTrue(shows.any { "deferred, airing elsewhere" in it.why })
assertTrue(shows.all { it.why.startsWith("block ") || it.why.startsWith("event ") })
}
@Test fun fallbacksAreNamed() {
assertTrue(plans().getValue("everythingClashes")!!.entries.any { "fallback: every show in turn airs elsewhere" in it.why })
assertTrue(plans().getValue("films")!!.entries.all { "rested" in it.why || "fallback" in it.why })
}
@Test fun reasonsSurviveASaveAndOldPlansStillRead() {
val folder = java.nio.file.Files.createTempDirectory("plans").toFile()
val store = ScheduleStore(folder)
val plan = plans().getValue("dayParts")!!
store.savePlan("u", 601, plan)
assertEquals(plan, store.loadPlan("u", 601))
// A file written before reasons existed: the same plan without the field.
val file = folder.listFiles()!!.first { it.name == "schedule-601.json" }
file.writeText(file.readText().replace(Regex(",\\\"w\\\":\\\"(?:[^\\\"\\\\]|\\\\.)*\\\""), ""))
assertTrue("w" !in file.readText().let { org.json.JSONObject(it).getJSONArray("entries").getJSONObject(0).keys().asSequence().toList() })
assertEquals(plan.entries.map { it.copy(why = "") }, store.loadPlan("u", 601)!!.entries)
folder.deleteRecursively()
}
@Test fun planningIsUnchanged() {
// Taken from 0.81.4, before reasons were recorded.
val expected = mapOf("turns" to "5cc4eee2cbfa29e6", "finishTogether" to "c357d2fae0d6a5df", "dayParts" to "764b7b7ac15a5b26", "clash" to "8d3c92c8c4beed8e",
"everythingClashes" to "9f38ee7d889140c5", "shuffle" to "cee9b485bc705af2", "films" to "2f4f2786280d4589", "filmsClash" to "7fe17b8f3746e164", "topUp" to "40ce5b5f8e0e8ed3")
assertEquals(expected, plans().mapValues { fingerprint(it.value) })
}
}