"""EH-08 — HmacVerificationError carries a machine-readable ``reason``."""

from __future__ import annotations

import pytest

from mgf.fastapi.exceptions import HmacVerificationError
from mgf.fastapi.webhooks import HmacWebhookVerifier

_V = HmacWebhookVerifier(secret=b"whsec_x", replay_window_seconds=300, now=lambda: 1000.0)


@pytest.mark.parametrize(
    ("headers", "expected_reason"),
    [
        ({"svix-timestamp": "1000", "svix-signature": "v1,x"}, "missing_header"),
        ({"svix-id": "e", "svix-signature": "v1,x"}, "missing_header"),
        ({"svix-id": "e", "svix-timestamp": "nope", "svix-signature": "v1,x"}, "bad_timestamp"),
        ({"svix-id": "e", "svix-timestamp": "1", "svix-signature": "v1,x"}, "replay_window"),
        (
            {"svix-id": "e", "svix-timestamp": "1000", "svix-signature": "v1,wrong"},
            "signature_mismatch",
        ),
    ],
)
def test_reason_is_set_per_failure_mode(headers: dict[str, str], expected_reason: str) -> None:
    with pytest.raises(HmacVerificationError) as info:
        _V.verify(headers, b"body")
    assert info.value.reason == expected_reason
