0.108.22: the sleep timer and Catch Up

Paul, 5 November 2026. The sleep timer sits in the Watching panel (right
while watching): Off, 30, 60 or 90 minutes, or after this programme. A
minute before, a warning says Orbit is switching off; any button keeps you
watching for the same length again. Then Orbit closes and the TV sleeps as
it normally would. Nothing is saved: it starts off each time Orbit opens.

Catch Up: anything a scheduled channel showed in the last two days can be
watched from its start. It replays the channel from that moment (a negative
clock offset), so what followed follows again and a two-part story carries
straight on. Left and right skip 30 seconds, OK pauses (the replay clock
stands still with the picture), up and down go back to live, Back leaves.
The guide scrolls back two days, with finished programmes dimmed and marked;
OK on one opens the Catch Up pop-up. The top bar gains a Catch Up tab
between TV Guide and Drive-In: Just missed, Earlier today, Yesterday and
the day before, newest first, each programme once.

The box now keeps 49 hours of each plan's past instead of 24, so Catch Up
only ever offers what the committed plan says aired. Bookings ahead are
untouched.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-05 19:59:39 +00:00
co-authored by Claude Opus 5.5
parent eefe1f26fb
commit 5981c2e355
14 changed files with 604 additions and 44 deletions
@@ -0,0 +1,130 @@
package tv.orbit.classic
import org.junit.After
import org.junit.Assert.*
import org.junit.Test
import java.time.ZoneId
import java.time.ZonedDateTime
/** Catch Up reads what the committed plans say aired in the last two days, and the sleep timer switches Orbit off. */
class CatchUpTest {
private val minute = 60_000L
private val hour = 60 * minute
private val day = 24 * hour
private val london = ZoneId.of("Europe/London")
// Thursday 5 November 2026, 20:14.
private val now = ZonedDateTime.of(2026, 11, 5, 20, 14, 0, 0, london).toInstant().toEpochMilli()
private fun show(id: String, title: String, episodes: Int, minutes: Long = 45) = PoolSeries(id, title, (1..episodes).map { n ->
PoolEpisode("$id-$n", "src-$id-$n", EpisodeMark(1, n), "$title / S1 E$n / Part $n", minutes * minute)
})
private val loop = SampleChannel(listOf(media("x"), media("y")), "Paradox", 106)
private fun media(id: String) = ScheduledMedia(SampleItem(id, "Episode", 1, "", id, minute), "s-$id", minute)
/** A plan that began three days ago and runs a week ahead, with everything older than the keep window trimmed. */
private fun channel(): ScheduledChannel {
val pool = listOf(show("dw", "Doctor Who", 40), show("xf", "The X-Files", 40))
val plan = extendSchedule(pool, ScheduleCursor(), now - 3 * day, now + 7 * day)
return ScheduledChannel(loop, ChannelPlan(plan.entries.dropWhile { it.endMs < now - SCHEDULE_KEEP_PAST_MS }, plan.tail))
}
@After fun clearTimer() = SleepTimer.clear()
@Test fun theBoxKeepsTwoDaysOfWhatAired() {
assertTrue(SCHEDULE_KEEP_PAST_MS >= CATCH_UP_MS)
val c = channel()
val earliest = catchUpEarliest(c, now)
assertTrue(earliest >= now - CATCH_UP_MS && earliest < now - CATCH_UP_MS + 45 * minute)
// A plain looping channel has nothing to catch up on.
assertEquals(now, catchUpEarliest(loop, now))
}
@Test fun onlyFinishedProgrammesInTheLastTwoDaysAndInThePlan() {
val c = channel()
val entries = c.plan.entries
val past = entries.last { it.endMs <= now }
val live = entries.first { it.startMs <= now && it.endMs > now }
assertTrue(catchUpAvailable(c, SampleBroadcast(past.media, past.startMs), now))
assertFalse(catchUpAvailable(c, SampleBroadcast(live.media, live.startMs), now))
// The loop's programmes before the plan began never aired.
assertFalse(catchUpAvailable(c, loop.at(now - 4 * day), now))
assertFalse(catchUpAvailable(loop, loop.at(now - hour), now))
}
@Test fun watchingReplaysTheChannelFromTheStartAndCarriesOn() {
val c = channel()
val past = c.plan.entries.last { it.endMs <= now - 2 * hour }
val entry = catchUpEntry(c, SampleBroadcast(past.media, past.startMs))
val offset = entry.offsetFrom(now)
assertTrue(offset < 0)
// At the replay clock the channel is at the programme's very start, then the next one follows as it aired.
assertEquals(past.startMs, c.at(now + offset).startMs)
assertEquals(0L, c.at(now + offset).offsetAt(now + offset))
assertEquals(entry.next!!.startMs, c.at(now + offset + past.media.durationMs).startMs)
assertEquals(past.startMs + CATCH_UP_MS, entry.untilMs)
assertTrue(ScheduledPlayback(c, c.at(now + offset), entry.next!!, offset).catchingUp())
assertFalse(ScheduledPlayback(c, c.at(now), c.at(c.at(now).endMs), 0).catchingUp())
}
@Test fun theNextPartCarriesOnWhenItFollows() {
val c = channel()
val dw = c.plan.entries.filter { it.endMs <= now }
val pair = dw.zipWithNext().firstOrNull { (a, b) -> a.seriesId == b.seriesId }
val split = dw.zipWithNext().first { (a, b) -> a.seriesId != b.seriesId }
if (pair != null) assertTrue(catchUpEntry(c, SampleBroadcast(pair.first.media, pair.first.startMs)).continues)
assertFalse(catchUpEntry(c, SampleBroadcast(split.first.media, split.first.startMs)).continues)
}
@Test fun thePageHasRowsNewestFirstWithEachProgrammeOnce() {
val c = channel()
val shelves = catchUpShelves(listOf(c, loop), now, london)
assertEquals(listOf("JUST MISSED", "EARLIER TODAY", "YESTERDAY", "THE DAY BEFORE"), shelves.map { it.title })
val just = shelves.first().entries
assertTrue(just.all { now - it.show.endMs <= JUST_MISSED_MS && it.show.endMs <= now })
assertEquals(just.sortedByDescending { it.show.endMs }, just)
val all = shelves.flatMap { it.entries }
assertEquals(all.size, all.map { it.show.media.item.id }.distinct().size)
assertTrue(all.all { it.show.startMs >= now - CATCH_UP_MS && catchUpAvailable(c, it.show, now) })
// Nothing from a channel without a plan, and nothing at all when nothing has aired.
assertTrue(catchUpShelves(listOf(loop), now, london).isEmpty())
}
@Test fun theGuideScrollsBackIntoCatchUpOnlyWhereThePlanHoldsIt() {
val c = channel()
val earliest = catchUpEarliest(c, now)
val live = c.at(now)
// Left from what's on now goes to the programme before it; it never goes before Catch Up's earliest.
assertEquals(live.startMs - 1, guideStep(c, now, now, false, earliest))
assertEquals(earliest, guideStep(c, earliest, now, false, earliest))
assertEquals(earliest, guideDayJump(now - day - hour, now, false, earliest))
// Finished programmes stay in the grid when they can be caught up, and the live one isn't stretched over them.
val start = guideWindowStart(now, -4)
val cells = guideWatchableCells(guideCells(c, start, start + 2 * hour), now, earliest)
assertTrue(cells.first().show.endMs <= now)
assertEquals(guideCells(c, start, start + 2 * hour), cells)
// Without Catch Up (a looping channel), as before.
assertTrue(guideWatchableCells(guideCells(loop, start, start + 2 * hour), now).none { it.show.endMs <= now })
}
@Test fun sleepTimerCountsDownWarnsAndSwitchesOff() {
SleepTimer.set(SleepChoice.Thirty, now, null)
assertEquals(now + 30 * minute, SleepTimer.endsAt)
assertFalse(SleepTimer.tick(now + 28 * minute)); assertFalse(SleepTimer.warning)
assertFalse(SleepTimer.tick(now + 29 * minute + 1)); assertTrue(SleepTimer.warning)
// Any button: the same length again.
SleepTimer.keepWatching(now + 29 * minute + 5_000)
assertFalse(SleepTimer.warning); assertEquals(now + 59 * minute + 5_000, SleepTimer.endsAt)
assertTrue(SleepTimer.tick(now + 60 * minute))
assertEquals("2 min left", sleepLeft(now + 90_000, now))
}
@Test fun afterThisProgrammeEndsWithItAndAlwaysWarnsFirst() {
SleepTimer.set(SleepChoice.AfterThis, now, now + 20 * minute)
assertEquals(now + 20 * minute, SleepTimer.endsAt)
assertEquals(now + SLEEP_WARNING_MS, sleepEnd(SleepChoice.AfterThis, now, now + 10_000))
// Keeping watching after a programme-length timer turns it off.
SleepTimer.keepWatching(now + 19 * minute + 30_000)
assertEquals(SleepChoice.Off, SleepTimer.choice); assertNull(SleepTimer.endsAt); assertFalse(SleepTimer.tick(now + day))
// With no programme to end, there's nothing to time.
SleepTimer.set(SleepChoice.AfterThis, now, null)
assertEquals(SleepChoice.Off, SleepTimer.choice)
}
}