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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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
from __future__ import annotations

import ast
import asyncio
from types import SimpleNamespace

import pytest
from conftest import ROOT
from telethon import Button, events, functions, types
from telethon.errors import BadRequestError, RPCError


def _load_helpers(helpers):
    path = ROOT / "plugins/_inline.py"
    tree = ast.parse(path.read_text(encoding="utf-8"))
    selected = {
        "_edit_rich_callback", "_show_deploy_guide",
        "_deploy_rpc_diagnostic", "_log_deploy_rpc",
    }
    functions_to_load = [
        node
        for node in tree.body
        if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef)) and node.name in selected
    ]
    module = ast.fix_missing_locations(ast.Module(body=functions_to_load, type_ignores=[]))
    namespace = {
        "RPCError": RPCError, "BadRequestError": BadRequestError,
        "InputBotInlineMessageID": types.InputBotInlineMessageID,
        "InputBotInlineMessageID64": types.InputBotInlineMessageID64,
        **helpers,
    }
    exec(compile(module, str(path), "exec"), namespace)  # noqa: S102 - isolated helpers
    namespace.update(helpers)
    return namespace


def test_hi_button_payload_matches_registered_telethon_callback():
    from custom.rich import button

    btn = button("Hi", data="alive_deploy_hi", preset="success")
    payload = btn.to_dict()["type"]["data"]
    matcher = events.CallbackQuery(data="alive_deploy_hi")
    event = SimpleNamespace(
        query=SimpleNamespace(data=payload, chat_instance="test-chat"),
        chat_id=None,
    )

    assert payload == b"alive_deploy_hi"
    assert matcher.filter(event) is True


def test_hi_callback_uses_native_layer230_inline_rich_edit_without_plain_sender():
    from pyUltroid._core import richmsg

    class FakeClient:
        def __init__(self):
            self.requests = []

        async def __call__(self, request):
            self.requests.append(request)
            return True

        def build_reply_markup(self, _buttons):
            return None

        async def send_message(self, *_args, **_kwargs):
            pytest.fail("the callback must not send a plain message")

    class FakeEvent:
        def __init__(self, client, inline_id):
            self.client = client
            self.query = SimpleNamespace(msg_id=inline_id)
            self.chat_id = None
            self.answers = []

        async def edit(self, *_args, **_kwargs):
            pytest.fail("the callback must not use the legacy event.edit path")

        async def answer(self, *args, **kwargs):
            self.answers.append((args, kwargs))

    inline_id = types.InputBotInlineMessageID(dc_id=2, id=5, access_hash=99)
    client = FakeClient()
    event = FakeEvent(client, inline_id)
    show = _load_helpers({"edit_rich": richmsg.edit_rich})["_show_deploy_guide"]

    result = asyncio.run(
        show(
            event,
            richmsg.RichMarkup("<p>Native deployment guide</p>", "html"),
            [[Button.url("Repository", "https://example.com/repository")]],
        )
    )

    assert result is True
    assert len(client.requests) == 1
    request = client.requests[0]
    assert isinstance(request, functions.messages.EditInlineBotMessageRequest)
    assert request.id is inline_id
    assert isinstance(request.rich_message, types.InputRichMessageHTML)
    assert event.answers == [((), {})]


def test_hi_callback_rich_edit_failure_answers_with_alert_without_plain_fallback():
    calls = []

    class FakeEvent:
        async def edit(self, *args, **kwargs):
            calls.append(("event.edit", args, kwargs))
            pytest.fail("a rich edit failure must not fall back to event.edit")

        async def answer(self, *args, **kwargs):
            calls.append(("answer", args, kwargs))

    async def fail_rich_edit(*_args, **_kwargs):
        raise BadRequestError(None, "MESSAGE_ID_INVALID", code=400)

    logs = SimpleNamespace(warning=lambda *args: calls.append(("log", args)))
    show = _load_helpers(
        {"LOGS": logs, "_edit_rich_callback": fail_rich_edit}
    )["_show_deploy_guide"]
    result = asyncio.run(show(FakeEvent(), "rich guide"))

    assert result is None
    assert len(calls) == 2
    assert calls[0][0] == "log"
    assert "rpc_id=%s rpc_type=%s" in calls[0][1][0]
    assert calls[0][1][1:3] == ("MESSAGE_ID_INVALID", "BadRequestError")
    assert calls[1] == (
        "answer",
        ("The guide could not be updated. Please try again.",),
        {"alert": True},
    )


def test_hi_callback_rich_message_unsupported_falls_back_to_layer230_plain_inline_edit():
    from pyUltroid._core import richmsg

    class FakeClient:
        def __init__(self):
            self.requests = []

        async def __call__(self, request):
            self.requests.append(request)
            return True

        def build_reply_markup(self, buttons):
            return ("inline-markup", buttons)

        async def send_message(self, *_args, **_kwargs):
            pytest.fail("fallback must edit the existing inline message")

    class FakeEvent:
        def __init__(self, client, inline_id):
            self.client = client
            self.query = SimpleNamespace(msg_id=inline_id)
            self.answers = []

        async def edit(self, *_args, **_kwargs):
            pytest.fail("fallback must not use the legacy event.edit path")

        async def answer(self, *args, **kwargs):
            self.answers.append((args, kwargs))

    inline_id = types.InputBotInlineMessageID(dc_id=2, id=5, access_hash=99)
    client = FakeClient()
    event = FakeEvent(client, inline_id)
    content = (
        richmsg.RichBlocks(native=True)
        .header("Deploy your own Ultroid")
        .expandable("Install", "Create a virtual environment")
    )

    async def reject_rich_edit(*_args, **_kwargs):
        raise BadRequestError(None, "RICH_MESSAGE_UNSUPPORTED", code=400)

    logs = SimpleNamespace(warning=lambda *_args: None)
    show = _load_helpers(
        {
            "LOGS": logs,
            "_edit_rich_callback": reject_rich_edit,
            "blocks_to_plain_text": richmsg.blocks_to_plain_text,
            "edit_inline_plain": richmsg.edit_inline_plain,
        }
    )["_show_deploy_guide"]
    buttons = [[Button.url("Repository", "https://example.com/repository")]]

    result = asyncio.run(show(event, content, buttons))

    assert result is True
    assert len(client.requests) == 1
    request = client.requests[0]
    assert isinstance(request, functions.messages.EditInlineBotMessageRequest)
    assert request.id is inline_id
    assert request.rich_message is None
    assert request.message == richmsg.blocks_to_plain_text(content)
    assert "Deploy your own Ultroid" in request.message
    assert "Create a virtual environment" in request.message
    assert request.reply_markup == ("inline-markup", buttons)
    assert event.answers == [((), {})]


def test_hi_callback_source_only_routes_guide_through_rich_helper():
    source = (ROOT / "plugins/_inline.py").read_text(encoding="utf-8")
    tree = ast.parse(source)
    callback = next(
        node
        for node in tree.body
        if isinstance(node, ast.AsyncFunctionDef)
        and node.name == "_cb_alive_deploy_hi"
    )
    calls = [node for node in ast.walk(callback) if isinstance(node, ast.Call)]

    assert any(
        isinstance(call.func, ast.Name) and call.func.id == "_show_deploy_guide"
        for call in calls
    )
    assert not any(
        isinstance(call.func, ast.Attribute)
        and isinstance(call.func.value, ast.Name)
        and call.func.value.id == "event"
        and call.func.attr == "edit"
        for call in calls
    )
    assert not any(
        isinstance(node, ast.Name) and node.id == "fallback_text"
        for node in ast.walk(callback)
    )


@pytest.mark.parametrize("message, expected", [
    ("RICH_MESSAGE_UNSUPPORTED", "RICH_MESSAGE_UNSUPPORTED"),
    ("MESSAGE_ID_INVALID", "MESSAGE_ID_INVALID"),
    ("RICH_MESSAGE_UNSUPPORTED secret-token chat-content", "UNKNOWN_RPC"),
    ("rich_message_unsupported", "UNKNOWN_RPC"),
    ("MESSAGE_ID_INVALID\nprivate-content", "UNKNOWN_RPC"),
    ("SECRET_IDENTIFIER", "UNKNOWN_RPC"),
    (None, "UNKNOWN_RPC"),
    ([], "UNKNOWN_RPC"),
])
def test_deploy_rpc_diagnostic_only_emits_allowlisted_labels(message, expected):
    diagnostic = _load_helpers({})["_deploy_rpc_diagnostic"]
    error = BadRequestError(None, "initial", code=400)
    error.message = message
    assert diagnostic(error) == (expected, "BadRequestError")


def test_deploy_rpc_diagnostic_does_not_stringify_or_emit_custom_class_name():
    class PrivateError(RPCError):
        def __str__(self):
            raise AssertionError("must not stringify an RPC error")

    error = PrivateError(None, "private-content", code=500)
    diagnostic = _load_helpers({})["_deploy_rpc_diagnostic"]
    assert diagnostic(error) == ("UNKNOWN_RPC", "RPCError")


def _diagnostic_scenario(rich_error, *, target="query", fallback_error=None, answer_error=None):
    records, attempts, answers = [], [], []
    inline_id = types.InputBotInlineMessageID(dc_id=2, id=5, access_hash=99)
    if target == "message64":
        inline_id = types.InputBotInlineMessageID64(dc_id=2, owner_id=1, id=5, access_hash=99)
    event = SimpleNamespace(
        client=object(), query=SimpleNamespace(msg_id=None),
        message_id=None, chat_id=None,
    )
    if target == "query":
        event.query.msg_id = inline_id
    elif target == "message64":
        event.message_id = inline_id
    elif target == "normal":
        event.message_id, event.chat_id = 7, 8
    elif target == "fake":
        event.query.msg_id = type("InputBotInlineMessageID", (), {})()

    async def edit_rich(*_args):
        raise rich_error

    async def fallback(*args, **kwargs):
        attempts.append((args, kwargs))
        if fallback_error is not None:
            raise fallback_error
        return True

    async def answer(*args, **kwargs):
        answers.append((args, kwargs))
        if answer_error is not None:
            raise answer_error

    event.answer = answer
    show = _load_helpers({
        "LOGS": SimpleNamespace(warning=lambda fmt, *args: records.append(fmt % args)),
        "_edit_rich_callback": edit_rich,
        "edit_inline_plain": fallback,
        "blocks_to_plain_text": lambda _content: "private-guide-content",
    })["_show_deploy_guide"]
    return show, event, records, attempts, answers


@pytest.mark.parametrize("target, recognized, inline", [
    ("query", True, True), ("message64", True, True),
    ("normal", True, False), ("missing", False, False), ("fake", False, False),
])
def test_deploy_target_and_fallback_diagnostic_branches(target, recognized, inline):
    show, event, records, attempts, answers = _diagnostic_scenario(
        BadRequestError(None, "RICH_MESSAGE_UNSUPPORTED", code=400), target=target,
    )
    assert asyncio.run(show(event, "private-guide-content")) is (True if inline else None)
    assert len(attempts) == int(inline)
    assert f"message_target_recognized={recognized}" in records[0]
    assert f"inline_target_recognized={inline}" in records[0]
    assert f"fallback_attempted={inline} fallback_succeeded=False" in records[0]
    assert f"fallback_attempted={inline} fallback_succeeded={inline}" in records[-1]
    assert answers[-1][1] == ({} if inline else {"alert": True})
    assert all("private-guide-content" not in record for record in records)


@pytest.mark.parametrize("message", [
    "MESSAGE_ID_INVALID", "private-token-message",
    "RICH_MESSAGE_UNSUPPORTED private-token-message", "rich_message_unsupported",
])
def test_deploy_other_rpc_errors_never_trigger_fallback_or_leak_text(message):
    show, event, records, attempts, answers = _diagnostic_scenario(
        BadRequestError(None, message, code=400),
    )
    assert asyncio.run(show(event, "private-guide-content")) is None
    assert not attempts
    assert answers[-1][1] == {"alert": True}
    assert all("private-token-message" not in record for record in records)
    assert all("fallback_attempted=False fallback_succeeded=False" in record for record in records)


def test_deploy_fallback_rpc_failure_is_sanitized_and_alerts():
    show, event, records, attempts, answers = _diagnostic_scenario(
        BadRequestError(None, "RICH_MESSAGE_UNSUPPORTED", code=400),
        fallback_error=BadRequestError(None, "private-token-message", code=400),
    )
    assert asyncio.run(show(event, "content")) is None
    assert len(attempts) == 1
    assert "rpc_id=UNKNOWN_RPC" in records[-1]
    assert "fallback_attempted=True fallback_succeeded=False" in records[-1]
    assert "private-token-message" not in " ".join(records)
    assert answers[-1][1] == {"alert": True}


@pytest.mark.parametrize("stage", ["rich", "fallback", "answer", "success_answer"])
def test_deploy_unrelated_exceptions_propagate(stage):
    unrelated = RuntimeError("private-programming-error")
    show, event, records, _attempts, _answers = _diagnostic_scenario(
        unrelated if stage == "rich" else BadRequestError(None, "RICH_MESSAGE_UNSUPPORTED", code=400),
        target="missing" if stage == "answer" else "query",
        fallback_error=unrelated if stage == "fallback" else None,
        answer_error=unrelated if stage in {"answer", "success_answer"} else None,
    )
    with pytest.raises(RuntimeError, match="private-programming-error"):
        asyncio.run(show(event, "content"))
    assert "private-programming-error" not in " ".join(records)


def test_deploy_expired_alert_rpc_is_sanitized():
    show, event, records, _attempts, _answers = _diagnostic_scenario(
        BadRequestError(None, "MESSAGE_ID_INVALID", code=400),
        answer_error=BadRequestError(None, "QUERY_ID_INVALID", code=400),
    )
    assert asyncio.run(show(event, "content")) is None
    assert "rpc_id=QUERY_ID_INVALID" in records[-1]


def _native_deploy_guide():
    path = ROOT / "plugins/_inline.py"
    node = next(
        node for node in ast.parse(path.read_text()).body
        if isinstance(node, ast.FunctionDef) and node.name == "_deploy_guide_blocks"
    )
    namespace = {}
    exec(compile(ast.Module(body=[node], type_ignores=[]), str(path), "exec"), namespace)  # noqa: S102 - isolated builder
    return namespace[node.name]("Example <User>")


def test_nested_guide_inline_fallback_is_bounded_and_honest_about_missing_controls():
    show, event, _records, attempts, answers = _diagnostic_scenario(
        BadRequestError(None, "RICH_MESSAGE_UNSUPPORTED", code=400),
    )
    assert asyncio.run(show(event, _native_deploy_guide())) is True
    assert len(attempts) == 1
    text = attempts[0][0][2]
    assert len(text.encode("utf-16-le")) // 2 <= 4096
    assert "collapsibles, tables and in-section copy/URL buttons are unavailable" in text
    assert "Step 7" in text and "uv run --no-sync python3 -m pyUltroid" in text
    assert "SESSION=" in text and "MONGO_URI=" in text
    assert "Never share SESSION, MONGO_URI" in text
    assert answers == [((), {})]


def test_hi_callback_keeps_actual_in_body_buttons_in_native_inline_edit():
    from pyUltroid._core import richmsg

    class Client:
        def __init__(self):
            self.requests = []

        async def __call__(self, request):
            self.requests.append(request)
            return True

        def build_reply_markup(self, _buttons):
            return None

    client = Client()
    inline_id = types.InputBotInlineMessageID(dc_id=2, id=5, access_hash=99)
    event = SimpleNamespace(client=client, query=SimpleNamespace(msg_id=inline_id))
    guide = _native_deploy_guide()
    edit = _load_helpers({"edit_rich": richmsg.edit_rich})["_edit_rich_callback"]
    assert asyncio.run(edit(event, guide, None)) is True
    request, = client.requests
    assert isinstance(request, functions.messages.EditInlineBotMessageRequest)
    assert request.reply_markup is None
    assert bytes(request.rich_message) == bytes(guide)
    step_one = next(block for block in request.rich_message.blocks if isinstance(block, types.PageBlockDetails))
    row = next(block for block in step_one.blocks if isinstance(block, types.PageBlockButtonRow))
    assert isinstance(row.buttons[0].type, types.InlineButtonTypeCopy)