askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_markdown_object.py · 7.0 KB · 182 lines
| 1 | """Tests for the shared markdown renderer (packages/app-theme/objects/site/markdown.py), |
| 2 | served at /markdown as window.dbbasicMarkdown -- the one place markdown |
| 3 | rendering lives, per that file's own docstring. Behavioral: executes the |
| 4 | real function under node against realistic input, not a source-text match. |
| 5 | """ |
| 6 | |
| 7 | import ast |
| 8 | import json |
| 9 | import shutil |
| 10 | import subprocess |
| 11 | from pathlib import Path |
| 12 | |
| 13 | import pytest |
| 14 | |
| 15 | MARKDOWN_PATH = ( |
| 16 | Path(__file__).resolve().parents[1] |
| 17 | / "packages" / "app-theme" / "objects" / "site" / "markdown.py" |
| 18 | ) |
| 19 | |
| 20 | |
| 21 | def _markdown_js(): |
| 22 | tree = ast.parse(MARKDOWN_PATH.read_text()) |
| 23 | for node in ast.walk(tree): |
| 24 | if isinstance(node, ast.Assign) and any( |
| 25 | getattr(t, "id", None) == "_JS" for t in node.targets |
| 26 | ): |
| 27 | return ast.literal_eval(node.value) |
| 28 | raise AssertionError("_JS not found in markdown.py") |
| 29 | |
| 30 | |
| 31 | def _render(*texts, tmp_path): |
| 32 | node = shutil.which("node") |
| 33 | if not node: |
| 34 | pytest.skip("node not available") |
| 35 | probe = ( |
| 36 | "global.window = {};\n" |
| 37 | + _markdown_js() |
| 38 | + "\nconsole.log(JSON.stringify(" |
| 39 | + json.dumps([{"text": t} for t in texts]) |
| 40 | + ".map(o => window.dbbasicMarkdown(o.text))));\n" |
| 41 | ) |
| 42 | probe_path = tmp_path / "markdown_probe.js" |
| 43 | probe_path.write_text(probe) |
| 44 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 45 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 46 | return json.loads(result.stdout) |
| 47 | |
| 48 | |
| 49 | def test_headings_render_as_real_heading_tags(tmp_path): |
| 50 | [html] = _render("# Title\n\n## Sub", tmp_path=tmp_path) |
| 51 | assert html == "<h1>Title</h1><h2>Sub</h2>" |
| 52 | |
| 53 | |
| 54 | def test_inline_bold_italic_code_and_autolink(tmp_path): |
| 55 | [html] = _render( |
| 56 | "Some **bold** and *italic* and `code` and https://example.com/page here.", |
| 57 | tmp_path=tmp_path, |
| 58 | ) |
| 59 | assert "<strong>bold</strong>" in html |
| 60 | assert "<em>italic</em>" in html |
| 61 | assert "<code>code</code>" in html |
| 62 | assert '<a href="https://example.com/page" target="_blank" rel="noopener">https://example.com/page</a>' in html |
| 63 | |
| 64 | |
| 65 | def test_unordered_and_ordered_lists(tmp_path): |
| 66 | [ul] = _render("- one\n- two\n- three", tmp_path=tmp_path) |
| 67 | assert ul == "<ul><li>one</li><li>two</li><li>three</li></ul>" |
| 68 | [ol] = _render("1. first\n2. second", tmp_path=tmp_path) |
| 69 | assert ol == "<ol><li>first</li><li>second</li></ol>" |
| 70 | |
| 71 | |
| 72 | def test_fenced_code_block_preserves_language_and_does_not_apply_inline_markdown(tmp_path): |
| 73 | [html] = _render("```js\nconst x = **not bold**;\n```", tmp_path=tmp_path) |
| 74 | assert html == '<pre><code class="language-js">const x = **not bold**;</code></pre>' |
| 75 | |
| 76 | |
| 77 | def test_blockquote_joins_lines(tmp_path): |
| 78 | [html] = _render("> line one\n> line two", tmp_path=tmp_path) |
| 79 | assert html == "<blockquote>line one line two</blockquote>" |
| 80 | |
| 81 | |
| 82 | def test_horizontal_rule(tmp_path): |
| 83 | [html] = _render("above\n\n---\n\nbelow", tmp_path=tmp_path) |
| 84 | assert "<hr>" in html |
| 85 | assert "<p>above</p>" in html |
| 86 | assert "<p>below</p>" in html |
| 87 | |
| 88 | |
| 89 | def test_gfm_table(tmp_path): |
| 90 | [html] = _render("| A | B |\n|---|---|\n| 1 | 2 |\n| 3 | 4 |", tmp_path=tmp_path) |
| 91 | assert html == ( |
| 92 | "<table><thead><tr><th>A</th><th>B</th></tr></thead>" |
| 93 | "<tbody><tr><td>1</td><td>2</td></tr><tr><td>3</td><td>4</td></tr></tbody></table>" |
| 94 | ) |
| 95 | # Never a doubled close tag -- a real bug caught while writing this. |
| 96 | assert html.count("</table>") == 1 |
| 97 | |
| 98 | |
| 99 | def test_paragraphs_separated_by_blank_lines(tmp_path): |
| 100 | [html] = _render("First paragraph.\n\nSecond paragraph.", tmp_path=tmp_path) |
| 101 | assert html == "<p>First paragraph.</p><p>Second paragraph.</p>" |
| 102 | |
| 103 | |
| 104 | def test_raw_html_is_escaped_before_any_markdown_parsing(tmp_path): |
| 105 | """The whole point: untrusted text (an AI reply, a user note, a |
| 106 | published article) must never inject a live tag, no matter how it's |
| 107 | combined with real markdown syntax on adjacent lines.""" |
| 108 | [html] = _render("<script>alert(1)</script>\n\n# **bold** header", tmp_path=tmp_path) |
| 109 | assert "<script>" not in html |
| 110 | assert "<script>alert(1)</script>" in html |
| 111 | assert "<h1><strong>bold</strong> header</h1>" in html |
| 112 | |
| 113 | |
| 114 | def test_empty_and_none_input_does_not_throw(tmp_path): |
| 115 | results = _render("", tmp_path=tmp_path) |
| 116 | assert results == [""] |
| 117 | |
| 118 | node = shutil.which("node") |
| 119 | if not node: |
| 120 | pytest.skip("node not available") |
| 121 | probe = ( |
| 122 | "global.window = {};\n" + _markdown_js() |
| 123 | + "\nconsole.log(JSON.stringify([window.dbbasicMarkdown(null), window.dbbasicMarkdown(undefined)]));\n" |
| 124 | ) |
| 125 | probe_path = tmp_path / "markdown_null_probe.js" |
| 126 | probe_path.write_text(probe) |
| 127 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 128 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 129 | assert json.loads(result.stdout) == ["", ""] |
| 130 | |
| 131 | |
| 132 | def test_bracket_links_render_with_href_allowlist(tmp_path): |
| 133 | [internal, external, anchor, mail, hostile] = _render( |
| 134 | "See the [deployment guide](/docs/single-vm-deployment) for more.", |
| 135 | "Read [the spec](https://example.com/spec).", |
| 136 | "Jump to [setup](#setup).", |
| 137 | "Mail [us](mailto:hi@example.com).", |
| 138 | "Click [here](javascript:alert(1)) now.", |
| 139 | tmp_path=tmp_path, |
| 140 | ) |
| 141 | # Relative links stay in-tab; external links open in a new one. |
| 142 | assert '<a href="/docs/single-vm-deployment">deployment guide</a>' in internal |
| 143 | assert "target" not in internal |
| 144 | assert '<a href="https://example.com/spec" target="_blank" rel="noopener">the spec</a>' in external |
| 145 | assert '<a href="#setup">setup</a>' in anchor |
| 146 | assert '<a href="mailto:hi@example.com">us</a>' in mail |
| 147 | # A javascript: href never becomes a link -- the literal text survives, |
| 148 | # escaped, exactly as written. |
| 149 | assert "<a" not in hostile |
| 150 | assert "[here](javascript:alert(1))" in hostile |
| 151 | |
| 152 | |
| 153 | def test_bracket_link_href_is_not_double_linked_by_the_autolink_pass(tmp_path): |
| 154 | [html] = _render("A [labeled link](https://example.com/x) in prose.", tmp_path=tmp_path) |
| 155 | # Exactly one anchor: the bare-URL pass must not wrap the href that the |
| 156 | # bracket-link pass already put inside href="...". |
| 157 | assert html.count("<a ") == 1 |
| 158 | assert html.count("</a>") == 1 |
| 159 | assert ">labeled link</a>" in html |
| 160 | |
| 161 | |
| 162 | def test_hard_wrapped_list_items_rejoin_their_bullet(tmp_path): |
| 163 | [html] = _render( |
| 164 | "- A small VM (a $5 droplet is plenty) running Debian 12 or Ubuntu\n" |
| 165 | " 22.04+, with root or `sudo`.\n" |
| 166 | "- Second bullet.", |
| 167 | tmp_path=tmp_path, |
| 168 | ) |
| 169 | # One list, two items -- the wrapped line rejoined the first bullet |
| 170 | # instead of escaping into a stray paragraph. |
| 171 | assert html.count("<li>") == 2 |
| 172 | assert "<p>" not in html |
| 173 | assert "Ubuntu 22.04+, with root or <code>sudo</code>." in html |
| 174 | |
| 175 | |
| 176 | def test_indented_nested_bullet_stays_a_bullet_not_a_continuation(tmp_path): |
| 177 | [html] = _render("- parent\n - nested\n- sibling", tmp_path=tmp_path) |
| 178 | # The documented wrong-but-lossless behavior: a nested marker becomes |
| 179 | # its own top-level bullet; it must never be glued into the parent as |
| 180 | # literal "- nested" text. |
| 181 | assert html.count("<li>") == 3 |
| 182 | assert "parent -" not in html |