0.96.0: special idents for the live blocks and events (Stage 3), keyed from bookings, entry-only, with Ring X and PPV overlays
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 1m55s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m29s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-03 23:07:01 +00:00
co-authored by Claude Opus 5.5
parent 1e02bc8971
commit ddba5796e7
45 changed files with 612 additions and 40 deletions
@@ -0,0 +1,94 @@
package tv.orbit.classic
import org.json.JSONObject
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.io.File
/**
* A two-week replay on copies of the box's real pools and committed plans, run by hand:
* ORBIT_REPLAY_DIR=<folder with schedule-, episodes- and progress-NNN.json> ./gradlew :app:testDebugUnitTest --tests '*PresentationReplayTest*'
* Writes presentation-replay.txt there: every changeover that would get a special ident, with the evidence for it,
* and checks that the committed programmes are kept exactly. Skipped when the folder isn't given.
*/
class PresentationReplayTest {
private val dir = System.getenv("ORBIT_REPLAY_DIR")?.let(::File)
private fun plan(file: File): ChannelPlan {
val root = JSONObject(file.readText())
val rows = root.getJSONArray("entries")
val entries = (0 until rows.length()).map { i ->
val o = rows.getJSONObject(i)
val d = o.getLong("d")
val n = o.getJSONArray("n")
ScheduleEntry(o.getString("s"), EpisodeMark(n.getInt(0), n.getInt(1)), ScheduledMedia(SampleItem(o.getString("t"), o.optString("k", "Episode"), d / 60_000, "", o.getString("i"), d), o.getString("c"), d),
o.getLong("a"), o.optString("w"), o.optString("p"))
}
return ChannelPlan(entries, cursor(root.getJSONObject("tail")))
}
private fun cursor(o: JSONObject): ScheduleCursor {
val marks = o.getJSONObject("marks")
return ScheduleCursor(if (o.isNull("last")) null else o.getString("last"),
marks.keys().asSequence().associateWith { id -> marks.getJSONArray(id).let { EpisodeMark(it.getInt(0), it.getInt(1)) } },
o.optJSONObject("shown")?.let { s -> s.keys().asSequence().associateWith { s.getLong(it) } }.orEmpty(),
o.optJSONObject("blocks")?.let { b -> b.keys().asSequence().associateWith { b.getString(it) } }.orEmpty())
}
private fun pool(file: File): List<PoolSeries> {
val rows = JSONObject(file.readText()).getJSONArray("series")
return (0 until rows.length()).map { i ->
val o = rows.getJSONObject(i)
val eps = o.getJSONArray("episodes")
PoolSeries(o.getString("id"), o.getString("title"), (0 until eps.length()).map { j ->
val e = eps.getJSONArray(j)
PoolEpisode(e.getString(0), e.getString(1), EpisodeMark(e.getInt(2), e.getInt(3)), e.getString(4), e.getLong(5))
}, o.optString("group"))
}
}
@Test fun replayTwoWeeks() {
assumeTrue(dir != null && dir.isDirectory)
val zone = java.time.ZoneId.of("Europe/London")
val out = StringBuilder()
var transitions = 0; var keptExactly = true
val counts = sortedMapOf<String, Int>()
for (programming in SmartChannels) {
val number = programming.number
val committedFile = File(dir, "schedule-$number.json"); val poolFile = File(dir, "episodes-$number.json")
if (!committedFile.exists() || !poolFile.exists()) continue
val committed = plan(committedFile)
val series = pool(poolFile)
var current = committed
var now = committed.entries.first().startMs + SCHEDULE_KEEP_PAST_MS
val end = now + 14 * 24 * 60 * 60_000L
val all = committed.entries.toMutableList()
while (now < end) {
current = topUpPlan(current, current.tail, series, now, programming.filmRestMs, programming.finishTogether, programming.dayParts, zone,
shuffle = programming.shuffleEpisodes, mixed = programming::mixes) ?: break
current.entries.filter { e -> all.none { it.startMs == e.startMs } }.forEach { all += it }
now += 6 * 60 * 60_000L
}
// The committed programmes must come through untouched.
val first = all.take(committed.entries.size)
if (first != committed.entries) { keptExactly = false; out.appendLine("CHANGED committed plan on $number") }
all.sortBy { it.startMs }
all.zipWithNext().forEach { (a, b) ->
if (b.startMs != a.endMs) return@forEach
val from = SampleBroadcast(a.media, a.startMs, presentationOf(a, programming.dayParts, zone))
val to = SampleBroadcast(b.media, b.startMs, presentationOf(b, programming.dayParts, zone))
if (blockIdentFor(from, to, enabled = true) == null) return@forEach
transitions++
counts.merge(to.presentation, 1, Int::plus)
val local = java.time.Instant.ofEpochMilli(b.startMs).atZone(zone)
val evidence = if (b.presentation.isNotEmpty()) "saved key" else "recorded reason: ${b.why.substringBefore(';')}"
val booked = if (b in committed.entries) "committed" else "new booking"
out.appendLine("$number ${local.toLocalDate()} ${local.toLocalTime()} ${to.presentation} -> ${b.media.item.title.take(50)} [$booked, $evidence]")
}
}
val summary = "transitions=$transitions committedKeptExactly=$keptExactly\n" + counts.entries.joinToString("\n") { " ${it.key}: ${it.value}" }
File(dir, "presentation-replay.txt").writeText(summary + "\n\n" + out)
println(summary)
assert(keptExactly)
}
}
@@ -0,0 +1,117 @@
package tv.orbit.classic
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import java.time.DayOfWeek
import java.time.LocalDateTime
import java.time.ZoneOffset
class PresentationTest {
private val zone = ZoneOffset.UTC
private fun at(day: Int, hour: Int, minute: Int = 0) = LocalDateTime.of(2026, 11, day, hour, minute).toInstant(zone).toEpochMilli()
private fun entry(startMs: Long, why: String = "", presentation: String = "") =
ScheduleEntry("s", EpisodeMark(1, 1), ScheduledMedia(SampleItem("Show / S1 E1 / One", "Episode", 30, "", "i", 30 * 60_000L), "c", 30 * 60_000L), startMs, why, presentation)
private fun broadcast(key: String, title: String = "Show / S1 E1 / One") = SampleBroadcast(ScheduledMedia(SampleItem(title, "Episode", 30, "", "i", 30 * 60_000L), "c", 30 * 60_000L), 0, key)
@Test fun everyApprovedBlockKeyHasItsIdent() {
val keys = SmartChannels.flatMap { it.dayParts }.map { it.key }.filter { it.isNotEmpty() }.toSet()
// Riot 3, After Dark 3, Horror, Gold, Three, Vortex, Orbit 2 2, Ring X 7 (six nights and PPV), Terra.
assertEquals(20, keys.size)
assertEquals(emptySet<String>(), keys - BlockIdents.keys)
assertTrue(keys.all { PRESENTATION_KEY.matches(it) })
}
@Test fun aSavedKeyWinsOverAnythingWorkedOut() {
assertEquals("riot.laughs", presentationOf(entry(at(3, 10), "block 6-18; next in turn", "riot.laughs"), RiotDayParts, zone))
}
@Test fun anOlderBookingGetsAKeyOnlyFromAnUnambiguousRecordedReason() {
// 2 November 2026 is a Monday. Orbit 2's Retro Action, recorded by name and covering the start.
assertEquals("orbit2.retro", presentationOf(entry(at(3, 1), "block 0-6; next in turn"), Orbit2DayParts, zone))
// Friday's Spooky Teatime is an event recorded as such.
assertEquals("orbit2.spooky", presentationOf(entry(at(6, 17, 30), "event 17-19; next in turn"), Orbit2DayParts, zone))
// Ring X's six nights share a name; the start's own day picks exactly one.
assertEquals("ringx.tuesday", presentationOf(entry(at(3, 19), "event 18-23; next in turn"), RingXDayParts, zone))
}
@Test fun anythingLessCertainGetsTheChannelIdent() {
// Booked before Riot's blocks existed: the reason says the whole pool, so no block is claimed, whatever the clock says.
assertEquals("", presentationOf(entry(at(2, 19), "whole pool; next in turn"), RiotDayParts, zone))
// No reason at all.
assertEquals("", presentationOf(entry(at(2, 19)), RiotDayParts, zone))
// A reason naming a block that doesn't cover the start.
assertEquals("", presentationOf(entry(at(2, 9), "block 18-21; next in turn"), RiotDayParts, zone))
// An old reason from a block layout that no longer exists.
assertEquals("", presentationOf(entry(at(2, 12), "event 0-24; next in turn"), RingXDayParts, zone))
}
@Test fun anEventPastMidnightKeepsItsPresentation() {
// Slasher Saturday at 01:00 on Sunday 8 November still belongs to Saturday night.
assertEquals("horror.slasher", presentationOf(entry(at(8, 1), "event 20-2; picked from 28 rested films"), HorrorDayParts, zone))
assertEquals("afterdark.cult", presentationOf(entry(at(9, 0, 30), "event 21-1; picked"), AfterDarkEvents, zone))
assertEquals("", presentationOf(entry(at(9, 2), "event 20-2; picked"), HorrorDayParts, zone))
}
@Test fun newBookingsSaveTheKeyOfTheBlockTheyWereDrawnFrom() {
fun show(title: String) = PoolSeries(title, title, (1..40).map { PoolEpisode("$title-$it", "src", EpisodeMark(1, it), "E$it", 25 * 60_000L) })
val pool = listOf(show("Seinfeld"), show("Frasier"), show("Family Guy"), show("South Park"), show("Aqua Teen Hunger Force"), show("The Simpsons"))
val parts = listOf(DayPart(18, 21, listOf("Seinfeld", "Frasier"), key = "riot.laughs"), DayPart(21, 2, listOf("Family Guy", "South Park"), key = "riot.afterhours"),
DayPart(2, 6, listOf("Aqua Teen Hunger Force")), DayPart(6, 18, listOf("The Simpsons")))
val plan = extendSchedule(pool, ScheduleCursor(), at(3, 17), at(3, 23), dayParts = parts, zone = zone)
plan.entries.forEach { e ->
val hour = java.time.Instant.ofEpochMilli(e.startMs).atZone(zone).hour
val expected = when (hour) { in 18..20 -> "riot.laughs"; in 21..23 -> "riot.afterhours"; else -> "" }
assertEquals("${e.media.item.title} at $hour", expected, e.presentation)
}
}
@Test fun aFilmEventSavesItsKeyAndEverydayFilmsDont() {
fun film(title: String) = PoolSeries(title, title, listOf(PoolEpisode(title, "src", EpisodeMark(1, 1), title, 95 * 60_000L)))
val pool = listOf("Halloween", "Scream", "Alien", "Heat", "Jaws", "Speed", "Big").map(::film)
val parts = listOf(DayPart(20, 2, listOf("Halloween", "Scream"), days = setOf(DayOfWeek.SATURDAY), key = "horror.slasher"))
val plan = extendFilmSchedule(pool, ScheduleCursor(), at(7, 18), at(8, 6), 60_000L, parts, zone)
plan.entries.forEach { e ->
val local = java.time.Instant.ofEpochMilli(e.startMs).atZone(zone)
val inWindow = local.hour >= 20 || local.hour < 2
// Inside Saturday's window only the event's films air, and they carry its key; outside it nothing does.
assertEquals("${e.media.item.title} at ${local.toLocalTime()}", if (inWindow) "horror.slasher" else "", e.presentation)
}
assertTrue(plan.entries.any { it.presentation == "horror.slasher" })
}
@Test fun aBlockIdentPlaysOnlyOnEnteringTheBlock() {
assertNotNull(blockIdentFor(broadcast(""), broadcast("riot.laughs"), enabled = true))
// The second programme inside the block gets the channel's ident.
assertNull(blockIdentFor(broadcast("riot.laughs"), broadcast("riot.laughs"), enabled = true))
// Moving from one keyed block straight into another plays the new one.
assertNotNull(blockIdentFor(broadcast("riot.laughs"), broadcast("riot.afterhours"), enabled = true))
// Leaving a block for an ordinary one: the channel's ident.
assertNull(blockIdentFor(broadcast("riot.afterhours"), broadcast(""), enabled = true))
}
@Test fun switchedOffOrMissingArtworkFallsBackToTheChannelIdent() {
assertNull(blockIdentFor(broadcast(""), broadcast("riot.laughs"), enabled = false))
assertNull(blockIdentFor(broadcast(""), broadcast("drivein.fright"), enabled = true))
}
@Test fun takeoversThemedNightsAndPremiereAreNotLiveYet() {
assertTrue(BlockIdents.keys.none { it.startsWith("drivein.") || it.startsWith("premiere.") || it.startsWith("themed.") })
}
@Test fun ringXNightsShareOneClipAndEachHasItsName() {
val nights = BlockIdents.filterValues { it.overlay == BlockOverlay.RingXNight }
assertEquals(6, nights.size)
assertEquals(1, nights.values.map { it.video }.toSet().size)
nights.keys.forEach { assertNotNull(RingXNights[it]) }
}
@Test fun thePpvLowerThirdUsesTheBookedTitleAndNothingIfThereIsNone() {
assertEquals("WWE WrestleMania III", eventTitle(broadcast("ringx.ppv", "WWE WrestleMania III")))
assertNull(eventTitle(broadcast("ringx.ppv", " ")))
val long = "NJPW Dominion 6.9 in Osaka-jo Hall and a very long subtitle that runs on"
assertEquals(long, eventTitle(broadcast("ringx.ppv", long)))
}
}