"""Markdown rendering and sanitisation. Model output is untrusted input: it routinely contains HTML and a model can be talked into emitting a script tag. These are the tests that keep that boundary. """ from __future__ import annotations from lembas.services.markdown import escape_text, render_markdown def test_basic_formatting(): html = render_markdown("Some **bold** and *italic* text.") assert "bold" in html assert "italic" in html def test_script_tags_are_stripped(): html = render_markdown("Hello world") assert "click') assert 'href="javascript:' not in html def test_event_handlers_are_stripped(): html = render_markdown('') assert "onerror" not in html def test_external_links_get_protective_rel(): html = render_markdown("[example](https://example.com)") assert "noopener" in html assert "noreferrer" in html def test_code_block_is_highlighted_and_not_double_wrapped(): html = render_markdown("```python\ndef f():\n return 1\n```") assert 'class="code-block"' in html assert "pg-k" in html # a Pygments keyword span # markdown-it wraps highlight output in its own
 unless the
    # fence rule is replaced outright. This is the regression guard.
    assert "
python<" in render_markdown("```python\nx = 1\n```")


def test_unlabelled_code_block_still_renders():
    html = render_markdown("```\njust text\n```")
    assert 'class="code-block"' in html
    assert "just text" in html


def test_code_content_is_escaped():
    html = render_markdown("```\n\n```")
    assert "