From ef57ee300d8ffd5326160c8663bb685cd9dbce71 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 1 Oct 2026 04:12:16 +0000 Subject: [PATCH] fix(scan): count every metadata source when deciding a ROM is identified The not-identified check in scan_rom listed 13 provider ids by hand and missed tgdb_id and libretro_id, so a ROM matched only by Libretro (or by a TGDB id from Hasheous) took the early return: it skipped the SteamGridDB lookup and missing_from_fs reset, while Rom.is_identified counted it as matched. Derive the check from METADATA_SOURCE_COLUMNS instead. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XZW9ChvmiTKXYYWY6JvTKU --- backend/handler/scan_handler.py | 24 +++---- backend/tests/handler/test_scan_identified.py | 69 +++++++++++++++++++ 2 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 backend/tests/handler/test_scan_identified.py diff --git a/backend/handler/scan_handler.py b/backend/handler/scan_handler.py index e90ba7f52..0d9cc5a5b 100644 --- a/backend/handler/scan_handler.py +++ b/backend/handler/scan_handler.py @@ -77,7 +77,13 @@ from logger.logger import log from models.assets import MemoryCardVersion, Save, Screenshot, State from models.firmware import Firmware from models.platform import Platform -from models.rom import Rom, RomFile, RomFileCategory, RomIdentity +from models.rom import ( + METADATA_SOURCE_COLUMNS, + Rom, + RomFile, + RomFileCategory, + RomIdentity, +) from models.user import User from utils import emoji from utils.audio_tags import persist_embedded_cover, remove_persisted_cover @@ -1616,20 +1622,8 @@ async def scan_rom( rom_attrs["url_cover"] = pico8_url # If not found in any metadata source, we return the rom with the default values - if ( - not rom_attrs.get("igdb_id") - and not rom_attrs.get("moby_id") - and not rom_attrs.get("ss_id") - and not rom_attrs.get("ra_id") - and not rom_attrs.get("launchbox_id") - and not rom_attrs.get("hasheous_id") - and not rom_attrs.get("flashpoint_id") - and not rom_attrs.get("hltb_id") - and not rom_attrs.get("demozoo_id") - and not rom_attrs.get("pouet_id") - and not rom_attrs.get("csdb_id") - and not rom_attrs.get("steam_id") - and not rom_attrs.get("gamelist_id") + if not any( + rom_attrs.get(column.key) for column in METADATA_SOURCE_COLUMNS.values() ): if inconclusive_sources: # Reporting a plain "not identified" here writes the ROM up as a diff --git a/backend/tests/handler/test_scan_identified.py b/backend/tests/handler/test_scan_identified.py new file mode 100644 index 000000000..6bcdf8a44 --- /dev/null +++ b/backend/tests/handler/test_scan_identified.py @@ -0,0 +1,69 @@ +"""Which provider matches count as an identified ROM during a scan. + +A scan that finds no match returns early and skips the SteamGridDB lookup, so +every source `Rom.is_identified` counts must keep the scan going. +""" + +from unittest.mock import AsyncMock, patch + +import pytest +from tests.handler.scan_stubs import add_n64_platform, add_rom, run_scan + +from handler.metadata.libretro_handler import LibretroRom +from handler.metadata.sgdb_handler import SGDBRom +from handler.scan_handler import MetadataSource, ScanType + +FS_NAME = "Mario Kart 64 (USA).z64" + + +@pytest.fixture +def sgdb_lookup(): + with patch( + "handler.scan_handler.meta_sgdb_handler.get_details_by_names", + new=AsyncMock(return_value=SGDBRom(sgdb_id=None)), + ) as lookup: + yield lookup + + +async def test_libretro_only_match_counts_as_identified(sgdb_lookup): + platform = add_n64_platform() + rom = add_rom(platform, FS_NAME, "Mario Kart 64") + + with patch( + "handler.scan_handler.meta_libretro_handler.get_rom", + new=AsyncMock( + return_value=LibretroRom( + libretro_id="Mario Kart 64 (USA)", name="Mario Kart 64" + ) + ), + ): + result = await run_scan( + platform, + rom, + scan_type=ScanType.COMPLETE, + metadata_sources=[MetadataSource.LIBRETRO, MetadataSource.SGDB], + ) + + assert result.libretro_id == "Mario Kart 64 (USA)" + assert result.is_identified + assert result.missing_from_fs is False + sgdb_lookup.assert_awaited_once() + + +async def test_unmatched_rom_skips_the_sgdb_lookup(sgdb_lookup): + platform = add_n64_platform() + rom = add_rom(platform, FS_NAME, "Mario Kart 64") + + with patch( + "handler.scan_handler.meta_libretro_handler.get_rom", + new=AsyncMock(return_value=LibretroRom(libretro_id=None)), + ): + result = await run_scan( + platform, + rom, + scan_type=ScanType.COMPLETE, + metadata_sources=[MetadataSource.LIBRETRO, MetadataSource.SGDB], + ) + + assert not result.is_identified + sgdb_lookup.assert_not_awaited()