From 8ac19b71ab5caecc82f5a08e0f0f12da5324ac89 Mon Sep 17 00:00:00 2001 From: Pauljd Date: Mon, 30 Nov 2026 09:54:17 +0000 Subject: [PATCH] Stories teen strand: simulation only (no channel change) Co-Authored-By: Claude Opus 5.5 --- .../classic/StoriesTeenSimulationTest.kt | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 android/app/src/test/java/tv/orbit/classic/StoriesTeenSimulationTest.kt diff --git a/android/app/src/test/java/tv/orbit/classic/StoriesTeenSimulationTest.kt b/android/app/src/test/java/tv/orbit/classic/StoriesTeenSimulationTest.kt new file mode 100644 index 00000000..f634d614 --- /dev/null +++ b/android/app/src/test/java/tv/orbit/classic/StoriesTeenSimulationTest.kt @@ -0,0 +1,101 @@ +package tv.orbit.classic + +import org.json.JSONArray +import org.junit.Assert.assertTrue +import org.junit.Assume.assumeTrue +import org.junit.Test +import java.io.File +import java.time.Instant +import java.time.ZoneId + +/** + * Orbit Stories' teen and young-adult strand, proposed and simulated only (Paul, 30 November 2026; option A). Nothing here + * changes Stories: the proposed blocks live in this test. From a TV snapshot's own Stories pool and plan (its bookings kept as + * they are), topped up daily for five weeks with the candidates added (their real season and episode counts, 43 minutes an + * episode; a series stops before its first unplayable episode), against the other channels' plans. Compared with the same five + * weeks as Stories is now. Env: SIM_SNAP, SIM_TEEN (candidates.json), SIM_OUT. + */ +class StoriesTeenSimulationTest { + private val afterSchoolNew = listOf("The Secret Life of the American Teenager", "Glee", "90210", "Switched at Birth", "Felicity", "Roswell", "Popular", + "Never Have I Ever", "Degrassi: Next Class", "Love, Victor", "The Summer I Turned Pretty", "Heartstopper", "Everything Sucks!", "Colin in Black and White", + "The Runarounds", "Waterloo Road") + private val primetimeNew = listOf("Riverdale", "Pretty Little Liars", "On My Block", "Ginny & Georgia", "My Mad Fat Diary") + private val lateNew = listOf("Outer Banks", "Atypical", "Misfits", "The Wilds", "I Am Not Okay With This") + /** Where the playable run ends, by season and episode: the series stops before its first unplayable episode. */ + private val stopAfter = mapOf("Never Have I Ever" to (2 to 7), "Love, Victor" to (2 to 99)) + + @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 == STORIES_NUMBER } + val existingPool = store.loadPool(user, STORIES_NUMBER)!!.series + val existingPlan = store.loadPlan(user, STORIES_NUMBER)!! + val rows = JSONArray(src!!.readText()).let { a -> (0 until a.length()).map { a.getJSONObject(it) } }.associateBy { it.getString("title") } + val added = (afterSchoolNew + primetimeNew + lateNew).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 } + val stop = stopAfter[t] + val eps = seasons.flatMap { (s, n) -> (1..n).map { e -> s to e } }.filter { (s, e) -> stop == null || s < stop.first || (s == stop.first && e <= stop.second) } + PoolSeries(o.getString("id"), t, eps.map { (s, 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 everyday = p.dayParts.filter { it.days.isEmpty() } + val events = p.dayParts.filter { it.days.isNotEmpty() } + fun titles(from: Int) = everyday.first { it.fromHour == from }.titles + val proposed = events + listOf( + DayPart(6, 12, titles(6)), DayPart(12, 16, titles(12)), DayPart(16, 20, titles(17) + afterSchoolNew), DayPart(20, 23, titles(20) + primetimeNew), + DayPart(23, 2, titles(23) + lateNew, key = "stories.afterhours"), DayPart(2, 6, titles(20) + titles(23) + primetimeNew + lateNew)) + val others = (SmartChannels.map { it.number } - setOf(STORIES_NUMBER) + DRIVE_IN_NUMBER).mapNotNull { n -> store.loadPlan(user, n)?.let { n to it } }.toMap() + val clash = clashesWithAirings(others.values.flatMap { airingsOf(it) }) + val report = StringBuilder() + val start = existingPlan.entries.last().endMs + fun run(label: String, pool: List, parts: List, weigh: ((PoolSeries) -> Double)? = p.tierWeights?.let { p::weightOf }): List { + var plan: ChannelPlan = existingPlan; var aired = ScheduleCursor() + val all = LinkedHashMap(); existingPlan.entries.forEach { all[it.startMs] = it } + var keptAll = true + 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, parts, zone, avoid = clash, shuffle = p.shuffleEpisodes, + mixed = p::mixes, weightOf = weigh, adaptiveRest = p.adaptiveFilmRest, fileRestMs = p.fileRestMs, stories = p.storyGrouping, + shorts = p.shorts, marathon = p.marathon, network = NoNetworkRest)!! + // Every booking made before stays exactly as it was, top-up after top-up. + plan.entries.forEach { e -> all[e.startMs]?.let { if (it != e) keptAll = false } ?: run { all[e.startMs] = e } } + } + val kept = keptAll + val fresh = all.values.filter { it.startMs >= start }.sortedBy { it.startMs } + fun local(ms: Long) = Instant.ofEpochMilli(ms).atZone(zone) + val tsv = StringBuilder("start\tminutes\ttitle\n") + fresh.forEach { tsv.append("${local(it.startMs).toLocalDateTime()}\t${(it.endMs - it.startMs) / 60_000}\t${it.media.item.title}\n") } + File(out, "plan-112-$label.tsv").writeText(tsv.toString()) + val newKeys = added.map { seriesKey(it.title) }.toSet() + val isNew = { e: ScheduleEntry -> seriesKey(pool.first { it.id == e.seriesId }.title) in newKeys } + val matureKeys = (lateNew + titles(23)).map(::seriesKey).toSet() + val matureEarly = fresh.count { e -> seriesKey(pool.first { it.id == e.seriesId }.title) in matureKeys && local(e.startMs).hour in 6..22 } + val clashes = fresh.count { e -> clash(pool.first { it.id == e.seriesId }, e.startMs, e.endMs) } + val gaps = fresh.zipWithNext().sumOf { (a, b) -> maxOf(0L, b.startMs - a.endMs) } / 60_000 + // Episodes of each show in order, with nothing skipped. + val order = fresh.groupBy { it.seriesId }.count { (_, l) -> l.zipWithNext().any { (a, b) -> + val ea = a.mark; val eb = b.mark; !(eb.season == ea.season && eb.episode == ea.episode + 1) && !(eb.season > ea.season && eb.episode == 1) } } + val hours = fresh.sumOf { it.endMs - it.startMs } / 3_600_000 + val newHours = fresh.filter(isNew).sumOf { it.endMs - it.startMs } / 3_600_000 + val teenExisting = titles(17).map(::seriesKey).toSet() + val teenHours = fresh.filter { e -> seriesKey(pool.first { it.id == e.seriesId }.title) in teenExisting }.sumOf { it.endMs - it.startMs } / 3_600_000 + val byBlock = fresh.groupBy { local(it.startMs).hour.let { h -> when (h) { in 6..11 -> "06-12"; in 12..15 -> "12-16"; in 16..19 -> "16-20"; in 20..22 -> "20-23"; in 23..23, in 0..1 -> "23-02"; else -> "02-06" } } } + .mapValues { (_, l) -> l.count(isNew) to l.size } + report.append("$label: ${fresh.size} new programmes ($hours h) over 35 days; existing bookings kept $kept; new series $newHours h; Stories' own teen dramas $teenHours h; " + + "mature before 23:00 $matureEarly; clashes $clashes; gaps $gaps min; series out of order $order; new/all by block $byBlock\n") + return fresh + } + run("now", existingPool, p.dayParts) + run("proposed", existingPool + added, proposed) + // The newcomers as supporting, so Stories' own series keep more of their airtime. + val newKeys = added.map { seriesKey(it.title) }.toSet() + run("proposed-supporting", existingPool + added, proposed) { show -> if (seriesKey(show.title) in newKeys) TIER_SUPPORTING else p.weightOf(show) } + File(out, "summary.txt").writeText(report.toString()); println(report) + assertTrue(report.toString(), report.lines().filter { it.isNotBlank() }.all { "kept true" in it && "mature before 23:00 0;" in it && "clashes 0;" in it }) + } +}