Files
romm/backend/tests/handler/test_scan_claim_priority.py
Tyler Vigario d96ec82e05 fix(scan): name a game by metadata priority, whatever order the scans ran in (#5546)
* fix(scan): name a game by metadata priority, whatever order the scans ran in

An unmatched scan asks only the sources a ROM has no match from yet, and
merges just their answers. A stored name that looks like a placeholder was
replaced by whatever name that subset gave, and a provider's name often looks
like one: LaunchBox calls Super C (USA).nes "Super C". So an unmatched
HowLongToBeat scan renamed it "Super Lucky's Tale", though LaunchBox ranks
above HowLongToBeat in scan.priority.metadata, and once a name no longer
looked like a placeholder no refresh could correct it.

Every source now keeps the title it gave in its own blob, as hash sources
keep their dump tags. On an unmatched, update or hashes scan the name goes to
the highest-ranked source among this scan's answers and the stored titles of
the sources the ROM is still matched to. A source matched before titles were
kept outranks the rest unknown, so the stored name stays. A stored name equal
to some source's title counts as derived, so a better-ranked source can
replace it; any other name is the user's and stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TsLtVzsT8WBDnc6ZSRyp2u

* fix(scan): count only a string as a source's kept title

The raw metadata editor stores any JSON, so a blob's name can be a list or an
object, which broke a refresh with an unhashable set element. kept_title now
reads a stored title for both settle_name and keep_base_fields, and anything
but a string counts as no title. settle_name's docstring is one sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TsLtVzsT8WBDnc6ZSRyp2u

* fix(scan): settle names across Hasheous-filled IGDB columns and pre-existing rows

code-review (xhigh) pass: stamp every blob a source fills, treat only ids the
row already held as stored matches, count this scan's titles as derived, and
drop non-string kept titles from responses.

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

* refactor(scan): share one priority ordering for the name settle

simplify pass: order_merge_sources returns the full ordering so settle_name
skips its own config read, kept_title moves next to NAME_CLAIM_KEY and backs
the response filter, keep_base_fields' comments fold into one, and the name
priority tests share constants and parametrize their near-duplicates.

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

* chore(scan): reword settle_name's unknown-title comment

review-polish pass.

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

* fix(scan): settle slug and artwork by priority too, under a scan_claims key

Each source's blob now keeps the name, slug, cover and screenshots it gave
under one namespaced scan_claims key (a ScanClaims TypedDict) instead of a
bare name key that shared the providers' own key space. settle_claims ranks
every claimed field by its own priority list, so a partial scan no longer
writes a lower-ranked source's cover over a better-ranked one. A matched
source with no claims on record holds the stored value only when that value
is set. The artwork orderings are computed once and shared with
apply_artwork_priority, so no config read is added.

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

* refactor(scan): hide scan_claims from the API

The claims stay in each source's blob, but the provider metadata types no
longer declare them, so they're out of the OpenAPI schema and the generated
frontend types. Responses strip the key, and a raw metadata edit keeps the
stored claims while the source's match stands instead of taking the client's.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-11-11 07:30:20 -05:00

298 lines
9.0 KiB
Python

"""How a refresh sets a ROM's base fields when it consults only some of its sources."""
from collections.abc import Iterator
from types import SimpleNamespace
from typing import Any, cast
from unittest.mock import AsyncMock, patch
import pytest
from tests.handler.scan_stubs import add_n64_platform, add_rom, run_scan
from handler.metadata.hasheous_handler import HasheousMetadata, HasheousRom
from handler.metadata.hltb_handler import HLTBMetadata, HLTBRom
from handler.metadata.igdb_handler import IGDBMetadata
from handler.metadata.launchbox_handler.types import LaunchboxMetadata, LaunchboxRom
from handler.scan.sources import MetadataSource, ScanType
from models.rom import Rom
FS_NAME = "Mario Kart 64 (USA).z64"
TITLE = "Mario Kart 64"
HLTB_TITLE = "Mario Kart 8"
LAUNCHBOX_BLOB = {"first_release_date": 851990400}
HLTB_BLOB = {"main_story": 150000}
LAUNCHBOX_COVER = "https://images.launchbox-app.com/mk64-cover.png"
LAUNCHBOX_SHOTS = ["https://images.launchbox-app.com/mk64-shot.png"]
HLTB_COVER = "https://howlongtobeat.com/games/mk8.jpg"
LAUNCHBOX = LaunchboxRom(
launchbox_id=266,
name=TITLE,
url_cover=LAUNCHBOX_COVER,
url_screenshots=LAUNCHBOX_SHOTS,
launchbox_metadata=cast(LaunchboxMetadata, LAUNCHBOX_BLOB),
)
HLTB_GUESS = HLTBRom(
hltb_id=9999,
name=HLTB_TITLE,
url_cover=HLTB_COVER,
hltb_metadata=cast(HLTBMetadata, HLTB_BLOB),
)
HASHEOUS_HIT = HasheousRom(
hasheous_id=77,
name=TITLE,
igdb_id=1234,
tgdb_id=None,
hasheous_metadata=cast(HasheousMetadata, {"tosec_match": True}),
)
@pytest.fixture
def lookups() -> Iterator[SimpleNamespace]:
with (
patch(
"handler.scan.lookups.resolve_launchbox_rom",
new=AsyncMock(return_value=LAUNCHBOX),
) as launchbox,
patch(
"handler.scan.lookups.meta_hltb_handler.get_rom",
new=AsyncMock(return_value=HLTB_GUESS),
) as hltb_by_name,
patch(
"handler.scan.lookups.meta_hltb_handler.get_rom_by_id",
new=AsyncMock(return_value=HLTB_GUESS),
),
):
yield SimpleNamespace(launchbox=launchbox, hltb_by_name=hltb_by_name)
async def _scan(
name: str, scan_type: ScanType, sources: list[str], **stored: Any
) -> Rom:
platform = add_n64_platform()
rom = add_rom(platform, FS_NAME, name, fs_name_no_tags=TITLE, **stored)
return await run_scan(platform, rom, scan_type=scan_type, metadata_sources=sources)
@pytest.mark.parametrize(
"launchbox_metadata",
[
pytest.param(
{**LAUNCHBOX_BLOB, "scan_claims": {"name": TITLE}}, id="title-kept"
),
# A source whose title isn't on record may outrank the newcomer.
pytest.param(LAUNCHBOX_BLOB, id="stored-before-titles-were-kept"),
],
)
async def test_a_lower_ranked_source_does_not_rename_a_game(
lookups, launchbox_metadata
):
result = await _scan(
TITLE,
ScanType.UNMATCHED,
[MetadataSource.HLTB],
launchbox_id=266,
launchbox_metadata=launchbox_metadata,
)
assert result.hltb_id == 9999
assert result.name == TITLE
async def test_a_higher_ranked_source_replaces_a_lower_ranked_title(lookups):
"""The order is the other way round: HowLongToBeat named it first."""
result = await _scan(
HLTB_TITLE,
ScanType.UNMATCHED,
[MetadataSource.LAUNCHBOX],
hltb_id=9999,
hltb_metadata={**HLTB_BLOB, "scan_claims": {"name": HLTB_TITLE}},
)
assert result.launchbox_id == 266
assert result.name == TITLE
@pytest.mark.parametrize(
"hltb_metadata",
[
pytest.param(
{**HLTB_BLOB, "scan_claims": {"name": HLTB_TITLE}}, id="title-kept"
),
pytest.param(HLTB_BLOB, id="stored-before-titles-were-kept"),
],
)
async def test_an_update_settles_on_the_higher_ranked_title(lookups, hltb_metadata):
result = await _scan(
HLTB_TITLE,
ScanType.UPDATE,
[MetadataSource.LAUNCHBOX, MetadataSource.HLTB],
launchbox_id=266,
launchbox_metadata=LAUNCHBOX_BLOB,
hltb_id=9999,
hltb_metadata=hltb_metadata,
)
assert result.name == TITLE
async def test_each_answering_source_keeps_its_title(lookups):
result = await _scan(TITLE, ScanType.UNMATCHED, [MetadataSource.HLTB])
assert result.hltb_metadata is not None
assert result.hltb_metadata["scan_claims"] == {
"name": HLTB_TITLE,
"url_cover": HLTB_COVER,
}
async def test_a_name_the_user_typed_is_kept(lookups):
result = await _scan(
"MK64",
ScanType.UPDATE,
[MetadataSource.LAUNCHBOX, MetadataSource.HLTB],
launchbox_id=266,
launchbox_metadata={**LAUNCHBOX_BLOB, "scan_claims": {"name": TITLE}},
hltb_id=9999,
hltb_metadata={**HLTB_BLOB, "scan_claims": {"name": HLTB_TITLE}},
)
assert result.name == "MK64"
async def test_a_lone_source_still_replaces_the_placeholder(lookups):
result = await _scan(FS_NAME, ScanType.UNMATCHED, [MetadataSource.HLTB])
assert result.name == HLTB_TITLE
async def test_a_match_without_metadata_gets_no_blob(lookups):
"""An unmatched scan retries a source whose blob is empty, so none is made."""
lookups.hltb_by_name.return_value = HLTBRom(hltb_id=9999, name=HLTB_TITLE)
result = await _scan(TITLE, ScanType.UNMATCHED, [MetadataSource.HLTB])
assert not result.hltb_metadata
async def _scan_hasheous(igdb_game: HasheousRom) -> Rom:
"""An unmatched Hasheous-only scan, where Hasheous fills IGDB's columns."""
platform = add_n64_platform(hasheous_id=64)
rom = add_rom(platform, "mk64 (USA).z64", "mk64")
with (
patch(
"handler.scan.lookups.meta_hasheous_handler.is_enabled", return_value=True
),
patch(
"handler.scan.lookups.meta_hasheous_handler.lookup_rom",
new=AsyncMock(return_value=(HASHEOUS_HIT, True)),
),
patch(
"handler.scan.lookups.meta_hasheous_handler.get_igdb_game",
new=AsyncMock(return_value=igdb_game),
),
):
return await run_scan(
platform,
rom,
scan_type=ScanType.UNMATCHED,
metadata_sources=[MetadataSource.HASHEOUS],
)
async def test_an_igdb_id_hasheous_gives_doesnt_hold_the_placeholder():
result = await _scan_hasheous(HASHEOUS_HIT)
assert result.igdb_id == 1234
assert result.name == TITLE
async def test_igdb_metadata_hasheous_gives_keeps_its_title():
igdb_metadata = cast(IGDBMetadata, {"total_rating": "90"})
result = await _scan_hasheous(
HasheousRom({**HASHEOUS_HIT, "igdb_metadata": igdb_metadata})
)
assert result.name == TITLE
assert result.igdb_metadata is not None
assert result.igdb_metadata["scan_claims"]["name"] == TITLE
async def test_a_hand_edited_title_that_isnt_text_is_ignored(lookups):
"""The raw metadata editor stores any JSON, so a title may not be a string."""
result = await _scan(
TITLE,
ScanType.UNMATCHED,
[MetadataSource.HLTB],
launchbox_id=266,
launchbox_metadata={**LAUNCHBOX_BLOB, "scan_claims": {"name": ["Mario"]}},
)
assert result.name == TITLE
async def test_a_lower_ranked_source_does_not_replace_the_artwork(lookups):
result = await _scan(
TITLE,
ScanType.UNMATCHED,
[MetadataSource.HLTB],
url_cover=LAUNCHBOX_COVER,
url_screenshots=LAUNCHBOX_SHOTS,
launchbox_id=266,
launchbox_metadata={
**LAUNCHBOX_BLOB,
"scan_claims": {
"name": TITLE,
"url_cover": LAUNCHBOX_COVER,
"url_screenshots": LAUNCHBOX_SHOTS,
},
},
)
assert result.hltb_id == 9999
assert result.url_cover == LAUNCHBOX_COVER
assert result.url_screenshots == LAUNCHBOX_SHOTS
async def test_a_higher_ranked_source_replaces_a_lower_ranked_cover(lookups):
result = await _scan(
HLTB_TITLE,
ScanType.UNMATCHED,
[MetadataSource.LAUNCHBOX],
url_cover=HLTB_COVER,
hltb_id=9999,
hltb_metadata={
**HLTB_BLOB,
"scan_claims": {"name": HLTB_TITLE, "url_cover": HLTB_COVER},
},
)
assert result.url_cover == LAUNCHBOX_COVER
assert result.url_screenshots == LAUNCHBOX_SHOTS
async def test_a_lower_ranked_source_fills_artwork_the_higher_one_lacks(lookups):
result = await _scan(
TITLE,
ScanType.UNMATCHED,
[MetadataSource.HLTB],
launchbox_id=266,
launchbox_metadata={**LAUNCHBOX_BLOB, "scan_claims": {"name": TITLE}},
)
assert result.name == TITLE
assert result.url_cover == HLTB_COVER
async def test_a_match_without_claims_on_record_holds_only_a_stored_value(lookups):
"""An older match keeps the stored cover, but an empty slot still fills."""
result = await _scan(
TITLE,
ScanType.UNMATCHED,
[MetadataSource.HLTB],
url_cover="",
launchbox_id=266,
launchbox_metadata=LAUNCHBOX_BLOB,
)
assert result.name == TITLE
assert result.url_cover == HLTB_COVER