"""KaTeX and Mermaid must be vendored and fetched only on first real use. They used to load from cdn.jsdelivr.net in every , costing ~985 KB on the wire per page load, breaking offline installs and announcing each session to a third party. These tests pin the replacement contract: one fetch per library, never before a formula or a ```mermaid fence actually shows up, and math still renders once the library lands. """ import json import re import shutil import subprocess import textwrap from pathlib import Path import pytest from tests.helpers.document_source import document_source _REPO = Path(__file__).resolve().parent.parent _HAS_NODE = shutil.which("node") is not None MERMAID_SRC = "/static/lib/mermaid.min.js" KATEX_SRC = "/static/lib/katex/katex.min.js" KATEX_CSS = "/static/lib/katex/katex.min.css" @pytest.fixture(scope="module") def node_available(): if not _HAS_NODE: pytest.skip("node binary not on PATH") def _katex_fonts_block(sw_source: str) -> str: """The literal body of sw.js's KATEX_FONTS array.""" match = re.search(r"const KATEX_FONTS = \[(.*?)\]", sw_source, re.S) assert match, "sw.js no longer defines a KATEX_FONTS array" return match.group(1) # A DOM stub small enough to reason about: it records every