"""Marking `@mentions` in a message somebody wrote.
This is the one render path where a person controls the bytes exactly, and
until now there was no render path at all -- the template printed the column
and let `white-space: pre-wrap` carry the newlines. So the first half of every
test here is that escaping still happens, and happens *before* anything is
injected.
"""
from __future__ import annotations
import pytest
from fastapi.testclient import TestClient
from lembas.services.markdown import highlight_tokens
def test_a_mention_is_marked():
assert highlight_tokens("look at @src/main.py") == (
'look at @src/main.py'
)
def test_a_mention_at_the_start_is_marked():
assert highlight_tokens("@README.md is wrong") == (
'@README.md is wrong'
)
def test_an_email_address_is_not_a_mention():
"""The whole reason the pattern is anchored on whitespace. Without it every
address in a message becomes a highlighted file reference."""
assert "tok-mention" not in highlight_tokens("write to frodo@shire.test")
def test_a_bare_at_is_left_alone():
assert "tok-mention" not in highlight_tokens("dinner @ 8")
def test_several_mentions_are_all_marked():
marked = highlight_tokens("@a.py and @b.py")
assert marked.count("tok-mention") == 2
@pytest.mark.parametrize(
"hostile",
[
"",
"@",
"
",
"@a\">",
],
)
def test_markup_is_escaped_before_anything_is_injected(hostile):
"""The order is the security property. Injecting first and escaping after
would escape our own span; escaping first means the span is the only markup
that can exist in the output."""
out = highlight_tokens(hostile)
assert "