0.108.0: curated Drive-In events (Stage F), built and replayed, not installed
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 2m16s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m31s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-04 19:50:29 +00:00
co-authored by Claude Opus 5.5
parent e7e953b49d
commit 74c00d347c
17 changed files with 802 additions and 23 deletions
@@ -0,0 +1,73 @@
package tv.orbit.classic
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.io.File
import java.time.Instant
import java.time.ZoneId
/**
* The curated events on copies of the box's own files, run by hand:
* ORBIT_REPLAY_DIR=<copies of the box's schedule folder, plus event-films.json> ./gradlew :app:testDebugUnitTest --tests '*DriveInEventsReplayTest*'
* event-films.json is the events' films as the library catalogue has them ([{id, title, ms}]); on the box the same films
* also pass the decoder and People checks first. Books the Drive-In day by day to 21 December, with and without the events,
* and writes drivein-events.txt and drivein-baseline.txt there: every screening booked, and why.
*/
class DriveInEventsReplayTest {
private val dir = System.getenv("ORBIT_REPLAY_DIR")?.let(::File)
private val zone = ZoneId.of("Europe/London")
private fun plan(file: File): ChannelPlan? {
if (!file.exists()) return null
val root = JSONObject(file.readText()); val rows = root.getJSONArray("entries"); val t = root.getJSONObject("tail")
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, ScheduleCursor(null, emptyMap(), t.optJSONObject("shown")?.let { s -> s.keys().asSequence().associateWith { s.getLong(it) } }.orEmpty()))
}
private fun pool(number: Int): List<PoolSeries> {
val file = File(dir, "episodes-$number.json").takeIf { it.exists() } ?: return emptyList()
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 toChristmas() {
assumeTrue(dir != null && File(dir, "event-films.json").exists())
val failed = File(dir, "failed-0.json").takeIf { it.exists() }?.let { f -> JSONObject(f.readText()).optJSONArray("ids")?.let { a -> (0 until a.length()).map { a.getString(it) }.toSet() } }.orEmpty()
fun films(numbers: List<Int>) = numbers.flatMap { withoutFailed(pool(it).filter { s -> s.isFilm }, failed) }.distinctBy { seriesKey(it.title) }
val avoid = clashesWith(SmartChannels.mapNotNull { plan(File(dir, "schedule-${it.number}.json")) })
val listed = JSONArray(File(dir, "event-films.json").readText())
val eventFilms = (0 until listed.length()).map { listed.getJSONObject(it) }.associate { o ->
o.getString("id") to PoolSeries(o.getString("id"), o.getString("title"), listOf(PoolEpisode(o.getString("id"), "src", EpisodeMark(1, 1), o.getString("title"), o.getLong("ms"))))
}
val committed = plan(File(dir, "schedule-300.json"))!!
val start = System.getenv("ORBIT_REPLAY_NOW")?.toLong() ?: committed.entries.first().startMs
for ((name, events) in listOf("drivein-events" to DriveInEvents, "drivein-baseline" to emptyList())) {
val notes = mutableListOf<String>()
var current: ChannelPlan? = committed
val all = committed.entries.toMutableList()
var now = start
val end = java.time.LocalDate.of(2026, 12, 21).atStartOfDay(zone).toInstant().toEpochMilli()
while (now < end) {
current = planDriveIn(current, films(DriveInMarqueeChannels), films(DriveInLateChannels), films(DriveInBillChannels), now, zone, avoid, events, eventFilms) { notes += it }
current!!.entries.filter { e -> all.none { it.startMs == e.startMs } }.forEach { all += it }
now += 24 * 60 * 60_000L
}
val text = all.sortedBy { it.startMs }.joinToString("\n") { e ->
val at = Instant.ofEpochMilli(e.startMs).atZone(zone)
"${at.toLocalDate()} ${at.dayOfWeek.toString().take(3)} %02d:%02d %3d min %s | %s | %s".format(at.hour, at.minute, e.media.durationMs / 60_000, e.media.item.title, e.why, e.presentation)
}
File(dir, "$name.txt").writeText(text + "\n\nnotes: " + notes.joinToString("; ") + "\n")
}
}
}
@@ -0,0 +1,193 @@
package tv.orbit.classic
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Test
import java.time.Instant
import java.time.LocalDate
import java.time.ZoneId
import java.time.ZonedDateTime
/** Stage F's curated Drive-In events: fixed bills on their evenings, only when every film is verified, never disturbing anything booked. */
class DriveInEventsTest {
private val london = ZoneId.of("Europe/London")
private val minute = 60_000L
private fun film(title: String, minutes: Long = 120, id: String = title.replace(Regex("[^A-Za-z0-9]"), "")) =
PoolSeries(id, title, listOf(PoolEpisode(id, "s$id", EpisodeMark(1, 1), title, minutes * minute)))
private fun at(month: Int, day: Int, h: Int, m: Int = 0) = ZonedDateTime.of(2026, month, day, h, m, 0, 0, london).toInstant().toEpochMilli()
private fun local(ms: Long) = Instant.ofEpochMilli(ms).atZone(london)
private val big = (1..40).map { film("Big $it", 110) }
private val late = (1..40).map { film("Late $it", 95) }
private val bills = listOf(film("Alien"), film("Aliens"))
// A Saturday triple, a Friday double, and a late double on the Thursday between.
private val triple = DriveInEvent("monsters", "Monster Night", LocalDate.of(2026, 11, 14), 19, 0, listOf(
EventFilm("Dracula", 1931, "tt1", "d31"), EventFilm("Frankenstein", 1931, "tt2", "f31"), EventFilm("The Wolf Man", 1941, "tt3", "w41")), "drivein.fright")
private val double = DriveInEvent("hammer", "Hammer Night", LocalDate.of(2026, 11, 13), 20, 0, listOf(
EventFilm("The Curse of Frankenstein", 1957, "tt4", "c57"), EventFilm("Dracula", 1958, "tt5", "d58")))
private val lateDouble = DriveInEvent("jhorror", "J-Horror After Midnight", LocalDate.of(2026, 11, 12), 23, 0, listOf(
EventFilm("Ring", 1998, "tt6", "r98"), EventFilm("Ju-on: The Grudge", 2002, "tt7", "j02")))
private val events = listOf(triple, double, lateDouble)
private val verified = mapOf("d31" to film("Dracula", 74, "d31"), "f31" to film("Frankenstein", 70, "f31"), "w41" to film("The Wolf Man", 69, "w41"),
"c57" to film("The Curse of Frankenstein", 83, "c57"), "d58" to film("Dracula", 82, "d58"), "r98" to film("Ring", 95, "r98"), "j02" to film("Ju-on: The Grudge", 92, "j02"))
/**
* Day by day, as the box does it, from the 9th to the end of the month. Returns every booking made on the way (the plan
* itself drops evenings once they're past), in order, as one plan.
*/
private fun run(existing: ChannelPlan? = null, from: Long = at(11, 9, 12), films: Map<String, PoolSeries> = verified, avoid: ProgrammeClash = { _, _, _ -> false },
notes: MutableList<String> = mutableListOf()): ChannelPlan {
var plan = existing
var now = from
val all = existing?.entries.orEmpty().toMutableList()
while (now < at(11, 30, 12)) {
plan = planDriveIn(plan, big, late, bills, now, london, avoid, events, films) { notes += it }
plan!!.entries.filter { e -> all.none { it.startMs == e.startMs } }.forEach { all += it }
now += 24 * 60 * minute
}
last = plan
return ChannelPlan(all.sortedBy { it.startMs }, plan!!.tail)
}
private var last: ChannelPlan? = null
@Test fun eventsBookOnTheirEveningsInOrderAtTheirOwnTimes() {
val bills = driveInBills(run(), london)
val monsters = bills.single { it.event == "Monster Night" }
assertEquals(listOf("Dracula", "Frankenstein", "The Wolf Man"), monsters.films)
assertEquals(at(11, 14, 19), monsters.startMs)
assertEquals(DriveInKind.Triple, monsters.kind)
assertEquals("drivein.fright", monsters.presentation)
assertEquals("Monster Night", monsters.title)
monsters.entries.zipWithNext { a, b -> assertEquals(a.endMs, b.startMs) }
// A triple ends the night; a double is followed by the Late Show as usual.
assertTrue(bills.none { it.startMs > monsters.startMs && it.startMs < at(11, 15, 12) })
val hammer = bills.single { it.event == "Hammer Night" }
assertEquals(at(11, 13, 20), hammer.startMs)
assertEquals("", hammer.presentation)
assertTrue(bills.any { it.kind == DriveInKind.LateShow && it.startMs > hammer.endMs && it.startMs < at(11, 14, 4) })
}
@Test fun aLateDoubleFollowsAShortEnoughMainFilmAtItsOwnTime() {
val plan = run()
val night = driveInBills(plan, london).filter { it.startMs in at(11, 12, 18)..at(11, 13, 6) }
assertEquals(listOf(DriveInKind.Marquee, DriveInKind.LateShow), night.map { it.kind })
assertEquals(at(11, 12, 23), night[1].startMs)
assertEquals("J-Horror After Midnight", night[1].title)
assertTrue(night[0].endMs + 30 * minute <= night[1].startMs)
// Past midnight, and nothing booked on top of it.
assertTrue(local(night[1].endMs).dayOfMonth == 13)
plan.entries.zipWithNext { a, b -> assertTrue(a.endMs <= b.startMs) }
}
@Test fun anEventWithAnUnverifiedFilmIsntBookedAndTheEveningIsAsUsual() {
val notes = mutableListOf<String>()
val plan = run(films = verified - "w41", notes = notes)
val saturday = driveInBills(plan, london).filter { it.startMs in at(11, 14, 18)..at(11, 15, 6) }
assertTrue(saturday.none { it.event != null })
assertTrue(saturday.isNotEmpty())
assertTrue("event=monsters skipped=unverified" in notes)
assertTrue(notes.none { "Dracula" in it })
}
@Test fun anEventClashingWithAChannelIsntBooked() {
val notes = mutableListOf<String>()
val plan = run(avoid = { film, _, _ -> film.id == "c57" }, notes = notes)
assertTrue(driveInBills(plan, london).none { it.event == "Hammer Night" })
assertTrue("event=hammer skipped=clash" in notes)
}
@Test fun restIsKeptBothWays() {
// Shown at the Drive-In a week before: not rested, so the event isn't booked.
val earlier = ChannelPlan(listOf(ScheduleEntry("r98", EpisodeMark(1, 1), ScheduledMedia(SampleItem("Ring", "Movie", 95, "", "r98", 95 * minute), "sr98", 95 * minute), at(11, 5, 23))),
ScheduleCursor(null, emptyMap(), mapOf("r98" to at(11, 5, 23))))
val notes = mutableListOf<String>()
val plan = run(existing = earlier, from = at(11, 5, 12), notes = notes)
assertTrue("event=jhorror skipped=rest" in notes)
assertTrue(driveInBills(plan, london).none { it.event == "J-Horror After Midnight" })
// And the weekly rotation keeps an event's film for the event: Alien isn't an event film, but a pool copy of Dracula is held back.
val withCopy = (1..40).map { film("Big $it", 110) } + film("Dracula", 82, "pool-dracula")
var p: ChannelPlan? = null; var now = at(11, 9, 12)
while (now < at(11, 30, 12)) { p = planDriveIn(p, withCopy, late, bills, now, london, events = events, eventFilms = verified); now += 24 * 60 * minute }
assertTrue(p!!.entries.none { it.media.item.id == "pool-dracula" && kotlin.math.abs(it.startMs - at(11, 13, 21)) < 14 * 24 * 60 * minute })
}
@Test fun everythingAlreadyBookedAndEveryTicketIsKept() {
// Booked up to the 14th before the events existed: the 12th, 13th and 14th stay exactly as they were.
var before: ChannelPlan? = null; var now = at(11, 7, 12)
while (now < at(11, 8, 12)) { before = planDriveIn(before, big, late, bills, now, london); now += 24 * 60 * minute }
val booked = before!!
val tickets = driveInBills(booked, london).filter { it.startMs > at(11, 12, 0) }.associate { it.startMs to 0 }
val after = run(existing = booked, from = at(11, 8, 12))
assertEquals(booked.entries, after.entries.take(booked.entries.size))
assertTrue(driveInBills(after, london).filter { it.startMs <= booked.entries.last().startMs }.none { it.event != null })
val afterStarts = driveInBills(after, london).map { it.startMs }.toSet()
assertTrue(tickets.keys.all { it in afterStarts })
// The first event after what was booked still books.
assertTrue(driveInBills(after, london).none { it.event == "Monster Night" } || booked.entries.last().startMs < at(11, 14, 19))
}
@Test fun restartingChangesNothing() {
run()
val saved = last!!
// Opened again the same day: the saved plan is already booked ahead, so nothing changes.
assertEquals(saved.entries, planDriveIn(saved, big, late, bills, at(11, 29, 13), london, events = events, eventFilms = verified)!!.entries)
// A day later, everything still to air is kept exactly.
val again = planDriveIn(saved, big, late, bills, at(11, 30, 13), london, events = events, eventFilms = verified)!!
assertTrue(saved.entries.filter { it.endMs > at(11, 30, 13) }.all { it in again.entries })
}
@Test fun lateShowLeavesOutCompilationsAndReelsButNotFilmsWithoutMetadata() {
assertFalse(lateShowEligible(film("Extra Weird Sampler", 222)))
assertFalse(lateShowEligible(film("Grindhouse Trailer Reel", 90)))
assertFalse(lateShowEligible(film("A Four-Hour Cut", 240)))
assertTrue(lateShowEligible(film("Braindead", 97)))
assertTrue(lateShowEligible(film("Thesis", 124)))
val notes = mutableListOf<String>()
val plan = planDriveIn(null, big, late + film("Extra Weird Sampler", 222), bills, at(11, 2, 12), london) { notes += it }!!
assertTrue(plan.entries.none { it.media.item.title == "Extra Weird Sampler" })
assertTrue("late excluded=1" in notes)
}
@Test fun whatsOnShowsOnlyBookedEvents() {
val plan = run()
val page = whatsOnPage(emptyMap(), plan, at(11, 14, 15), london)
assertEquals("Monster Night", page.pick!!.title)
assertEquals("DRIVE-IN EVENT", page.pick!!.tag)
// A proposed event that isn't booked (no verified films) appears nowhere.
val unbooked = run(films = emptyMap())
val page2 = whatsOnPage(emptyMap(), unbooked, at(11, 14, 15), london)
assertTrue((listOfNotNull(page2.pick) + page2.tonight + page2.comingUp).none { it.tag == "DRIVE-IN EVENT" })
}
private fun row(id: String, imdb: String?, year: Int, people: List<Pair<String, String>> = emptyList()) = JSONObject().put("Id", id).put("Type", "Movie").put("Name", "Commando")
.put("ProductionYear", year).put("RunTimeTicks", 91 * 60 * 10_000_000L)
.put("ProviderIds", JSONObject().apply { if (imdb != null) put("Imdb", imdb) })
.put("MediaSources", JSONArray().put(JSONObject().put("Id", "src$id").put("Container", "mkv").put("RunTimeTicks", 91 * 60 * 10_000_000L)
.put("MediaStreams", JSONArray().put(JSONObject().put("Type", "Video").put("Codec", "h264")))))
.put("People", JSONArray().apply { people.forEach { (name, type) -> put(JSONObject().put("Name", name).put("Type", type)) } })
@Test fun eventFilmsAreCheckedByItemImdbAndCast() {
val commando = EventFilm("Commando", 1985, "tt0088944", "6048687")
val arnie = "Arnold Schwarzenegger"
assertEquals("verified", checkEventFilm(commando, row("6048687", "tt0088944", 1985, listOf(arnie to "Actor")), arnie) { true }.reason)
assertEquals("person not in cast", checkEventFilm(commando, row("6048687", "tt0088944", 1985, listOf(arnie to "Producer")), arnie) { true }.reason)
assertEquals("person not in cast", checkEventFilm(commando, row("6048687", "tt0088944", 1985), arnie) { true }.reason)
// The library's unlabelled copy of the same title and year is never taken for it.
assertEquals("another item", checkEventFilm(commando, row("7859713", null, 1985, listOf(arnie to "Actor")), arnie) { true }.reason)
assertEquals("unverified: no IMDb ID", checkEventFilm(commando, row("6048687", null, 1985, listOf(arnie to "Actor")), arnie) { true }.reason)
assertEquals("another film", checkEventFilm(commando, row("6048687", "tt9999999", 1985), null) { true }.reason)
assertEquals("missing", checkEventFilm(commando, null, null) { true }.reason)
assertEquals("unplayable", checkEventFilm(commando, row("6048687", "tt0088944", 1985), null) { false }.reason)
}
@Test fun theApprovedTenAreAsAgreed() {
assertEquals(10, DriveInEvents.size)
assertEquals(DriveInEvents.size, DriveInEvents.map { it.evening }.distinct().size)
assertTrue(DriveInEvents.all { it.presentation.isEmpty() || it.presentation in DriveInTakeovers })
assertEquals("Arnold Schwarzenegger", DriveInEvents.single { it.key == "arnie" }.person)
assertTrue(DriveInEvents.flatMap { it.films }.all { it.imdb.matches(Regex("tt\\d{7,8}")) && EmbyClient.validId(it.itemId) })
// Three triples run past midnight; nothing else is booked on top of them by design (a triple has no late show).
assertTrue(DriveInEvents.filter { it.films.size == 3 }.all { it.hour == 19 })
}
}