from __future__ import annotations import ast import asyncio import html from collections.abc import Sequence from pathlib import Path from types import SimpleNamespace from typing import Any from conftest import ROOT def _load_functions(path: Path, names: set[str], namespace: dict): tree = ast.parse(path.read_text(encoding="utf-8")) nodes = [ node for node in tree.body if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in names ] module = ast.fix_missing_locations(ast.Module(body=nodes, type_ignores=[])) exec(compile(module, str(path), "exec"), namespace) # noqa: S102 - isolated helpers return namespace def test_inline_send_handler_waits_and_edits_only_the_deploy_result_with_full_name(): path = ROOT / "plugins/_inline.py" waits = [] edits = [] class InputBotInlineMessageID64: pass class FakeClient: async def get_entity(self, user_id): assert user_id == 90210 return SimpleNamespace(first_name="Ada", last_name="Lovelace") async def sleep(delay): waits.append(delay) async def edit_rich(*args, **kwargs): edits.append((args, kwargs)) return "edited" helper = _load_functions( path, {"_is_deploy_inline_send", "_handle_deploy_inline_send"}, { "asyncio": SimpleNamespace(sleep=sleep), "INLINE_ALIVE_RESULT_ID": "alive-deploy", "INLINE_DEPLOY_SEND_DELAY_SECONDS": 3.5, "asst": None, "get_display_name": lambda user: f"{user.first_name} {user.last_name}", "LOGS": SimpleNamespace(debug=lambda *_args, **_kwargs: None), "_alive_rich_markup": lambda name: f"status:{name}", "edit_rich": edit_rich, "_btn": lambda *args, **kwargs: (args, kwargs), }, ) update = SimpleNamespace( query="", id="alive-deploy", user_id=90210, msg_id=InputBotInlineMessageID64(), ) client = FakeClient() result = asyncio.run(helper["_handle_deploy_inline_send"](update, client)) assert result == "edited" assert waits == [3.5] assert edits[0][0] == (client, update.msg_id, None, "status:Ada Lovelace") assert edits[0][1]["format"] == "html" assert edits[0][1]["link_preview"] is False assert edits[0][1]["buttons"][0][0][0] == ("Hi",) def test_inline_send_handler_ignores_unrelated_inline_results_without_delay(): path = ROOT / "plugins/_inline.py" waits = [] edits = [] async def sleep(delay): waits.append(delay) async def edit_rich(*args, **kwargs): edits.append((args, kwargs)) helper = _load_functions( path, {"_is_deploy_inline_send", "_handle_deploy_inline_send"}, { "asyncio": SimpleNamespace(sleep=sleep), "INLINE_ALIVE_RESULT_ID": "alive-deploy", "INLINE_DEPLOY_SEND_DELAY_SECONDS": 3.5, "asst": None, "get_display_name": lambda _user: "unused", "LOGS": SimpleNamespace(debug=lambda *_args, **_kwargs: None), "_alive_rich_markup": lambda _name: "unused", "edit_rich": edit_rich, "_btn": lambda *args, **kwargs: (args, kwargs), }, ) result = asyncio.run( helper["_handle_deploy_inline_send"]( SimpleNamespace(query="", id="some-other-result", user_id=1, msg_id=object()), object(), ) ) assert result is None assert waits == [] assert edits == [] def test_deployment_guide_uses_native_details_code_and_italic_footer(): from telethon import types from pyUltroid._core.richmsg import rich_message helper = _load_functions(ROOT / "plugins/_inline.py", {"_deploy_guide_blocks"}, {}) native = rich_message(helper["_deploy_guide_blocks"]("Ada ")) assert isinstance(native, types.InputRichMessage) assert "Deploy your own Ultroid" in native.blocks[0].text.text.text assert native.blocks[1].text.text == "Hey Ada ! Browse these steps to deploy your own Ultroid." steps = [block for block in native.blocks if isinstance(block, types.PageBlockDetails)] assert len(steps) == 7 all_code = [] def visit(blocks): for block in blocks: if isinstance(block, types.PageBlockPreformatted): all_code.append(block.text.text) visit(getattr(block, "blocks", ()) or ()) visit(native.blocks) assert any("git clone https://github.com/Mahesh953-hub/SarahUB.git Ultroid" in code for code in all_code) assert any("pip install -r requirements.txt" in code for code in all_code) assert any("API_HASH=YOUR_API_HASH" in code for code in all_code) assert any("BOT_TOKEN=" in code and "SESSION=" in code for code in all_code) assert any("python3 -m pyUltroid" in code for code in all_code) assert any("nohup" in code for code in all_code) assert isinstance(native.blocks[-1].text, types.TextItalic) assert bytes(native) def test_assistant_callback_target_uses_message_id_not_random_query_id(): helper = _load_functions( ROOT / "assistant/rich_pages.py", {"callback_edit_target"}, {"Any": Any}, )["callback_edit_target"] event = SimpleNamespace( chat_id=123, id=987654321012345678, message_id=42, query=SimpleNamespace(msg_id=42), ) assert helper(event) == (123, 42) class InputBotInlineMessageID64: pass inline_id = InputBotInlineMessageID64() inline_event = SimpleNamespace( chat_id=None, id=987654321012345678, message_id=inline_id, query=SimpleNamespace(msg_id=inline_id), ) assert helper(inline_event) == (inline_id, None) def test_alive_inline_card_is_a_separate_rich_status_without_hi_or_guide_flow(): class Markup: def __init__(self, text, format="html", files=()): self.text = text self.format = format self.files = tuple(files) helper = _load_functions( ROOT / "plugins/bot.py", {"_alive_inline_markup"}, { "html": html, "RichMarkup": Markup, "RichFile": object, "coerce_to_rich_markup": lambda text, **_kwargs: Markup(text), "asst": SimpleNamespace(me=SimpleNamespace(first_name="FuckingClonerBot")), "udB": SimpleNamespace( get_key=lambda key: "Hey & welcome" if key == "ALIVE_TEXT" else None ), "get_string": lambda _key: "Welcome", "ultroid_version": "1.2.3", "HOSTED_ON": "test", "UltVer": "2.0", "pyver": lambda: "3.12", "__version__": "1.0", "_telethon_build_label": lambda: "1.45.0 [#e8019a5@230]", "_alive_plugin_counts": lambda: (2, 1), }, ) card = helper["_alive_inline_markup"]("2 hours", "main") assert "Ultroid · Alive" in card.text assert "Hey & welcome" in card.text assert "1.45.0 [#e8019a5@230]" in card.text assert "Plugins" in card.text and "2" in card.text assert "Addons" in card.text and "1" in card.text assert "Status" not in card.text and "PyUltroid" not in card.text assert "Runtime snapshot" not in card.text assert "SarahUB source" not in card.text and "Support" not in card.text assert "", [("API key", "Set & hidden")], columns=("Option", "Purpose"), page=2, pages=3, total=8, ) assert page.text.startswith("

Settings <safe>

") assert page.text.count("
") == 2 assert '' in page.text assert "API key" in page.text and "Set & hidden" in page.text assert page.text.endswith("

page 2 of 3 • total 8

") media_page = helper["body_page"]( "Welcome", "

Welcome.

", media=("photo", "uploaded-photo", "Hi") ) assert 'src="assistant-page-media"' in media_page.text assert media_page.files[0].media == "uploaded-photo" assert media_page.files[0].kind == "photo"