Files
romm/backend/tests/handler/metadata/test_playmatch_handler.py
Sam Dornan 5a6c895d33 chore(mypy): give every generic its type arguments (#4818)
* chore(mypy): switch to strict mode with listed exceptions

Replace the hand-picked flags with `strict = True`, turning off only the
four flags that aren't clean yet (disallow_any_generics,
warn_return_any, disallow_untyped_calls, disallow_untyped_decorators).
Any other strict flag, including those future mypy releases add to the
preset, is on by default.

That turns on no_implicit_reexport, strict_equality and extra_checks:
- Import each name from the module that defines it. About 240 test
  sites patched a dependency through the module that imported it
  (`scan_module.fs_rom_handler`); they now import the same object from
  its source, so every patch targets what it did before. Tests that
  replace a module attribute are unchanged.
- handler.streaming.memory_cards re-exports store_memory_card_version
  explicitly, since endpoints call it there and tests patch it there.
- mutagen gets implicit_reexport, as it re-exports without __all__.
- Test comparisons build typed values (CoverResult, a typed expected
  dict, enum .value) and the CORS lookup casts for its identity check.
- One ss_handler ** spread keeps a coded ignore: extra_checks wants its
  NotRequired dump_* keys spelled out, and the media dict never has them.

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

* chore(mypy): give every generic its type arguments

Drop disallow_any_generics from the strict-mode exceptions and fix the
347 bare generics it reports, 230 of them in app code.

- Bare `dict` becomes `dict[str, Any]`: update payloads, raw provider
  JSON and endpoint bodies map string keys to mixed values, so the Any is
  real and now written down. The OpenAPI document is unchanged.
- Rom query filters take and return RomSelect; _music_facet_joins and
  _exclude_hidden are generic over the Select they're given, and
  _build_hash_selects returns id-only selects.
- Redis clients are typed by what they return: the decoding caches are
  Redis[str], the RQ connection and binary cache Redis[bytes].
- The rest get explicit parameters: Row, Pattern, ColumnElement,
  QueryableAttribute, SpooledTemporaryFile, Task, Future and friends.

Precise types reached callers that had been getting Any, which surfaced:
- The LaunchBox cache writer is fed binary-cache pipelines.
- The flashpoint and IGDB request helpers return raw JSON of varying
  shape, not always a dict.
- The rom search endpoint returned dicts under a list[SearchRomSchema]
  annotation; it now validates them into that schema.
- The audit filter's id subquery selects strings.
- In tests the binary cache is the non-decoding fake shared with
  async_cache, so it is cast to Redis[bytes] rather than changed.

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

* test(upload): assert cleanup against the patched upload directory

The 409 test checked config's ROM_UPLOAD_TMP_BASE while the fixture patches
the endpoint's copy to tmp_path/uploads, so it passed even if cleanup failed.

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

* chore(mypy): keep the row type through the ROM filter helpers

filter_roms and its helpers also receive select(Rom.id) membership queries,
so they now take and return the caller's Select type instead of narrowing to
RomSelect. merged_ra_metadata mixes scalar and list values.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 16:32:29 -05:00

296 lines
9.8 KiB
Python

import json
from typing import Any
from unittest.mock import ANY, AsyncMock, MagicMock, patch
import httpx
from handler.metadata.playmatch_handler import PlaymatchHandler
from models.rom import Rom, RomFile
from utils import get_version
@patch("handler.metadata.playmatch_handler.ctx_httpx_client")
async def test_heartbeat_accepts_plain_text_health_response(mock_ctx_httpx_client):
# /health returns a 200 with the plain-text body "Healthy", not JSON.
handler = PlaymatchHandler()
mock_client = AsyncMock()
mock_response = MagicMock()
mock_response.text = "Healthy"
mock_response.json.side_effect = json.JSONDecodeError(
"Expecting value", "Healthy", 0
)
mock_client.get.return_value = mock_response
mock_ctx_httpx_client.get.return_value = mock_client
with (
patch.object(handler, "is_enabled", return_value=True),
patch(
"handler.metadata.playmatch_handler._rate_limiter.acquire",
new_callable=AsyncMock,
),
):
assert await handler.heartbeat() is True
mock_client.get.assert_awaited_once_with(
handler.healthcheck_url,
headers={"user-agent": f"RomM/{get_version()}"},
timeout=60,
)
mock_response.raise_for_status.assert_called_once()
@patch("handler.metadata.playmatch_handler.ctx_httpx_client")
async def test_heartbeat_returns_false_on_http_error(mock_ctx_httpx_client):
handler = PlaymatchHandler()
mock_client = AsyncMock()
mock_response = MagicMock()
mock_response.raise_for_status.side_effect = httpx.HTTPStatusError(
"Service Unavailable", request=MagicMock(), response=MagicMock()
)
mock_client.get.return_value = mock_response
mock_ctx_httpx_client.get.return_value = mock_client
with (
patch.object(handler, "is_enabled", return_value=True),
patch(
"handler.metadata.playmatch_handler._rate_limiter.acquire",
new_callable=AsyncMock,
),
):
assert await handler.heartbeat() is False
async def test_heartbeat_returns_false_when_disabled():
handler = PlaymatchHandler()
with patch.object(handler, "is_enabled", return_value=False):
assert await handler.heartbeat() is False
def _rom_file(*, is_top_level: bool = True, **kwargs) -> RomFile:
"""Build a RomFile whose `is_top_level` cached_property is pre-seeded, so it
reaches lookup_rom's filtering without a persisted rom."""
file = RomFile(file_path="psx/Game", **kwargs)
file.__dict__["is_top_level"] = is_top_level
return file
async def _captured_lookup_payload(
handler: PlaymatchHandler, files
) -> dict[str, Any] | None:
with (
patch.object(handler, "is_enabled", return_value=True),
patch.object(handler, "_request", new_callable=AsyncMock) as mock_request,
):
mock_request.return_value = {}
await handler.lookup_rom(files)
if not mock_request.await_args_list:
return None
return mock_request.await_args_list[-1].args[1]
async def test_lookup_rom_identifies_an_archive_by_its_largest_member():
"""Playmatch indexes a multi-file archive by the ROM inside it, so the
archive's composite hash must not be what we ask about. The file name and
size stay those of the archive on disk."""
archive = _rom_file(
file_name="set.zip",
file_size_bytes=300,
crc_hash="compositecrc",
md5_hash="compositemd5",
sha1_hash="compositesha1",
archive_members=[
{
"name": "readme.txt",
"size": 10,
"crc_hash": "readmecrc",
"md5_hash": "readmemd5",
"sha1_hash": "readmesha1",
},
{
"name": "game.rom",
"size": 2048,
"crc_hash": "gamecrc",
"md5_hash": "gamemd5",
"sha1_hash": "gamesha1",
},
],
)
payload = await _captured_lookup_payload(PlaymatchHandler(), [archive])
assert payload == {
"fileName": "set.zip",
"fileSize": 300,
"md5": "gamemd5",
"sha1": "gamesha1",
"crc": "gamecrc",
}
def _multi_disc_files(disc_two_size: int = 200) -> list[RomFile]:
"""A folder ROM the way the scanner emits it: a small playlist next to the
discs that actually carry the game."""
return [
_rom_file(file_name="game.m3u", file_size_bytes=20, md5_hash="playlistmd5"),
_rom_file(
file_name="disc1.chd", file_size_bytes=300, chd_sha1_hash="disconesha1"
),
_rom_file(
file_name="disc2.chd",
file_size_bytes=disc_two_size,
chd_sha1_hash="disctwosha1",
),
]
async def test_lookup_rom_asks_about_a_disc_not_the_playlist():
"""The playlist has no game data in it, so identifying a multi-disc ROM by
it can only ever miss."""
payload = await _captured_lookup_payload(PlaymatchHandler(), _multi_disc_files())
assert payload == {
"fileName": "disc1.chd",
"fileSize": 300,
"md5": None,
"sha1": "disconesha1",
"crc": None,
}
async def test_lookup_rom_payload_does_not_depend_on_file_order():
"""The scanner walks the filesystem unsorted and the files relationship has
no order_by, so anything that leans on list order asks about a different
file on a different machine."""
handler = PlaymatchHandler()
# Equally sized discs, so only the tie-break can settle which one wins.
files = _multi_disc_files(disc_two_size=300)
payloads = [
await _captured_lookup_payload(handler, list(order))
for order in (files, reversed(files), files[1:] + files[:1])
]
assert payloads[0] is not None
assert payloads[0] == payloads[1] == payloads[2]
assert payloads[0]["fileName"] == "disc1.chd"
async def test_lookup_rom_ignores_files_nested_inside_the_rom():
"""Hasheous and ScreenScraper both filter on is_top_level; a bundled extra
or translation patch is not what the ROM should be identified by."""
files = [
_rom_file(file_name="game.iso", file_size_bytes=100, md5_hash="gamemd5"),
_rom_file(
file_name="bonus.iso",
file_size_bytes=9000,
md5_hash="bonusmd5",
is_top_level=False,
),
]
payload = await _captured_lookup_payload(PlaymatchHandler(), files)
assert payload is not None
assert payload["fileName"] == "game.iso"
async def test_lookup_rom_skips_the_request_when_no_file_qualifies():
"""No eligible file must not blow up on an empty max()."""
files = [
_rom_file(file_name="empty.iso", file_size_bytes=0, md5_hash="emptymd5"),
_rom_file(
file_name="nested.iso",
file_size_bytes=100,
md5_hash="nestedmd5",
is_top_level=False,
),
]
assert await _captured_lookup_payload(PlaymatchHandler(), files) is None
assert await _captured_lookup_payload(PlaymatchHandler(), []) is None
def _unhashed_file(rom: Rom, file_path: str, file_name: str) -> RomFile:
file = RomFile(file_path=file_path, file_name=file_name, file_size_bytes=1024)
file.rom = rom
file.__dict__["is_top_level"] = True
return file
async def test_lookup_rom_skips_an_unhashed_folder_member():
"""Wii U content files share generic names across titles, so asking by name
and size alone matches every title to the same unrelated game."""
rom = Rom(fs_path="wiiu", fs_name="Adventure Island [0005000010134100]")
member = _unhashed_file(rom, rom.full_path, "00000005.app")
assert await _captured_lookup_payload(PlaymatchHandler(), [member]) is None
async def test_lookup_rom_asks_about_an_unhashed_single_file_by_name():
rom = Rom(fs_path="switch", fs_name="Game [0100000000010000].nsp")
single = _unhashed_file(rom, "switch", rom.fs_name)
payload = await _captured_lookup_payload(PlaymatchHandler(), [single])
assert payload is not None
assert payload["fileName"] == rom.fs_name
async def _captured_suggestion_payload(rom: Rom) -> dict[str, Any] | None:
handler = PlaymatchHandler()
mock_client = AsyncMock()
mock_client.post.return_value = MagicMock()
with (
patch.object(handler, "is_enabled", return_value=True),
patch(
"handler.metadata.playmatch_handler.ctx_httpx_client"
) as mock_ctx_httpx_client,
patch(
"handler.metadata.playmatch_handler._rate_limiter.acquire",
new_callable=AsyncMock,
),
):
mock_ctx_httpx_client.get.return_value = mock_client
await handler.submit_manual_match_suggestion(rom)
if not mock_client.post.await_args_list:
return None
return mock_client.post.await_args.kwargs["json"]
async def test_suggestion_contributes_the_selected_files_hashes():
"""A suggestion writes a hash-to-game mapping into a public index, so it
must carry the same digests a lookup would be answered by."""
rom = Rom(igdb_id=1234)
rom.files = _multi_disc_files() + [
_rom_file(
file_name="extras.zip",
file_size_bytes=50,
md5_hash="compositemd5",
sha1_hash="compositesha1",
)
]
payload = await _captured_suggestion_payload(rom)
assert payload == {
"md5": None,
"sha1": "disconesha1",
"sha256": None,
"fileName": "disc1.chd",
"fileSize": 300,
"mappings": ANY,
}
async def test_suggestion_is_skipped_when_no_file_qualifies():
"""Falling back to the ROM-level hash would contribute a composite spanning
every file, which is not a digest of anything Playmatch indexes."""
rom = Rom(igdb_id=1234, fs_name="game.zip", fs_size_bytes=100)
rom.md5_hash = "compositemd5"
rom.sha1_hash = "compositesha1"
rom.files = []
assert await _captured_suggestion_payload(rom) is None