"""The airport flag band: every launch country has a flag file and a map entry.

Brand pass two (11 Sep) put a flag band on the airport cards. The band is CSS
fed by `web/src/lib/countries.ts`, declared data keyed on the country name the
database stores; a name the map does not know gets no band. What this pins:
the nineteen launch airports' countries are all mapped, every mapped file
exists under `web/public/flags/`, and no file is dead weight. Cost if it
drifts: a card silently loses its band, and nobody notices until Adam does.
"""

import pathlib
import re

WEB = pathlib.Path(__file__).resolve().parents[1] / "web"

# Country names exactly as `locations.country` stores them for the nineteen.
LAUNCH_COUNTRIES = {
    "Greece", "Spain", "Colombia", "France", "Ireland", "United Arab Emirates",
    "Argentina", "Hong Kong", "South Korea", "United States", "United Kingdom",
    "Mexico", "Panama", "El Salvador", "Singapore", "Canada", "Switzerland",
}


def _map() -> dict[str, str]:
    src = (WEB / "src" / "lib" / "countries.ts").read_text()
    body = src.split("FLAG_OF_COUNTRY", 1)[1].split("};", 1)[0]
    return dict(re.findall(r'"([^"]+)":\s*"([a-z]{2})"', body))


class TestFlagMap:
    def test_every_launch_country_has_a_flag(self):
        missing = LAUNCH_COUNTRIES - set(_map())
        assert not missing, f"no flag mapped for {sorted(missing)}"

    def test_every_mapped_flag_file_exists_and_is_small(self):
        for country, code in _map().items():
            f = WEB / "public" / "flags" / f"{code}.svg"
            assert f.is_file(), f"{country} -> {code}.svg missing"
            assert f.stat().st_size < 20_000, f"{code}.svg is {f.stat().st_size} bytes; a band never shows that detail"

    def test_no_orphan_flag_file(self):
        mapped = set(_map().values())
        on_disk = {p.stem for p in (WEB / "public" / "flags").glob("*.svg")}
        assert on_disk <= mapped, f"unmapped flag files: {sorted(on_disk - mapped)}"
