Spaces:
Paused
Paused
File size: 19,232 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 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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | from __future__ import annotations
import ast
import importlib.util
from collections import OrderedDict
from types import SimpleNamespace
from conftest import ROOT
def _command_help():
path = ROOT / "pyUltroid/fns/command_help.py"
spec = importlib.util.spec_from_file_location("command_help_under_test", path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def test_command_syntax_hides_regex_and_keeps_alternatives_readable():
command_help = _command_help()
assert command_help.command_syntax("help( (.*)|$)", ".") == ".help [arguments]"
assert command_help.command_syntax("(?:whisper|whspr)(?: |$)(.*)", ".") == (
".whisper / .whspr [arguments]"
)
assert command_help.command_syntax("(auto|stop)name$", ".") == (
".autoname / .stopname"
)
assert command_help.command_syntax("vcmute$", ".") == ".vcmute"
assert command_help.command_matches("(?:whisper|whspr)(?: |$)(.*)", "whspr")
assert not command_help.command_matches("vcmute$", "vcunmute")
def test_callback_wrapper_no_longer_injects_settings_callback_heading():
source = (ROOT / "pyUltroid/_misc/_assistant.py").read_text(encoding="utf-8")
assert '"Settings Callback"' not in source
def test_start_update_claim_is_idempotent_and_bounded():
path = ROOT / "assistant/start.py"
tree = ast.parse(path.read_text(encoding="utf-8"))
helper = next(
node
for node in tree.body
if isinstance(node, ast.FunctionDef) and node.name == "_claim_start_event"
)
module = ast.fix_missing_locations(ast.Module(body=[helper], type_ignores=[]))
namespace = {
"OrderedDict": OrderedDict,
"_START_EVENTS": OrderedDict(),
"_START_EVENT_LIMIT": 3,
}
exec(compile(module, str(path), "exec"), namespace) # noqa: S102 - isolated helper
claim = namespace["_claim_start_event"]
event = SimpleNamespace(
chat_id=10,
sender_id=20,
message=SimpleNamespace(id=30),
)
assert claim(event)
assert not claim(event)
assert claim(
SimpleNamespace(chat_id=10, sender_id=20, message=SimpleNamespace(id=31))
)
assert claim(
SimpleNamespace(chat_id=10, sender_id=20, message=SimpleNamespace(id=32))
)
assert claim(
SimpleNamespace(chat_id=10, sender_id=20, message=SimpleNamespace(id=33))
)
assert len(namespace["_START_EVENTS"]) == 3
def test_settings_buttons_use_plain_consistent_captions():
source = (ROOT / "assistant/callbackstuffs.py").read_text(encoding="utf-8")
for inconsistent in (
"Oғғ",
"Inғo",
"PMPermit Type",
"PMLOGGER",
"Chat Bot On",
"Addons On",
"Sudo Mode On",
):
assert inconsistent not in source
def _load_functions(path, names, namespace):
tree = ast.parse(path.read_text(encoding="utf-8"))
selected = [
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=selected, type_ignores=[]))
exec(compile(module, str(path), "exec"), namespace) # noqa: S102 - isolated helpers
return namespace
def test_settings_summary_removes_live_inventory_and_secret_values():
values = {
"DEEP_AI": "must-not-appear",
"PMSETTING": True,
"VC_SESSION": "private-session-string",
}
db = SimpleNamespace(get_key=values.get)
def render_page(*args, **kwargs):
return args, kwargs
helpers = _load_functions(
ROOT / "assistant/start.py",
{"_settings_sections", "_settings_page"},
{"html": __import__("html"), "udB": db, "rich_page": render_page},
)
page = helpers["_settings_page"]()
rendered = repr(page)
assert page[0][0] == "Assistant Settings"
assert "Current state" not in rendered
assert "DEEP_AI" not in rendered and "PMSETTING" not in rendered
assert "VC_SESSION" not in rendered
assert "must-not-appear" not in rendered
assert "private-session-string" not in rendered
def test_language_settings_edit_inline_callback_id_directly():
import asyncio
calls = []
async def edit_rich(*args, **kwargs):
calls.append((args, kwargs))
return "edited"
class InputBotInlineMessageID64:
pass
client = object()
inline_id = InputBotInlineMessageID64()
event = SimpleNamespace(
client=client,
chat_id=12345,
query=SimpleNamespace(msg_id=inline_id),
)
callback_target = _load_functions(
ROOT / "assistant/rich_pages.py",
{"callback_edit_target"},
{"Any": object},
)["callback_edit_target"]
helper = _load_functions(
ROOT / "assistant/localization.py",
{"_edit_language"},
{"edit_rich": edit_rich, "callback_edit_target": callback_target},
)["_edit_language"]
assert asyncio.run(helper(event, "content", "buttons")) == "edited"
args, kwargs = calls[0]
assert args == (client, inline_id, None, "content")
assert kwargs["buttons"] == "buttons"
def test_force_subscribe_accepts_native_and_legacy_values_without_character_splitting():
path = ROOT / "assistant/pmbot.py"
parser = _load_functions(path, {"_force_sub_chats"}, {"ast": ast})[
"_force_sub_chats"
]
assert parser([-100123, -100456]) == (-100123, -100456)
assert parser("[-100123, -100456]") == (-100123, -100456)
assert parser("-100123") == (-100123,)
assert parser("@example_channel") == ("@example_channel",)
assert parser(None) == ()
settings = (ROOT / "assistant/callbackstuffs.py").read_text(encoding="utf-8")
assert 'udB.set_key("PMBOT_FSUB", str(Ll))' in settings
def test_consumed_assistant_dm_replies_stop_telethon_dispatch():
bridge = (ROOT / "assistant/agent_bridge.py").read_text(encoding="utf-8")
sessions = (ROOT / "assistant/ai_dm.py").read_text(encoding="utf-8")
assert "raise events.StopPropagation" in bridge
assert "raise events.StopPropagation" in sessions
def test_owner_info_setting_is_read_dynamically_and_false_disables_the_button():
path = ROOT / "assistant/start.py"
class FakeDB:
value = None
def get_key(self, key):
if key == "BOT_INFO_START":
return self.value
if key == "PMBOT":
return False
raise AssertionError(f"Unexpected configuration lookup: {key}")
db = FakeDB()
namespace = {
"udB": db,
"OWNER_NAME": "Owner",
"OWNER_ID": 123,
"ultroid_version": "1.2.3",
}
namespace = _load_functions(
path, {"_default_owner_info", "_owner_info_state"}, namespace
)
state = namespace["_owner_info_state"]
assert state() == (namespace["_default_owner_info"](), False, True)
db.value = "Custom {mention}"
assert state() == ("Custom {mention}", True, True)
db.value = "False"
assert state() == ("", True, False)
def test_paste_callback_builds_keyboard_with_ikm_call_not_subscript():
path = ROOT / "assistant/callbackstuffs.py"
tree = ast.parse(path.read_text(encoding="utf-8"))
broken = [
node.lineno
for node in ast.walk(tree)
if isinstance(node, ast.Subscript)
and isinstance(node.value, ast.Name)
and node.value.id == "ikm"
]
assert not broken, f"ikm must be called as a builder, not indexed: {broken}"
def test_deployment_guide_falls_back_to_text_when_photo_delivery_fails():
import asyncio
path = ROOT / "plugins/_ultroid.py"
helper = _load_functions(
path,
{"_send_fallback_guide"},
{"DEPLOYMENT_FALLBACK": "readable setup help"},
)["_send_fallback_guide"]
class FakeClient:
def __init__(self):
self.calls = []
async def send_file(self, *args, **kwargs):
self.calls.append(("file", args, kwargs))
raise OSError("photo unavailable")
async def send_message(self, *args, **kwargs):
self.calls.append(("message", args, kwargs))
return "sent"
client = FakeClient()
result = asyncio.run(helper(client, 42))
assert result == "sent"
assert [call[0] for call in client.calls] == ["file", "message"]
assert client.calls[-1][1] == (42, "readable setup help")
def test_pm_bot_profile_link_matches_html_parse_mode():
source = (ROOT / "assistant/pmbot.py").read_text(encoding="utf-8")
assert "fns.inline_mention(k, html=True)" in source
assert "event.text.startswith" not in source
def test_ai_command_prefix_is_anchored_and_consumed_once():
import re
path = ROOT / "assistant/ai_dm.py"
tree = ast.parse(path.read_text(encoding="utf-8"))
assignment = next(
node
for node in tree.body
if isinstance(node, ast.Assign)
and any(
isinstance(target, ast.Name) and target.id == "_AI_PREFIX"
for target in node.targets
)
)
module = ast.fix_missing_locations(ast.Module(body=[assignment], type_ignores=[]))
namespace = {"re": re}
exec(compile(module, str(path), "exec"), namespace) # noqa: S102 - isolated regex
prefix = namespace["_AI_PREFIX"]
assert prefix.sub("", "/ai@helperbot summarize this", count=1) == "summarize this"
assert prefix.sub("", "please mention /ai in the report", count=1) == (
"please mention /ai in the report"
)
def test_ai_session_list_uses_a_structured_list_without_markdown_indentation():
import asyncio
import time
path = ROOT / "assistant/ai_dm.py"
class Blocks:
def __init__(self, **_kwargs):
self.items = None
def header(self, *_args, **_kwargs):
return self
def list(self, items):
self.items = list(items)
return self
sent = {}
async def send_rich(_client, _chat_id, blocks, **kwargs):
sent["items"] = blocks.items
sent["kwargs"] = kwargs
return "sent"
helper = _load_functions(
path,
{"_send_session_list"},
{
"RichBlocks": Blocks,
"send_rich": send_rich,
"time": time,
"_LOGS": SimpleNamespace(exception=lambda *_: None),
},
)["_send_session_list"]
event = SimpleNamespace(client=object(), chat_id=44)
result = asyncio.run(
helper(
event,
[
{
"title": "A < B",
"created": 0,
"anchor": 12,
}
],
)
)
assert result == "sent"
assert sent["items"] == ["A < B — 1970-01-01 00:00 (reply to message 12)"]
assert not sent["items"][0].startswith(" ")
assert "**" not in sent["items"][0]
assert sent["kwargs"]["reply_to"] is event
def test_akinator_provider_text_is_escaped_while_intentional_bold_survives():
from custom.fmt import fmtmsg, rawhtml
import html
path = ROOT / "assistant/games.py"
result = _load_functions(
path,
{"_akinator_result"},
{"fmtmsg": fmtmsg, "rawhtml": rawhtml, "html": html},
)["_akinator_result"]("A & B <script>", "literal <strong> and &")
assert "<b>A & B <script></b>" in result
assert "literal <strong> and &amp;" in result
assert "<script>" not in result
assert "<strong>" not in result
def test_deploy_notice_state_is_not_saved_when_delivery_fails():
path = ROOT / "pyUltroid/startup/funcs.py"
class FakeDB:
def __init__(self):
self.values = {}
def set_key(self, key, value):
self.values[key] = value
helper = _load_functions(path, {"_record_deploy_notice_sent"}, {})[
"_record_deploy_notice_sent"
]
db = FakeDB()
assert helper(db, None, initial_deploy=True) is False
assert db.values == {}
assert helper(db, SimpleNamespace(id=21, media=None), initial_deploy=True)
assert db.values == {"LAST_UPDATE_LOG_SPAM": 21, "INIT_DEPLOY": "Done"}
def test_admin_verification_marks_missing_or_expired_tokens_as_stale():
path = ROOT / "assistant/manager/admins.py"
helper = _load_functions(path, {"_verification_pending"}, {})[
"_verification_pending"
]
assert helper("token", {"token": object()})
assert not helper("token", {})
assert not helper("token", None)
def test_locale_callback_resolves_only_supported_language_codes():
path = ROOT / "assistant/localization.py"
helper = _load_functions(path, {"_language_key"}, {})["_language_key"]
supported = {"en": {}, "hi": {}}
assert helper("hi", supported) == "hi"
assert helper("HI", supported) == "hi"
assert helper("xx", supported) is None
def test_timezone_input_accepts_trimmed_iana_names_and_rejects_unknown_names():
import pytz
helper = _load_functions(
ROOT / "assistant/start.py",
{"_valid_timezone"},
{
"tz": pytz.timezone,
"UnknownTimeZoneError": pytz.UnknownTimeZoneError,
},
)["_valid_timezone"]
assert helper(" Asia/Kolkata ") == "Asia/Kolkata"
assert helper("UTC") == "UTC"
assert helper("") is None
assert helper("not/a-real-timezone") is None
def test_message_formatter_passes_native_rich_payloads_without_calling_strip():
path = ROOT / "pyUltroid/_misc/_assistant.py"
tree = ast.parse(path.read_text(encoding="utf-8"))
formatter = next(
node
for node in tree.body
if isinstance(node, ast.ClassDef) and node.name == "MessageFormatter"
)
method = next(
node
for node in formatter.body
if isinstance(node, ast.FunctionDef) and node.name == "format"
)
module = ast.fix_missing_locations(ast.Module(body=[method], type_ignores=[]))
namespace = {"Any": object}
exec(compile(module, str(path), "exec"), namespace) # noqa: S102 - isolated method
class Formatter:
format = namespace["format"]
native = SimpleNamespace(__rich_format__="html", text="rich payload")
assert Formatter().format(native) is native
def test_callback_settings_plain_values_are_clean_and_secrets_are_not_shown():
path = ROOT / "assistant/callbackstuffs.py"
class FakeDB:
def get_key(self, key):
return "super-secret-token" if key == "VC_SESSION" else None
namespace = _load_functions(
path,
{"_mask_val", "_plain"},
{"udB": FakeDB()},
)
assert namespace["_mask_val"]("VC_SESSION") == "Set (hidden)"
assert "super-secret-token" not in namespace["_mask_val"]("VC_SESSION")
assert namespace["_plain"]("**Status** and `code`") == "Status and code"
def test_user_client_help_fallback_has_no_assistant_bot_callback_data():
path = ROOT / "plugins/_help.py"
def fake_ikm(rows, **_kwargs):
return rows
namespace = _load_functions(
path,
{"_fallback_help_buttons"},
{
"asst": SimpleNamespace(me=SimpleNamespace(username="helper_bot")),
"ikm": fake_ikm,
},
)
buttons = namespace["_fallback_help_buttons"]()
assert buttons == [
[("Open assistant", "url", "https://t.me/helper_bot?start=start", "primary")]
]
assert all(button[1] == "url" for row in buttons for button in row)
def test_assistant_manager_package_exports_runtime_names_for_siblings():
source = (ROOT / "assistant/manager/__init__.py").read_text(encoding="utf-8")
assert "from .. import *" in source
assert (
'__all__ = tuple(name for name in globals() if not name.startswith("_"))'
in source
)
def test_onboarding_callback_has_missing_user_guard_and_native_message_path():
source = (ROOT / "assistant/manager/_on_adds.py").read_text(encoding="utf-8")
assert 'user is None or not getattr(user, "is_self", False)' in source
assert "send_rich(" in source
assert "except Exception" not in source
def test_afk_command_detection_honors_configured_prefix_and_bot_alias():
path = ROOT / "assistant/manager/afk.py"
helper = _load_functions(
path,
{"_is_afk_command"},
{"HNDLR": "."},
)["_is_afk_command"]
assert helper(".afk away")
assert helper("/afk@helper_bot reason")
assert not helper(".afker")
assert not helper("ordinary message")
def test_start_text_fallback_escapes_literal_html_and_owner_info_uses_rich_body():
import html
path = ROOT / "assistant/start.py"
def no_parser(value, *, parse_mode):
assert parse_mode == "mdv3"
return None
helper = _load_functions(
path,
{"_html_body"},
{"coerce_to_rich_markup": no_parser, "html": html},
)["_html_body"]
assert helper("<strong>literal</strong> &") == (
"<strong>literal</strong> &amp;"
)
source = path.read_text(encoding="utf-8")
assert "_rich_menu(\"Owner Information\", msg)" in source
assert "await edit_callback_page(" in source
assert "await send_rich(" in source
def test_manager_help_search_section_lists_both_search_commands():
path = ROOT / "assistant/manager/_help.py"
tree = ast.parse(path.read_text(encoding="utf-8"))
assignments = {
node.targets[0].id: ast.literal_eval(node.value)
for node in tree.body
if isinstance(node, ast.Assign)
and len(node.targets) == 1
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in {"HELP_SECTIONS", "CATEGORY_LABELS"}
}
assert assignments["CATEGORY_LABELS"]["Search"] == "Search"
assert assignments["HELP_SECTIONS"]["Search"] == [
("/search <query>", "Search the web for a query."),
("/deepresearch <query>", "Run a deeper, multi-step research search."),
]
def test_manager_help_page_footer_shows_counts_and_navigation_hint():
import html
path = ROOT / "assistant/manager/_help.py"
seen = []
def rich_page(_title, _rows, **kwargs):
seen.append(kwargs)
return SimpleNamespace(
text=(
f"<p><i>page {kwargs['page']} of {kwargs['pages']} "
f"• total {kwargs['total']}</i></p>"
)
)
namespace = {
"CATEGORY_LABELS": {"Search": "Search"},
"HELP_PAGE_SIZE": 1,
"HELP_SECTIONS": {
"Search": [
("/search <query>", "Search the web."),
("/deepresearch <query>", "Research in depth."),
]
},
"HELP_HOME_DATA": "home",
"cb": lambda *parts: ":".join(map(str, parts)),
"html": html,
"ikm": lambda rows, **_kwargs: rows,
"rich_page": rich_page,
}
helper = _load_functions(path, {"_help_page"}, namespace)["_help_page"]
first_page, _buttons = helper("Search", 0)
assert first_page.text.endswith("<p><i>page 1 of 2 • total 2</i></p>")
last_page, _buttons = helper("Search", 1)
assert last_page.text.endswith("<p><i>page 2 of 2 • total 2</i></p>")
assert seen[0]["total"] == 2 and seen[1]["total"] == 2
|