diff --git a/tests/test_splitters_markdown.py b/tests/test_splitters_markdown.py new file mode 100644 index 0000000..513f462 --- /dev/null +++ b/tests/test_splitters_markdown.py @@ -0,0 +1,43 @@ +"""MarkdownSplitter 边界测试.""" +import pytest +from src.core.splitters import MarkdownSplitter + + +class TestMarkdownSplitterEdgeCases: + """Markdown 分块边界情况.""" + + @pytest.fixture + def splitter(self): + return MarkdownSplitter(max_size=1000, overlap=100) + + def test_no_headings_document(self, splitter): + """无标题文档正常分块.""" + md = "这是一段没有标题的纯文本。\n\n第二段内容。" + chunks = splitter.split(md, source_file="nohead.md") + assert len(chunks) >= 1 + + def test_deep_headings(self, splitter): + """h4-h6 深层标题.""" + md = "# 一级\n## 二级\n### 三级\n#### 四级\n内容在这里。\n##### 五级\n更多内容。\n###### 六级\n最深的内容。" + chunks = splitter.split(md, source_file="deep.md") + assert len(chunks) >= 1 + + def test_hash_in_code_block_not_heading(self, splitter): + """代码块中的 # 号不被误识别为标题.""" + md = "# 真实标题\n这是内容。\n```python\n# 这不是标题,是注释\nx = 1\n## 这也不是标题\n```\n更多内容。" + chunks = splitter.split(md, source_file="codehash.md") + section_titles = [c.get("section_title", "") for c in chunks] + for title in section_titles: + assert "不是标题" not in title + + def test_adjacent_headings_empty_content(self, splitter): + """标题后紧接标题(空内容).""" + md = "# 标题 A\n# 标题 B\n内容 B。" + chunks = splitter.split(md, source_file="adjacent.md") + assert len(chunks) >= 1 + + def test_only_headings_no_content(self, splitter): + """仅有标题无正文.""" + md = "# 只有标题\n## 没有内容" + chunks = splitter.split(md, source_file="headingsonly.md") + assert isinstance(chunks, list)