Files
romm/backend/tests/handler/metadata/test_hasheous_handler.py
Georges-Antoine Assi 8b2931a56d fix(scan): don't let a Hasheous outage read as a conclusive miss
`_request` swallowed a non-404 HTTP error and a timeout into an empty dict,
which `lookup_rom` then reported as conclusive: Hasheous answered and knows
nothing about these hashes. A 500 or a timeout therefore de-verified a rom on
a rehash and cleared its id on a complete rescan.

Both paths now raise, so the conclusive flag already carried by `lookup_rom`
tells the two empty matches apart on its own.

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

78 lines
2.3 KiB
Python

from unittest.mock import AsyncMock, patch
import httpx
import pytest
from fastapi import HTTPException
from handler.metadata.hasheous_handler import (
HasheousHandler,
extract_metadata_from_igdb_rom,
)
# The proxy keys expanded collections by id and returns `company` as a bare id,
# unlike IGDB's own list-of-objects shape.
PROXY_ROM = {
"involved_companies": {
"148214": {"id": 148214, "company": 70, "developer": True, "publisher": False},
"225579": {"id": 225579, "company": 812, "developer": False, "publisher": True},
},
}
IGDB_ROM = {
"involved_companies": [
{"company": {"name": "Retro Studios"}, "developer": True, "publisher": False},
{"company": {"name": "Nintendo"}, "developer": False, "publisher": True},
],
}
def test_reads_the_proxys_dict_shaped_involvements():
metadata = extract_metadata_from_igdb_rom(PROXY_ROM)
assert metadata["companies"] == []
assert metadata["publishers"] == []
assert metadata["developers"] == []
def test_reads_igdbs_list_shaped_involvements():
metadata = extract_metadata_from_igdb_rom(IGDB_ROM)
assert metadata["publishers"] == ["Nintendo"]
assert metadata["developers"] == ["Retro Studios"]
def test_involvements_are_optional():
metadata = extract_metadata_from_igdb_rom({})
assert metadata["publishers"] == []
assert metadata["developers"] == []
@pytest.mark.asyncio
@pytest.mark.parametrize(
"failure",
[
httpx.HTTPStatusError(
"boom",
request=httpx.Request("POST", "https://hasheous.org/api"),
response=httpx.Response(
500, request=httpx.Request("POST", "https://hasheous.org/api")
),
),
httpx.TimeoutException("too slow"),
],
ids=["server_error", "timeout"],
)
async def test_request_propagates_an_unreachable_hasheous(failure: Exception):
"""A failed request has to stay distinguishable from a game Hasheous lacks."""
handler = HasheousHandler()
client = AsyncMock()
client.request = AsyncMock(side_effect=failure)
with (
patch("handler.metadata.hasheous_handler.ctx_httpx_client") as ctx,
pytest.raises(HTTPException),
):
ctx.get.return_value = client
await handler._request("https://hasheous.org/api")