Files
turnstone/tests/test_renderer_js.py
T
Patrick Buckley 33d16d19ce fix(renderer): handle LaTeX-style \(...\) and \[...\] math delimiters (#425)
* fix(renderer): handle LaTeX-style \(...\) and \[...\] math delimiters

The browser renderer at turnstone/shared_static/renderer.js only
recognized TeX-style $...$ / $$...$$ delimiters. Most modern LLMs
(GPT-5 / o-series, Claude with reasoning effort) emit LaTeX-style
\(...\) for inline math and \[...\] for display by default — those
slipped through as raw text in the coord + interactive WebUIs,
making KaTeX appear "broken when nested inside a markdown block"
(actually broken everywhere, the surrounding markdown just made
the failure noticeable).

Added a second pass for each delimiter style alongside the
existing $...$ / $$...$$ patterns. Both styles now feed the same
mathBlocks / inlineMaths placeholder pipeline so all the existing
nested-block handling (lists, blockquotes, tables, bold, headings,
details, post-render KaTeX markup) Just Works.

Edge cases verified by the new test_renderer_js.py harness:
- \(...\) inside inline code stays literal
- \(...\) inside fenced code blocks stays literal
- Solo \[ with no closing \] doesn't trigger spurious math
- Markdown links [text](url) untouched (regex uses \[ \], not [ ])
- Mixed TeX + LaTeX delimiters in one message both render

The harness drives renderer.js through Node via vm.runInThisContext
with stubbed document/katex globals — first JS-side regression
guard for the renderer; previously it had no test coverage at all.

* fix(renderer): apply Copilot feedback on PR #425

Three review items from Copilot:

1. Display-math sentinel could leak through inline-code spans.
   The original ordering ran $$...$$ / \[...\] extraction BEFORE
   inline code, so a backtick span around math (e.g. `$$x$$` or
   `\[x\]`) had its delimiters consumed by the math regex and
   replaced with \x00MB…\x00. Inline code then captured the
   sentinel; restore order put MB after IC, leaving the null-byte
   placeholder visible inside the rendered <code>. Reorder: inline
   code first, then display math, then inline math. Code spans
   now seal their content before any math regex sees it. The
   reverse edge case (math containing backticks, e.g. \verb|`x`|)
   is much rarer and KaTeX rejects \verb anyway.

2. Inline LaTeX-style \(...\) regex used [\s\S]+? which allowed
   newlines, so an unterminated \( on one line would eat the
   next paragraph until it found a closing \). Aligned with the
   existing $...$ behavior by switching to [^\n]+? — display
   math (\[...\] / $$...$$) stays multi-line by design.

3. tests/test_renderer_js.py was guarded with a node-availability
   skip, but CI's test + test-postgres jobs didn't explicitly
   install Node, so the suite would have silently no-op'd if the
   runner image dropped Node. Added actions/setup-node@v5 to
   both jobs.

Four new regression tests cover the leak (both delimiter styles
inside backticks must stay literal) and the cross-paragraph span
(both \(...\) and $...$ must not eat newlines).
2026-04-27 12:19:19 -07:00

231 lines
7.8 KiB
Python

"""Smoke tests for ``turnstone/shared_static/renderer.js``.
The renderer is browser-only JS with no test framework on the project
side. These tests drive it through ``node`` against a minimal browser-
shim harness so a regression on the markdown / KaTeX wiring surfaces
in CI rather than at runtime in the operator's browser.
Each test invokes ``node -e`` with a small wrapper that loads
``utils.js`` + ``renderer.js`` via ``vm.runInThisContext``, stubs
``document`` / ``katex`` enough for the renderer to run, then prints
the rendered HTML for a sample input. The assertions check the
resulting markup contains the expected ``<span class="katex">…</span>``
placeholder and not the raw delimiter.
"""
from __future__ import annotations
import json
import shutil
import subprocess
from pathlib import Path
import pytest
_REPO_ROOT = Path(__file__).resolve().parent.parent
_UTILS_JS = _REPO_ROOT / "turnstone/shared_static/utils.js"
_RENDERER_JS = _REPO_ROOT / "turnstone/shared_static/renderer.js"
def _has_node() -> bool:
return shutil.which("node") is not None
pytestmark = pytest.mark.skipif(not _has_node(), reason="node not available")
_HARNESS_TEMPLATE = """
const vm = require('vm');
const fs = require('fs');
global.document = {
createElement: () => {
let t = '';
return {
get textContent() { return t; },
set textContent(v) { t = v; },
get innerHTML() {
return t.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
},
};
},
addEventListener: () => {},
};
global.katex = {
renderToString: (tex, opts) =>
'<span class="katex">[KATEX:' +
tex.replace(/\\n/g, '\\\\n') +
(opts.displayMode ? ':display' : ':inline') +
']</span>',
};
global.window = global;
vm.runInThisContext(fs.readFileSync(%(utils)s, 'utf8'));
vm.runInThisContext(fs.readFileSync(%(renderer)s, 'utf8'));
const input = %(input)s;
process.stdout.write(renderMarkdown(input));
"""
def _render(markdown: str) -> str:
"""Render ``markdown`` through renderer.js + return the HTML."""
harness = _HARNESS_TEMPLATE % {
"utils": json.dumps(str(_UTILS_JS)),
"renderer": json.dumps(str(_RENDERER_JS)),
"input": json.dumps(markdown),
}
result = subprocess.run(
["node", "-e", harness],
capture_output=True,
text=True,
timeout=10,
check=True,
)
return result.stdout
# ---------------------------------------------------------------------------
# KaTeX delimiter handling — both TeX and LaTeX styles
# ---------------------------------------------------------------------------
def test_tex_inline_math_renders() -> None:
out = _render("The formula $E = mc^2$ is famous.")
assert '<span class="katex">' in out
assert "[KATEX:E = mc^2:inline]" in out
assert "$E = mc^2$" not in out # raw delimiters consumed
def test_tex_display_math_renders() -> None:
out = _render("$$\nE = mc^2\n$$")
assert '<span class="katex">' in out
assert ":display]" in out
def test_latex_inline_math_renders() -> None:
r"""LaTeX-style \(...\) inline math. GPT-5 / o-series / Claude
with reasoning effort emit this style by default; without
explicit support the model output passed through as raw \(x\)
text in coord + interactive UIs."""
out = _render(r"The formula \(E = mc^2\) is famous.")
assert '<span class="katex">' in out
assert "[KATEX:E = mc^2:inline]" in out
assert r"\(E = mc^2\)" not in out
def test_latex_display_math_renders() -> None:
r"""LaTeX-style \[...\] display math."""
out = _render("Intro\n\n\\[\nE = mc^2\n\\]\n\nMore")
assert '<span class="katex">' in out
assert ":display]" in out
assert "\\[" not in out
assert "\\]" not in out
def test_latex_math_in_list_item_renders() -> None:
"""Nested-in-markdown-block — the original bug report. The list
item is processed via line-by-line + inlineMarkdown; the math
placeholder must survive that path."""
out = _render(r"- Item with \(E = mc^2\) math")
assert "<li>" in out
assert '<span class="katex">' in out
assert "[KATEX:E = mc^2:inline]" in out
def test_latex_math_in_blockquote_renders() -> None:
out = _render(r"> Note: \(x^2\) is squared.")
assert "<blockquote>" in out
assert '<span class="katex">' in out
def test_latex_math_in_bold_renders() -> None:
out = _render(r"Then **\(x^2\)** end.")
assert "<strong>" in out
assert '<span class="katex">' in out
def test_mixed_tex_and_latex_styles() -> None:
out = _render(r"Here $x$ then \(y\) end.")
assert out.count('<span class="katex">') == 2
assert "[KATEX:x:inline]" in out
assert "[KATEX:y:inline]" in out
def test_latex_math_inside_inline_code_preserved() -> None:
r"""\(...\) inside inline code must NOT render as math —
code is escaped + left literal."""
out = _render(r"Code: `\(x\)` raw.")
assert r"<code>\(x\)</code>" in out
assert '<span class="katex">' not in out
def test_latex_math_inside_fenced_code_preserved() -> None:
r"""\(...\) inside a fenced block must stay literal."""
out = _render("```\nA \\(x\\) sample\n```")
assert "<pre><code>" in out
assert '<span class="katex">' not in out
def test_solo_escaped_bracket_does_not_render_as_math() -> None:
r"""A lone \[ with no matching \] is not math — it's a markdown
bracket escape. Don't hijack it."""
out = _render(r"No math: \[ alone.")
assert '<span class="katex">' not in out
def test_markdown_link_unaffected_by_math_protection() -> None:
r"""Math regex uses \[ / \] (escaped brackets), not bare [...].
Markdown links must still render."""
out = _render("See [docs](https://example.com).")
assert '<a href="https://example.com"' in out
assert ">docs</a>" in out
# ---------------------------------------------------------------------------
# Edge cases — Copilot review on PR #425
# ---------------------------------------------------------------------------
def test_display_math_inside_inline_code_stays_literal() -> None:
r"""``$$...$$`` inside backticks must NOT trigger display-math
extraction — otherwise the math sentinel ends up wrapped inside
the <code> placeholder and leaks into rendered HTML as a raw
null-byte sentinel string.
Pre-#425 ordering ran display-math before inline code, which
caused this leak. The reordering makes inline code seal first.
"""
out = _render(r"Use `$$x$$` for display math.")
assert "<code>$$x$$</code>" in out
assert '<span class="katex">' not in out
assert "\x00" not in out # no leaked sentinel
def test_latex_display_math_inside_inline_code_stays_literal() -> None:
r"""Same as above, but for the LaTeX-style \[...\] delimiter."""
out = _render(r"Use `\[x\]` for display math.")
assert r"<code>\[x\]</code>" in out
assert '<span class="katex">' not in out
assert "\x00" not in out
def test_inline_latex_math_does_not_span_paragraphs() -> None:
r"""An unterminated \(...\) on one line must not eat the
following paragraph until it finds a closing \) — that would
consume large chunks of text under streaming markdown where
the closer hasn't arrived yet. Mirrors the $...$ behavior."""
src = "Open \\(unterminated\n\nNext paragraph with \\(x\\) here."
out = _render(src)
# The bare \( on line 1 should NOT match; the well-formed \(x\)
# on the second paragraph should render normally.
assert out.count('<span class="katex">') == 1
assert "[KATEX:x:inline]" in out
# The "unterminated" stays as raw text.
assert "unterminated" in out
def test_inline_tex_math_does_not_span_newlines() -> None:
"""Existing $...$ behavior — regression guard."""
src = "Open $unterminated\n\nNext paragraph $x$ here."
out = _render(src)
assert out.count('<span class="katex">') == 1
assert "[KATEX:x:inline]" in out