Files
romm/backend/tests/handler/test_scan_hltb_match.py
Georges-Antoine Assi 3b2a9e3043 refactor(scan): share one failure path across the provider lookups
Every gathered provider already resolved through a single loop that logs the
failure, drops the source from the attempted set and falls back to an empty
match. SteamGridDB, which searches on the names the other providers produce and
so cannot join that gather, hand-wrote the same three steps again, and the
search endpoint hand-wrote a third copy. Extract the loop body into
`resolve_fetch` and route SteamGridDB through it.

In the search endpoint the cover gathers now tolerate exceptions instead, so a
failed lookup costs a thumbnail rather than the whole result set. That also
covers Libretro, whose lookup had no guard at all and could fail the request.

On the test side, the platform, rom and `fs_rom` scaffolding was written out
three times across the two scan tests; it now lives in `tests/handler/scan_stubs.py`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-04 20:58:27 -04:00

117 lines
3.5 KiB
Python

"""How a scan picks a ROM's HowLongToBeat match.
The filename lookup matches fuzzily, so re-running it on a ROM that already
carries an ID lets a different game outscore a match the user pinned by hand.
"""
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
import pytest
from tests.handler.scan_stubs import add_n64_platform, add_rom, run_scan
from handler.metadata.hltb_handler import HLTBMetadata, HLTBRom
from handler.scan_handler import MetadataSource, ScanType
from models.rom import Rom
FS_NAME = "Mario Kart 64 (USA).z64"
PINNED = HLTBRom(hltb_id=2255, name="Mario Kart 64")
GUESSED = HLTBRom(hltb_id=9999, name="Mario Kart 8")
NO_MATCH = HLTBRom(hltb_id=None)
@pytest.fixture
def lookups():
"""Patch both HowLongToBeat lookups, defaulting each to a miss."""
with (
patch(
"handler.scan_handler.meta_hltb_handler.get_rom_by_id",
new=AsyncMock(return_value=NO_MATCH),
) as by_id,
patch(
"handler.scan_handler.meta_hltb_handler.get_rom",
new=AsyncMock(return_value=NO_MATCH),
) as by_name,
):
yield SimpleNamespace(by_id=by_id, by_name=by_name)
async def _scan(
scan_type: ScanType,
hltb_id: int | None = None,
hltb_metadata: HLTBMetadata | None = None,
) -> Rom:
platform = add_n64_platform()
rom = add_rom(
platform,
FS_NAME,
"Mario Kart 64",
hltb_id=hltb_id,
hltb_metadata=dict(hltb_metadata or {}),
)
return await run_scan(
platform, rom, scan_type=scan_type, metadata_sources=[MetadataSource.HLTB]
)
async def test_no_stored_id_uses_the_file_name_lookup(lookups):
lookups.by_name.return_value = GUESSED
result = await _scan(ScanType.COMPLETE)
lookups.by_id.assert_not_awaited()
lookups.by_name.assert_awaited_once_with(FS_NAME, "n64")
assert result.hltb_id == 9999
async def test_complete_rescan_rematches_a_stored_id(lookups):
"""A complete rescan wipes external IDs by design, so it re-runs the search."""
lookups.by_name.return_value = GUESSED
result = await _scan(ScanType.COMPLETE, hltb_id=2255)
lookups.by_id.assert_not_awaited()
lookups.by_name.assert_awaited_once_with(FS_NAME, "n64")
assert result.hltb_id == 9999
async def test_update_scan_refetches_a_stored_id(lookups):
"""A pinned match is refreshed by ID, never re-guessed from the filename."""
lookups.by_id.return_value = PINNED
result = await _scan(
ScanType.UPDATE, hltb_id=2255, hltb_metadata=HLTBMetadata(release_year=1997)
)
lookups.by_id.assert_awaited_once_with(2255)
lookups.by_name.assert_not_awaited()
assert result.hltb_id == 2255
async def test_stored_id_is_not_replaced_by_a_file_name_guess(lookups):
"""An ID the provider can't resolve stays put rather than being re-guessed."""
await _scan(ScanType.UPDATE, hltb_id=2255)
lookups.by_id.assert_awaited_once_with(2255)
lookups.by_name.assert_not_awaited()
async def test_unmatched_scan_refetches_metadata_for_a_stored_id(lookups):
lookups.by_id.return_value = PINNED
result = await _scan(ScanType.UNMATCHED, hltb_id=2255)
lookups.by_id.assert_awaited_once_with(2255)
lookups.by_name.assert_not_awaited()
assert result.hltb_id == 2255
async def test_unmatched_scan_without_a_stored_id_uses_the_file_name_lookup(lookups):
lookups.by_name.return_value = GUESSED
await _scan(ScanType.UNMATCHED)
lookups.by_id.assert_not_awaited()
lookups.by_name.assert_awaited_once_with(FS_NAME, "n64")