"""
Unit tests for the converter module.
"""
import pytest
# Test imports with graceful handling
try:
from deckbuilder.content.frontmatter_to_json_converter import markdown_to_canonical_json
HAS_CONVERTER = True
except ImportError:
HAS_CONVERTER = False
@pytest.mark.skipif(not HAS_CONVERTER, reason="Converter not available")
@pytest.mark.unit
@pytest.mark.deckbuilder
class TestConverter:
"""Test cases for the markdown_to_canonical_json function."""
def test_convert_four_columns_structured_to_placeholders(self):
"""Test converting four columns structured frontmatter with direct fields."""
markdown_input = """---
layout: Four Columns With Titles
title: Feature Comparison
title_col1: Performance
content_col1: Fast processing with optimized algorithms
title_col2: Security
content_col2: Enterprise-grade encryption and compliance
title_col3: Usability
content_col3: Intuitive interface with minimal learning curve
title_col4: Cost
content_col4: Competitive pricing with flexible plans
---"""
result = markdown_to_canonical_json(markdown_input)
expected_slides = [
{
"layout": "Four Columns With Titles",
"style": "default_style",
"placeholders": {
"title": "Feature Comparison",
"title_col1": "Performance",
"content_col1": "Fast processing with optimized algorithms",
"title_col2": "Security",
"content_col2": "Enterprise-grade encryption and compliance",
"title_col3": "Usability",
"content_col3": "Intuitive interface with minimal learning curve",
"title_col4": "Cost",
"content_col4": "Competitive pricing with flexible plans",
},
"content": [],
}
]
# Compare the generated result with the expected structure
assert len(result["slides"]) == len(expected_slides)
for i, slide in enumerate(result["slides"]):
assert slide["layout"] == expected_slides[i]["layout"]
assert slide["style"] == expected_slides[i]["style"]
assert slide["content"] == expected_slides[i]["content"]
assert slide["placeholders"] == expected_slides[i]["placeholders"]
def test_convert_comparison_structured_to_placeholders(self):
"""Test converting comparison structured frontmatter with direct fields."""
markdown_input = """---
layout: Comparison
title: Solution Analysis
title_left: Current Solution
content_left: Proven reliability
title_right: New Solution
content_right: Enhanced features
---"""
result = markdown_to_canonical_json(markdown_input)
expected_slides = [
{
"layout": "Comparison",
"style": "default_style",
"placeholders": {
"title": "Solution Analysis",
"title_left": "Current Solution",
"content_left": "Proven reliability",
"title_right": "New Solution",
"content_right": "Enhanced features",
},
"content": [],
}
]
assert len(result["slides"]) == len(expected_slides)
for i, slide in enumerate(result["slides"]):
assert slide["layout"] == expected_slides[i]["layout"]
assert slide["style"] == expected_slides[i]["style"]
assert slide["content"] == expected_slides[i]["content"]
assert slide["placeholders"] == expected_slides[i]["placeholders"]
def test_convert_simple_markdown_with_headings_paragraphs_bullets(self):
"""Test conversion of title and content with structured frontmatter."""
markdown_input = """---
layout: Title and Content
title: My Simple Slide
content: "- Bullet 1\\n- Bullet 2\\n- Bullet 3"
---"""
result = markdown_to_canonical_json(markdown_input)
expected_slides = [
{
"layout": "Title and Content",
"style": "default_style",
"placeholders": {
"title": "My Simple Slide",
"content": "• Bullet 1\n• Bullet 2\n• Bullet 3",
},
"content": [],
}
]
assert len(result["slides"]) == len(expected_slides)
for i, slide in enumerate(result["slides"]):
assert slide["layout"] == expected_slides[i]["layout"]
assert slide["style"] == expected_slides[i]["style"]
assert slide["placeholders"] == expected_slides[i]["placeholders"]
assert slide["content"] == expected_slides[i]["content"]
def test_convert_markdown_with_table(self):
"""Test conversion of structured frontmatter with table content."""
markdown_input = """---
layout: Title and Content
title: My Table Slide
content: "| Header 1 | Header 2 |\\n|---|---|\\n| Row 1 Col 1 | Row 1 Col 2 |"
---"""
result = markdown_to_canonical_json(markdown_input)
# Test structure - content should be JSON table object
assert len(result["slides"]) == 1
slide = result["slides"][0]
assert slide["layout"] == "Title and Content"
assert slide["style"] == "default_style"
assert slide["placeholders"]["title"] == "My Table Slide"
assert isinstance(slide["placeholders"]["content"], dict)
assert slide["placeholders"]["content"]["type"] == "table"
assert "data" in slide["placeholders"]["content"]
assert len(slide["placeholders"]["content"]["data"]) == 2 # Header and data row
assert slide["content"] == []
def test_convert_two_content_layout(self):
"""Test conversion for Two Content layout with structured frontmatter."""
markdown_input = """---
layout: Two Content
title: Side by Side
content_left: "- Left bullet 1\\n- Left bullet 2"
content_right: "| A | B |\\n|---|---|\\n| 1 | 2 |"
---"""
result = markdown_to_canonical_json(markdown_input)
# Test the structure rather than exact format due to complex processing
assert len(result["slides"]) == 1
slide = result["slides"][0]
assert slide["layout"] == "Two Content"
assert slide["style"] == "default_style"
assert slide["placeholders"]["title"] == "Side by Side"
assert slide["content"] == []
# Test that content_left is plain string with bullet markdown processed
assert isinstance(slide["placeholders"]["content_left"], str)
assert "• Left bullet 1" in slide["placeholders"]["content_left"]
# Test that content_right is JSON table object
assert isinstance(slide["placeholders"]["content_right"], dict)
assert slide["placeholders"]["content_right"]["type"] == "table"
assert "data" in slide["placeholders"]["content_right"]
def test_convert_missing_layout(self):
"""Test conversion when layout field is missing."""
markdown_input = """---
title: Test Title
content: Test Content
---"""
result = markdown_to_canonical_json(markdown_input)
assert "slides" in result
assert len(result["slides"]) == 1
slide = result["slides"][0]
assert slide["layout"] == "Title and Content" # Should default
placeholders = slide["placeholders"]
assert placeholders["title"] == "Test Title"
assert placeholders["content"] == "Test Content"
assert slide["content"] == []
def test_convert_unsupported_layout(self):
"""Test conversion for unsupported layout."""
markdown_input = """---
layout: Unsupported Layout
title: Test Title
content: Some content here.
---"""
result = markdown_to_canonical_json(markdown_input)
assert "slides" in result
assert len(result["slides"]) == 1
slide = result["slides"][0]
assert slide["layout"] == "Unsupported Layout"
placeholders = slide["placeholders"]
assert placeholders["title"] == "Test Title"
assert placeholders["content"] == "Some content here."
assert slide["content"] == []
def test_h1_heading_processing(self):
"""Test that H1 headings (# H1) are preserved for semantic processing."""
markdown_input = """---
layout: Title and Content
title: Test Slide
content: "# Main Heading\\nThis is content after the heading."
---"""
result = markdown_to_canonical_json(markdown_input)
slide = result["slides"][0]
content = slide["placeholders"]["content"]
# H1 heading should be preserved for semantic processing by content formatter
assert "Main Heading" in content
assert "# Main Heading" in content # Markers preserved for font scaling
assert "This is content after the heading." in content
def test_h1_h2_h3_heading_processing(self):
"""Test that all heading levels are preserved for semantic processing."""
markdown_input = """---
layout: Title and Content
title: Test Slide
content: "# H1 Heading\\n## H2 Heading\\n### H3 Heading\\nRegular text"
---"""
result = markdown_to_canonical_json(markdown_input)
slide = result["slides"][0]
content = slide["placeholders"]["content"]
# All heading prefixes should be preserved for semantic processing
assert "H1 Heading" in content
assert "H2 Heading" in content
assert "H3 Heading" in content
assert "# H1 Heading" in content # Markers preserved for font scaling
assert "## H2 Heading" in content # Markers preserved for font scaling
assert "### H3 Heading" in content # Markers preserved for font scaling
assert "Regular text" in content
def test_bullet_list_processing(self):
"""Test that bullet lists (- and *) are processed correctly."""
markdown_input = """---
layout: Title and Content
title: Test Slide
content: "- First bullet\\n- Second bullet\\n* Third bullet\\n* Fourth bullet"
---"""
result = markdown_to_canonical_json(markdown_input)
slide = result["slides"][0]
content = slide["placeholders"]["content"]
# Currently fails - bullet lists should be processed into structured format
# For now, we expect the content to be processed in some way
assert isinstance(content, (str, dict, list))
# The test should pass when bullet processing is implemented
# This test will fail initially and pass after implementation
def test_numbered_list_processing(self):
"""Test that numbered lists (1. 2. 3.) are processed correctly."""
markdown_input = """---
layout: Title and Content
title: Test Slide
content: "1. First item\\n2. Second item\\n3. Third item"
---"""
result = markdown_to_canonical_json(markdown_input)
slide = result["slides"][0]
content = slide["placeholders"]["content"]
# Currently fails - numbered lists should be processed into structured format
# For now, we expect the content to be processed in some way
assert isinstance(content, (str, dict, list))
# The test should pass when numbered list processing is implemented
# This test will fail initially and pass after implementation
def test_blockquote_processing(self):
"""Test that blockquotes (> quote) are processed correctly."""
markdown_input = """---
layout: Title and Content
title: Test Slide
content: "> This is a blockquote\\n> Second line of quote\\nRegular text"
---"""
result = markdown_to_canonical_json(markdown_input)
slide = result["slides"][0]
content = slide["placeholders"]["content"]
# Currently fails - blockquotes should be processed
# For now, we expect the content to be processed in some way
assert isinstance(content, (str, dict, list))
# The test should pass when blockquote processing is implemented
# This test will fail initially and pass after implementation
def test_mixed_markdown_processing(self):
"""Test mixed markdown content with headings, lists, and blockquotes."""
markdown_input = """---
layout: Title and Content
title: Test Slide
content: "# Main Section\\n- Bullet one\\n- Bullet two\\n## Subsection\\n1. First step\\n2. Second step\\n> Important note\\nRegular paragraph"
---"""
result = markdown_to_canonical_json(markdown_input)
slide = result["slides"][0]
content = slide["placeholders"]["content"]
# Currently fails - mixed content should be properly processed
# For now, we expect the content to be processed in some way
assert isinstance(content, (str, dict, list))
# The test should pass when all markdown processing is implemented
# This test will fail initially and pass after implementation