|
| 1 | +"""Tests for the protocol-version registry and comparison helpers.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mcp.shared.version import ( |
| 6 | + DRAFT_PROTOCOL_VERSION, |
| 7 | + KNOWN_PROTOCOL_VERSIONS, |
| 8 | + STATEFUL_PROTOCOL_VERSIONS, |
| 9 | + SUPPORTED_PROTOCOL_VERSIONS, |
| 10 | + is_stateful_protocol_version, |
| 11 | + is_version_at_least, |
| 12 | +) |
| 13 | +from mcp.types import LATEST_PROTOCOL_VERSION |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize( |
| 17 | + ("version", "minimum", "expected"), |
| 18 | + [ |
| 19 | + # equal |
| 20 | + ("2025-11-25", "2025-11-25", True), |
| 21 | + ("2024-11-05", "2024-11-05", True), |
| 22 | + # above |
| 23 | + ("2025-11-25", "2025-06-18", True), |
| 24 | + ("2026-07-28", "2024-11-05", True), |
| 25 | + # below |
| 26 | + ("2025-06-18", "2025-11-25", False), |
| 27 | + ("2024-11-05", "2026-07-28", False), |
| 28 | + ], |
| 29 | +) |
| 30 | +def test_is_version_at_least_ordering(version: str, minimum: str, expected: bool) -> None: |
| 31 | + assert is_version_at_least(version, minimum) is expected |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("version", ["zzz", "", "2025-11-26", "draft", "9999-99-99"]) |
| 35 | +def test_is_version_at_least_unknown_version_is_false(version: str) -> None: |
| 36 | + """Unrecognized peer strings compare conservatively, never accidentally.""" |
| 37 | + assert is_version_at_least(version, "2024-11-05") is False |
| 38 | + |
| 39 | + |
| 40 | +def test_is_version_at_least_unknown_minimum_raises() -> None: |
| 41 | + """An unknown minimum is programmer error, not peer input.""" |
| 42 | + with pytest.raises(ValueError, match="zzz"): |
| 43 | + is_version_at_least("2025-11-25", "zzz") |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize( |
| 47 | + ("version", "minimum"), [(v, m) for v in KNOWN_PROTOCOL_VERSIONS for m in KNOWN_PROTOCOL_VERSIONS] |
| 48 | +) |
| 49 | +def test_is_version_at_least_matches_lexicographic_for_known_versions(version: str, minimum: str) -> None: |
| 50 | + """Drop-in equivalence: for every known (date-shaped) revision pair the helper |
| 51 | + agrees with the string comparison it replaced.""" |
| 52 | + assert is_version_at_least(version, minimum) is (version >= minimum) |
| 53 | + |
| 54 | + |
| 55 | +def test_draft_version_is_known_but_not_negotiable_and_not_stateful() -> None: |
| 56 | + assert DRAFT_PROTOCOL_VERSION in KNOWN_PROTOCOL_VERSIONS |
| 57 | + assert DRAFT_PROTOCOL_VERSION not in SUPPORTED_PROTOCOL_VERSIONS |
| 58 | + assert not is_stateful_protocol_version(DRAFT_PROTOCOL_VERSION) |
| 59 | + |
| 60 | + |
| 61 | +def test_draft_version_is_at_least_every_released_version() -> None: |
| 62 | + for released in SUPPORTED_PROTOCOL_VERSIONS: |
| 63 | + assert is_version_at_least(DRAFT_PROTOCOL_VERSION, released) |
| 64 | + |
| 65 | + |
| 66 | +def test_every_supported_version_is_stateful() -> None: |
| 67 | + for version in SUPPORTED_PROTOCOL_VERSIONS: |
| 68 | + assert is_stateful_protocol_version(version) |
| 69 | + |
| 70 | + |
| 71 | +def test_supported_versions_are_a_strict_subset_of_known() -> None: |
| 72 | + assert set(SUPPORTED_PROTOCOL_VERSIONS) < set(KNOWN_PROTOCOL_VERSIONS) |
| 73 | + |
| 74 | + |
| 75 | +def test_latest_version_is_stateful() -> None: |
| 76 | + assert LATEST_PROTOCOL_VERSION in STATEFUL_PROTOCOL_VERSIONS |
| 77 | + |
| 78 | + |
| 79 | +def test_known_versions_are_strictly_ordered() -> None: |
| 80 | + """The registry tuple is the ordering source of truth: ascending, no duplicates.""" |
| 81 | + assert list(KNOWN_PROTOCOL_VERSIONS) == sorted(set(KNOWN_PROTOCOL_VERSIONS)) |
0 commit comments