"""Unit tests for Frontmatter Parser component.
Tests YAML frontmatter parsing and content extraction from checklist files.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pathlib import Path
class TestFrontmatterParser:
"""Tests for Frontmatter Parser functionality."""
def test_parse_valid_frontmatter(self, temp_dir: Path) -> None:
"""Test parsing a markdown file with valid YAML frontmatter."""
from sso_mcp_server.checklists.parser import parse_checklist_file
# Create test file with frontmatter
test_file = temp_dir / "test.md"
test_file.write_text(
"""---
name: Test Checklist
description: A test checklist for unit testing
---
# Test Checklist
- [ ] Item 1
- [ ] Item 2
"""
)
result = parse_checklist_file(test_file)
assert result is not None
assert result["name"] == "Test Checklist"
assert result["description"] == "A test checklist for unit testing"
assert "# Test Checklist" in result["content"]
assert "- [ ] Item 1" in result["content"]
def test_parse_frontmatter_extracts_name(self, temp_dir: Path) -> None:
"""Test that parser extracts name from frontmatter."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "named.md"
test_file.write_text(
"""---
name: Coding Standards
---
Content here.
"""
)
result = parse_checklist_file(test_file)
assert result["name"] == "Coding Standards"
def test_parse_frontmatter_extracts_description(self, temp_dir: Path) -> None:
"""Test that parser extracts description from frontmatter."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "described.md"
test_file.write_text(
"""---
name: Test
description: This is a detailed description of the checklist.
---
Content here.
"""
)
result = parse_checklist_file(test_file)
assert result["description"] == "This is a detailed description of the checklist."
def test_parse_file_without_frontmatter(self, temp_dir: Path) -> None:
"""Test parsing a file without YAML frontmatter."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "no_frontmatter.md"
test_file.write_text(
"""# Just a Title
Some content without frontmatter.
"""
)
result = parse_checklist_file(test_file)
assert result is not None
# Name should default to filename without extension
assert result["name"] == "no_frontmatter"
assert result["description"] is None or result["description"] == ""
assert "# Just a Title" in result["content"]
def test_parse_file_with_empty_frontmatter(self, temp_dir: Path) -> None:
"""Test parsing a file with empty frontmatter."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "empty_frontmatter.md"
test_file.write_text(
"""---
---
Content after empty frontmatter.
"""
)
result = parse_checklist_file(test_file)
assert result is not None
assert result["name"] == "empty_frontmatter"
assert "Content after empty frontmatter" in result["content"]
def test_parse_returns_content_body(self, temp_dir: Path) -> None:
"""Test that parser returns the content body without frontmatter."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "content.md"
test_file.write_text(
"""---
name: Test
description: Test description
---
# Main Content
This is the main content that should be returned.
- Item 1
- Item 2
"""
)
result = parse_checklist_file(test_file)
# Content should NOT include the frontmatter delimiters
assert "---" not in result["content"]
assert "name: Test" not in result["content"]
assert "# Main Content" in result["content"]
assert "This is the main content" in result["content"]
def test_parse_preserves_markdown_formatting(self, temp_dir: Path) -> None:
"""Test that parser preserves markdown formatting in content."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "formatted.md"
test_file.write_text(
"""---
name: Formatted
---
# Header
## Subheader
**Bold** and *italic* text.
```python
def example():
pass
```
| Column 1 | Column 2 |
|----------|----------|
| Cell 1 | Cell 2 |
"""
)
result = parse_checklist_file(test_file)
assert "# Header" in result["content"]
assert "## Subheader" in result["content"]
assert "**Bold**" in result["content"]
assert "```python" in result["content"]
assert "| Column 1 |" in result["content"]
def test_parse_nonexistent_file_returns_none(self, temp_dir: Path) -> None:
"""Test that parsing a nonexistent file returns None."""
from sso_mcp_server.checklists.parser import parse_checklist_file
nonexistent = temp_dir / "does_not_exist.md"
result = parse_checklist_file(nonexistent)
assert result is None
def test_parse_includes_file_path(self, temp_dir: Path) -> None:
"""Test that result includes the file path."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "with_path.md"
test_file.write_text(
"""---
name: Path Test
---
Content.
"""
)
result = parse_checklist_file(test_file)
assert result["path"] == test_file
class TestFrontmatterParserEdgeCases:
"""Edge case tests for Frontmatter Parser."""
def test_parse_file_with_multiple_frontmatter_delimiters(self, temp_dir: Path) -> None:
"""Test parsing file with --- in content (not frontmatter)."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "multiple_dashes.md"
test_file.write_text(
"""---
name: Multiple Dashes
---
Content with horizontal rule:
---
More content after.
"""
)
result = parse_checklist_file(test_file)
assert result["name"] == "Multiple Dashes"
# Horizontal rule should be in content
assert "---" in result["content"]
assert "More content after" in result["content"]
def test_parse_file_with_special_characters_in_name(self, temp_dir: Path) -> None:
"""Test parsing file with special characters in frontmatter values."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "special.md"
test_file.write_text(
"""---
name: "Test: Special & Characters!"
description: Description with "quotes" and 'apostrophes'
---
Content.
"""
)
result = parse_checklist_file(test_file)
assert result["name"] == "Test: Special & Characters!"
assert "quotes" in result["description"]
def test_parse_file_with_unicode_content(self, temp_dir: Path) -> None:
"""Test parsing file with unicode characters."""
from sso_mcp_server.checklists.parser import parse_checklist_file
test_file = temp_dir / "unicode.md"
test_file.write_text(
"""---
name: Unicode Test 日本語
description: Description with émojis 🎉
---
Content with unicode: café, naïve, 中文
"""
)
result = parse_checklist_file(test_file)
assert "日本語" in result["name"]
assert "🎉" in result["description"]
assert "中文" in result["content"]