diff --git a/backend/handler/scan_handler.py b/backend/handler/scan_handler.py index 473f928e3..8b53100fe 100644 --- a/backend/handler/scan_handler.py +++ b/backend/handler/scan_handler.py @@ -585,6 +585,10 @@ async def scan_rom( # no id for it never enters the set, so a rescan can't clear what it can't redo. attempted_sources: set[MetadataSource] = set() + # Sources this scan asked and never got an answer from. A miss they did not + # rule out is not a coverage gap, so the outcome must not be reported as one. + inconclusive_sources: set[MetadataSource] = set() + def resolve_fetch(source: MetadataSource, result: Any, fallback: Any) -> Any: """Unwrap a gathered lookup, falling back to an empty match when it failed.""" if not isinstance(result, BaseException): @@ -595,6 +599,7 @@ async def scan_rom( # A provider that blew up ruled nothing out, so it no longer counts as # attempted and a complete rescan keeps the id it already had. attempted_sources.discard(source) + inconclusive_sources.add(source) log.error( f"Error fetching {hl(source)} metadata for {hl(rom_attrs['fs_name'])}: {result}", extra=LOGGER_MODULE_NAME, @@ -651,9 +656,14 @@ async def scan_rom( ) ) ): - return await meta_hasheous_handler.lookup_rom( + match, conclusive = await meta_hasheous_handler.lookup_rom( platform.slug, get_match_files() ) + # Hasheous swallows its own failures, so an empty match that is not + # conclusive is the only sign the lookup never got an answer. + if not conclusive: + inconclusive_sources.add(MetadataSource.HASHEOUS) + return match, conclusive return ( HasheousRom(hasheous_id=None, igdb_id=None, tgdb_id=None, ra_id=None), @@ -1473,10 +1483,22 @@ async def scan_rom( and not rom_attrs.get("steam_id") and not rom_attrs.get("gamelist_id") ): - log.warning( - f"{hl(rom_attrs['fs_name'])} not identified {emoji.EMOJI_CROSS_MARK}", - extra=LOGGER_MODULE_NAME, - ) + if inconclusive_sources: + # Reporting a plain "not identified" here writes the ROM up as a + # coverage gap the providers confirmed, when one of them simply + # never answered. + silent = ", ".join(hl(source) for source in sorted(inconclusive_sources)) + log.warning( + f"{hl(rom_attrs['fs_name'])} not identified, but {silent} gave " + f"no answer, so this is not a confirmed miss - an UNMATCHED " + f"scan will retry it {emoji.EMOJI_WARNING}", + extra=LOGGER_MODULE_NAME, + ) + else: + log.warning( + f"{hl(rom_attrs['fs_name'])} not identified {emoji.EMOJI_CROSS_MARK}", + extra=LOGGER_MODULE_NAME, + ) return Rom(**rom_attrs) async def fetch_sgdb_details(playmatch_rom: PlaymatchRomMatch) -> SGDBRom: diff --git a/backend/tests/handler/test_fastapi.py b/backend/tests/handler/test_fastapi.py index 91b25fd3e..9c371cc72 100644 --- a/backend/tests/handler/test_fastapi.py +++ b/backend/tests/handler/test_fastapi.py @@ -1,3 +1,5 @@ +import logging +from contextlib import contextmanager from unittest.mock import AsyncMock, patch import pytest @@ -941,6 +943,104 @@ async def test_scan_rom_hashes_keeps_match_when_hasheous_unreachable( assert result.hasheous_metadata == {"nointro_match": True, "ra_match": True} +@contextmanager +def _capture_romm_logs(caplog): + """The "romm" logger has propagate=False, so caplog's handler has to be + attached to it directly rather than to the root.""" + romm_logger = logging.getLogger("romm") + romm_logger.addHandler(caplog.handler) + try: + with caplog.at_level(logging.WARNING, logger="romm"): + yield + finally: + romm_logger.removeHandler(caplog.handler) + + +async def _scan_unmatched_rom(platform: Platform) -> Rom: + """Scan a rom nothing identifies, so the outcome is the miss branch.""" + rom = db_rom_handler.add_rom( + Rom( + platform_id=platform.id, + fs_name="Nothing Knows This (USA).7z", + fs_name_no_tags="Nothing Knows This", + fs_name_no_ext="Nothing Knows This (USA)", + fs_extension="7z", + fs_path="n64/Nothing Knows This (USA)", + fs_size_bytes=1024, + tags=[], + ) + ) + return await scan_rom( + platform=platform, + scan_type=ScanType.UNMATCHED, + rom=rom, + fs_rom={ + "fs_name": rom.fs_name, + "fs_path": rom.fs_path, + "flat": True, + "files": [], + "crc_hash": "crc", + "md5_hash": "md5", + "sha1_hash": "sha1", + "ra_hash": "", + }, + metadata_sources=[MetadataSource.HASHEOUS], + newly_added=False, + ) + + +@patch.object(meta_playmatch_handler, "is_enabled", return_value=False) +@patch.object(meta_hasheous_handler, "get_ra_game", new_callable=AsyncMock) +@patch.object(meta_hasheous_handler, "get_igdb_game", new_callable=AsyncMock) +@patch.object(meta_hasheous_handler, "lookup_rom", new_callable=AsyncMock) +async def test_scan_rom_says_an_unanswered_miss_is_not_confirmed( + mock_lookup, mock_get_igdb, mock_get_ra, mock_playmatch_enabled, caplog +): + """A provider that never answered has ruled nothing out, so the outcome + must not read as a coverage gap the provider confirmed.""" + no_match = HasheousRom(hasheous_id=None, igdb_id=None, tgdb_id=None, ra_id=None) + mock_lookup.return_value = (no_match, False) + mock_get_igdb.return_value = no_match + mock_get_ra.return_value = no_match + + platform = db_platform_handler.add_platform( + Platform(id=1, slug="n64", fs_slug="n64", name="Nintendo 64", hasheous_id=64) + ) + + async with initialize_context(): + with _capture_romm_logs(caplog): + result = await _scan_unmatched_rom(platform) + + assert result.hasheous_id is None + assert "not a confirmed miss" in caplog.text + assert "hasheous" in caplog.text + + +@patch.object(meta_playmatch_handler, "is_enabled", return_value=False) +@patch.object(meta_hasheous_handler, "get_ra_game", new_callable=AsyncMock) +@patch.object(meta_hasheous_handler, "get_igdb_game", new_callable=AsyncMock) +@patch.object(meta_hasheous_handler, "lookup_rom", new_callable=AsyncMock) +async def test_scan_rom_still_reports_a_confirmed_miss_plainly( + mock_lookup, mock_get_igdb, mock_get_ra, mock_playmatch_enabled, caplog +): + """Hasheous answering "I don't know these hashes" is a real coverage gap.""" + no_match = HasheousRom(hasheous_id=None, igdb_id=None, tgdb_id=None, ra_id=None) + mock_lookup.return_value = (no_match, True) + mock_get_igdb.return_value = no_match + mock_get_ra.return_value = no_match + + platform = db_platform_handler.add_platform( + Platform(id=1, slug="n64", fs_slug="n64", name="Nintendo 64", hasheous_id=64) + ) + + async with initialize_context(): + with _capture_romm_logs(caplog): + await _scan_unmatched_rom(platform) + + assert "not identified" in caplog.text + assert "gave no answer" not in caplog.text + + def _top_level_rom_file(**kwargs) -> RomFile: """Build a RomFile whose `is_top_level` cached_property is pre-seeded to True, so it passes lookup_rom's filtering without a persisted rom."""