File size: 4,942 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
"""Focused tests for brain's narrowly scoped rich-edit fallback."""
from __future__ import annotations

import ast
import asyncio
from pathlib import Path
from types import SimpleNamespace

import pytest
from telethon import types
from telethon.errors import BadRequestError, RPCError

ROOT = Path(__file__).resolve().parents[1]


def load_edit_function():
    tree = ast.parse((ROOT / "plugins" / "brain.py").read_text())
    node = next(
        item for item in tree.body
        if isinstance(item, ast.AsyncFunctionDef) and item.name == "_edit_ai_output"
    )
    namespace = {
        "edit_rich": None,
        "rich_output": lambda text: f"rich:{text}",
        "_plain_ai_text": lambda text: f"plain:{text}",
        "BadRequestError": BadRequestError,
        "RPCError": RPCError,
    }
    exec(compile(ast.Module(body=[node], type_ignores=[]), "plugins/brain.py", "exec"), namespace)  # noqa: S102
    return namespace["_edit_ai_output"], namespace


class Msg:
    def __init__(self, error=None):
        self.error = error
        self.edits = []

    async def edit(self, text, **kwargs):
        self.edits.append((text, kwargs))
        if self.error:
            raise self.error


def err(message, error_type=BadRequestError):
    return error_type(None, message, code=400)


def run(coro):
    return asyncio.run(coro)


def event():
    return SimpleNamespace(client=object(), chat_id=42)


def test_rich_success_preserves_rich_attempt_without_plain_edit():
    fn, ns = load_edit_function()
    calls = []
    async def rich(*args):
        calls.append(args)
    ns["edit_rich"] = rich
    msg = Msg()
    run(fn(event(), msg, "answer", True))
    assert len(calls) == 1
    assert calls[0][1:3] == (42, msg)
    assert isinstance(calls[0][3], types.InputRichMessage)
    assert isinstance(calls[0][3].blocks[-1], types.PageBlockDetails)
    assert msg.edits == []


def test_plain_unprivileged_branch_uses_existing_message():
    fn, ns = load_edit_function()
    ns["edit_rich"] = pytest.fail
    msg = Msg()
    run(fn(event(), msg, "answer", False))
    assert msg.edits == [("plain:answer", {"parse_mode": None})]


def test_exact_unsupported_rpc_retries_same_message_as_plain_once():
    fn, ns = load_edit_function()
    calls = []
    async def rich(client, chat_id, target, text):
        calls.append((client, chat_id, target, text))
        raise err("RICH_MESSAGE_UNSUPPORTED")
    ns["edit_rich"] = rich
    msg = Msg()
    evt = event()
    run(fn(evt, msg, "answer", True))
    assert len(calls) == 1
    assert calls[0][:3] == (evt.client, 42, msg)
    assert isinstance(calls[0][3], types.InputRichMessage)
    assert msg.edits == [("plain:answer", {"parse_mode": None})]


@pytest.mark.parametrize("error", [
    err("MESSAGE_ID_INVALID"),
    err(None),
    err("RICH_MESSAGE_UNSUPPORTED "),
    err("prefix RICH_MESSAGE_UNSUPPORTED"),
    err("rich_message_unsupported"),
    RuntimeError("RICH_MESSAGE_UNSUPPORTED"),
])
def test_unrelated_or_nonexact_errors_propagate(error):
    fn, ns = load_edit_function()
    async def rich(*args):
        raise error
    ns["edit_rich"] = rich
    msg = Msg()
    with pytest.raises(type(error)) as caught:
        run(fn(event(), msg, "answer", True))
    assert caught.value is error
    assert msg.edits == []


def test_fallback_error_propagates():
    fn, ns = load_edit_function()
    async def rich(*args):
        raise err("RICH_MESSAGE_UNSUPPORTED")
    ns["edit_rich"] = rich
    fallback_error = RuntimeError("plain edit failed")
    msg = Msg(fallback_error)
    with pytest.raises(RuntimeError, match="plain edit failed"):
        run(fn(event(), msg, "answer", True))
    assert msg.edits == [("plain:answer", {"parse_mode": None})]


@pytest.mark.parametrize("as_markup", [False, True])
def test_unsupported_details_fallback_preserves_titles_bodies_and_commands(as_markup):
    from pyUltroid._core.richmsg import (
        RichBlocks,
        blocks_to_plain_text,
        rich_markup_from_blocks,
    )

    details = RichBlocks().paragraph("Install the dependencies").code("pip install -r requirements.txt", lang="bash")
    content = RichBlocks(native=True).header("Setup guide").expandable("Install", details)
    if as_markup:
        content = rich_markup_from_blocks(content, format="html")
    fn, ns = load_edit_function()
    ns["rich_output"] = lambda _text: content
    ns["_plain_ai_text"] = lambda text: blocks_to_plain_text(ns["rich_output"](text))

    async def rich(*_args):
        raise err("RICH_MESSAGE_UNSUPPORTED")

    ns["edit_rich"] = rich
    msg = Msg()
    run(fn(event(), msg, "answer", True))
    assert len(msg.edits) == 1
    text, options = msg.edits[0]
    assert all(part in text for part in (
        "Setup guide", "Install", "Install the dependencies", "pip install -r requirements.txt",
    ))
    assert "<blockquote" not in text and "<code" not in text
    assert options == {"parse_mode": None}