|
| 1 | +"""Tests for mcp-server-reddit server logic.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch, PropertyMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from mcp_server_reddit.server import RedditServer, SubredditInfo |
| 8 | + |
| 9 | + |
| 10 | +class TestGetSubredditInfo: |
| 11 | + """Test get_subreddit_info with normal and fallback paths.""" |
| 12 | + |
| 13 | + def _make_server_with_mock_client(self): |
| 14 | + """Create a RedditServer with a mocked redditwarp client.""" |
| 15 | + with patch.object(RedditServer, '__init__', lambda self: None): |
| 16 | + server = RedditServer() |
| 17 | + server.client = MagicMock() |
| 18 | + return server |
| 19 | + |
| 20 | + def test_normal_path(self): |
| 21 | + """Returns SubredditInfo when redditwarp model construction succeeds.""" |
| 22 | + server = self._make_server_with_mock_client() |
| 23 | + |
| 24 | + mock_subr = MagicMock() |
| 25 | + mock_subr.name = 'python' |
| 26 | + mock_subr.subscriber_count = 1200000 |
| 27 | + mock_subr.public_description = 'News about Python' |
| 28 | + server.client.p.subreddit.fetch_by_name.return_value = mock_subr |
| 29 | + |
| 30 | + result = server.get_subreddit_info('python') |
| 31 | + |
| 32 | + assert isinstance(result, SubredditInfo) |
| 33 | + assert result.name == 'python' |
| 34 | + assert result.subscriber_count == 1200000 |
| 35 | + assert result.description == 'News about Python' |
| 36 | + |
| 37 | + def test_fallback_on_keyerror(self): |
| 38 | + """Falls back to raw API when redditwarp raises KeyError. |
| 39 | +
|
| 40 | + This happens when Reddit removes fields from the API response |
| 41 | + that redditwarp expects (e.g. active_user_count). |
| 42 | + """ |
| 43 | + server = self._make_server_with_mock_client() |
| 44 | + |
| 45 | + # Simulate redditwarp KeyError during model construction |
| 46 | + server.client.p.subreddit.fetch_by_name.side_effect = KeyError('active_user_count') |
| 47 | + |
| 48 | + # Mock the raw API fallback |
| 49 | + server.client.request.return_value = { |
| 50 | + 'data': { |
| 51 | + 'display_name': 'python', |
| 52 | + 'subscribers': 1200000, |
| 53 | + 'public_description': 'News about Python', |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + result = server.get_subreddit_info('python') |
| 58 | + |
| 59 | + assert isinstance(result, SubredditInfo) |
| 60 | + assert result.name == 'python' |
| 61 | + assert result.subscriber_count == 1200000 |
| 62 | + assert result.description == 'News about Python' |
| 63 | + server.client.request.assert_called_once_with('GET', '/r/python/about') |
| 64 | + |
| 65 | + def test_fallback_with_missing_fields(self): |
| 66 | + """Fallback handles missing fields gracefully with defaults.""" |
| 67 | + server = self._make_server_with_mock_client() |
| 68 | + server.client.p.subreddit.fetch_by_name.side_effect = KeyError('active_user_count') |
| 69 | + |
| 70 | + # Minimal API response — missing optional fields |
| 71 | + server.client.request.return_value = { |
| 72 | + 'data': {} |
| 73 | + } |
| 74 | + |
| 75 | + result = server.get_subreddit_info('test_sub') |
| 76 | + |
| 77 | + assert result.name == 'test_sub' # falls back to argument |
| 78 | + assert result.subscriber_count == -1 # default |
| 79 | + assert result.description is None |
| 80 | + |
| 81 | + def test_non_keyerror_exceptions_propagate(self): |
| 82 | + """Non-KeyError exceptions are not caught by the fallback.""" |
| 83 | + server = self._make_server_with_mock_client() |
| 84 | + server.client.p.subreddit.fetch_by_name.side_effect = ConnectionError('network down') |
| 85 | + |
| 86 | + with pytest.raises(ConnectionError): |
| 87 | + server.get_subreddit_info('python') |
0 commit comments