Spaces:
Running
Running
File size: 17,276 Bytes
9983566 bae36ef 9983566 0562341 9983566 0562341 9983566 0562341 9983566 0562341 9983566 bae36ef 9983566 bae36ef 9983566 2dcccd0 bae36ef 9983566 2dcccd0 bae36ef 2dcccd0 9983566 b33ab22 9983566 | 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 | """
bot_test_helpers.py — общие фейки и харнесы для test_bot_*.py (вынесено из test_bot.py, P2 аудита). Не содержит тестов.
"""
from types import SimpleNamespace
import asyncio
import bot
import lumen_telegram_transport
class _FakeResolveResponse:
def __init__(self, status: int, url: str):
self.status = status
self.url = url
async def __aenter__(self):
return self
async def __aexit__(self, *args):
return False
class _FakeResolveSession:
"""Мокает session.head()/session.get() для _resolve_tiktok_short."""
def __init__(self, head_response, get_response):
self._head_response = head_response
self._get_response = get_response
self.get_called = False
def head(self, url, *args, **kwargs):
return self._head_response
def get(self, url, *args, **kwargs):
self.get_called = True
return self._get_response
class _FakeChainResolveResponse:
def __init__(self, status: int, url: str = "", headers=None):
self.status = status
self.url = url
self.headers = headers or {}
async def __aenter__(self):
return self
async def __aexit__(self, *args):
return False
class _FakeChainResolveSession:
"""head()/get() по карте URL с записью kwargs — для проверки ручной цепочки редиректов."""
def __init__(self, mapping: dict):
self._mapping = mapping
self.calls = []
def head(self, url, *args, **kwargs):
self.calls.append(("head", url, dict(kwargs)))
return self._mapping[url][0]
def get(self, url, *args, **kwargs):
self.calls.append(("get", url, dict(kwargs)))
return self._mapping[url][1]
class _FakeExc(Exception):
def __init__(self, msg, status_code=None):
super().__init__(msg)
self.status_code = status_code
class _FakeStatusExc(Exception):
def __init__(self, msg, status_code=None):
super().__init__(msg)
self.status_code = status_code
class _FakeAdminRequest:
def __init__(self, headers=None, query_params=None):
self.headers = headers or {}
self.query_params = query_params or {}
class _FakeVoiceBot:
"""Минимальная замена aiogram Bot для inline_tts — нужен только send_voice."""
def __init__(self):
self.sent_voice: dict | None = None
async def send_voice(self, **kwargs):
self.sent_voice = kwargs
return SimpleNamespace()
class _FakeInlineData:
def __init__(self, data, mime_type):
self.data = data
self.mime_type = mime_type
class _FakePart:
def __init__(self, inline_data):
self.inline_data = inline_data
class _FakeTTSContent:
def __init__(self, parts):
self.parts = parts
class _FakeTTSCandidate:
def __init__(self, content):
self.content = content
class _FakeTTSResponse:
def __init__(self, candidates):
self.candidates = candidates
def _fake_tts_response(wav_bytes: bytes) -> "_FakeTTSResponse":
return _FakeTTSResponse(candidates=[
_FakeTTSCandidate(_FakeTTSContent(parts=[_FakePart(_FakeInlineData(wav_bytes, "audio/wav"))]))
])
def _run_core_capturing_prompt(message, monkeypatch):
captured = {}
async def fake_run_route(chat_id, ai_prompt, route, message, **kwargs):
captured["prompt"] = ai_prompt
return "ok", False
monkeypatch.setattr(bot, "_run_route", fake_run_route)
asyncio.run(bot._handle_message_core(message))
return captured.get("prompt", "")
class _FakeCandidate:
def __init__(self, finish_reason="STOP", content=None):
self.finish_reason = finish_reason
self.content = content
class _FakeGeminiResponse:
def __init__(self, text="", candidates=None):
self._text = text
self.candidates = candidates or []
@property
def text(self):
return self._text
class _FakeSentMessage:
def __init__(self):
self.edits = []
self.deleted = False
async def edit_text(self, text, **kwargs):
self.edits.append((text, kwargs.get("parse_mode")))
return self
async def delete(self):
self.deleted = True
class _FakeChat:
def __init__(self, chat_id, chat_type):
self.id = chat_id
self.type = chat_type
class _FakeIncomingMessage:
def __init__(self, chat_id):
self.chat = _FakeChat(chat_id, bot.ChatType.PRIVATE)
self.reply_to_message = None
self.sent: list[_FakeSentMessage] = []
async def reply(self, text, **kwargs):
msg = _FakeSentMessage()
self.sent.append(msg)
return msg
class _FakeAudioBot:
"""Минимальная замена aiogram Bot для _send_tiktok_music — нужен только send_audio."""
def __init__(self):
self.sent_audio: dict | None = None
async def send_audio(self, **kwargs):
self.sent_audio = kwargs
return SimpleNamespace()
def _run_send_tiktok_music(media_data: dict, language_code: str | None = "ru"):
"""Общий harness: мокает скачивание байтов и запись MP3-тегов, возвращает
(title, performer), которые реально дошли бы до _write_mp3_tags/send_audio."""
incoming = _FakeIncomingMessage(999601)
incoming.message_id = 12345
incoming.from_user = SimpleNamespace(language_code=language_code) if language_code is not None else None
captured: dict = {}
async def fake_download(session, url, headers=None):
return b"fake-bytes"
def fake_write_tags(path, title, artist, cover):
captured["title"] = title
captured["artist"] = artist
original_download = bot._download_url_bin
original_write_tags = bot._write_mp3_tags
original_bot = bot.bot
bot._download_url_bin = fake_download
bot._write_mp3_tags = fake_write_tags
bot.bot = _FakeAudioBot()
try:
asyncio.run(bot._send_tiktok_music(None, media_data, incoming, "VideoPosterNickname", {}))
finally:
bot._download_url_bin = original_download
bot._write_mp3_tags = original_write_tags
bot.bot = original_bot
return captured.get("title"), captured.get("artist")
class _FakeAsyncLineIter:
def __init__(self, lines: list[bytes]):
self._lines = list(lines)
def __aiter__(self):
return self
async def __anext__(self):
if not self._lines:
raise StopAsyncIteration
return self._lines.pop(0)
class _FakeSSEResponse:
def __init__(self, lines: list[bytes], status: int = 200):
self.status = status
self._lines = lines
async def __aenter__(self):
return self
async def __aexit__(self, *args):
return False
async def read(self):
return b""
@property
def content(self):
return _FakeAsyncLineIter(self._lines)
class _FakeSessionForSSE:
def __init__(self, resp):
self._resp = resp
def post(self, *args, **kwargs):
return self._resp
class _FakeWebhookRequest:
def __init__(self, headers: dict | None = None, body: dict | None = None):
import json as _json
self.headers = headers or {}
self._body = body or {}
self._raw = _json.dumps(self._body).encode("utf-8")
self.json_calls = 0
self.body_calls = 0
async def json(self):
self.json_calls += 1
return self._body
async def body(self):
self.body_calls += 1
return self._raw
async def _run_webhook_handler(req: "_FakeWebhookRequest") -> dict:
"""asyncio.create_task внутри webhook_handler запускает обработку апдейта не
дожидаясь её завершения — один await asyncio.sleep(0) после вызова даёт
планировщику шанс выполнить уже запланированную задачу (фейковый обработчик
ниже не делает собственных await, поэтому этого достаточно для детерминизма)."""
result = await bot.webhook_handler(req)
await asyncio.sleep(0)
return result
class _FakeOwnerBot:
def __init__(self):
self.sent: list[dict] = []
async def send_message(self, **kwargs):
self.sent.append(kwargs)
return SimpleNamespace()
class _FakeTikTokBot:
def __init__(self):
self.sent_videos: list[dict] = []
async def send_video(self, **kwargs):
self.sent_videos.append(kwargs)
return SimpleNamespace()
async def send_audio(self, **kwargs):
return SimpleNamespace()
class _FakeTikTokResponse:
def __init__(self, status=200, json_body=None):
import json as _json
self.status = status
self._json_body = json_body or {}
# Честный фейк: настоящий aiohttp парсит .json() из тела — контент
# выводим из того же json, чтобы код шёл боевым путём чтения с капом.
self.headers = {}
self.content = _FakeDownloadContent([_json.dumps(self._json_body).encode("utf-8")])
async def __aenter__(self):
return self
async def __aexit__(self, *args):
return False
async def json(self, content_type=None):
return self._json_body
class _FakeTikTokSession:
"""Мокает aiohttp.ClientSession для целого handle_tiktok: session.get() отдаёт
ответ TikWM API. _download_url_bin/_resolve_tiktok_short патчатся отдельно
(они принимают сессию, но не обязаны реально использовать её методы)."""
def __init__(self, tikwm_json: dict):
self._tikwm_json = tikwm_json
def get(self, url, *args, **kwargs):
if "tikwm.com/api" in url:
return _FakeTikTokResponse(200, self._tikwm_json)
return _FakeTikTokResponse(200, {})
class _FakeTikwmApiResponse:
def __init__(self, status=200, json_body=None, body_bytes: bytes | None = None):
import json as _json
self.status = status
self._json_body = json_body or {}
# Явно заданные байты (включая пустые) едут и в read(), и в стрим,
# как у настоящего ответа.
self._body_bytes = body_bytes if body_bytes is not None else _json.dumps(self._json_body).encode("utf-8")
self.headers = {}
self.content = _FakeDownloadContent([self._body_bytes])
async def __aenter__(self):
return self
async def __aexit__(self, *args):
return False
async def json(self, content_type=None):
return self._json_body
async def read(self):
return self._body_bytes
class _FakeTikwmApiSession:
"""session.get(url) — очередь заранее заготовленных ответов (по одному на
каждый вызов, в порядке вызова) плюс список реально запрошенных URL/заголовков —
тесты ниже проверяют и содержимое ответов, и сам порядок/число обращений, и
заголовки, с которыми ушёл каждый запрос."""
def __init__(self, responses: list):
self._responses = list(responses)
self.requested_urls: list[str] = []
self.requested_headers: list[dict] = []
def get(self, url, *args, **kwargs):
self.requested_urls.append(url)
self.requested_headers.append(kwargs.get("headers") or {})
return self._responses.pop(0) if self._responses else _FakeTikwmApiResponse(status=500)
class _FakeDownloadContent:
"""Мокает resp.content.iter_chunked(n) — асинхронный генератор чанков байт,
как у настоящего aiohttp.StreamReader."""
def __init__(self, chunks: list[bytes]):
self._chunks = list(chunks)
def iter_chunked(self, n: int):
chunks = self._chunks
async def _gen():
for c in chunks:
yield c
return _gen()
class _FakeDownloadResponse:
def __init__(self, chunks: list[bytes], *, status: int = 200, content_length: str | None = None):
self.status = status
self.headers = {"Content-Length": content_length} if content_length is not None else {}
self.content = _FakeDownloadContent(chunks)
async def __aenter__(self):
return self
async def __aexit__(self, *args):
return False
class _FakeDownloadSession:
def __init__(self, resp):
self._resp = resp
def get(self, url, *args, **kwargs):
return self._resp
class _FakeProc:
def __init__(self, *, hang: bool = False, fail_communicate: bool = False):
self._hang = hang
self._fail_communicate = fail_communicate
self.killed = False
self.kill_count = 0
self.wait_count = 0
self.returncode = None
async def communicate(self):
if self._fail_communicate:
raise RuntimeError("pipe broken")
while self._hang and not self.killed:
await asyncio.sleep(0.01)
return b"out", b"err"
def kill(self):
self.kill_count += 1
self.killed = True
self.returncode = -9
self._hang = False
async def wait(self):
self.wait_count += 1
return self.returncode
def _run_proxy_middleware(url, *, secret="proxy-secret-abc-0123456789abcdef", bases=("https://proxy.example/fetch/api.telegram.org",), headers=None):
from multidict import CIMultiDict
from yarl import URL
(authenticate,) = lumen_telegram_transport.proxy_auth_middlewares(
proxy_secret=secret, proxy_base_urls=bases,
)
seen = {}
class _FakeRequest:
def __init__(self):
self.url = URL(url)
self.headers = CIMultiDict(headers or {})
async def _handler(request):
seen["sent"] = dict(request.headers)
info_headers = CIMultiDict(request.headers)
class _FakeResponse:
status = 200
def __init__(self):
self.request_info = SimpleNamespace(
url=request.url, method="GET", headers=info_headers, real_url=request.url,
)
self.closed = False
def close(self):
self.closed = True
return _FakeResponse()
request = _FakeRequest()
response = asyncio.run(authenticate(request, _handler))
return request, response, seen
class _FakeQueryMessage:
def __init__(self, chat_id=777, message_id=55):
self.chat = SimpleNamespace(id=chat_id, type=bot.ChatType.PRIVATE)
self.message_id = message_id
self.edits = []
async def edit_text(self, text, **kwargs):
self.edits.append((text, kwargs))
return self
def _make_pick_query(data, user_id=111, msg=None):
q = SimpleNamespace()
q.data = data
q.from_user = SimpleNamespace(id=user_id)
q.message = msg if msg is not None else _FakeQueryMessage()
q.answered = []
async def answer(text=None, show_alert=False):
q.answered.append((text, show_alert))
q.answer = answer
return q
class _FakeRichMessage:
def __init__(self):
self.edits = []
self.deleted = False
self.chat = SimpleNamespace(id=777)
self.message_id = 42
async def edit_text(self, text, **kwargs):
self.edits.append((text, kwargs.get("parse_mode")))
return self
class _FakeRichBot:
"""Бот с рич-методами: фиксирует rich_message.html и возвращает НАСТОЯЩИЙ
aiogram Message (нужен isinstance-гейт в хелперах)."""
def __init__(self):
from aiogram.types import Chat as TgChat
from aiogram.types import Message as TgMessage
self.rich_sent = []
self.rich_edited = []
self._TgMessage = TgMessage
self._chat = TgChat(id=777, type="private")
def _real_message(self, message_id):
import datetime
return self._TgMessage(message_id=message_id, date=datetime.datetime.now(), chat=self._chat)
async def send_rich_message(self, **kwargs):
self.rich_sent.append(kwargs)
return self._real_message(43)
async def edit_message_text(self, **kwargs):
self.rich_edited.append(kwargs)
return self._real_message(kwargs.get("message_id", 42))
class _FakeRichFailingBot:
async def send_rich_message(self, **kwargs):
raise RuntimeError("rich not supported here")
async def edit_message_text(self, **kwargs):
raise RuntimeError("rich not supported here")
def _make_lang_query(data, chat_type=None, user_id=111):
q = _make_pick_query(data, user_id=user_id)
q.message.chat.type = chat_type or bot.ChatType.PRIVATE
return q
|