"""The locations CLI resolves user-typed codes strictly.

A demo set silently missing an airport is worse than a rejected command, so an
unmatched token must fail loudly rather than be skipped.
"""

from types import SimpleNamespace

import pytest

from app.cli import match_locations


def loc(code: str, iata: str | None = None):
    return SimpleNamespace(code=code, iata=iata)


class TestMatchLocations:
    def test_matches_by_code_case_insensitively(self):
        rows = [loc("heathrow", "LHR"), loc("jfk-t1", "JFK")]
        assert match_locations(rows, ["HEATHROW"]) == [rows[0]]

    def test_matches_by_iata(self):
        rows = [loc("heathrow", "LHR"), loc("jfk-t1", "JFK")]
        assert match_locations(rows, ["lhr"]) == [rows[0]]

    def test_one_iata_matches_every_terminal(self):
        rows = [loc("jfk-t1", "JFK"), loc("jfk-t4", "JFK"), loc("heathrow", "LHR")]
        assert match_locations(rows, ["JFK"]) == [rows[0], rows[1]]

    def test_unknown_token_fails_loudly(self):
        rows = [loc("heathrow", "LHR")]
        with pytest.raises(SystemExit, match="XYZ"):
            match_locations(rows, ["LHR", "XYZ"])

    def test_location_without_iata_still_matches_by_code(self):
        rows = [loc("bordershop", None)]
        assert match_locations(rows, ["bordershop"]) == rows
