Schedules: a reading range from the oldest supported format (audit A7), newer files kept aside

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 17:45:35 +00:00
co-authored by Claude Opus 5.5
parent 92ec9cdf63
commit 127191abbc
2 changed files with 46 additions and 2 deletions
@@ -43,7 +43,14 @@ internal fun migrateChannelNumbers(directory: File) {
* the committed schedule and the aired progress. Each can be lost without affecting the others. Holds no login.
*/
internal class ScheduleStore(private val directory: File) {
companion object { const val SCHEMA = 1 }
companion object {
const val SCHEMA = 1
/**
* The oldest format this Orbit still reads (audit A7, 9 November 2026). When the format changes, SCHEMA goes up and
* this stays, so an update never strands a TV's plans; the reader then upgrades the older file as it goes.
*/
const val OLDEST_READABLE = 1
}
internal class SavedProgress(val enabled: Boolean, val aired: ScheduleCursor)
internal class SavedPool(val series: List<PoolSeries>, val fetchedAtMs: Long, val signature: String = "")
@@ -69,6 +76,8 @@ internal class ScheduleStore(private val directory: File) {
/** Replaces in one step, keeping the last good copy. An interrupted write can't damage either. */
private fun write(target: File, root: JSONObject) {
directory.mkdirs()
// A file from a newer Orbit (after a recovery to an older build) is never lost: kept aside before it's replaced.
newerSchema(target)?.let { v -> if (!File(directory, "${target.name}.schema$v").exists()) runCatching { target.copyTo(File(directory, "${target.name}.schema$v")) } }
val temporary = File(directory, "${target.name}.tmp")
FileOutputStream(temporary).use { output -> output.write(root.toString().toByteArray(Charsets.UTF_8)); output.fd.sync() }
if (target.exists()) target.copyTo(File(directory, "${target.name}.prev"), overwrite = true)
@@ -81,13 +90,16 @@ internal class ScheduleStore(private val directory: File) {
try {
if (!candidate.exists()) continue
val root = JSONObject(candidate.readText(Charsets.UTF_8))
require(root.getInt("schema") == SCHEMA && root.getString("user") == userId && root.getInt("channel") == number)
require(root.getInt("schema") in OLDEST_READABLE..SCHEMA && root.getString("user") == userId && root.getInt("channel") == number)
return parse(root)
} catch (_: Exception) { /* Damaged: try the last good copy. */ }
}
return null
}
/** The schema of [target] when a newer Orbit wrote it, else null. */
private fun newerSchema(target: File): Int? = runCatching { JSONObject(target.readText(Charsets.UTF_8)).getInt("schema") }.getOrNull()?.takeIf { it > SCHEMA }
private fun root(userId: String, number: Int) = JSONObject().put("schema", SCHEMA).put("user", userId).put("channel", number)
private fun encodeCursor(cursor: ScheduleCursor) = JSONObject().put("last", cursor.lastSeriesId ?: JSONObject.NULL)
@@ -0,0 +1,32 @@
package tv.orbit.classic
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File
/** Audit A7 (9 November 2026): schedule formats an update or a recovery can't strand. */
class ScheduleFormatTest {
@get:Rule val tmp = TemporaryFolder()
private val empty = ChannelPlan(emptyList(), ScheduleCursor(null, emptyMap()))
@Test fun readsEveryFormatFromTheOldestOn() {
assertTrue(ScheduleStore.OLDEST_READABLE <= ScheduleStore.SCHEMA)
ScheduleStore(tmp.root).savePlan("user", 304, empty)
assertNotNull(ScheduleStore(tmp.root).loadPlan("user", 304))
}
@Test fun aNewerFormatIsKeptAsideNotLost() {
val store = ScheduleStore(tmp.root)
store.savePlan("user", 304, empty)
val f = tmp.root.listFiles()!!.first { it.name.startsWith("schedule-") && it.name.endsWith(".json") }
f.writeText(JSONObject(f.readText()).put("schema", ScheduleStore.SCHEMA + 1).toString()) // as a newer Orbit would
assertNull(store.loadPlan("user", 304)) // not read as something it isn't
store.savePlan("user", 304, empty) // a recovered older Orbit plans again...
val kept = File(tmp.root, f.name + ".schema${ScheduleStore.SCHEMA + 1}")
assertTrue(kept.exists()) // ...but the newer file is kept beside it
assertEquals(ScheduleStore.SCHEMA + 1, JSONObject(kept.readText()).getInt("schema"))
}
}