0.99.2: permanent clash checks (network test, real-pool replay assertion, clashes= on the box's log line)
pauljd/davidson-ci: Davidson CI / quality (push) Failing after 2m10s
pauljd/davidson-ci: Davidson CI / ai-review (push) Failing after 1m35s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-04 01:11:41 +00:00
co-authored by Claude Opus 5.5
parent 1d0defd778
commit 1a8e2647ab
8 changed files with 108 additions and 9 deletions
@@ -1,6 +1,7 @@
package tv.orbit.classic
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.io.File
@@ -87,6 +88,8 @@ class EditorialCorrectionsAuditTest {
fun d(x: Pair<Int, ScheduleEntry>) = "${names[x.first]} ${local(x.second.startMs).toLocalDateTime().withSecond(0).withNano(0)}${if (x.second.startMs in committedStarts.getValue(x.first)) " (committed)" else ""} [${x.second.why.take(90)}]"
out.appendLine(" ${a.second.media.item.title.substringBefore(" / ").take(40)}: ${d(a)} / ${d(b)}")
}
// The permanent check: nothing booked from now on is on another channel at the same time.
assertEquals("new bookings clashing", 0, clashes.count { (a, b) -> a.second.startMs !in committedStarts.getValue(a.first) || b.second.startMs !in committedStarts.getValue(b.first) })
// Titles in more than one film channel's pool: the ones that can clash.
val filmPools = channels.filter { it.filmRestMs != null }
val shared = filmPools.flatMap { p -> pools.getValue(p.number).filter { it.isFilm }.map { seriesKey(it.title) to p.number } }.groupBy({ it.first }, { it.second }).filterValues { it.size > 1 }
@@ -0,0 +1,68 @@
package tv.orbit.classic
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
/** The same title never on two channels at once: channels topping up in turn, each avoiding the others' plans, as the box does. */
class NetworkClashTest {
private val zone = java.time.ZoneOffset.UTC
private val day = 24 * 60 * 60_000L
private val start = java.time.LocalDate.of(2026, 11, 10).atStartOfDay(zone).toInstant().toEpochMilli()
private fun film(title: String, minutes: Int) = PoolSeries("$title-id", title, listOf(PoolEpisode("$title-file", "src", EpisodeMark(1, 1), title, minutes * 60_000L)))
private fun series(title: String, episodes: Int, minutes: Int = 25) =
PoolSeries(title, title, (1..episodes).map { PoolEpisode("$title-$it", "src", EpisodeMark(1, it), "E$it", minutes * 60_000L) })
private fun network(channels: List<Triple<Int, List<PoolSeries>, ChannelProgramming>>): Map<Int, List<ScheduleEntry>> {
val plans = HashMap<Int, ChannelPlan?>()
val all = channels.associate { it.first to mutableListOf<ScheduleEntry>() }
var now = start
while (now < start + 28 * day) {
for ((number, pool, p) in channels) {
val others = clashesWith(plans.filterKeys { it != number }.values.filterNotNull())
val plan = topUpPlan(plans[number], plans[number]?.tail ?: ScheduleCursor(), pool, now, p.filmRestMs, p.finishTogether, p.dayParts, zone, others,
weightOf = p.tierWeights?.let { p::weightOf })!!
plans[number] = plan
plan.entries.filter { e -> all.getValue(number).none { it.startMs == e.startMs } }.forEach { all.getValue(number) += it }
}
now += 6 * 60 * 60_000L
}
return all
}
private fun overlaps(run: Map<Int, List<ScheduleEntry>>) = run.flatMap { (n, es) -> es.map { n to it } }.groupBy { seriesKey(it.second.media.item.title.substringBefore(" / ")) }.values.sumOf { list ->
list.indices.sumOf { i -> list.drop(i + 1).count { b -> b.first != list[i].first && list[i].second.startMs < b.second.endMs && b.second.startMs < list[i].second.endMs } }
}
@Test fun filmChannelsSharingFilmsNeverScreenOneTogether() {
// Two film channels, half their films shared, and small enough pools that both want the shared films often.
val shared = (1..10).map { film("Shared $it", 90 + 7 * it) }
val a = shared + (1..20).map { film("A $it", 95 + 3 * it) }; val b = shared + (1..20).map { film("B $it", 88 + 5 * it) }
fun films(n: Int) = ChannelProgramming(n, "Films $n", emptyList(), filmRestMs = 3 * day)
val run = network(listOf(Triple(1, a, films(1)), Triple(2, b, films(2))))
assertEquals(0, overlaps(run))
assertTrue(run.values.all { es -> es.zipWithNext().all { (x, y) -> x.endMs == y.startMs } })
}
@Test fun seriesChannelsOnEitherRuleNeverAirAShowTogether() {
// A show shared between an equal-turns channel and a weighted one.
val common = series("Common", 200)
val equal = ChannelProgramming(1, "Equal", emptyList(), finishTogether = true)
val weighted = ChannelProgramming(2, "Weighted", emptyList(), tierWeights = mapOf(seriesKey("Common") to TIER_CORE))
val run = network(listOf(Triple(1, listOf(common, series("X", 50), series("Y", 60)), equal), Triple(2, listOf(common, series("P", 80), series("Q", 70)), weighted)))
assertEquals(0, overlaps(run))
assertTrue(run.values.all { es -> es.any { it.seriesId == "Common" } })
}
@Test fun anUnavoidableClashIsNamedAndCounted() {
// One show only, on two channels each way round: something has to give, and the reason says so on either rule.
val only = series("Only", 100)
val weighted = ChannelProgramming(1, "Weighted", emptyList(), tierWeights = emptyMap()); val equal = ChannelProgramming(2, "Equal", emptyList())
for (order in listOf(listOf(Triple(1, listOf(only), weighted), Triple(2, listOf(only), equal)), listOf(Triple(2, listOf(only), equal), Triple(1, listOf(only), weighted)))) {
val run = network(order)
val second = order.last().first
assertTrue(run.getValue(second).any { "airs elsewhere then" in it.why })
}
assertTrue(SchedulerReport(1, 1, emptyList(), 0, 0, 0, 0, true, 0, clashes = 3).logLine(1).endsWith("clashes=3"))
}
}