"""Party-size ceiling behaviour (spec rsvp-flow FR-2.2).

Regression test for a bug found the first time the open-link flow ran against a real
database: a walk-in submitting party_size=2 was silently recorded as 1, because the
invitation's max_guests defaulted to 1 while the form offered up to 5. Under-counting
here means under-ordering catering, which is the outcome this system exists to prevent.
"""

from app.config import Settings


def clamp(requested: int, max_guests: int) -> int:
    """Mirrors the ceiling logic in rsvp_service.accept."""
    return max(1, min(requested, max_guests))


def test_host_issued_ceiling_is_enforced() -> None:
    """A host who invited a family of four should not receive six."""
    assert clamp(6, 4) == 4
    assert clamp(3, 4) == 3


def test_party_size_never_drops_below_one() -> None:
    assert clamp(0, 4) == 1
    assert clamp(-5, 4) == 1


def test_open_link_default_allows_a_real_party() -> None:
    """The open-link ceiling must match what the form actually offers, or submissions get
    silently truncated."""
    ceiling = Settings().open_link_max_guests
    assert ceiling >= 5, "open-link ceiling must cover the 1-5 range the form presents"
    assert clamp(2, ceiling) == 2
    assert clamp(5, ceiling) == 5


def test_model_default_of_one_would_have_truncated() -> None:
    """Documents the exact bug, so nobody 'simplifies' the default back."""
    assert clamp(2, 1) == 1
