Character cameos, phase 2: the chooser and its rules, no artwork (0.108.66)

Paul, 6 November 2026, with his answers: Riot, ToonZ evenings and Nite Owl
first; off by default; Luna's glance and eye-roll end about one gag in three.
chooseCameo decides when the cast may appear between programmes: never next to
a film, never on Tots, the Drive-In or for Kids, at most one per channel every
60 to 90 minutes, 20 minutes apart, no gag twice in a session, and the same
answer for the same changeover. CameoTest checks a synthetic week; the
hand-run CameoReplayTest held every rule over the box's real week. Character
cameos and Mini shorts switches added to On screen, off. Nothing new shows.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-06 18:01:49 +00:00
co-authored by Claude Opus 5.5
parent f5dbfa6d22
commit 99fa3adfb8
7 changed files with 232 additions and 5 deletions
@@ -0,0 +1,51 @@
package tv.orbit.classic
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.io.File
import java.time.ZoneId
/**
* Character cameos over the box's real schedules (Paul, 6 November 2026): every channel's booked programmes for the week,
* one session an evening (18:00 to 02:00), through the chooser. Run by hand:
* ORBIT_REPLAY_DIR=<copies of the box's schedule folder> ./gradlew :app:testDebugUnitTest --tests '*CameoReplayTest*'
* Writes cameo-replay.txt there (counts and times only), and fails on any broken rule. Reads the copies; changes nothing.
*/
class CameoReplayTest {
private val dir = System.getenv("ORBIT_REPLAY_DIR")?.let(::File)
private val zone = ZoneId.of("Europe/London")
private val hour = 60 * 60_000L; private val day = 24 * hour
private fun changeovers(file: File): List<Changeover> {
val channel = Regex("""schedule-(\d+)\.json""").find(file.name)!!.groupValues[1].toInt()
val rows = JSONObject(file.readText()).getJSONArray("entries")
val shows = (0 until rows.length()).map { rows.getJSONObject(it) }.map { Triple(it.getLong("a"), it.getLong("d"), it.optString("k", "Episode")) }.sortedBy { it.first }
return shows.zipWithNext().filter { (a, b) -> a.first + a.second == b.first }.map { (a, b) -> Changeover(channel, a.first, a.third, b.first, b.third) }
}
@Test fun aWeekOfRealSchedules() {
assumeTrue("set ORBIT_REPLAY_DIR to run", dir != null && dir.isDirectory)
val all = dir!!.listFiles { f -> f.name.matches(Regex("""schedule-\d+\.json""")) }!!.flatMap(::changeovers).sortedBy { it.nextStartMs }
assertTrue(all.isNotEmpty())
val start = java.time.Instant.ofEpochMilli(all.first().nextStartMs).atZone(zone).toLocalDate().atStartOfDay(zone).toInstant().toEpochMilli()
val report = StringBuilder("Cameo replay: ${all.size} changeovers on ${all.map { it.channel }.distinct().size} channels\n")
var total = 0; var luna = 0
for (d in 0 until 7) {
val from = start + d * day + 18 * hour; val to = from + 8 * hour
val diary = CameoDiary()
val evening = all.filter { it.nextStartMs in from until to }.mapNotNull { c -> chooseCameo(c, diary, true, false, zone)?.let { c to it } }
evening.forEach { (c, cameo) ->
assertTrue(c.channel in setOf(105, 605, NITE_OWL.channel)); assertFalse(c.endingType == "Movie" || c.nextType == "Movie")
report.append(" day ${d + 1} ${java.time.Instant.ofEpochMilli(c.nextStartMs).atZone(zone).toLocalTime()} channel ${c.channel} ${cameo.gag.title}${if (cameo.lunaEnds) " + Luna" else ""}\n")
}
evening.zipWithNext().forEach { (a, b) -> assertTrue(b.first.nextStartMs - a.first.nextStartMs >= CAMEO_HOUSEHOLD_MS) }
evening.groupBy { it.first.channel }.values.forEach { one -> one.zipWithNext().forEach { (a, b) -> assertTrue(b.first.nextStartMs - a.first.nextStartMs >= CAMEO_MIN_GAP_MS) } }
assertEquals(evening.size, evening.map { it.second.gag }.distinct().size)
total += evening.size; luna += evening.count { it.second.lunaEnds }
}
report.append("Total $total cameos over 7 evenings, $luna ending with Luna\n")
File(dir, "cameo-replay.txt").writeText(report.toString())
}
}
@@ -0,0 +1,70 @@
package tv.orbit.classic
import org.junit.Assert.*
import org.junit.Test
import java.time.ZoneId
/** Character cameos, phase 2 (Paul, 6 November 2026): a week of changeovers through the chooser, checking every rule. */
class CameoTest {
private val zone = ZoneId.of("Europe/London")
private val minute = 60_000L; private val hour = 60 * minute; private val day = 24 * hour
// Monday 3 November 2025, 00:00 in London.
private val monday = java.time.LocalDate.of(2025, 11, 3).atStartOfDay(zone).toInstant().toEpochMilli()
/** Half-hour episodes on each channel, with a two-hour film on Riot every six hours. */
private fun week(channels: List<Int>): List<Changeover> = channels.flatMap { channel ->
val out = mutableListOf<Changeover>(); var t = monday; var prevStart = monday - 30 * minute; var prevType = "Episode"
while (t < monday + 7 * day) {
val film = channel == 105 && ((t - monday) / hour) % 6 == 0L && (t - monday) % hour == 0L
val type = if (film) "Movie" else "Episode"
out += Changeover(channel, prevStart, prevType, t, type)
prevStart = t; prevType = type; t += if (film) 2 * hour else 30 * minute
}
out
}.sortedBy { it.nextStartMs }
/** One session an evening, 18:00 to 02:00, as the household might watch. */
private fun sessions(all: List<Changeover>, on: Boolean = true, kids: Boolean = false): List<Pair<Changeover, Cameo>> = (0 until 7).flatMap { d ->
val from = monday + d * day + 18 * hour; val to = from + 8 * hour
val diary = CameoDiary()
all.filter { it.nextStartMs in from until to }.mapNotNull { c -> chooseCameo(c, diary, on, kids, zone)?.let { c to it } }
}
private val all = week(listOf(101, 105, 605, NITE_OWL.channel, TOTS_NUMBER, DRIVE_IN_NUMBER))
@Test fun cameosKeepToEveryRule() {
val chosen = sessions(all)
assertTrue("some cameos over a week", chosen.size >= 7)
for ((c, _) in chosen) {
assertTrue("first channels only: ${c.channel}", c.channel in setOf(105, 605, NITE_OWL.channel))
assertFalse("never next to a film", c.endingType == "Movie" || c.nextType == "Movie")
assertTrue(cameoWindow(c.channel, c.nextStartMs, zone))
}
// Gaps: per channel 60 minutes or more, between any two 20 or more, no gag twice in an evening.
chosen.groupBy { (c, _) -> (c.nextStartMs - monday - 18 * hour) / day }.values.forEach { evening ->
evening.zipWithNext().forEach { (a, b) -> assertTrue(b.first.nextStartMs - a.first.nextStartMs >= CAMEO_HOUSEHOLD_MS) }
evening.groupBy { it.first.channel }.values.forEach { one -> one.zipWithNext().forEach { (a, b) -> assertTrue(b.first.nextStartMs - a.first.nextStartMs >= CAMEO_MIN_GAP_MS) } }
assertEquals(evening.size, evening.map { it.second.gag }.distinct().size)
evening.filter { it.second.gag.overnightOnly }.forEach { assertEquals(NITE_OWL.channel, it.first.channel) }
}
}
@Test fun theSameChangeoverAlwaysGetsTheSameAnswer() {
assertEquals(sessions(all), sessions(all))
val diary = CameoDiary(); val c = all.first { it.channel == 105 && it.nextType == "Episode" && it.endingType == "Episode" && cameoWindow(105, it.nextStartMs, zone) }
val first = chooseCameo(c, diary, true, false, zone)
assertEquals(first, chooseCameo(c, diary, true, false, zone))
assertEquals(if (first != null) 1 else 0, diary.seen.size)
}
@Test fun offOrForKidsNothingAppears() {
assertTrue(sessions(all, on = false).isEmpty())
assertTrue(sessions(all, kids = true).isEmpty())
}
@Test fun lunaEndsAboutOneGagInThree() {
val many = (0 until 400).mapNotNull { i -> chooseCameo(Changeover(105, monday + i * day, "Episode", monday + i * day + 30 * minute, "Episode"), CameoDiary(), true, false, zone) }
val share = many.count { it.lunaEnds }.toDouble() / many.size
assertTrue("Luna ends $share of gags", share in .2..0.45)
}
}