Provider contract step 3: catalogue import through the contract behind a switch (off), read-only shadow comparison (0.110.6)

- ProviderSwitches.catalogue, off by default, set only from adb; today's import stays the fallback and is unchanged
  when the switch is off. The import loop gains a fetch parameter whose default is today's reader.
- CatalogueItem carries every field today's import stores, and Page a cursor (rows read, including skipped ones), so
  the contract path writes byte-identical catalogue records, positions and totals.
- CatalogueShadow: reads the same pages both ways, compares ids, every field, paging and totals, records time, heap
  and errors, never writes the catalogue; keeps sanitised copies of the first raw pages as fixtures.
- MainActivity reads adb requests in onNewIntent too, since Orbit is singleTask.

772 tests (one PhoneLink port flake on a first run, passed on re-run), lint clean, replay tests unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 21:47:40 +00:00
co-authored by Claude Opus 5.5
parent 3da2159429
commit ae62a0750f
14 changed files with 277 additions and 21 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ android {
applicationId = "tv.orbit.classic"
minSdk = 26
targetSdk = 36
versionCode = 280
versionName = "0.110.5-own-issues"
versionCode = 281
versionName = "0.110.6-provider-shadow"
}
buildFeatures { compose = true }
// The finished build for the TVs (Paul, 6 November 2026: the test build stuttered on the box). Not debuggable, so
@@ -76,7 +76,9 @@ internal fun fetchCataloguePage(transport: EmbyTransport, userId: String, token:
*/
internal fun importCatalogue(transport: EmbyTransport, userId: String, token: () -> String, sink: CatalogueSink,
maxPages: Int = CATALOGUE_PAGES_PER_RUN, pause: () -> Unit = {}, checkActive: () -> Unit = {}, onPage: () -> Unit = {},
backoff: (Int) -> Unit = {}, onRetry: (Int) -> Unit = {}): CatalogueRun {
backoff: (Int) -> Unit = {}, onRetry: (Int) -> Unit = {},
/** Where a page comes from: today's reader, unless the provider switch routes it through the contract (step 3). */
fetch: (start: Long, size: Int, withTotal: Boolean) -> CataloguePage = { start, size, withTotal -> fetchCataloguePage(transport, userId, token, start, size, withTotal) }): CatalogueRun {
require(maxPages in 1..CATALOGUE_PAGES_FULL_RUN)
var start = sink.position(userId)
var pages = 0
@@ -91,7 +93,7 @@ internal fun importCatalogue(transport: EmbyTransport, userId: String, token: ()
val began = System.currentTimeMillis()
val page = try {
// A retry skips the count, in case the count is what's slow.
fetchCataloguePage(transport, userId, token, start, size, pages == 0 && failures == 0).also { if (it.total != null) total = it.total }
fetch(start, size, pages == 0 && failures == 0).also { if (it.total != null) total = it.total }
} catch (failure: EmbyCheckException) {
if (!failure.retryable) throw failure
if (++failures >= CATALOGUE_MAX_ATTEMPTS)
@@ -116,3 +118,19 @@ internal fun importCatalogue(transport: EmbyTransport, userId: String, token: ()
}
return result(false)
}
/**
* A catalogue page read through the provider contract (step 3, 8 November 2026), shaped exactly as today's reader
* returns it, so the same import loop (resume, smaller pages on a slow answer, back-off) runs unchanged. The contract's
* failures go back to the kinds that loop knows: unreachable is retryable, a sign-in failure stops it as a 401 does.
*/
internal fun cataloguePageVia(provider: MediaProvider, start: Long, size: Int, withTotal: Boolean): CataloguePage = try {
val page = kotlinx.coroutines.runBlocking { provider.catalogue(PageRequest(start.toInt(), size, withTotal)) }
CataloguePage(page.items.map(::catalogueRecordOf), (page.next - start.toInt()).coerceAtLeast(0), page.total.takeIf { withTotal && it >= 0 }?.toLong())
} catch (f: ProviderException) {
throw when (f.error) {
ProviderError.Unreachable -> EmbyCheckException(f.message.orEmpty(), retryable = true)
ProviderError.Authentication -> EmbyCheckException(f.message.orEmpty(), 401)
else -> EmbyCheckException(f.message.orEmpty())
}
}
@@ -0,0 +1,108 @@
package tv.orbit.classic
import android.content.Context
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
/**
* The provider switches (step 3, 8 November 2026). Each routes one existing path through the provider contract; all are
* off by default, and today's path stays the immediate fallback. Set from adb only, kept in Orbit's settings:
* am start -n tv.orbit.classic/.MainActivity --es orbit_provider_catalogue on|off
*/
internal object ProviderSwitches {
@Volatile var catalogue = false
private const val KEY = "provider_catalogue"
fun load(context: Context) { catalogue = context.getSharedPreferences("orbit_settings", Context.MODE_PRIVATE).getBoolean(KEY, false) }
fun setCatalogue(context: Context, on: Boolean) {
catalogue = on; context.getSharedPreferences("orbit_settings", Context.MODE_PRIVATE).edit().putBoolean(KEY, on).apply()
android.util.Log.i("OrbitProvider", "catalogue switch=${if (on) "on" else "off"}")
}
}
/**
* The read-only shadow comparison (step 3): today's catalogue reader and the provider path read the same pages side by
* side, and every page is compared (ids, every stored field, paging, totals). It never writes the live catalogue: nothing
* here touches CatalogueStore. It records time, memory and errors, writes a report, and keeps a sanitised copy of the
* first raw pages (titles, people and ids outside Orbit replaced, structure kept) as a test fixture. Asked for from adb:
* am start -n tv.orbit.classic/.MainActivity --ei orbit_shadow_catalogue <pages, 0 = the whole library>
* The report lands in Orbit's shared folder, shadow-catalogue/, for adb to pull. Counts and ids only; no titles.
*/
internal object CatalogueShadow {
/** Pages asked for; null when no comparison is waiting. */
@Volatile var pending: Int? = null
private const val FIXTURE_PAGES = 3
fun runIfPending(session: EmbySampleSession, context: Context) {
val pages = pending ?: return
pending = null
val out = File(context.getExternalFilesDir(null), "shadow-catalogue").apply { deleteRecursively(); mkdirs() }
val raw = mutableListOf<String>()
val report = compare(
today = { start, size, withTotal -> session.cataloguePageRecorded(start, size, withTotal) { if (raw.size < FIXTURE_PAGES) raw += it } },
viaProvider = { start, size, withTotal -> cataloguePageVia(session.provider(), start, size, withTotal) },
maxPages = if (pages <= 0) CATALOGUE_PAGES_FULL_RUN else pages)
File(out, "report.json").writeText(report.toString(2))
raw.forEachIndexed { i, body -> File(out, "page-$i.json").writeText(sanitiseCataloguePage(body).toString()) }
android.util.Log.i("OrbitProvider", "shadow catalogue pages=${report.optInt("pages")} same=${report.optBoolean("identical")} mismatches=${report.optInt("mismatches")}")
}
/**
* Reads [maxPages] pages from both paths, the same way today's import does (counted first page, CATALOGUE_PAGE_SIZE rows),
* and compares them. Pure: pass any two readers. The report holds counts, timings, memory and the first mismatching ids.
*/
fun compare(today: (Long, Int, Boolean) -> CataloguePage, viaProvider: (Long, Int, Boolean) -> CataloguePage, maxPages: Int,
heapUsed: () -> Long = { Runtime.getRuntime().let { it.totalMemory() - it.freeMemory() } }): JSONObject {
var start = 0L; var pages = 0; var mismatches = 0; var records = 0
var todayMs = 0L; var providerMs = 0L; var peakHeap = heapUsed(); val heapBefore = peakHeap
val firstMismatches = JSONArray(); var error: String? = null; var total: Long? = null
while (pages < maxPages) {
val withTotal = pages == 0
val a: CataloguePage; val b: CataloguePage
try {
var t = System.nanoTime(); a = today(start, CATALOGUE_PAGE_SIZE, withTotal); todayMs += (System.nanoTime() - t) / 1_000_000
t = System.nanoTime(); b = viaProvider(start, CATALOGUE_PAGE_SIZE, withTotal); providerMs += (System.nanoTime() - t) / 1_000_000
} catch (f: Exception) { error = "${f.javaClass.simpleName} at position $start"; break }
peakHeap = maxOf(peakHeap, heapUsed())
if (withTotal) total = a.total
val same = a.rawCount == b.rawCount && a.total == b.total && a.records == b.records
if (!same) {
mismatches++
if (firstMismatches.length() < 20) firstMismatches.put(JSONObject().put("position", start).put("rawToday", a.rawCount).put("rawProvider", b.rawCount)
.put("totalToday", a.total ?: JSONObject.NULL).put("totalProvider", b.total ?: JSONObject.NULL)
.put("ids", JSONArray((a.records.map { it.id } + b.records.map { it.id }).distinct().filter { id ->
a.records.firstOrNull { it.id == id } != b.records.firstOrNull { it.id == id } }.take(10))))
}
records += a.records.size; pages++
start += a.rawCount
if (a.rawCount == 0 || total?.let { start >= it } == true) break
}
return JSONObject().put("pages", pages).put("records", records).put("positionReached", start).put("total", total ?: JSONObject.NULL)
.put("identical", mismatches == 0 && error == null).put("mismatches", mismatches).put("firstMismatches", firstMismatches)
.put("todayMs", todayMs).put("providerMs", providerMs).put("heapBeforeKb", heapBefore / 1024).put("heapPeakKb", peakHeap / 1024)
.put("error", error ?: JSONObject.NULL).put("wroteCatalogue", false)
}
}
/**
* A raw catalogue page with the personal parts replaced and its structure kept (step 3 fixtures): every row, key and type
* stays; titles become "Title n", original titles and tags keep their shape with neutral words, external ids keep their
* keys with digits of the same length, and fields the import doesn't read are dropped.
*/
internal fun sanitiseCataloguePage(body: String): JSONObject {
val page = JSONObject(body)
val rows = page.optJSONArray("Items") ?: JSONArray()
val clean = JSONArray()
val keep = setOf("Id", "Type", "Name", "ProductionYear", "OfficialRating", "Genres", "Studios", "ProviderIds", "PremiereDate", "RunTimeTicks", "OriginalTitle", "Tags", "TagItems")
for (i in 0 until rows.length()) {
val row = rows.optJSONObject(i) ?: run { clean.put(rows.opt(i)); null } ?: continue
val out = JSONObject()
for (key in row.keys()) if (key in keep) out.put(key, row.get(key))
if (out.has("Name")) out.put("Name", "Title ${i + 1}")
if (out.optString("OriginalTitle").isNotBlank()) out.put("OriginalTitle", "Original title ${i + 1}")
out.optJSONObject("ProviderIds")?.let { ids -> val fake = JSONObject(); ids.keys().forEach { k -> fake.put(k, (ids.opt(k) as? String)?.map { c -> if (c.isDigit()) '7' else c }?.joinToString("") ?: ids.opt(k)) }; out.put("ProviderIds", fake) }
for (tagKey in listOf("Tags", "TagItems")) out.optJSONArray(tagKey)?.let { a -> out.put(tagKey, JSONArray((0 until a.length()).map { j -> if (a.opt(j) is JSONObject) JSONObject().put("Name", "tag${j + 1}") else "tag${j + 1}" })) }
clean.put(out)
}
return JSONObject().put("Items", clean).apply { if (page.has("TotalRecordCount")) put("TotalRecordCount", page.get("TotalRecordCount")) }
}
@@ -72,7 +72,8 @@ internal class ChannelScheduler(private val store: ScheduleStore,
catalogue.write(userId, records, nextStart, total, finished); found += records
}
}
val run = session.importCatalogue(sink, ARRIVALS_REFRESH_PAGES)
// Through the provider contract only when its switch is on (step 3, off by default); today's path stays the fallback.
val run = if (ProviderSwitches.catalogue) session.importCatalogueViaProvider(sink, ARRIVALS_REFRESH_PAGES) else session.importCatalogue(sink, ARRIVALS_REFRESH_PAGES)
val arrivals = if (baseline) found.map { arrivalOf(it, now) } else emptyList()
store.saveArrivals(userId, mergeArrivals(kept, arrivals, now), now)
Log.i("OrbitSchedule", "arrivals pages=${run.pages} read=${found.size} new=${arrivals.size} baseline=$baseline")
@@ -62,9 +62,9 @@ internal class HttpsEmbyTransport(internal val deviceId: String = UUID.randomUUI
}
/** The sign-in headers for this kind of server. */
internal fun authHeaders(token: String?): Map<String, String> = when (kind) {
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.110.5\"") +
ServerKind.Emby -> mapOf("X-Emby-Authorization" to "Emby Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.110.6\"") +
(token?.let { mapOf("X-Emby-Token" to it) } ?: emptyMap())
ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.110.5\"" +
ServerKind.Jellyfin -> mapOf("Authorization" to "MediaBrowser Client=\"Orbit TV\", Device=\"Orbit TV\", DeviceId=\"$deviceId\", Version=\"0.110.6\"" +
(token?.let { ", Token=\"$it\"" } ?: ""))
}
/** One picture from the provider, or nothing. Same fixed origin and no redirects, and failures are never shown or logged. */
@@ -386,6 +386,11 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
runCatching { withContext(Dispatchers.IO) { Taste.watching(channel.number, show, channel.seriesIdAt(show.startMs), at - show.startMs, now) } }
}
}
// The read-only catalogue shadow comparison (provider contract step 3), when asked for from adb. Never writes the catalogue.
LaunchedEffect(session) {
val active = session ?: return@LaunchedEffect
while (true) { delay(10_000); if (CatalogueShadow.pending != null) withContext(Dispatchers.IO) { runCatching { CatalogueShadow.runIfPending(active, appContext) } } }
}
// Rooms sync (7 November 2026): every two minutes while Orbit is open, with each paired room it can reach on the Wi-Fi.
LaunchedEffect(workspace) {
val finder = RoomFinder(appContext); finder.start()
@@ -454,6 +454,24 @@ internal class EmbySampleSession(private val transport: EmbyTransport, private v
fun importCatalogue(sink: CatalogueSink, maxPages: Int = CATALOGUE_PAGES_PER_RUN, pause: () -> Unit = {}, checkActive: () -> Unit = {}, onPage: () -> Unit = {},
backoff: (Int) -> Unit = {}, onRetry: (Int) -> Unit = {}): CatalogueRun =
importCatalogue(transport, userId, ::currentToken, sink, maxPages, pause, checkActive, onPage, backoff, onRetry)
/**
* This library behind the provider contract (step 3, 8 November 2026). Its id is the server's own, read once from
* /System/Info/Public; if that can't be read, a hash of the address stands in until it can.
*/
fun provider(images: (String) -> ByteArray? = ::fetchArtwork): EmbyProvider = EmbyProvider(this, transport, userId, ::currentToken,
if (kind == ServerKind.Jellyfin) ProviderKind.Jellyfin else ProviderKind.Emby, serverId, images)
private val serverId: String by lazy {
runCatching { EmbyClient.parse(transport.execute(EmbyRequest("GET", "/System/Info/Public")).body).optString("Id") }.getOrNull()?.takeIf(EmbyClient::validId)
?: ("addr-" + java.security.MessageDigest.getInstance("SHA-256").digest(origin.toByteArray()).take(8).joinToString("") { "%02x".format(it) })
}
/** Today's catalogue reader for the shadow comparison, handing each raw reply to [onRaw] (sanitised before it's kept). */
fun cataloguePageRecorded(start: Long, size: Int, withTotal: Boolean, onRaw: (String) -> Unit): CataloguePage =
fetchCataloguePage(EmbyTransport { r -> transport.execute(r).also { if (it.status in 200..299) onRaw(it.body) } }, userId, ::currentToken, start, size, withTotal)
/** Today's import, with pages read through the contract instead (step 3; off unless ProviderSwitches.catalogue is on). */
fun importCatalogueViaProvider(sink: CatalogueSink, maxPages: Int = CATALOGUE_PAGES_PER_RUN): CatalogueRun {
val p = provider()
return importCatalogue(transport, userId, ::currentToken, sink, maxPages, fetch = { start, size, withTotal -> cataloguePageVia(p, start, size, withTotal) })
}
fun checkAfterDarkMovies(genre: String): List<AfterDarkMovie> {
val found = discoverAfterDarkMovies(transport, userId, ::currentToken, genre)
currentToken()
@@ -31,11 +31,8 @@ internal class EmbyProvider(
override suspend fun catalogue(page: PageRequest): Page<CatalogueItem> = guarded {
// Today's reader, unchanged: oldest first, Movie and Series only, at most CATALOGUE_PAGE_SIZE a page.
val read = fetchCataloguePage(transport, userId, token, page.start.toLong(), minOf(page.limit, CATALOGUE_PAGE_SIZE), withTotal = true)
Page(read.records.map { r ->
CatalogueItem(ItemId(r.id), if (r.type == "Series") MediaType.Series else MediaType.Movie, r.title, r.year, r.runtimeMs,
r.genres, r.rating.ifBlank { null }, r.premiere.ifBlank { null })
}, (read.total ?: 0L).toInt())
val read = fetchCataloguePage(transport, userId, token, page.start.toLong(), minOf(page.limit, CATALOGUE_PAGE_SIZE), page.withTotal)
Page(read.records.map(::catalogueItemOf), (read.total ?: -1L).toInt(), page.start + read.rawCount)
}
override suspend fun episodes(seriesId: ItemId, includeSpecials: Boolean): List<Episode> = guarded {
@@ -123,3 +120,10 @@ internal fun providerErrorOf(f: Throwable, playback: Boolean = false): ProviderE
ProviderException(ProviderError.UnsupportedPlayback, "This programme can't be played directly on this TV.")
else -> ProviderException(ProviderError.InvalidResponse, "The library sent something Orbit couldn't read.")
}
/** Today's catalogue row as the contract's item, and back: every field kept, so the round trip is exact. */
internal fun catalogueItemOf(r: CatalogueRecord) = CatalogueItem(ItemId(r.id), if (r.type == "Series") MediaType.Series else MediaType.Movie, r.title, r.year,
r.runtimeMs, r.genres, r.rating.ifBlank { null }, r.premiere.ifBlank { null }, r.studios, r.providerIds, r.originalTitle.ifBlank { null }, r.tags)
internal fun catalogueRecordOf(i: CatalogueItem) = CatalogueRecord(i.id.value, if (i.type == MediaType.Series) "Series" else "Movie", i.title, i.year,
i.rating.orEmpty(), i.genres, i.studios, i.externalIds, i.premiere.orEmpty(), i.runtimeMs, i.originalTitle.orEmpty(), i.tags)
@@ -58,6 +58,19 @@ class MainActivity : ComponentActivity() {
// was in another app), and paired rooms keep syncing; the phone says Orbit isn't on screen instead of failing to load.
override fun onDestroy() { InstallSnapshot.screenOpen = false; super.onDestroy() }
override fun onStop() { OrbitCompanion.onTv = false; PhoneLink.offerRoom(null); if (PhoneLink.phones.isEmpty() && RoomLinks.links.none { it.on }) { RoomBeacon.off(this); PhoneLink.stop() }; super.onStop() }
/**
* The adb-only requests (inspection, evidence, the provider switch and shadow comparison). Read at start and, since Orbit
* opens as one copy (singleTask, 8 November 2026), also when adb sends them to an Orbit already open.
*/
private fun readAdbRequests(intent: android.content.Intent?) {
if (intent?.getBooleanExtra("orbit_intro_test", false) == true) IntroTrial.nextTune = true
intent?.getStringExtra("orbit_inspect_series")?.take(120)?.let { SeriesInspection.title = it }
if (intent?.getBooleanExtra("orbit_read_evidence", false) == true) EvidenceRequest.pending = true
// Provider contract, step 3: the catalogue switch and the read-only shadow comparison.
when (intent?.getStringExtra("orbit_provider_catalogue")) { "on" -> ProviderSwitches.setCatalogue(this, true); "off" -> ProviderSwitches.setCatalogue(this, false) }
if (intent?.hasExtra("orbit_shadow_catalogue") == true) CatalogueShadow.pending = intent.getIntExtra("orbit_shadow_catalogue", 5)
}
override fun onNewIntent(intent: android.content.Intent) { super.onNewIntent(intent); setIntent(intent); readAdbRequests(intent) }
override fun onCreate(savedInstanceState: Bundle?) {
InstallSnapshot.screenOpen = true
OrbitCompanion.activity = java.lang.ref.WeakReference(this)
@@ -66,9 +79,8 @@ class MainActivity : ComponentActivity() {
Motion.load(OrbitSettings(this))
// The guide's logos, decoded ahead on a background thread so the guide opens with them ready.
Thread({ Pictures.warmLogos(resources) }, "orbit-logos").apply { priority = Thread.MIN_PRIORITY }.start()
if (intent?.getBooleanExtra("orbit_intro_test", false) == true) IntroTrial.nextTune = true
intent?.getStringExtra("orbit_inspect_series")?.take(120)?.let { SeriesInspection.title = it }
if (intent?.getBooleanExtra("orbit_read_evidence", false) == true) EvidenceRequest.pending = true
ProviderSwitches.load(this)
readAdbRequests(intent)
// The hand-run playback test opens over Orbit, which stops in the background as it would for any other app, and comes back when it ends.
intent?.getStringExtra("orbit_play_test")?.takeIf(EmbyClient::validId)?.let { item ->
startActivity(android.content.Intent(this, PlaybackTestActivity::class.java).putExtra(PlaybackTestActivity.ITEM, item))
@@ -82,15 +82,26 @@ internal fun validMediaId(value: String): Boolean =
/** Media named everywhere it could leave its own library (room sync, TV Times): never confused with another library's. */
internal data class MediaRef(val library: LibraryId, val item: ItemId, val source: SourceId? = null)
internal data class PageRequest(val start: Int, val limit: Int) {
/** [withTotal]: whether to count the whole library too; costly on a big server, so a long read asks only on its first page. */
internal data class PageRequest(val start: Int, val limit: Int, val withTotal: Boolean = true) {
init { require(start >= 0 && limit in 1..PAGE_MAX) }
companion object { const val PAGE_MAX = 500 }
}
internal data class Page<T>(val items: List<T>, val total: Int)
/**
* One page. [next] is where the following page starts: the rows the server returned, including any Orbit skipped (an item
* with no title, say), so a resumed read never re-reads or misses rows. [total] is -1 when it wasn't asked for.
*/
internal data class Page<T>(val items: List<T>, val total: Int, val next: Int = -1)
/**
* A film or series as the local catalogue keeps it (step 3, 8 November 2026: the fields today's import stores, so the
* contract path writes exactly the same catalogue rows). [externalIds] are the library's IMDb, TMDb and similar ids.
*/
internal data class CatalogueItem(val id: ItemId, val type: MediaType, val title: String, val year: Int? = null, val runtimeMs: Long? = null,
val genres: List<String> = emptyList(), val rating: String? = null, val premiere: String? = null)
val genres: List<String> = emptyList(), val rating: String? = null, val premiere: String? = null,
val studios: List<String> = emptyList(), val externalIds: Map<String, String> = emptyMap(), val originalTitle: String? = null,
val tags: List<String> = emptyList())
internal data class Episode(val id: ItemId, val seriesId: ItemId, val season: Int, val episode: Int, val title: String,
val runtimeMs: Long? = null, val sources: List<SourceId> = emptyList())
@@ -39,7 +39,7 @@ internal class SecureMediaDataSource(private val session: EmbySampleSession) : B
next.instanceFollowRedirects = false
next.connectTimeout = 10_000
next.readTimeout = 20_000
next.setRequestProperty("User-Agent", "OrbitTV/0.110.5")
next.setRequestProperty("User-Agent", "OrbitTV/0.110.6")
next.setRequestProperty("Accept-Encoding", "identity")
session.mediaHeaders(address).forEach { (key, value) -> next.setRequestProperty(key, value) }
if (dataSpec.position != 0L || dataSpec.length != C.LENGTH_UNSET.toLong()) {
@@ -0,0 +1,79 @@
package tv.orbit.classic
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Test
/** Step 3 (8 November 2026): the catalogue import through the contract writes exactly what today's import writes. */
class CatalogueViaProviderTest {
/** A library with the awkward rows real servers have: an episode in the list, a row with no title, a bad id. */
private class Awkward : EmbyTransport {
val rows = (1..37).map { i -> JSONObject().put("Id", "item$i").put("Type", if (i % 5 == 0) "Series" else "Movie").put("Name", "Fictional $i")
.put("ProductionYear", 1980 + i).put("RunTimeTicks", (40L + i) * 60_000 * 10_000).put("OfficialRating", if (i % 3 == 0) "" else "15")
.put("Genres", JSONArray().put("Drama")).put("Studios", JSONArray().put(JSONObject().put("Name", "Studio $i")))
.put("ProviderIds", JSONObject().put("Imdb", "tt00$i").put("Tmdb", "$i")).put("PremiereDate", "${1980 + i}-01-01T00:00:00Z")
.put("OriginalTitle", if (i % 7 == 0) "Original $i" else "").put("Tags", JSONArray().put("tag$i")) } +
listOf(JSONObject().put("Id", "ep1").put("Type", "Episode").put("Name", "An episode"), JSONObject().put("Id", "untitled").put("Type", "Movie"),
JSONObject().put("Id", "../bad").put("Type", "Movie").put("Name", "Bad id"))
override fun execute(request: EmbyRequest): EmbyReply {
val q = { k: String -> Regex("[?&]$k=([^&]*)").find(request.path)!!.groupValues[1] }
val start = q("StartIndex").toInt(); val limit = q("Limit").toInt()
return EmbyReply(200, JSONObject().put("Items", JSONArray(rows.drop(start).take(limit))).put("TotalRecordCount", if (request.path.contains("EnableTotalRecordCount=false")) 0 else rows.size).toString())
}
}
private class Capture : CatalogueSink {
var pos = 0L; val writes = mutableListOf<List<Any?>>()
override fun position(userId: String) = pos
override fun write(userId: String, records: List<CatalogueRecord>, nextStart: Long, total: Long?, finished: Boolean) { writes += listOf(records, nextStart, total, finished); pos = nextStart }
}
private fun provider(t: EmbyTransport) = EmbyProvider(EmbySampleSession(t, "fixture-token-private", "fixtureuser", EmbySample(emptyList(), 0)), t, "fixtureuser",
{ "fixture-token-private" }, ProviderKind.Emby, "fixtureserver1", images = { null })
@Test fun importThroughTheContractWritesTheSameRecordsPositionsAndTotals() {
for (pageSize in listOf(5, 200)) {
val t = Awkward(); val today = Capture(); val via = Capture(); val p = provider(t)
importCatalogue(t, "fixtureuser", { "fixture-token-private" }, today, maxPages = 50, fetch = { s, _, w -> fetchCataloguePage(t, "fixtureuser", { "x" }, s, pageSize, w) })
importCatalogue(t, "fixtureuser", { "fixture-token-private" }, via, maxPages = 50, fetch = { s, _, w -> cataloguePageVia(p, s, pageSize, w) })
assertEquals(today.writes, via.writes)
assertEquals(37, today.writes.sumOf { (it[0] as List<*>).size })
}
}
@Test fun theShadowComparisonFindsThemIdenticalAndNeverWrites() {
val t = Awkward(); val p = provider(t)
val r = CatalogueShadow.compare({ s, n, w -> fetchCataloguePage(t, "fixtureuser", { "x" }, s, n, w) }, { s, n, w -> cataloguePageVia(p, s, n, w) }, 50)
assertTrue(r.getBoolean("identical")); assertEquals(37, r.getInt("records")); assertEquals(40L, r.getLong("positionReached"))
assertFalse(r.getBoolean("wroteCatalogue"))
assertTrue(r.has("todayMs") && r.has("providerMs") && r.has("heapPeakKb"))
}
@Test fun theShadowComparisonReportsADifferenceByIdOnly() {
val t = Awkward(); val p = provider(t)
val r = CatalogueShadow.compare({ s, n, w -> fetchCataloguePage(t, "fixtureuser", { "x" }, s, n, w) },
{ s, n, w -> cataloguePageVia(p, s, n, w).let { pg -> CataloguePage(pg.records.map { if (it.id == "item3") it.copy(year = 1900) else it }, pg.rawCount, pg.total) } }, 50)
assertFalse(r.getBoolean("identical")); assertEquals(1, r.getInt("mismatches"))
assertEquals("item3", r.getJSONArray("firstMismatches").getJSONObject(0).getJSONArray("ids").getString(0))
assertFalse(r.toString().contains("Fictional")) // no titles in the report
}
@Test fun theShadowComparisonRecordsAnErrorInsteadOfFailing() {
val t = Awkward()
val r = CatalogueShadow.compare({ s, n, w -> fetchCataloguePage(t, "fixtureuser", { "x" }, s, n, w) }, { _, _, _ -> throw EmbyCheckException("down", retryable = true) }, 5)
assertFalse(r.getBoolean("identical")); assertTrue(r.getString("error").startsWith("EmbyCheckException"))
}
@Test fun sanitisingKeepsStructureAndRemovesTitles() {
val raw = JSONObject().put("Items", JSONArray(Awkward().rows)).put("TotalRecordCount", 40).toString()
val clean = sanitiseCataloguePage(raw)
assertEquals(40, clean.getJSONArray("Items").length()); assertEquals(40, clean.getInt("TotalRecordCount"))
assertFalse(clean.toString().contains("Fictional"))
val before = fetchCataloguePage(EmbyTransport { EmbyReply(200, raw) }, "fixtureuser", { "x" }, 0, 200)
val after = fetchCataloguePage(EmbyTransport { EmbyReply(200, clean.toString()) }, "fixtureuser", { "x" }, 0, 200)
assertEquals(before.records.map { Triple(it.id, it.type, it.runtimeMs) }, after.records.map { Triple(it.id, it.type, it.runtimeMs) })
assertEquals(before.rawCount, after.rawCount)
assertEquals("tt777", after.records.first { it.id == "item7" }.providerIds["Imdb"]) // "tt007": same length, digits replaced
}
@Test fun theSwitchStartsOff() { assertFalse(ProviderSwitches.catalogue) }
}
@@ -34,7 +34,7 @@ internal object ProviderContractChecks {
private fun check(ok: Boolean, what: () -> String) { if (!ok) throw AssertionError(what()) }
private fun everything(p: MediaProvider, pageSize: Int = 7): List<CatalogueItem> = runBlocking {
val out = ArrayList<CatalogueItem>(); var start = 0
while (true) { val page = p.catalogue(PageRequest(start, pageSize)); out += page.items; start += page.items.size; if (page.items.isEmpty() || start >= page.total) break }
while (true) { val page = p.catalogue(PageRequest(start, pageSize)); out += page.items; start = if (page.next >= 0) page.next else start + page.items.size; if (page.items.isEmpty() || start >= page.total) break }
out
}
@@ -37,7 +37,7 @@ internal class ReferenceProvider(private val flaw: Flaw = Flaw.None, private val
fail(); reads++
val list = if (flaw == Flaw.UnstableIds && reads % 2 == 0) items.map { it.copy(id = ItemId(it.id.value + "x")) } else items
val from = if (flaw == Flaw.PagingRepeats) maxOf(0, page.start - 1) else page.start
return Page(list.drop(from).take(page.limit), list.size)
return Page(list.drop(from).take(page.limit), list.size, from + minOf(page.limit, maxOf(0, list.size - from)))
}
override suspend fun episodes(seriesId: ItemId, includeSpecials: Boolean): List<Episode> {
fail()