|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from requests.exceptions import RequestException, Timeout |
| 5 | +from repo_context.webpage import Webpage |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def webpage(): |
| 10 | + """Base webpage instance with default settings.""" |
| 11 | + return Webpage() |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def mock_response(): |
| 16 | + """Mock successful response fixture.""" |
| 17 | + response = Mock() |
| 18 | + response.text = "<h1>Test</h1><p>Content</p>" |
| 19 | + response.raise_for_status.return_value = None |
| 20 | + return response |
| 21 | + |
| 22 | + |
| 23 | +class TestWebpage: |
| 24 | + def test_init_default_values(self): |
| 25 | + """Test initialization with default values.""" |
| 26 | + webpage = Webpage() |
| 27 | + assert webpage.timeout == 20 |
| 28 | + assert webpage.allowed_schemes == ("http", "https") |
| 29 | + assert webpage.max_retries == 3 |
| 30 | + assert "Mozilla" in webpage.user_agent |
| 31 | + |
| 32 | + @pytest.mark.parametrize( |
| 33 | + "url,valid", |
| 34 | + [ |
| 35 | + ("https://example.com", True), |
| 36 | + ("http://test.com", True), |
| 37 | + ("ftp://invalid.com", False), |
| 38 | + ("invalid-url", False), |
| 39 | + ], |
| 40 | + ) |
| 41 | + def test_validate_url(self, webpage, url, valid): |
| 42 | + """Test URL validation with various inputs.""" |
| 43 | + if valid: |
| 44 | + webpage._validate_url(url) |
| 45 | + else: |
| 46 | + with pytest.raises(ValueError): |
| 47 | + webpage._validate_url(url) |
| 48 | + |
| 49 | + @patch("requests.Session.get") |
| 50 | + def test_fetch_content_success(self, mock_get, webpage, mock_response): |
| 51 | + """Test successful content fetching.""" |
| 52 | + mock_get.return_value = mock_response |
| 53 | + content = webpage._fetch_content("https://example.com") |
| 54 | + assert content == "<h1>Test</h1><p>Content</p>" |
| 55 | + mock_get.assert_called_once() |
| 56 | + |
| 57 | + @patch("requests.Session.get") |
| 58 | + def test_fetch_content_timeout_retry(self, mock_get, webpage): |
| 59 | + """Test timeout handling with retries.""" |
| 60 | + mock_get.side_effect = Timeout() |
| 61 | + with pytest.raises(RuntimeError, match="timed out"): |
| 62 | + webpage._fetch_content("https://example.com") |
| 63 | + assert mock_get.call_count == webpage.max_retries |
| 64 | + |
| 65 | + @patch("requests.Session.get") |
| 66 | + def test_fetch_content_request_error(self, mock_get, webpage): |
| 67 | + """Test request exception handling.""" |
| 68 | + mock_get.side_effect = RequestException("Network error") |
| 69 | + with pytest.raises(RuntimeError, match="Failed to fetch"): |
| 70 | + webpage._fetch_content("https://example.com") |
| 71 | + |
| 72 | + def test_convert_to_markdown(self, webpage): |
| 73 | + """Test HTML to markdown conversion.""" |
| 74 | + html = "<h1>Test</h1><p>Content</p>\n\n\n<p>More</p>" |
| 75 | + markdown = webpage._convert_to_markdown(html) |
| 76 | + assert "Test\n====" in markdown |
| 77 | + assert "\n\n\n" not in markdown |
| 78 | + |
| 79 | + @patch("requests.Session.get") |
| 80 | + def test_get_markdown_integration(self, mock_get, webpage, mock_response): |
| 81 | + """Test complete markdown conversion flow.""" |
| 82 | + mock_get.return_value = mock_response |
| 83 | + result = webpage.get_markdown("https://example.com") |
| 84 | + assert "Test\n====" in result |
| 85 | + assert "Content" in result |
| 86 | + |
| 87 | + @patch("requests.Session.get") |
| 88 | + def test_get_markdown_caching(self, mock_get, webpage, mock_response): |
| 89 | + """Test LRU caching functionality.""" |
| 90 | + mock_get.return_value = mock_response |
| 91 | + url = "https://example.com" |
| 92 | + |
| 93 | + # First call |
| 94 | + webpage.get_markdown(url) |
| 95 | + # Second call (should use cache) |
| 96 | + webpage.get_markdown(url) |
| 97 | + |
| 98 | + mock_get.assert_called_once() |
| 99 | + |
| 100 | + def test_custom_timeout(self): |
| 101 | + """Test custom timeout configuration.""" |
| 102 | + webpage = Webpage(timeout=30) |
| 103 | + assert webpage.timeout == 30 |
| 104 | + |
| 105 | + def test_custom_schemes(self): |
| 106 | + """Test custom allowed schemes.""" |
| 107 | + webpage = Webpage(allowed_schemes=("https",)) |
| 108 | + assert webpage.allowed_schemes == ("https",) |
| 109 | + with pytest.raises(ValueError): |
| 110 | + webpage._validate_url("http://example.com") |
0 commit comments