"""Tests for the template view builder."""
from __future__ import annotations
from pathlib import Path
from genshin_tcg.data.catalog import Catalog
from genshin_tcg.models import parse_catalog
from genshin_tcg.rendering.context import (
ActionView,
CharacterView,
build_view,
load_faction_overrides,
load_labels,
)
def _character(catalog: Catalog) -> CharacterView:
view = build_view(
catalog.get("test_pyro"),
assets_dir=Path("assets"),
html_dir=Path("build") / "html",
width_px=744,
height_px=1039,
)
assert isinstance(view, CharacterView)
return view
def _action(catalog: Catalog) -> ActionView:
view = build_view(
catalog.get("test_talent"),
assets_dir=Path("assets"),
html_dir=Path("build") / "html",
width_px=744,
height_px=1039,
)
assert isinstance(view, ActionView)
return view
def test_character_view_fields(sample_catalog: Catalog) -> None:
view = _character(sample_catalog)
assert view.kind == "character"
assert view.name == "Test Pyro"
assert view.hp == 10
assert view.energy == 3
assert view.css_class == "el-pyro"
assert view.width_px == 744
assert view.illustration == "../../assets/tcg/test_pyro.png"
assert view.energy_icon.endswith("assets/tcg/icons/energy_card.png")
assert view.hp_icon.endswith("assets/tcg/icons/hp.png")
def test_character_tags_include_element_and_weapon(sample_catalog: Catalog) -> None:
view = _character(sample_catalog)
kinds = [tag.kind for tag in view.tags]
assert kinds == ["element", "weapon"]
assert view.tags[0].label == "Pyro"
element_icon = view.tags[0].icon
assert element_icon is not None
assert element_icon.endswith("assets/tcg/icons/elements/pyro.png")
assert view.tags[1].label == "Épée à deux mains"
def test_character_tag_order_matches_the_official_print(
tmp_path: Path, sample_catalog: Catalog
) -> None:
"""The official layout stacks element, nation/faction, then weapon."""
view = build_view(
sample_catalog.get("test_pyro"),
assets_dir=tmp_path / "assets",
html_dir=tmp_path / "build" / "html",
factions={"test_pyro": "liyue"},
width_px=744,
height_px=1039,
)
assert isinstance(view, CharacterView)
assert [tag.kind for tag in view.tags] == ["element", "faction", "weapon"]
def test_none_token_labels_do_not_collide(tmp_path: Path) -> None:
"""``WeaponType.NONE`` ("other weapon") and ``CardType.NONE`` ("event card")
share the ``"none"`` token but must not share a label."""
catalog = Catalog(
cards=tuple(
parse_catalog(
[
{
"_id": 1,
"id": "monster",
"type": "character",
"name": "Monstre",
"hp": 8,
"energy": 0,
"element": "hydro",
"weapon": "none",
"skills": [
{
"_id": 11,
"type": "Attaque normale",
"name": "Charge",
"desc": "Inflige 2 pts de DGT physiques.",
"cost": [{"type": "hydro", "count": 1}],
}
],
},
{
"_id": 2,
"id": "event",
"type": "none",
"name": "Événement",
"skills": {"cost": [], "desc": "Rien."},
},
]
)
)
)
monster = build_view(
catalog.get("monster"),
assets_dir=tmp_path / "assets",
html_dir=tmp_path / "build" / "html",
width_px=744,
height_px=1039,
)
event = build_view(
catalog.get("event"),
assets_dir=tmp_path / "assets",
html_dir=tmp_path / "build" / "html",
width_px=744,
height_px=1039,
)
assert isinstance(monster, CharacterView)
assert isinstance(event, ActionView)
assert monster.weapon_label == "Autre arme"
assert [tag.label for tag in monster.tags if tag.kind == "weapon"] == ["Autre arme"]
assert event.type_label == "Événement"
def test_character_skills_and_costs(sample_catalog: Catalog) -> None:
view = _character(sample_catalog)
assert len(view.skills) == 3
first = view.skills[0]
assert first.name == "Frappe de test"
assert first.icon is not None and first.icon.endswith("skills/test_pyro/skill_1.png")
assert [die.count for die in first.cost] == [1, 2]
assert first.cost[0].icon.endswith("assets/tcg/icons/pyro.png")
assert first.cost[1].icon.endswith("assets/tcg/icons/void.png")
def test_character_description_is_cleaned_and_highlighted(sample_catalog: Catalog) -> None:
view = _character(sample_catalog)
assert "
" in view.skills[0].description_html
assert 'DGT ' in view.skills[1].description_html
assert "inline-die" in view.skills[1].description_html
assert view.skills[1].sub_name == "Flamme éternelle"
assert view.skills[1].sub_description_html is not None
def test_inline_element_icons_can_be_disabled(sample_catalog: Catalog) -> None:
with_icons = _character(sample_catalog)
assert "inline-die" in with_icons.skills[1].description_html
without = build_view(
sample_catalog.get("test_pyro"),
assets_dir=Path("assets"),
html_dir=Path("build") / "html",
width_px=744,
height_px=1039,
inline_element_icons=False,
)
assert isinstance(without, CharacterView)
assert "inline-die" not in without.skills[1].description_html
def test_action_view_fields(sample_catalog: Catalog) -> None:
view = _action(sample_catalog)
assert view.kind == "action"
assert view.css_class == "type-talent"
assert view.type_label == "Aptitude"
assert view.type_icon.endswith("assets/tcg/icons/types/talent.png")
assert view.requirement == "test_pyro"
assert [die.count for die in view.cost] == [4]
assert "
" in view.description_html
assert 'Action de combat' in view.description_html
def test_action_without_cost_gets_placeholder_die(sample_catalog: Catalog) -> None:
view = build_view(
sample_catalog.get("test_artifact"),
assets_dir=Path("assets"),
html_dir=Path("build") / "html",
width_px=744,
height_px=1039,
)
assert isinstance(view, ActionView)
assert len(view.cost) == 1
assert view.cost[0].count == 0
assert view.cost[0].icon.endswith("assets/tcg/icons/same.png")
def test_faction_override_without_icon(tmp_path: Path, sample_catalog: Catalog) -> None:
view = build_view(
sample_catalog.get("test_pyro"),
assets_dir=tmp_path / "assets",
html_dir=tmp_path / "build" / "html",
factions={"test_pyro": "liyue"},
width_px=744,
height_px=1039,
)
assert isinstance(view, CharacterView)
faction = next(tag for tag in view.tags if tag.kind == "faction")
assert faction.label == "Liyue"
assert faction.icon is None
def test_faction_override_with_icon(tmp_path: Path, sample_catalog: Catalog) -> None:
icon = tmp_path / "assets" / "tcg" / "icons" / "factions" / "liyue.png"
icon.parent.mkdir(parents=True)
icon.write_bytes(b"x")
view = build_view(
sample_catalog.get("test_pyro"),
assets_dir=tmp_path / "assets",
html_dir=tmp_path / "build" / "html",
factions={"test_pyro": "liyue"},
width_px=744,
height_px=1039,
)
assert isinstance(view, CharacterView)
faction = next(tag for tag in view.tags if tag.kind == "faction")
assert faction.icon is not None and faction.icon.endswith("factions/liyue.png")
def test_load_labels_merges_file(tmp_path: Path) -> None:
path = tmp_path / "tags_fr.json"
path.write_text('{"pyro": "Feu", "unique": "Unique"}', encoding="utf-8")
labels = load_labels(path)
assert labels["pyro"] == "Feu"
assert labels["unique"] == "Unique"
assert labels["talent"] == "Aptitude" # built-in fallback retained
def test_load_labels_missing_file_uses_defaults(tmp_path: Path) -> None:
labels = load_labels(tmp_path / "nope.json")
assert labels["cryo"] == "Cryo"
def test_load_faction_overrides(tmp_path: Path) -> None:
path = tmp_path / "factions.json"
path.write_text('{"ganyu": "liyue"}', encoding="utf-8")
assert load_faction_overrides(path) == {"ganyu": "liyue"}
def test_load_faction_overrides_missing(tmp_path: Path) -> None:
assert load_faction_overrides(tmp_path / "nope.json") == {}