Fix sparse request metadata hydration

This commit is contained in:
2026-08-31 15:21:47 +12:00
parent 372f4a1bfc
commit 82d87d968e
3 changed files with 162 additions and 2 deletions
+83 -1
View File
@@ -28,7 +28,12 @@ from backend.app.services.operation_progress import (
reset_operation,
start_remote_call,
)
from backend.app.services.snapshot import _build_presentation, _episode_availability, _torrent_progress
from backend.app.services.snapshot import (
_apply_arr_identity,
_build_presentation,
_episode_availability,
_torrent_progress,
)
def _build_request(ip: str = "127.0.0.1", user_agent: str = "backend-test") -> Request:
@@ -388,6 +393,27 @@ class RequestPresentationTests(unittest.TestCase):
class RequestCreationFlowTests(unittest.IsolatedAsyncioTestCase):
def test_sparse_seerr_request_is_enriched_with_media_lookup(self) -> None:
sparse = {
"id": 3925,
"type": "movie",
"status": 2,
"media": {"id": 2444, "mediaType": "movie", "tmdbId": 209112},
}
details = {
"title": "Batman v Superman: Dawn of Justice",
"releaseDate": "2016-03-23",
"posterPath": "/poster.jpg",
}
enriched = requests_router._merge_request_media_details(sparse, details)
parsed = requests_router._parse_request_payload(enriched)
self.assertEqual(parsed["title"], "Batman v Superman: Dawn of Justice")
self.assertEqual(parsed["year"], 2016)
self.assertEqual(enriched["media"]["posterPath"], "/poster.jpg")
self.assertNotIn("title", sparse["media"])
async def test_seerr_search_percent_encodes_multi_word_titles(self) -> None:
client = requests_router.JellyseerrClient("http://seerr.test", "key")
client.get = AsyncMock(return_value={"results": []})
@@ -623,6 +649,62 @@ class RequestRecheckTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(result["status"], "ok")
self.assertIs(result["snapshot"], snapshot)
async def test_recheck_hydrates_sparse_seerr_request_before_caching(self) -> None:
runtime = SimpleNamespace(jellyseerr_base_url="http://seerr.test", jellyseerr_api_key="key")
sparse_request = {
"id": 3925,
"type": "movie",
"status": 2,
"requestedBy": {"username": "viewer"},
"media": {"id": 2444, "mediaType": "movie", "tmdbId": 209112},
}
seerr = SimpleNamespace(
configured=lambda: True,
get_request=AsyncMock(return_value=sparse_request),
get_movie=AsyncMock(
return_value={
"title": "Batman v Superman: Dawn of Justice",
"releaseDate": "2016-03-23",
}
),
)
snapshot = Snapshot(
request_id="3925",
title="Batman v Superman: Dawn of Justice",
request_type=RequestType.movie,
state=NormalizedState.downloading,
presentation={"status": {"label": "Download in progress"}},
)
with patch.object(requests_router, "get_runtime_settings", return_value=runtime), patch.object(
requests_router, "JellyseerrClient", return_value=seerr
), patch.object(requests_router, "upsert_request_cache") as upsert, patch.object(
requests_router, "_cache_set"
) as cache_set, patch.object(requests_router, "_refresh_recent_cache_from_db"), patch.object(
requests_router, "build_snapshot", new=AsyncMock(return_value=snapshot)
), patch.object(requests_router, "save_action"):
await requests_router.action_recheck(
"3925", user={"username": "viewer", "role": "user"}
)
cached_record = upsert.call_args.kwargs
self.assertEqual(cached_record["title"], "Batman v Superman: Dawn of Justice")
cached_payload = cache_set.call_args.args[1]
self.assertEqual(cached_payload["media"]["title"], "Batman v Superman: Dawn of Justice")
class SnapshotIdentityTests(unittest.TestCase):
def test_radarr_identity_replaces_unknown_cached_title(self) -> None:
snapshot = Snapshot(request_id="3925", title="Unknown", request_type=RequestType.movie)
_apply_arr_identity(
snapshot,
{"title": "Batman v Superman: Dawn of Justice", "year": 2016},
)
self.assertEqual(snapshot.title, "Batman v Superman: Dawn of Justice")
self.assertEqual(snapshot.year, 2016)
class LiveDownloadProgressTests(unittest.IsolatedAsyncioTestCase):
async def test_live_download_progress_uses_saved_hash_and_current_qbittorrent_value(self) -> None: