Files
romm/backend/tests/handler/test_resolve_launchbox_rom.py
Georges-Antoine Assi b35eb4d07c fix(scan): clean up unreferenced rescan media, split scan_rom and move its parts into handler/scan (#5408)
* fix(scan): delete screenshots and media a rescan leaves unreferenced

A complete rescan keeps the stored artwork, so two kinds of file stayed on
disk with nothing pointing at them:

- screenshots indexed past a shorter new list, since files are named by
  position and only the indexes in range were rewritten
- media of a type removed from the preferences, since providers record no
  path for it and stale media was compared over the preferred types only

get_rom_screenshots now deletes the indexes past the new list, and
remove_stale_media compares every media type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(scan): split scan_rom's merge into named steps

scan_rom held seeding the row, following Demozoo links, splicing scene
summaries, resetting rematched sources, the scene lock, rehash cleanup,
priority merges for fields, tags and artwork, and the SteamGridDB pass in one
body. Each step is now its own helper, the SteamGridDB lookup a _RomLookups
method, and the providers' answers a dict keyed by source instead of fourteen
locals. The Pouet and CSDb link follows share one helper. Behaviour is
unchanged.

McCabe complexity of scan_rom goes from 68 to 13, and the function from 664
to 215 lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scan): trim remove_stale_media's docstring per Greptile review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(scan): move the scan vocabulary, lookups and merge into handler/scan

scan_handler.py held the scan types and metadata sources, the per-source
lookups, the merge of their answers and the orchestration in one 2,000-line
module. It now builds on a handler/scan package:

- sources.py: ScanType, MetadataSource, the tag constants, RESCAN_RULES,
  should_consult, stored_match_id and the priority ordering
- lookups.py: RomLookups, the LaunchBox and Steam resolvers and
  get_main_platform_igdb_id
- merge.py: the pure steps that merge the sources' answers into a rom's
  columns

scan_handler.py keeps scan_platform, scan_rom, the asset scans and the media
downloads. Importers point at the module that now defines each name, and the
helpers that crossed a module boundary lost their leading underscore.
clear_rehash_misses takes the attempted sources instead of the lookups, so
merge does not depend on lookups. Behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(ci): import ScanType from handler.scan.sources in the e2e seed script

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scan): harden leftover screenshot cleanup (pr-ready: code-review)

List the screenshots folder once, use isdecimal() for the index test, and
log rather than raise when a leftover can't be removed. Point three
comments at handler/scan instead of scan_handler.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(scan): tidy the merge helpers and patch paths (pr-ready: simplify)

- fetch_sgdb_rom refetches by stored_id and builds the SGDB name list
  itself, so the name sources stay private to lookups.
- follow_demozoo_link checks the source is enabled; scan_rom loops over
  Pouët and CSDb.
- order_merge_sources holds the one ordering pass, and
  build_metadata_handlers adds the empty SGDB entry.
- Leftover screenshots go through the quiet-delete helper, renamed
  _discard_file.
- Tests patch rom lookups through handler.scan.lookups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scan): never leave a recorded media or screenshot path without its file

- remove_stale_media only deletes a file whose owner or source changed
  when that new owner stores the type again in this pass. A file nothing
  records is still deleted.
- Screenshots past a shorter list are pruned by remove_screenshots_past
  once the new paths are saved, in both download_rom_resources and
  update_rom, so a failed cover or manual leaves them in place.
- The release-notes schema diff watches scan_handler.py beside
  scan/sources.py, for ranges that span the move.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 15:23:13 -04:00

164 lines
4.8 KiB
Python

"""How a scan picks a ROM's LaunchBox match.
Playmatch answers with a LaunchBox ID for nearly every ROM on some platforms
(N64, SNES, Genesis), so an unhandled miss on that ID would leave those
platforms with no LaunchBox metadata at all while a manual match still works.
"""
from types import SimpleNamespace
from typing import Any
from unittest.mock import AsyncMock, patch
import pytest
from handler.metadata.launchbox_handler.types import LaunchboxRom
from handler.metadata.playmatch_handler import PlaymatchRomMatch
from handler.scan.lookups import resolve_launchbox_rom
from handler.scan.sources import ScanType
MATCH = LaunchboxRom(launchbox_id=266, name="Mario Kart 64")
NO_MATCH = LaunchboxRom(launchbox_id=None)
def _playmatch(launchbox_id: int | None) -> PlaymatchRomMatch:
return PlaymatchRomMatch(
igdb_id=None,
moby_id=None,
ss_id=None,
launchbox_id=launchbox_id,
sgdb_id=None,
ra_id=None,
hasheous_id=None,
tgdb_id=None,
flashpoint_id=None,
hltb_id=None,
gamelist_id=None,
libretro_id=None,
)
def _rom(
launchbox_id: int | None = None, launchbox_metadata: dict[str, Any] | None = None
):
return SimpleNamespace(
launchbox_id=launchbox_id,
launchbox_metadata=launchbox_metadata or {},
)
@pytest.fixture
def lookups():
"""Patch both LaunchBox lookups, defaulting each to a miss."""
with (
patch(
"handler.scan.lookups.meta_launchbox_handler.get_rom_by_id",
new=AsyncMock(return_value=NO_MATCH),
) as by_id,
patch(
"handler.scan.lookups.meta_launchbox_handler.get_rom",
new=AsyncMock(return_value=NO_MATCH),
) as by_name,
):
yield SimpleNamespace(by_id=by_id, by_name=by_name)
async def test_unresolved_playmatch_id_falls_back_to_file_name(lookups):
lookups.by_name.return_value = MATCH
result = await resolve_launchbox_rom(
rom=_rom(),
fs_name="Mario Kart 64 (USA).zip",
platform_slug="n64",
scan_type=ScanType.COMPLETE,
playmatch_rom=_playmatch(266),
remote_enabled=True,
)
lookups.by_id.assert_awaited_once()
lookups.by_name.assert_awaited_once_with(
"Mario Kart 64 (USA).zip", "n64", remote_enabled=True
)
assert result["launchbox_id"] == 266
async def test_resolved_playmatch_id_skips_the_file_name_lookup(lookups):
lookups.by_id.return_value = MATCH
result = await resolve_launchbox_rom(
rom=_rom(),
fs_name="Mario Kart 64 (USA).zip",
platform_slug="n64",
scan_type=ScanType.COMPLETE,
playmatch_rom=_playmatch(266),
remote_enabled=True,
)
lookups.by_name.assert_not_awaited()
assert result["launchbox_id"] == 266
async def test_no_playmatch_id_uses_the_file_name_lookup(lookups):
lookups.by_name.return_value = MATCH
result = await resolve_launchbox_rom(
rom=_rom(),
fs_name="Mario Kart 64 (USA).zip",
platform_slug="n64",
scan_type=ScanType.COMPLETE,
playmatch_rom=_playmatch(None),
remote_enabled=True,
)
lookups.by_id.assert_not_awaited()
assert result["launchbox_id"] == 266
async def test_stored_id_is_not_replaced_by_a_file_name_guess(lookups):
"""An ID the store can't resolve stays put rather than being re-guessed."""
result = await resolve_launchbox_rom(
rom=_rom(launchbox_id=999),
fs_name="Mario Kart 64 (USA).zip",
platform_slug="n64",
scan_type=ScanType.UPDATE,
playmatch_rom=_playmatch(266),
remote_enabled=True,
)
lookups.by_id.assert_awaited_once()
lookups.by_name.assert_not_awaited()
assert result["launchbox_id"] is None
async def test_unmatched_scan_refetches_metadata_for_a_stored_id(lookups):
lookups.by_id.return_value = MATCH
result = await resolve_launchbox_rom(
rom=_rom(launchbox_id=266),
fs_name="Mario Kart 64 (USA).zip",
platform_slug="n64",
scan_type=ScanType.UNMATCHED,
playmatch_rom=_playmatch(None),
remote_enabled=True,
)
lookups.by_id.assert_awaited_once()
lookups.by_name.assert_not_awaited()
assert result["launchbox_id"] == 266
async def test_local_only_never_calls_the_remote_id_lookup(lookups):
"""With the remote store off, only the local filename lookup may run."""
await resolve_launchbox_rom(
rom=_rom(launchbox_id=266),
fs_name="Mario Kart 64 (USA).zip",
platform_slug="n64",
scan_type=ScanType.UPDATE,
playmatch_rom=_playmatch(266),
remote_enabled=False,
)
lookups.by_id.assert_not_awaited()
lookups.by_name.assert_awaited_once_with(
"Mario Kart 64 (USA).zip", "n64", remote_enabled=False
)