test: 添加 PDFSplitter 和 HTMLSplitter 测试

This commit is contained in:
2026-07-10 14:25:40 +08:00
parent 9db0c15dee
commit b8afc9043d
2 changed files with 91 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
"""HTMLSplitter 测试."""
import pytest
bs4 = pytest.importorskip("bs4", reason="beautifulsoup4 未安装")
class TestHTMLSplitter:
"""HTMLSplitter 测试(需 beautifulsoup4."""
def test_split_simple_html(self):
from src.core.splitters.html import HTMLSplitter
html = "<html><body><h1>标题</h1><p>这是段落内容。</p><p>第二段。</p></body></html>"
s = HTMLSplitter(max_size=500)
chunks = s.split(html, source_file="test.html")
assert len(chunks) >= 1
all_text = "".join(c["content"] for c in chunks)
assert "标题" in all_text
assert "段落内容" in all_text
assert "第二段" in all_text
def test_strips_script_and_style(self):
from src.core.splitters.html import HTMLSplitter
html = """<html><head><style>.a{color:red}</style><script>alert('xss')</script></head>
<body><p>可见内容。</p></body></html>"""
s = HTMLSplitter()
chunks = s.split(html, source_file="test.html")
all_text = "".join(c["content"] for c in chunks)
assert "可见内容" in all_text
assert "alert" not in all_text
assert ".a{color:red}" not in all_text
def test_empty_html(self):
from src.core.splitters.html import HTMLSplitter
s = HTMLSplitter()
assert s.split("<html></html>") == []
assert s.split("") == []
def test_html_has_split_method(self):
"""HTMLSplitter 遵循 Splitter Protocol."""
from src.core.splitters.html import HTMLSplitter
s = HTMLSplitter()
assert hasattr(s, "split")