From 5de2305ac9853146c1e3964f520458fcda75b5e4 Mon Sep 17 00:00:00 2001 From: Pauljd Date: Mon, 30 Nov 2026 18:54:54 +0000 Subject: [PATCH] Heroes Live (115): simulation only, no channel change Co-Authored-By: Claude Opus 5.5 --- .../orbit/classic/HeroesLiveSimulationTest.kt | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 android/app/src/test/java/tv/orbit/classic/HeroesLiveSimulationTest.kt diff --git a/android/app/src/test/java/tv/orbit/classic/HeroesLiveSimulationTest.kt b/android/app/src/test/java/tv/orbit/classic/HeroesLiveSimulationTest.kt new file mode 100644 index 00000000..a8298106 --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/HeroesLiveSimulationTest.kt @@ -0,0 +1,100 @@ +package tv.orbit.classic + +import org.json.JSONArray +import org.json.JSONObject +import org.junit.Assert.assertTrue +import org.junit.Assume.assumeTrue +import org.junit.Test +import java.io.File +import java.time.DayOfWeek +import java.time.Instant +import java.time.LocalDateTime +import java.time.ZoneId + +/** + * Heroes Live (115), proposed and simulated only (Paul, 30 November 2026: a candidate for build planning). Nothing here adds + * the channel: its blocks live in this test. From the audit's playable runs (each series stops before its first unplayable or + * missing episode) and films (their real lengths), planned from a fixed start and topped up daily for five weeks, against a TV + * snapshot's other plans with the box's clash and network film-rest rules. Premiere's superhero films and Orbit Action's Blade + * and Dredd are guests: they stay those channels' own, and only play here when rested there. Env: SIM_SNAP, SIM_HEROES + * (candidates.json), SIM_OUT. + */ +class HeroesLiveSimulationTest { + @Test fun simulate() { + val dir = System.getenv("SIM_SNAP")?.let(::File); val src = System.getenv("SIM_HEROES")?.let(::File) + assumeTrue(dir != null && dir.isDirectory && src != null && src.exists()) + val out = File(System.getenv("SIM_OUT")!!).also { it.mkdirs() } + val zone = ZoneId.of("Europe/London") + val user = dir!!.listFiles()!!.first { it.name.startsWith("schedule-") }.let { JSONObject(it.readText()).getString("user") } + val store = ScheduleStore(dir) + val doc = JSONObject(src!!.readText()) + fun list(key: String) = doc.getJSONArray(key).let { a -> (0 until a.length()).map { a.getString(it) } } + val series = doc.getJSONArray("series").let { a -> (0 until a.length()).map { a.getJSONObject(it) } }.map { o -> + val t = o.getString("title"); val id = o.getString("id"); val min = o.getInt("minutes") + val eps = o.getJSONArray("episodes").let { a -> (0 until a.length()).map { a.getJSONArray(it) } } + PoolSeries(id, t, eps.map { e -> val s = e.getInt(0); val n = e.getInt(1) + PoolEpisode("$id$s${"%03d".format(n)}", "9$id$s$n", EpisodeMark(s, n), "$t / S$s E$n", min * 60_000L) }) + } + val films = doc.getJSONArray("films").let { a -> (0 until a.length()).map { a.getJSONObject(it) } }.map { o -> + PoolSeries(o.getString("id"), o.getString("title"), listOf(PoolEpisode(o.getString("id"), "9" + o.getString("id"), EpisodeMark(1, 1), o.getString("title"), o.getInt("minutes") * 60_000L)), + ref = "imdb." + o.optString("imdb"), year = o.optInt("year")) + } + val guests = list("guests").toSet() + fun key(t: String) = seriesKey(t) + val pool = series + films + val featureFilms = list("feature"); val lateFilms = list("late") + val parts = listOf( + DayPart(21, 23, featureFilms, group = "Feature", once = true, onTime = true, key = "heroeslive.feature"), + DayPart(1, 3, lateFilms, group = "Late Feature", once = true, key = "heroeslive.late"), + DayPart(6, 14, list("day")), + DayPart(14, 21, list("evening")), + // After a feature ends, the rest of its window: the evening shows until 23:00, the mature ones until 03:00. + DayPart(21, 23, list("evening")), + DayPart(1, 3, list("mature")), + DayPart(23, 1, list("mature"), key = "heroeslive.afterdark"), + DayPart(3, 6, list("mature") + list("evening"))) + val others = (SmartChannels.map { it.number } + DRIVE_IN_NUMBER).mapNotNull { n -> store.loadPlan(user, n)?.let { n to it } }.toMap() + val clash = clashesWithAirings(others.values.flatMap { airingsOf(it) }) + val resting = restingElsewhere(others.values.flatMap { filmAiringsOf(it) }) + val avoid: ProgrammeClash = { show, s, e -> clash(show, s, e) || (show.isFilm && resting(show, s)) } + val report = StringBuilder("pool: ${series.size} series (${series.sumOf { it.episodes.size }} episodes), ${films.size} films, ${guests.size} of them guests\n") + val checks = mutableListOf() + val mature = (list("mature") + lateFilms).map(::key).toSet() + for ((label, start) in listOf("december" to LocalDateTime.of(2026, 12, 1, 6, 0), "march" to LocalDateTime.of(2027, 3, 1, 6, 0))) { + val now = start.atZone(zone).toInstant().toEpochMilli() + val all = LinkedHashMap(); var plan: ChannelPlan? = null; var aired = ScheduleCursor(); var keptAll = true + for (day in 0 until 35) { + plan?.let { aired = airedCursor(aired, it.entries, now + day * 86_400_000L) } + plan = topUpPlan(plan, aired, pool, now + day * 86_400_000L, null, true, parts, zone, avoid = avoid, shuffle = false, + fileRestMs = NETWORK_FILM_REST_MS, stories = true, network = NoNetworkRest)!! + plan.entries.forEach { e -> all["${e.startMs}"]?.let { if (it != e) keptAll = false } ?: run { all["${e.startMs}"] = e } } + } + val entries = all.values.sortedBy { it.startMs } + fun local(ms: Long) = Instant.ofEpochMilli(ms).atZone(zone) + fun show(e: ScheduleEntry) = pool.first { it.id == e.seriesId } + val tsv = StringBuilder("start\tend\tminutes\ttitle\tblock\tfilm\tguest\n") + entries.forEach { e -> tsv.append("${local(e.startMs).toLocalDateTime()}\t${local(e.endMs).toLocalDateTime()}\t${(e.endMs - e.startMs) / 60_000}\t${e.media.item.title}\t${presentationOf(e, parts, zone)}\t${show(e).isFilm}\t${show(e).title in guests}\n") } + File(out, "plan-115-$label.tsv").writeText(tsv.toString()) + val filmEntries = entries.filter { show(it).isFilm } + val guestEntries = filmEntries.filter { show(it).title in guests } + val matureEarly = entries.count { e -> key(show(e).title) in mature && local(e.startMs).hour in 6..22 } + val clashes = entries.count { e -> clash(show(e), e.startMs, e.endMs) } + val restBroken = filmEntries.count { e -> resting(show(e), e.startMs) } + val gaps = entries.zipWithNext().sumOf { (a, b) -> maxOf(0L, b.startMs - a.endMs) } / 60_000 + val order = entries.filter { !show(it).isFilm }.groupBy { it.seriesId }.count { (id, l) -> val eps = pool.first { it.id == id }.episodes.map { it.mark } + l.zipWithNext().any { (a, b) -> val i = eps.indexOf(a.mark); i < 0 || eps[(i + 1) % eps.size] != b.mark } } + val repeats = filmEntries.groupBy { it.seriesId }.values.filter { it.size > 1 }.map { l -> l.zipWithNext { a, b -> (b.startMs - a.startMs) / 86_400_000L }.minOrNull() ?: 0 } + val featureLate = filmEntries.count { e -> show(e).title in featureFilms && local(e.startMs).hour !in 21..22 } + val byBlock = entries.groupBy { presentationOf(it, parts, zone).ifEmpty { "everyday" } }.mapValues { (_, l) -> l.sumOf { it.endMs - it.startMs } / 3_600_000 } + val seriesHours = entries.filter { !show(it).isFilm }.groupBy { show(it).title }.mapValues { (_, l) -> l.sumOf { it.endMs - it.startMs } / 3_600_000 }.toList().sortedByDescending { it.second } + report.append("$label: ${entries.size} programmes; films ${filmEntries.size} (guests ${guestEntries.size}: ${guestEntries.joinToString { show(it).title }}); " + + "mature before 23:00 $matureEarly; clashes $clashes; network rest broken $restBroken; features off 21:00 $featureLate; gaps $gaps min; series out of order $order; " + + "shortest gap between a film's showings ${repeats.minOrNull() ?: "none repeated"} days; bookings kept $keptAll\n hours by block $byBlock\n top series hours ${seriesHours.take(12)}\n") + val nightsWithoutFeature = (0 until 35).count { d -> val day = start.toLocalDate().plusDays(d.toLong()); filmEntries.none { local(it.startMs).toLocalDate() == day && local(it.startMs).hour in 21..22 } } + report.append(" nights without a 21:00 feature $nightsWithoutFeature\n") + checks += listOf(matureEarly == 0, clashes == 0, restBroken == 0, keptAll, order == 0) + } + File(out, "summary.txt").writeText(report.toString()); println(report) + assertTrue(report.toString(), checks.all { it }) + } +}