from __future__ import annotations
import pytest
from mcp_docugen.pdf_renderer import (
PdfRenderError,
_md_to_html,
_normalize_pdf_options,
_split_frontmatter,
)
def test_split_frontmatter_extracts_yaml_dict():
md = "---\ntitle: Foo\npdf_options:\n format: A4\n---\n\n# Body\n"
fm, body = _split_frontmatter(md)
assert fm == {"title": "Foo", "pdf_options": {"format": "A4"}}
assert body.lstrip().startswith("# Body")
def test_split_frontmatter_no_frontmatter_returns_empty_dict():
md = "# Just body\nNo YAML here.\n"
fm, body = _split_frontmatter(md)
assert fm == {}
assert body == md
def test_split_frontmatter_invalid_yaml_raises():
md = "---\ntitle: : : broken\n---\nbody"
with pytest.raises(PdfRenderError):
_split_frontmatter(md)
def test_md_to_html_renders_table_and_html_passthrough():
body = "| A | B |\n|---|---|\n| 1 | 2 |\n\n
raw
\n"
html = _md_to_html(body)
assert "" in html
assert "| A | " in html
assert 'raw
' in html
def test_normalize_pdf_options_translates_camelcase_to_snake():
src = {
"format": "A4",
"printBackground": True,
"displayHeaderFooter": True,
"headerTemplate": "H
",
"footerTemplate": "F
",
"margin": {"top": "18mm", "bottom": "18mm"},
"landscape": False,
"scale": 0.95,
}
out = _normalize_pdf_options(src)
assert out["format"] == "A4"
assert out["print_background"] is True
assert out["display_header_footer"] is True
assert out["header_template"] == "H
"
assert out["footer_template"] == "F
"
assert out["margin"]["top"] == "18mm"
assert out["landscape"] is False
assert out["scale"] == pytest.approx(0.95)
def test_normalize_pdf_options_drops_unknown_keys():
src = {"format": "A4", "weird": 123, "viewportWidth": 1200}
out = _normalize_pdf_options(src)
assert out == {"format": "A4"}
def test_normalize_pdf_options_handles_non_dict():
assert _normalize_pdf_options(None) == {}
assert _normalize_pdf_options("not a dict") == {}
@pytest.mark.skipif(
True, reason="requires Chromium binary; runs only in container/CI with Playwright installed"
)
async def test_render_markdown_to_pdf_smoke():
from mcp_docugen.pdf_renderer import render_markdown_to_pdf
md = (
"---\npdf_options:\n format: A4\n---\n\n"
"\n\n# Hello\n\nBody text.\n"
)
result = await render_markdown_to_pdf(md)
assert result.pdf_bytes.startswith(b"%PDF-")
assert result.size_bytes > 1000