from __future__ import annotations
import shutil
import pytest
from mcp_docugen.docx_preprocessor import (
_strip_frontmatter,
_strip_style_blocks,
preprocess_for_docx as _preprocess,
)
from mcp_docugen.docx_renderer import (
DocxRenderError,
render_markdown_to_docx,
)
def test_strip_style_blocks_removes_simple_block():
md = "Pre\n\nPost"
assert "\n"
"After"
)
cleaned = _strip_style_blocks(md)
assert "\n\n"
"# Body\n\nContent.\n"
)
out = _preprocess(md)
assert "foo: bar" not in out
assert "\n\n"
"# Hello\n\n"
"| A | B |\n|---|---|\n| 1 | 2 |\n\n"
"**bold** and *italic*.\n"
)
result = await render_markdown_to_docx(md)
# DOCX is a ZIP archive; signature: PK\x03\x04
assert result.docx_bytes.startswith(b"PK\x03\x04")
assert result.size_bytes > 1000
async def test_render_empty_markdown_raises():
with pytest.raises(DocxRenderError):
await render_markdown_to_docx("")
with pytest.raises(DocxRenderError):
await render_markdown_to_docx(" \n\n ")
async def test_render_only_frontmatter_and_style_raises():
md = "---\nfoo: bar\n---\n\n\n\n \n"
with pytest.raises(DocxRenderError):
await render_markdown_to_docx(md)