From f33844be409cc31e185054d95ee6542f5eb32d73 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sat, 18 Apr 2026 12:32:54 -0400 Subject: [PATCH] Find local LaunchBox media for remote-only rom matches remote_media_req built a MediaRequest with platform_name=None, which made _build_local_media_context bail and never search on-disk Images/Manuals/Videos. Any rom that matched only via Metadata.xml (no local XML entry) ended up with images: [] even though LaunchBox art was sitting on disk. Thread platform_name and fs_name into remote_media_req, falling back to the remote entry's Platform field when no slug is available (e.g. lookups by database id). Co-Authored-By: Claude Opus 4.7 (1M context) --- .../metadata/launchbox_handler/handler.py | 2 + .../metadata/launchbox_handler/media.py | 12 ++- .../metadata/test_launchbox_handler.py | 100 ++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) diff --git a/backend/handler/metadata/launchbox_handler/handler.py b/backend/handler/metadata/launchbox_handler/handler.py index 414b185e5..850f667ae 100644 --- a/backend/handler/metadata/launchbox_handler/handler.py +++ b/backend/handler/metadata/launchbox_handler/handler.py @@ -162,6 +162,8 @@ class LaunchboxHandler(MetadataHandler): remote=index_entry, remote_images=remote_images, remote_enabled=remote_available, + platform_name=get_platform(platform_slug).get("name"), + fs_name=fs_name, ) return build_rom( diff --git a/backend/handler/metadata/launchbox_handler/media.py b/backend/handler/metadata/launchbox_handler/media.py index 475545d45..118e2abb8 100644 --- a/backend/handler/metadata/launchbox_handler/media.py +++ b/backend/handler/metadata/launchbox_handler/media.py @@ -54,11 +54,19 @@ def remote_media_req( remote: dict | None, remote_images: list[dict] | None, remote_enabled: bool, + platform_name: str | None = None, + fs_name: str = "", ) -> MediaRequest: title = ((remote or {}).get("Name") or "").strip() + # Without a platform_name, _build_local_media_context bails and local + # Images/Manuals/Videos never get searched. Fall back to the platform + # recorded on the remote entry so remote-matched ROMs can still surface + # on-disk media. + if not platform_name and remote: + platform_name = (remote.get("Platform") or "").strip() or None return MediaRequest( - None, - "", + platform_name, + fs_name, title, None, remote_images, diff --git a/backend/tests/handler/metadata/test_launchbox_handler.py b/backend/tests/handler/metadata/test_launchbox_handler.py index a630f2363..cdd27e808 100644 --- a/backend/tests/handler/metadata/test_launchbox_handler.py +++ b/backend/tests/handler/metadata/test_launchbox_handler.py @@ -24,6 +24,7 @@ from handler.metadata.launchbox_handler.media import ( build_launchbox_metadata, build_rom, populate_rom_specific_paths, + remote_media_req, ) from handler.metadata.launchbox_handler.platforms import get_platform from handler.metadata.launchbox_handler.remote_source import RemoteSource @@ -810,6 +811,105 @@ class TestPopulateRomSpecificPaths: assert "video_path" not in metadata +class TestRemoteMediaReq: + def test_explicit_platform_name_wins(self): + req = remote_media_req( + remote={"Name": "Super Mario Bros.", "Platform": "Wii"}, + remote_images=None, + remote_enabled=True, + platform_name="Nintendo Entertainment System", + fs_name="mario.nes", + ) + assert req.platform_name == "Nintendo Entertainment System" + assert req.fs_name == "mario.nes" + assert req.title == "Super Mario Bros." + + def test_falls_back_to_remote_platform(self): + req = remote_media_req( + remote={"Name": "H.E.R.O.", "Platform": "Atari 2600"}, + remote_images=None, + remote_enabled=True, + ) + assert req.platform_name == "Atari 2600" + assert req.fs_name == "" + assert req.title == "H.E.R.O." + + def test_no_platform_available_is_none(self): + req = remote_media_req( + remote={"Name": "Some Game"}, + remote_images=None, + remote_enabled=True, + ) + assert req.platform_name is None + + +class TestRemoteMatchLocalImages: + """Regression test for bug where remote-matched roms skipped local image lookup. + + When a ROM matches only via the remote Metadata.xml (no local XML entry), + the handler previously passed platform_name=None to remote_media_req, which + caused _build_local_media_context to bail and never search on-disk images. + """ + + async def test_remote_match_finds_local_images( + self, tmp_path: Path, monkeypatch + ) -> None: + # Arrange: images on disk, no local XML match, remote returns a hit. + # Use "Clear Logo" + "Box - Front" to exercise both _get_images and + # _get_cover through the same remote-match path. + lb_root = tmp_path / "launchbox" + images_root = lb_root / "Images" + logo_dir = images_root / "Atari 2600" / "Clear Logo" + box_dir = images_root / "Atari 2600" / "Box - Front" + logo_dir.mkdir(parents=True) + box_dir.mkdir(parents=True) + (logo_dir / "H.E.R.O-01.png").write_bytes(b"") + (box_dir / "H.E.R.O-01.png").write_bytes(b"") + + monkeypatch.setattr( + "handler.metadata.launchbox_handler.media.LAUNCHBOX_IMAGES_DIR", + images_root, + ) + monkeypatch.setattr( + "handler.metadata.launchbox_handler.utils.LAUNCHBOX_LOCAL_DIR", + lb_root, + ) + + h = LaunchboxHandler() + h._local = MagicMock(spec=LocalSource) + h._remote = MagicMock(spec=RemoteSource) + h._local.get_rom = AsyncMock(return_value=None) # type: ignore[method-assign] + h._remote.get_mame_entry = AsyncMock(return_value=None) # type: ignore[method-assign] + h._remote.get_rom = AsyncMock( # type: ignore[method-assign] + return_value={ + "DatabaseID": "42", + "Name": "H.E.R.O.", + "Platform": "Atari 2600", + } + ) + h._remote.fetch_images = AsyncMock(return_value=None) # type: ignore[method-assign] + monkeypatch.setattr(LaunchboxHandler, "is_enabled", lambda *_: True) + monkeypatch.setattr(async_cache, "exists", AsyncMock(return_value=True)) + + with patch( + "handler.metadata.launchbox_handler.handler.fs_rom_handler" + ) as mock_fs: + mock_fs.get_file_name_with_no_tags.return_value = "hero" + result = await h.get_rom("hero.a26", "atari2600") + + # Assert: both the clear logo and box-front cover resolved to + # launchbox-file:// URLs, even though the local XML never matched. + images = result["launchbox_metadata"]["images"] + assert len(images) == 1 + assert images[0]["type"] == "Clear Logo" + assert images[0]["url"] == ( + "launchbox-file://Images/Atari 2600/Clear Logo/H.E.R.O-01.png" + ) + assert result["url_cover"] == ( + "launchbox-file://Images/Atari 2600/Box - Front/H.E.R.O-01.png" + ) + + class TestBuildRom: def test_name_from_local_title(self): local = {"Title": "Super Mario Bros.", "Notes": "Classic platformer"}