487 lines
17 KiB
Python
487 lines
17 KiB
Python
"""Tests for the Fandom wiki faction resolver (offline: mocked HTTP)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping, Sequence
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from genshin_tcg import pipeline
|
|
from genshin_tcg.config import Settings
|
|
from genshin_tcg.constants import Faction
|
|
from genshin_tcg.data.catalog import Catalog, save_catalog
|
|
from genshin_tcg.data.fandom import (
|
|
FactionIconReport,
|
|
FactionResolution,
|
|
FandomError,
|
|
FandomSource,
|
|
faction_from_family,
|
|
faction_from_region,
|
|
parse_character_regions,
|
|
parse_enemy_families,
|
|
parse_region_emblems,
|
|
resolve_factions,
|
|
slug,
|
|
sync_faction_icons,
|
|
)
|
|
|
|
_CHARACTER_TABLE = """
|
|
<table class="fandom-table article-table">
|
|
<tr><th>Icon</th><th>Name</th><th>Quality</th><th>Region</th><th>Version</th></tr>
|
|
<tr>
|
|
<td><a href="/wiki/Ganyu" title="Ganyu"><img alt="Ganyu Icon"></a></td>
|
|
<td data-name="Ganyu"><a href="/wiki/Ganyu" title="Ganyu">Ganyu</a></td>
|
|
<td>5 Stars</td>
|
|
<td><span class="no-wrap"><a href="/wiki/Liyue" title="Liyue">
|
|
<img alt="Liyue Emblem Night" data-image-name="Liyue Emblem Night.png"></a>
|
|
<a href="/wiki/Liyue" title="Liyue">Liyue</a></span></td>
|
|
<td>1.2</td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td><a href="/wiki/Aloy" title="Aloy">Aloy</a></td>
|
|
<td>5 Stars</td>
|
|
<td>None</td>
|
|
<td>2.1</td>
|
|
</tr>
|
|
</table>
|
|
"""
|
|
|
|
_ENEMY_LIST = """
|
|
<div class="mw-parser-output">
|
|
<h2><span class="mw-headline" id="Fatui">Fatui</span></h2>
|
|
<span class="card-list-container">
|
|
<div class="card-list-header">There are 30 enemies in Fatui:</div>
|
|
<div class="card-container mini-card"><span class="card-wrapper"><span class="card-body">
|
|
<a href="/wiki/Fatui_Pyro_Agent" title="Fatui Pyro Agent"><img alt="icon"></a>
|
|
<a href="/wiki/Fatui_Pyro_Agent" title="Fatui Pyro Agent">Fatui Pyro Agent</a>
|
|
</span></span></div>
|
|
<span class="card-image-container">
|
|
<a href="/wiki/File:Thing.png" title="File:Thing.png">img</a></span>
|
|
</span>
|
|
<h2><span class="mw-headline" id="Hilichurls">Hilichurls</span></h2>
|
|
<span class="card-list-container">
|
|
<div><a href="/wiki/Stonehide_Lawachurl" title="Stonehide Lawachurl"><img alt="icon"></a>
|
|
<a href="/wiki/Stonehide_Lawachurl" title="Stonehide Lawachurl">Stonehide Lawachurl</a></div>
|
|
</span>
|
|
</div>
|
|
"""
|
|
|
|
|
|
class _FakeSource:
|
|
"""A tiny stand-in for :class:`FandomSource` in unit tests."""
|
|
|
|
def __init__(
|
|
self,
|
|
regions: Mapping[str, str] | None = None,
|
|
families: Mapping[str, str] | None = None,
|
|
) -> None:
|
|
self._regions = dict(regions or {})
|
|
self._families = dict(families or {})
|
|
|
|
def character_regions(self) -> dict[str, str]:
|
|
return dict(self._regions)
|
|
|
|
def enemy_families(self) -> dict[str, str]:
|
|
return dict(self._families)
|
|
|
|
|
|
class _FakeIconSource:
|
|
"""A tiny stand-in for the emblem side of :class:`FandomSource`."""
|
|
|
|
def __init__(
|
|
self,
|
|
emblems: Mapping[str, str] | None = None,
|
|
urls: Mapping[str, str] | None = None,
|
|
) -> None:
|
|
self._emblems = dict(emblems or {})
|
|
self._urls = dict(urls or {})
|
|
self.downloads: list[tuple[str, Path]] = []
|
|
|
|
def region_emblems(self) -> dict[str, str]:
|
|
return dict(self._emblems)
|
|
|
|
def file_urls(self, names: Sequence[str]) -> dict[str, str]:
|
|
return {name: self._urls[name] for name in names if name in self._urls}
|
|
|
|
def download(self, url: str, destination: Path, *, force: bool = False) -> bool:
|
|
self.downloads.append((url, destination))
|
|
destination.parent.mkdir(parents=True, exist_ok=True)
|
|
destination.write_bytes(b"png")
|
|
return True
|
|
|
|
|
|
# --- Pure helpers ------------------------------------------------------------
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("text", "expected"),
|
|
[
|
|
("Kamisato Ayaka", "kamisato_ayaka"),
|
|
("Nod-Krai", "nod_krai"),
|
|
("Khaenri'ah", "khaenriah"),
|
|
(" Spaced Name ", "spaced_name"),
|
|
("Élégant", "elegant"),
|
|
],
|
|
)
|
|
def test_slug(text: str, expected: str) -> None:
|
|
assert slug(text) == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("region", "expected"),
|
|
[
|
|
("Mondstadt", Faction.MONDSTADT),
|
|
("Liyue", Faction.LIYUE),
|
|
("Snezhnaya", Faction.SNEZHNAYA),
|
|
("Nod-Krai", Faction.NOD_KRAI),
|
|
("Khaenri'ah", Faction.KHAENRIAH),
|
|
("None", None),
|
|
("Something Else", None),
|
|
],
|
|
)
|
|
def test_faction_from_region(region: str, expected: Faction | None) -> None:
|
|
assert faction_from_region(region) is expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("family", "expected"),
|
|
[
|
|
("Fatui", Faction.FATUI),
|
|
("Hilichurls", Faction.HILICHURL),
|
|
("Automatons", Faction.MONSTER),
|
|
("Mystical Beasts", Faction.MONSTER),
|
|
],
|
|
)
|
|
def test_faction_from_family(family: str, expected: Faction) -> None:
|
|
assert faction_from_family(family) is expected
|
|
|
|
|
|
# --- HTML extractors ---------------------------------------------------------
|
|
|
|
|
|
def test_parse_character_regions() -> None:
|
|
assert parse_character_regions(_CHARACTER_TABLE) == {"ganyu": "Liyue", "aloy": "None"}
|
|
|
|
|
|
def test_parse_character_regions_requires_table() -> None:
|
|
with pytest.raises(FandomError):
|
|
parse_character_regions("<p>no table here</p>")
|
|
|
|
|
|
def test_parse_character_regions_rejects_unexpected_header() -> None:
|
|
with pytest.raises(FandomError):
|
|
parse_character_regions('<table class="fandom-table"><tr><th>Foo</th></tr></table>')
|
|
|
|
|
|
def test_parse_region_emblems() -> None:
|
|
assert parse_region_emblems(_CHARACTER_TABLE) == {"Liyue": "Liyue Emblem Night.png"}
|
|
|
|
|
|
def test_parse_enemy_families_skips_icons_and_non_wiki_links() -> None:
|
|
families = parse_enemy_families(_ENEMY_LIST)
|
|
assert families == {
|
|
"fatui_pyro_agent": "Fatui",
|
|
"stonehide_lawachurl": "Hilichurls",
|
|
}
|
|
|
|
|
|
def test_parse_enemy_families_without_wrapper_is_tolerant() -> None:
|
|
assert parse_enemy_families("<p>empty</p>") == {}
|
|
|
|
|
|
# --- Resolution --------------------------------------------------------------
|
|
|
|
|
|
def test_resolve_factions_matches_playable_and_enemy(sample_catalog: Catalog) -> None:
|
|
source = _FakeSource(regions={"test_pyro": "Mondstadt"})
|
|
resolution = resolve_factions(sample_catalog, source=source)
|
|
assert isinstance(resolution, FactionResolution)
|
|
assert resolution.assignments == {"test_pyro": "mondstadt"}
|
|
assert resolution.playable == ("test_pyro",)
|
|
assert resolution.matched == 1
|
|
|
|
|
|
def test_resolve_factions_falls_back_to_family(sample_catalog: Catalog) -> None:
|
|
source = _FakeSource(families={"test_pyro": "Fatui"})
|
|
resolution = resolve_factions(sample_catalog, source=source)
|
|
assert resolution.assignments == {"test_pyro": "fatui"}
|
|
assert resolution.enemies == ("test_pyro",)
|
|
|
|
|
|
def test_resolve_factions_ignores_unmappable_region(sample_catalog: Catalog) -> None:
|
|
source = _FakeSource(regions={"test_pyro": "None"})
|
|
resolution = resolve_factions(sample_catalog, source=source)
|
|
assert resolution.assignments == {}
|
|
assert resolution.unmatched == ("test_pyro",)
|
|
|
|
|
|
def test_resolve_factions_reports_unmatched(sample_catalog: Catalog) -> None:
|
|
resolution = resolve_factions(sample_catalog, source=_FakeSource())
|
|
assert resolution.unmatched == ("test_pyro",)
|
|
assert resolution.matched == 0
|
|
|
|
|
|
# --- Emblem sync -------------------------------------------------------------
|
|
|
|
|
|
def _resolution(assignments: Mapping[str, str]) -> FactionResolution:
|
|
return FactionResolution(assignments=dict(assignments))
|
|
|
|
|
|
def test_sync_faction_icons_downloads_nation_emblems(tmp_path: Path) -> None:
|
|
resolution = _resolution({"ganyu": "liyue", "diluc": "mondstadt", "rhodeia": "monster"})
|
|
source = _FakeIconSource(
|
|
emblems={"Liyue": "Liyue Emblem Night.png", "Mondstadt": "Mondstadt Emblem Night.png"},
|
|
urls={
|
|
"Liyue Emblem Night.png": "https://wiki/liyue.png",
|
|
"Mondstadt Emblem Night.png": "https://wiki/mondstadt.png",
|
|
},
|
|
)
|
|
|
|
report = sync_faction_icons(resolution, source=source, assets_dir=tmp_path)
|
|
|
|
assert isinstance(report, FactionIconReport)
|
|
assert report.downloaded == ("liyue", "mondstadt")
|
|
assert report.missing == ("monster",)
|
|
assert report.ok
|
|
assert (tmp_path / "liyue.png").is_file()
|
|
assert (tmp_path / "mondstadt.png").is_file()
|
|
|
|
|
|
def test_sync_faction_icons_reports_missing_url(tmp_path: Path) -> None:
|
|
resolution = _resolution({"ganyu": "liyue"})
|
|
source = _FakeIconSource(emblems={"Liyue": "Liyue Emblem Night.png"})
|
|
|
|
report = sync_faction_icons(resolution, source=source, assets_dir=tmp_path)
|
|
|
|
assert report.failed == {"liyue": "no image URL for 'Liyue Emblem Night.png'"}
|
|
assert not report.ok
|
|
|
|
|
|
# --- HTTP client -------------------------------------------------------------
|
|
|
|
|
|
def _ok_response(text: str) -> httpx.Response:
|
|
return httpx.Response(200, json={"parse": {"title": "Page", "text": text}})
|
|
|
|
|
|
def test_fandom_source_caches_pages(tmp_path: Path) -> None:
|
|
calls: list[str] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
calls.append(str(request.url))
|
|
return _ok_response("<html>ok</html>")
|
|
|
|
client = httpx.Client(transport=httpx.MockTransport(handler))
|
|
with FandomSource(cache_dir=tmp_path, client=client) as source:
|
|
assert source.render_page("Character/List") == "<html>ok</html>"
|
|
assert source.render_page("Character/List") == "<html>ok</html>"
|
|
|
|
assert len(calls) == 1
|
|
assert (tmp_path / "character_list.html").is_file()
|
|
|
|
|
|
def test_fandom_source_force_refetches(tmp_path: Path) -> None:
|
|
calls: list[str] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
calls.append(str(request.url))
|
|
return _ok_response("<html>ok</html>")
|
|
|
|
client = httpx.Client(transport=httpx.MockTransport(handler))
|
|
with FandomSource(cache_dir=tmp_path, client=client) as source:
|
|
source.render_page("Character/List")
|
|
source.render_page("Character/List", force=True)
|
|
assert len(calls) == 2
|
|
|
|
|
|
def test_fandom_source_builds_lookups(tmp_path: Path) -> None:
|
|
bodies = {"Character/List": _CHARACTER_TABLE, "Enemy/List": _ENEMY_LIST}
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
page = request.url.params["page"]
|
|
return _ok_response(bodies[page])
|
|
|
|
client = httpx.Client(transport=httpx.MockTransport(handler))
|
|
with FandomSource(cache_dir=tmp_path, client=client) as source:
|
|
assert source.character_regions() == {"ganyu": "Liyue", "aloy": "None"}
|
|
assert source.enemy_families() == {
|
|
"fatui_pyro_agent": "Fatui",
|
|
"stonehide_lawachurl": "Hilichurls",
|
|
}
|
|
assert source.region_emblems() == {"Liyue": "Liyue Emblem Night.png"}
|
|
|
|
|
|
def test_fandom_source_file_urls(tmp_path: Path) -> None:
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
assert request.url.params["action"] == "query"
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"query": {
|
|
"pages": [
|
|
{
|
|
"title": "File:Liyue Emblem Night.png",
|
|
"imageinfo": [{"url": "https://wiki/liyue.png"}],
|
|
},
|
|
{"title": "File:Nope.png", "missing": True},
|
|
]
|
|
}
|
|
},
|
|
)
|
|
|
|
client = httpx.Client(transport=httpx.MockTransport(handler))
|
|
with FandomSource(cache_dir=tmp_path, client=client) as source:
|
|
assert source.file_urls(["Liyue Emblem Night.png", "Nope.png", ""]) == {
|
|
"Liyue Emblem Night.png": "https://wiki/liyue.png"
|
|
}
|
|
|
|
|
|
def test_fandom_source_file_urls_empty(tmp_path: Path) -> None:
|
|
client = httpx.Client(transport=httpx.MockTransport(lambda r: httpx.Response(500)))
|
|
with FandomSource(cache_dir=tmp_path, client=client) as source:
|
|
assert source.file_urls([]) == {}
|
|
|
|
|
|
def test_fandom_source_downloads_and_caches(tmp_path: Path) -> None:
|
|
calls: list[str] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
calls.append(str(request.url))
|
|
return httpx.Response(200, content=b"image-bytes")
|
|
|
|
destination = tmp_path / "liyue.png"
|
|
client = httpx.Client(transport=httpx.MockTransport(handler))
|
|
with FandomSource(cache_dir=tmp_path, client=client) as source:
|
|
assert source.download("https://wiki/liyue.png", destination) is True
|
|
assert source.download("https://wiki/liyue.png", destination) is False
|
|
|
|
assert destination.read_bytes() == b"image-bytes"
|
|
assert len(calls) == 1
|
|
|
|
|
|
def test_fandom_source_download_raises_on_error(tmp_path: Path) -> None:
|
|
client = httpx.Client(transport=httpx.MockTransport(lambda r: httpx.Response(404)))
|
|
with FandomSource(cache_dir=tmp_path, client=client) as source, pytest.raises(FandomError):
|
|
source.download("https://wiki/missing.png", tmp_path / "missing.png")
|
|
|
|
|
|
def test_fandom_source_raises_on_bad_payload(tmp_path: Path) -> None:
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(200, json={"error": "nope"})
|
|
|
|
client = httpx.Client(transport=httpx.MockTransport(handler))
|
|
with FandomSource(cache_dir=tmp_path, client=client) as source, pytest.raises(FandomError):
|
|
source.render_page("Character/List")
|
|
|
|
|
|
def test_fandom_source_raises_on_http_error(tmp_path: Path) -> None:
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
return httpx.Response(500)
|
|
|
|
client = httpx.Client(transport=httpx.MockTransport(handler))
|
|
with FandomSource(cache_dir=tmp_path, client=client) as source, pytest.raises(FandomError):
|
|
source.render_page("Character/List")
|
|
|
|
|
|
# --- Pipeline stage ----------------------------------------------------------
|
|
|
|
|
|
def test_pipeline_factions_writes_overrides(
|
|
settings: Settings, sample_catalog: Catalog
|
|
) -> None:
|
|
settings.ensure_directories()
|
|
save_catalog(sample_catalog, settings.catalog_path)
|
|
|
|
result = pipeline.factions(settings, source=_FakeSource(regions={"test_pyro": "Mondstadt"}))
|
|
|
|
assert result.written is True
|
|
assert result.added == ("test_pyro",)
|
|
assert result.output.is_file()
|
|
assert '"test_pyro": "mondstadt"' in result.output.read_text(encoding="utf-8")
|
|
|
|
|
|
def test_pipeline_factions_preserves_hand_made_entries(
|
|
settings: Settings, sample_catalog: Catalog
|
|
) -> None:
|
|
settings.ensure_directories()
|
|
save_catalog(sample_catalog, settings.catalog_path)
|
|
settings.factions_override.parent.mkdir(parents=True, exist_ok=True)
|
|
settings.factions_override.write_text(
|
|
'{"test_pyro": "liyue", "manual_card": "fatui"}\n', encoding="utf-8"
|
|
)
|
|
|
|
result = pipeline.factions(settings, source=_FakeSource(regions={"test_pyro": "Mondstadt"}))
|
|
|
|
assert result.conflicts == ("test_pyro",)
|
|
written = result.output.read_text(encoding="utf-8")
|
|
assert '"test_pyro": "liyue"' in written
|
|
assert '"manual_card": "fatui"' in written
|
|
|
|
|
|
def test_pipeline_factions_overwrite_discards_hand_made_entries(
|
|
settings: Settings, sample_catalog: Catalog
|
|
) -> None:
|
|
settings.ensure_directories()
|
|
save_catalog(sample_catalog, settings.catalog_path)
|
|
settings.factions_override.parent.mkdir(parents=True, exist_ok=True)
|
|
settings.factions_override.write_text('{"manual_card": "fatui"}\n', encoding="utf-8")
|
|
|
|
result = pipeline.factions(
|
|
settings, overwrite=True, source=_FakeSource(regions={"test_pyro": "Mondstadt"})
|
|
)
|
|
|
|
written = result.output.read_text(encoding="utf-8")
|
|
assert '"test_pyro": "mondstadt"' in written
|
|
assert "manual_card" not in written
|
|
|
|
|
|
def test_pipeline_factions_dry_run_does_not_write(
|
|
settings: Settings, sample_catalog: Catalog
|
|
) -> None:
|
|
settings.ensure_directories()
|
|
save_catalog(sample_catalog, settings.catalog_path)
|
|
|
|
result = pipeline.factions(
|
|
settings, write=False, source=_FakeSource(regions={"test_pyro": "Mondstadt"})
|
|
)
|
|
|
|
assert result.written is False
|
|
assert not result.output.exists()
|
|
|
|
|
|
def test_pipeline_factions_downloads_icons(settings: Settings, sample_catalog: Catalog) -> None:
|
|
settings.ensure_directories()
|
|
save_catalog(sample_catalog, settings.catalog_path)
|
|
icon_source = _FakeIconSource(
|
|
emblems={"Mondstadt": "Mondstadt Emblem Night.png"},
|
|
urls={"Mondstadt Emblem Night.png": "https://wiki/mondstadt.png"},
|
|
)
|
|
|
|
result = pipeline.factions(
|
|
settings,
|
|
source=_FakeSource(regions={"test_pyro": "Mondstadt"}),
|
|
icon_source=icon_source,
|
|
)
|
|
|
|
assert result.icons is not None
|
|
assert result.icons.downloaded == ("mondstadt",)
|
|
assert (settings.faction_icons_dir / "mondstadt.png").is_file()
|
|
|
|
|
|
def test_pipeline_factions_can_skip_icons(settings: Settings, sample_catalog: Catalog) -> None:
|
|
settings.ensure_directories()
|
|
save_catalog(sample_catalog, settings.catalog_path)
|
|
|
|
result = pipeline.factions(
|
|
settings,
|
|
icons=False,
|
|
source=_FakeSource(regions={"test_pyro": "Mondstadt"}),
|
|
)
|
|
|
|
assert result.icons is None
|
|
assert not settings.faction_icons_dir.exists()
|