|
| 1 | +"""Tests for the distill_html function and distill parameter integration.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from mcp_server_fetch.server import distill_html, extract_content_from_html |
| 6 | + |
| 7 | + |
| 8 | +class TestDistillHtml: |
| 9 | + """Tests for the distill_html function using BeautifulSoup.""" |
| 10 | + |
| 11 | + def test_removes_script_tags(self): |
| 12 | + html = "<html><body><script>evil();</script><p>Safe</p></body></html>" |
| 13 | + result = distill_html(html) |
| 14 | + assert "<script" not in result.lower() |
| 15 | + assert "Safe" in result |
| 16 | + |
| 17 | + def test_removes_style_tags(self): |
| 18 | + html = "<html><body><style>.x{color:red}</style><p>Content</p></body></html>" |
| 19 | + result = distill_html(html) |
| 20 | + assert "<style" not in result.lower() |
| 21 | + assert "Content" in result |
| 22 | + |
| 23 | + def test_removes_nav_tags(self): |
| 24 | + html = "<html><body><nav><a href='/'>Home</a></nav><p>Article</p></body></html>" |
| 25 | + result = distill_html(html) |
| 26 | + assert "<nav" not in result.lower() |
| 27 | + assert "Article" in result |
| 28 | + |
| 29 | + def test_removes_header_tags(self): |
| 30 | + html = "<html><body><header>Logo</header><p>Main</p></body></html>" |
| 31 | + result = distill_html(html) |
| 32 | + assert "<header" not in result.lower() |
| 33 | + assert "Main" in result |
| 34 | + |
| 35 | + def test_removes_footer_tags(self): |
| 36 | + html = "<html><body><p>Content</p><footer>Copyright</footer></body></html>" |
| 37 | + result = distill_html(html) |
| 38 | + assert "<footer" not in result.lower() |
| 39 | + assert "Content" in result |
| 40 | + |
| 41 | + def test_removes_aside_tags(self): |
| 42 | + html = "<html><body><aside>Sidebar</aside><p>Main</p></body></html>" |
| 43 | + result = distill_html(html) |
| 44 | + assert "<aside" not in result.lower() |
| 45 | + assert "Main" in result |
| 46 | + |
| 47 | + def test_removes_iframe_tags(self): |
| 48 | + html = '<html><body><iframe src="ad.html"></iframe><p>Content</p></body></html>' |
| 49 | + result = distill_html(html) |
| 50 | + assert "<iframe" not in result.lower() |
| 51 | + assert "Content" in result |
| 52 | + |
| 53 | + def test_removes_svg_tags(self): |
| 54 | + html = "<html><body><svg><circle/></svg><p>Content</p></body></html>" |
| 55 | + result = distill_html(html) |
| 56 | + assert "<svg" not in result.lower() |
| 57 | + assert "Content" in result |
| 58 | + |
| 59 | + def test_removes_form_elements(self): |
| 60 | + html = "<html><body><form><input/><button>Go</button></form><p>Content</p></body></html>" |
| 61 | + result = distill_html(html) |
| 62 | + assert "<form" not in result.lower() |
| 63 | + assert "Content" in result |
| 64 | + |
| 65 | + def test_removes_html_comments(self): |
| 66 | + html = "<html><body><!-- secret --><p>Visible</p></body></html>" |
| 67 | + result = distill_html(html) |
| 68 | + assert "<!--" not in result |
| 69 | + assert "secret" not in result |
| 70 | + assert "Visible" in result |
| 71 | + |
| 72 | + def test_removes_nested_nav_tags(self): |
| 73 | + """Verifies BeautifulSoup handles nested tags (unlike regex).""" |
| 74 | + html = "<html><body><nav><nav>inner</nav>outer</nav><p>Content</p></body></html>" |
| 75 | + result = distill_html(html) |
| 76 | + assert "<nav" not in result.lower() |
| 77 | + assert "inner" not in result |
| 78 | + assert "outer" not in result |
| 79 | + assert "Content" in result |
| 80 | + |
| 81 | + def test_removes_elements_with_ad_class(self): |
| 82 | + html = '<html><body><div class="sidebar-ad">Buy now!</div><p>Real</p></body></html>' |
| 83 | + result = distill_html(html) |
| 84 | + assert "Buy now" not in result |
| 85 | + assert "Real" in result |
| 86 | + |
| 87 | + def test_removes_elements_with_cookie_id(self): |
| 88 | + html = '<html><body><div id="cookie-consent">Accept</div><p>Content</p></body></html>' |
| 89 | + result = distill_html(html) |
| 90 | + assert "Accept" not in result |
| 91 | + assert "Content" in result |
| 92 | + |
| 93 | + def test_removes_elements_with_social_class(self): |
| 94 | + html = '<html><body><div class="social-share">Share us</div><p>Article</p></body></html>' |
| 95 | + result = distill_html(html) |
| 96 | + assert "Share us" not in result |
| 97 | + assert "Article" in result |
| 98 | + |
| 99 | + def test_removes_empty_elements(self): |
| 100 | + html = "<html><body><div></div><span> </span><p>Content</p></body></html>" |
| 101 | + result = distill_html(html) |
| 102 | + assert "Content" in result |
| 103 | + |
| 104 | + def test_preserves_br_hr_img(self): |
| 105 | + html = '<html><body><p>Before</p><br/><hr/><img src="x.jpg"/><p>After</p></body></html>' |
| 106 | + result = distill_html(html) |
| 107 | + assert "Before" in result |
| 108 | + assert "After" in result |
| 109 | + |
| 110 | + def test_returns_string(self): |
| 111 | + html = "<html><body><p>Hello</p></body></html>" |
| 112 | + result = distill_html(html) |
| 113 | + assert isinstance(result, str) |
| 114 | + |
| 115 | + |
| 116 | +class TestExtractContentDistillIntegration: |
| 117 | + """Tests for the distill parameter in extract_content_from_html.""" |
| 118 | + |
| 119 | + def test_distill_false_returns_content(self): |
| 120 | + html = "<html><body><article><h1>Title</h1><p>Body text.</p></article></body></html>" |
| 121 | + result = extract_content_from_html(html, distill=False) |
| 122 | + assert "Body text" in result |
| 123 | + |
| 124 | + def test_distill_true_returns_content(self): |
| 125 | + html = "<html><body><article><h1>Title</h1><p>Body text.</p></article></body></html>" |
| 126 | + result = extract_content_from_html(html, distill=True) |
| 127 | + assert "Body text" in result |
| 128 | + |
| 129 | + def test_distill_true_does_not_produce_empty_output(self): |
| 130 | + """Ensures distill doesn't strip core content (the 13-token bug).""" |
| 131 | + html = """<html><body> |
| 132 | + <article> |
| 133 | + <h1>Important Article</h1> |
| 134 | + <p>First paragraph with real content.</p> |
| 135 | + <p>Second paragraph with more details.</p> |
| 136 | + </article> |
| 137 | + </body></html>""" |
| 138 | + result = extract_content_from_html(html, distill=True) |
| 139 | + # Should have meaningful content, not just a few tokens |
| 140 | + assert len(result.split()) > 5 |
| 141 | + |
| 142 | + def test_distill_default_is_false(self): |
| 143 | + html = "<html><body><article><p>Content</p></article></body></html>" |
| 144 | + result_default = extract_content_from_html(html) |
| 145 | + result_explicit = extract_content_from_html(html, distill=False) |
| 146 | + assert result_default == result_explicit |
0 commit comments