diff --git a/android/app/src/test/java/tv/orbit/classic/ParadoxFantasySimulationTest.kt b/android/app/src/test/java/tv/orbit/classic/ParadoxFantasySimulationTest.kt new file mode 100644 index 00000000..cb05fd45 --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/ParadoxFantasySimulationTest.kt @@ -0,0 +1,82 @@ +package tv.orbit.classic + +import org.json.JSONArray +import org.junit.Assume.assumeTrue +import org.junit.Test +import java.io.File +import java.time.Instant +import java.time.ZoneId + +/** + * Paradox with six fantasy series, proposed and simulated only (Paul, 30 November 2026). Nothing here changes Paradox. From a + * TV snapshot's Paradox pool and plan (its bookings kept), topped up daily for five weeks against the other channels' plans. + * Two placements are compared with Paradox as it is: (A) the four in prime time from 18:00, Sabrina and Wednesday late; (B) the + * four only from 21:00, so Buffy, Angel, Supernatural and Fringe keep 18:00 to 21:00. Env: SIM_SNAP, SIM_TEEN, SIM_OUT. + */ +class ParadoxFantasySimulationTest { + private val prime = listOf("The Vampire Diaries", "The 100", "Shadowhunters", "Locke & Key") + private val late = listOf("Chilling Adventures of Sabrina", "Wednesday") + + @Test fun simulate() { + val dir = System.getenv("SIM_SNAP")?.let(::File); val src = System.getenv("SIM_TEEN")?.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 { org.json.JSONObject(it.readText()).getString("user") } + val store = ScheduleStore(dir) + val p = SmartChannels.first { it.number == 106 } + val existingPool = store.loadPool(user, 106)!!.series + val existingPlan = store.loadPlan(user, 106)!! + val rows = JSONArray(src!!.readText()).let { a -> (0 until a.length()).map { a.getJSONObject(it) } }.associateBy { it.getString("title") } + val added = (prime + late).mapNotNull { t -> + val o = rows[t] ?: return@mapNotNull null + val seasons = o.getJSONArray("seasons").let { a -> (0 until a.length()).map { a.getJSONArray(it) }.map { it.getInt(0) to it.getInt(1) } }.sortedBy { it.first } + PoolSeries(o.getString("id"), t, seasons.flatMap { (s, n) -> (1..n).map { e -> PoolEpisode("${o.getString("id")}$s${"%03d".format(e)}", "9${o.getString("id")}$s$e", EpisodeMark(s, e), "$t / S$s E$e", 43 * 60_000L) } }) + } + val parts = p.dayParts + fun at(h: Int) = parts.first { it.days.isEmpty() && it.covers(h) } + val primeShows = at(18).titles; val lateShows = at(23).titles + fun with(newPrimeFrom18: Boolean) = parts.filter { it.days.isNotEmpty() } + parts.filter { it.days.isEmpty() }.map { part -> + when { + part.covers(18) && newPrimeFrom18 -> part.copy(titles = part.titles + prime) + part.covers(21) && part.fromHour == 21 -> part.copy(titles = part.titles + prime) + part.covers(23) -> part.copy(titles = part.titles + late) + else -> part + } } + val others = (SmartChannels.map { it.number } - setOf(106) + DRIVE_IN_NUMBER).mapNotNull { n -> store.loadPlan(user, n)?.let { n to it } }.toMap() + val clash = clashesWithAirings(others.values.flatMap { airingsOf(it) }) + val start = existingPlan.entries.last().endMs + val report = StringBuilder() + fun run(label: String, pool: List, dp: List) { + var plan: ChannelPlan = existingPlan; var aired = ScheduleCursor(); var kept = true + val all = LinkedHashMap(); existingPlan.entries.forEach { all[it.startMs] = it } + for (day in 0 until 35) { + val now = start - 6 * 86_400_000L + day * 86_400_000L + aired = airedCursor(aired, plan.entries, now) + plan = topUpPlan(plan, aired, pool, now, p.filmRestMs, p.finishTogether, dp, zone, avoid = clash, shuffle = p.shuffleEpisodes, + mixed = p::mixes, weightOf = p.tierWeights?.let { p::weightOf }, adaptiveRest = p.adaptiveFilmRest, fileRestMs = p.fileRestMs, stories = p.storyGrouping, + shorts = p.shorts, marathon = p.marathon, network = NoNetworkRest)!! + plan.entries.forEach { e -> all[e.startMs]?.let { if (it != e) kept = false } ?: run { all[e.startMs] = e } } + } + val fresh = all.values.filter { it.startMs >= start }.sortedBy { it.startMs } + fun local(ms: Long) = Instant.ofEpochMilli(ms).atZone(zone) + fun title(e: ScheduleEntry) = pool.first { it.id == e.seriesId }.title + fun hours(f: (ScheduleEntry) -> Boolean) = fresh.filter(f).sumOf { it.endMs - it.startMs } / 3_600_000 + val newKeys = added.map { seriesKey(it.title) }.toSet() + val primeKeys = primeShows.map(::seriesKey).toSet() + val clashes = fresh.count { clash(pool.first { p0 -> p0.id == it.seriesId }, it.startMs, it.endMs) } + val gaps = fresh.zipWithNext().sumOf { (a, b) -> maxOf(0L, b.startMs - a.endMs) } / 60_000 + val inOrder = setOf("Buffy the Vampire Slayer", "Angel", "Supernatural", "Fringe") + prime + late + val order = fresh.groupBy { it.seriesId }.filter { (id, _) -> pool.first { it.id == id }.title in inOrder }.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 = fresh.groupBy { "${it.seriesId}/${it.mark.season}/${it.mark.episode}" }.count { (_, l) -> l.zipWithNext().any { (a, b) -> b.startMs - a.startMs < 7 * 86_400_000L } } + report.append("$label: kept $kept; new series ${hours { seriesKey(title(it)) in newKeys }} h; Buffy, Angel, Supernatural and Fringe ${hours { seriesKey(title(it)) in primeKeys }} h " + + "(18:00 to 21:00: ${hours { seriesKey(title(it)) in primeKeys && local(it.startMs).hour in 18..20 }} h); daytime standalones ${hours { local(it.startMs).hour in 6..17 }} h; " + + "clashes $clashes; gaps $gaps min; out of order $order; repeats within a week $repeats\n") + } + run("now", existingPool, parts) + run("A: from 18:00", existingPool + added, with(true)) + run("B: from 21:00", existingPool + added, with(false)) + File(out, "paradox-summary.txt").writeText(report.toString()); println(report) + } +}