Server IDs are temporary references: repairs kept, failures cooled down, lookups shared, nothing substituted (build 66)

Paul, 18 November 2026: IMDb, TMDB and TVDB IDs identify the content; a server's ID only says where it is today. Every
provider-ID repair goes through IdRepairs: a repair that worked is kept per workspace (id-repairs-0.json) and never asked
twice; a lookup that found nothing cools down (6 hours, doubling, at most a week), playback's refiles included; two
lookups for one title at once become one; every lookup is an ordinary paced request within the household budget; only
what's needed when it's needed. pickByProvider never substitutes: rows that could be two different titles repair
nothing, and a different film by name and year is refused. A kept repair that has gone stale is looked up afresh once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 15:00:53 +00:00
co-authored by Claude Opus 5.5
parent 788ea9268a
commit 224b10499a
5 changed files with 226 additions and 15 deletions
@@ -0,0 +1,68 @@
package tv.orbit.classic
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Test
import java.nio.file.Files
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicInteger
/** Server IDs are temporary references (Paul, 18 November 2026): repairs kept, failures cooled down, lookups shared, nothing substituted. */
class IdRepairsTest {
@org.junit.Before fun clean() = IdRepairs.resetForTest()
@org.junit.After fun after() = IdRepairs.resetForTest()
@Test fun aRepairIsKeptAndNeverAskedTwiceEvenAfterARestart() {
val dir = Files.createTempDirectory("repairs").toFile()
IdRepairs.attach(dir)
val asked = AtomicInteger()
assertEquals("new1", IdRepairs.lookup("Movie", "imdb.tt1", 0L) { asked.incrementAndGet(); "new1" })
assertEquals("new1", IdRepairs.lookup("Movie", "imdb.tt1", 1L) { asked.incrementAndGet(); "other" })
IdRepairs.attach(dir) // as after Orbit restarts
assertEquals("new1", IdRepairs.lookup("Movie", "imdb.tt1", 2L) { asked.incrementAndGet(); "other" })
assertEquals(1, asked.get())
dir.deleteRecursively()
}
@Test fun aFailedLookupCoolsDownAndBacksOff() {
val asked = AtomicInteger()
val h = 60 * 60_000L
assertNull(IdRepairs.lookup("Movie", "imdb.tt2", 0L) { asked.incrementAndGet(); null })
assertNull(IdRepairs.lookup("Movie", "imdb.tt2", 5 * h) { asked.incrementAndGet(); null }) // within 6 hours: not asked
assertEquals(1, asked.get())
assertNull(IdRepairs.lookup("Movie", "imdb.tt2", 7 * h) { asked.incrementAndGet(); null }) // after 6 hours: asked once more
assertNull(IdRepairs.lookup("Movie", "imdb.tt2", 18 * h) { asked.incrementAndGet(); null }) // now 12 hours
assertEquals(2, asked.get())
assertEquals(IdRepairs.LONGEST_COOLDOWN_MS, IdRepairs.cooldownMs(30)) // never longer than a week
}
@Test fun simultaneousLookupsForOneTitleBecomeOne() {
val asked = AtomicInteger(); val start = CountDownLatch(1)
val threads = (1..8).map { Thread { start.await(); IdRepairs.lookup("Series", "tvdb.9", 0L) { asked.incrementAndGet(); Thread.sleep(50); "show9" } } }
threads.forEach { it.start() }; start.countDown(); threads.forEach { it.join() }
assertEquals(1, asked.get())
}
@Test fun aStaleRepairIsForgottenAndLookedUpAfresh() {
IdRepairs.lookup("Movie", "imdb.tt3", 0L) { "old" }
IdRepairs.forget("Movie", "imdb.tt3")
assertEquals("fresh", IdRepairs.lookup("Movie", "imdb.tt3", 1L) { "fresh" })
}
private fun row(id: String, name: String, year: Int? = null) = JSONObject().put("Id", id).put("Name", name).apply { year?.let { put("ProductionYear", it) } }
@Test fun copiesOfOneFilmAreOneAnswer() =
assertEquals("a", pickByProvider(listOf(row("a", "Tremors", 1990), row("b", "Tremors", 1990)), "Tremors", 1990)?.optString("Id"))
@Test fun twoDifferentFilmsUnderOneIdAreNeverSubstituted() {
val rows = listOf(row("a", "Halloween", 1978), row("b", "Halloween", 2018))
assertNull(pickByProvider(rows, "Halloween", null))
assertEquals("a", pickByProvider(rows, "Halloween", 1978)?.optString("Id")) // the year settles it
}
@Test fun aRenamedFilmWithItsYearIsStillIt() =
assertEquals("a", pickByProvider(listOf(row("a", "John Carpenter's Halloween", 1978)), "Halloween", 1978)?.optString("Id"))
@Test fun aDifferentFilmWithAnotherNameAndYearIsRefused() =
assertNull(pickByProvider(listOf(row("a", "Krull", 1983)), "Tremors", 1990))
}
@@ -7,7 +7,7 @@ import org.junit.Test
/** 12 to 13 November 2026: the first scan re-files titles under new numbers; Orbit finds a booked one again, once. */
class RefiledTest {
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com" }
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com"; IdRepairs.resetForTest() }
@org.junit.After fun noServer() { EmbyServer.origin = "" }
private val gone = SampleItem("Fixture Show / S2 E3 / The Third", "Episode", 22, "", "oldep1", 22 * 60_000L)
private fun source(id: String) = JSONArray().put(JSONObject().put("Id", id).put("Container", "mkv").put("RunTimeTicks", 22 * 60_000L * 10_000))
@@ -99,7 +99,7 @@ class CopyChoiceTest {
/** 13 November 2026: a film already booked as a heavy copy swaps to a lighter one when tuned, searched for once. */
class LighterCopyTest {
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com" }
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com"; IdRepairs.resetForTest() }
@org.junit.After fun noServer() { EmbyServer.origin = "" }
private val booked = SampleItem("Fixture Werewolf", "Movie", 97, "", "heavy1", 97 * 60_000L)
private fun source(id: String, width: Int, bitrate: Long) = JSONObject().put("Id", id).put("Container", "mkv").put("Bitrate", bitrate)
@@ -127,7 +127,7 @@ class LighterCopyTest {
/** 15 November 2026: one renumbered programme means a rescan; the rest of the channel's schedule is checked in one go. */
class RecheckAfterRescanTest {
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com" }
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com"; IdRepairs.resetForTest() }
@org.junit.After fun noServer() { EmbyServer.origin = "" }
private val min = 60_000L
private fun ep(id: String, n: Int, start: Long) = ScheduleEntry("show", EpisodeMark(1, n),
@@ -185,7 +185,7 @@ class RefiledPlaysTest {
* provider ID and year), so it's found again after a rescan even when its name is shared or has changed.
*/
class RenumberingTest {
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com" }
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com"; IdRepairs.resetForTest() }
@org.junit.After fun noServer() { EmbyServer.origin = "" }
private fun source(id: String, minutes: Long = 91) = JSONArray().put(JSONObject().put("Id", id).put("Container", "mkv").put("RunTimeTicks", minutes * 60_000L * 10_000))
private fun film(id: String, name: String, year: Int, imdb: String) = JSONObject().put("Id", id).put("Name", name).put("Type", "Movie").put("ProductionYear", year)
@@ -250,7 +250,7 @@ class RenumberingTest {
/** 18 November 2026: an approval whose item has gone (the server renumbered, or it came from another server) is found again. */
class SelfHealingApprovalTest {
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com" }
@org.junit.Before fun server() { EmbyServer.origin = "https://emby.example.com"; IdRepairs.resetForTest() }
@org.junit.After fun noServer() { EmbyServer.origin = "" }
private fun source(id: String, minutes: Long = 90) = JSONArray().put(JSONObject().put("Id", id).put("Container", "mkv").put("RunTimeTicks", minutes * 60_000L * 10_000))
private fun film(id: String, name: String) = JSONObject().put("Id", id).put("Name", name).put("Type", "Movie").put("MediaSources", source("src$id"))
@@ -260,8 +260,11 @@ class SelfHealingApprovalTest {
val asked = mutableListOf<String>()
val transport = EmbyTransport { r ->
asked += r.path
if ("AnyProviderIdEquals=imdb.tt0100814" in r.path) EmbyReply(200, JSONObject().put("Items", JSONArray().put(film("new1", "Tremors"))).toString())
else EmbyReply(200, JSONObject().put("Items", JSONArray()).toString()) // the old ID: nothing there
when {
"AnyProviderIdEquals=imdb.tt0100814" in r.path -> EmbyReply(200, JSONObject().put("Items", JSONArray().put(film("new1", "Tremors"))).toString())
"Ids=new1" in r.path -> EmbyReply(200, JSONObject().put("Items", JSONArray().put(film("new1", "Tremors"))).toString())
else -> EmbyReply(200, JSONObject().put("Items", JSONArray()).toString()) // the old ID: nothing there
}
}
val s = EmbySampleSession(transport, "fixture-token-private", "fixture-user", EmbySample(emptyList(), 0))
val read = s.readChannelPool(channel, null, ChannelChoices(listOf(Suggestion("old1", "Tremors", 1990, "Movie", "day", "", ref = "imdb.tt0100814"))))
@@ -269,6 +272,19 @@ class SelfHealingApprovalTest {
assertEquals("day", read.series.single().group)
assertEquals(mapOf("old1" to "new1"), read.refound)
assertEquals(1, asked.count { "AnyProviderIdEquals" in it })
// The repair is kept: the next read asks for the film by its new ID, with no lookup.
s.readChannelPool(channel, null, ChannelChoices(listOf(Suggestion("old1", "Tremors", 1990, "Movie", "day", "", ref = "imdb.tt0100814"))))
assertEquals(1, asked.count { "AnyProviderIdEquals" in it })
}
@Test fun aLookupThatFindsNothingIsntRepeatedDuringItsCooldown() {
val asked = mutableListOf<String>()
val transport = EmbyTransport { r -> asked += r.path; EmbyReply(200, JSONObject().put("Items", JSONArray()).toString()) }
val s = EmbySampleSession(transport, "fixture-token-private", "fixture-user", EmbySample(emptyList(), 0))
val choices = ChannelChoices(listOf(Suggestion("moved-1", "Lost Film", 1970, "Movie", "", "", ref = "imdb.tt7")))
repeat(3) { s.readChannelPool(channel, null, choices) }
assertEquals(1, asked.count { "AnyProviderIdEquals" in it })
assertTrue(asked.none { "Ids=moved-1" in it }) // a placeholder is never asked for
}
@Test fun anApprovalWithoutAnIdIsLeftAsItWas() {