Spaces:
Paused
Paused
File size: 12,581 Bytes
c0e9d84 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | """Tests for the unified inline-article rendering (``article_rich``).
Covers the follow-up that made ``pyUltroid/_core/inlineresults.py``'s
``article_rich`` delegate its block -> HTML/text rendering entirely to
``pyUltroid/_core/richmsg.py`` (the ``RichBlocks`` block model), instead of
keeping a second, "deliberately minimal" renderer in ``inlineresults.py``,
and dropped ``assistant/inlinestuff.py``'s ``_simple_blocks`` degradation
adapter now that ``article_rich`` understands every ``RichBlocks`` block
type (including ``table``/``expandable``) on its own.
What must keep working, regardless of which module renders it:
* a raw HTML string handed to ``article_rich``/its helpers passes through
verbatim (never re-escaped or reinterpreted as a block);
* plain strings inside a block list/iterable still work (no need to
hand-build a dict or a :class:`RichBlocks`);
* the loose dict shapes the old minimal renderer accepted --
``{"type": "header"|"paragraph"|"code", "text": ...}`` and
``{"type": "list", "items": [...], "ordered": bool}`` -- still render;
* a full :class:`RichBlocks` (including ``table()``/``expandable()``)
renders correctly into the native RichMessage payload;
* missing native RichMessage constructors fail loudly rather than degrading
into a plain Telethon article.
``pyUltroid._core.inlineresults``/``pyUltroid._core.richmsg`` only depend
on stdlib + telethon, so this imports them directly without the pyUltroid
production bootstrap (the repo-root ``sys.path`` insertion lives in
``tests/conftest.py``).
"""
from __future__ import annotations
import asyncio
import pytest
from telethon.tl import types
import pyUltroid._core.inlineresults as inlineresults
import pyUltroid._core.richmsg as richmsg
from pyUltroid._core.inlineresults import (
InlineResults,
_blocks_to_html,
_blocks_to_text,
)
from pyUltroid._core.richmsg import RichBlocks
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# a minimal fake client good enough for InlineBuilder.article()/article_rich()
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class _FakeClient:
"""Just enough client behavior for native rich inline markup -- no network."""
def build_reply_markup(self, buttons):
return None
async def _parse_message_text(self, text, parse_mode):
# No real MarkdownV3/HTML parsing needed for these tests -- return
# the text untouched with no entities, mirroring what happens when
# parse_mode is falsy/unavailable.
return text, []
def _run(coro):
return asyncio.run(coro)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 1. raw HTML string passes through verbatim
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_blocks_to_html_passes_raw_html_verbatim():
raw = "<p>already & valid <b>HTML</b></p>"
assert _blocks_to_html(raw) == raw
def test_blocks_to_text_passes_raw_html_verbatim():
raw = "<p>already & valid <b>HTML</b></p>"
assert _blocks_to_text(raw) == raw
def test_article_rich_html_passthrough_end_to_end():
ires = InlineResults(_FakeClient())
raw = "<h1>Title</h1><p>Body & more</p>"
result = _run(ires.article_rich("T", raw))
assert isinstance(result, types.InputBotInlineResult)
rich_message = result.send_message.rich_message
assert isinstance(rich_message, types.InputRichMessageHTML)
assert rich_message.html == raw
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2. plain strings inside a block list
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_blocks_to_html_accepts_plain_strings():
html = _blocks_to_html(["Hello", "World"])
assert html == "<p>Hello</p><p>World</p>"
def test_blocks_to_text_accepts_plain_strings():
text = _blocks_to_text(["Hello", "World"])
assert text == "Hello\n\nWorld"
def test_blocks_to_html_escapes_plain_strings():
html = _blocks_to_html(["<script>evil()</script>"])
assert "<script>" not in html
assert "<script>" in html
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 3. loose dict shapes {"type": "header"|"paragraph"|"code", "text": ...}
# and {"type": "list", "items": [...], "ordered": bool}
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_blocks_to_html_loose_header_paragraph_code():
blocks = [
{"type": "header", "text": "Heading"},
{"type": "paragraph", "text": "Body text"},
{"type": "code", "text": "x = 1", "lang": "py"},
]
html = _blocks_to_html(blocks)
assert "<h1>Heading</h1>" in html
assert "<p>Body text</p>" in html
assert '<code class="language-py">x = 1</code>' in html
def test_blocks_to_html_loose_list_ordered_and_unordered():
ordered = _blocks_to_html([{"type": "list", "items": ["a", "b"], "ordered": True}])
assert ordered == "<ol><li>a</li><li>b</li></ol>"
bulleted = _blocks_to_html([{"type": "list", "items": ["a", "b"]}])
assert bulleted == "<ul><li>a</li><li>b</li></ul>"
def test_blocks_to_text_loose_shapes():
blocks = [
{"type": "header", "text": "Heading"},
{"type": "paragraph", "text": "Body text"},
{"type": "list", "items": ["a", "b"], "ordered": False},
{"type": "code", "text": "x = 1"},
]
text = _blocks_to_text(blocks)
assert "Heading" in text
assert "Body text" in text
assert "a" in text and "b" in text
assert "x = 1" in text
def test_loose_dict_missing_optional_keys_does_not_raise():
# No "level" on header, no "lang" on code, no "ordered" on list --
# every optional key must fall back to a sane default rather than
# KeyError/TypeError.
blocks = [
{"type": "header", "text": "H"},
{"type": "code", "text": "c"},
{"type": "list", "items": ["x"]},
]
assert _blocks_to_html(blocks)
assert _blocks_to_text(blocks)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 4. a full RichBlocks (the single block model) renders correctly
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_blocks_to_html_richblocks_matches_richmsg_native_render():
blocks = RichBlocks().header("Stats", level=2).paragraph("hello").list(["a", "b"])
expected, _files = richmsg.render_blocks_native_html(
richmsg.normalize_blocks(blocks)
)
assert _blocks_to_html(blocks) == expected
def test_blocks_to_text_richblocks_matches_richmsg_plain_text():
blocks = RichBlocks().header("Stats", level=2).paragraph("hello").list(["a", "b"])
expected = richmsg.blocks_to_plain_text(richmsg.normalize_blocks(blocks))
assert _blocks_to_text(blocks) == expected
def test_article_rich_richblocks_end_to_end():
ires = InlineResults(_FakeClient())
blocks = RichBlocks().header("Title").paragraph("Body")
result = _run(ires.article_rich("T", blocks))
rich_message = result.send_message.rich_message
assert isinstance(rich_message, types.InputRichMessageHTML)
assert rich_message.html == "<h1>Title</h1><p>Body</p>"
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 5. table()/expandable() -- the degradation path _simple_blocks used to
# special-case in assistant/inlinestuff.py -- now render natively (HTML)
# and degrade sensibly on the plain-text fallback, with no adapter.
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_table_block_renders_natively_as_html_table():
blocks = RichBlocks().table([["Field", "Value"], ["A", "1"]], caption="Overview")
html = _blocks_to_html(blocks)
assert "<table" in html
assert "<caption>Overview</caption>" in html
assert "<th>Field</th>" in html
assert "<td>A</td>" in html
def test_table_block_degrades_to_aligned_text_grid_on_fallback():
blocks = RichBlocks().table([["Field", "Value"], ["A", "1"]], caption="Overview")
text = _blocks_to_text(blocks)
# blocks_to_plain_text renders a table via richmsg.table_to_text --
# caption + aligned columns, never blank/dropped.
assert "Overview" in text
assert "Field" in text and "Value" in text
assert "A" in text and "1" in text
def test_expandable_block_renders_natively_as_blockquote():
blocks = RichBlocks().expandable("Details", "Everything is fine.")
html = _blocks_to_html(blocks)
assert "<blockquote expandable>" in html
assert "Details" in html
assert "Everything is fine." in html
def test_expandable_block_degrades_to_title_and_body_on_fallback():
blocks = RichBlocks().expandable("Details", "Everything is fine.")
text = _blocks_to_text(blocks)
assert "Details" in text
assert "Everything is fine." in text
def test_article_rich_table_and_expandable_end_to_end_native():
ires = InlineResults(_FakeClient())
blocks = (
RichBlocks()
.header("Report")
.table([["Plugins", "42"], ["Addons", "7"]], caption="Overview")
.expandable("More", "details here")
)
result = _run(ires.article_rich("T", blocks))
html = result.send_message.rich_message.html
assert "<table" in html
assert "Overview" in html
assert "<blockquote expandable>" in html
def test_inline_results_has_no_layer_availability_wrapper():
from telethon.tl import types
assert hasattr(types, "InputRichMessageHTML")
assert hasattr(types, "InputBotInlineMessageRichMessage")
assert not hasattr(InlineResults, "has")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# assistant/inlinestuff.py no longer defines _simple_blocks at all
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_simple_blocks_adapter_is_gone():
import ast
from pathlib import Path
src = Path(__file__).resolve().parents[1] / "assistant" / "inlinestuff.py"
tree = ast.parse(src.read_text())
names = {
node.name
for node in ast.walk(tree)
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
}
assert "_simple_blocks" not in names
def test_inlineresults_minimal_renderers_are_gone():
for name in ("_block_to_html", "_escape"):
assert not hasattr(inlineresults, name)
if __name__ == "__main__":
import sys
sys.exit(pytest.main([__file__, "-q"]))
|