Files
Downloader-Bot/tests/test_bot_profile_cache.py
Mak5erandClaude Fable 5 caffc343af refactor: apply full-project audit — bug fixes, performance, deduplication
Full-codebase audit (149 raw findings, 57 verified and applied).

Fixes:
- download resume: recompute Range header per retry attempt; never resume
  preallocated multipart .part files (silent media corruption)
- inline sends: canonical error tail (rate-limit/queue-busy/timeout/failure)
  for all platforms; YouTube inline no longer strands tokens in
  'processing' for 6h
- admin mailing: exclude banned users; fix Telegram error-text matching
- middlewares: instantiate once and share across message/callback/inline
  so antiflood/ban state no longer fragments
- event loop: move ZIP compression, JSON state persistence, admin FS
  walks, PIL/ffmpeg/ffprobe work off the loop
- download queue: prune idle per-user scopes; cancelled leader releases
  joined duplicate requests
- worker subprocess: disable log sinks (stderr error protocol and log
  rotation no longer race the parent)
- third-party loggers route through app handlers instead of lastResort
- tiktok: fix mojibake reaction emoji; per-user settings in doc callback;
  per-URL locks for short-URL expansion; parallel photo-set downloads
- ci: fix broken yt-dlp auto-bump workflow baseline
- db: drop redundant analytics_events indexes (migration 000010)

Refactoring:
- services/inline/send_flow.py: shared inline delivery flow for all five
  platforms (~640 duplicated lines removed)
- services/media/audio_flow.py: shared MP3 pipeline (was duplicated 6x
  across youtube/youtube_inline/soundcloud/spotify)
- CobaltMediaService base + shared Cobalt response parsing
- services/platforms/ytdlp_helpers.py: shared yt-dlp scaffolding
- register_inline_send_handlers: one chosen_inline_result/callback
  registration factory for 8 handlers
- merge pinterest/threads single-media and media-group flows
- logging decorator factory; remove ~11 dead repository methods

Tests: 470 -> 518 passing, coverage 77.3% -> 80.9% (CI gate 74%).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-26 16:25:59 +03:00

60 lines
2.0 KiB
Python

import asyncio
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from handlers import bot_profile_cache
@pytest.mark.asyncio
async def test_get_bot_avatar_thumbnail_serializes_concurrent_cold_cache_calls(
monkeypatch, tmp_path
):
monkeypatch.setattr(bot_profile_cache, "_bot_username", "downloader_bot")
monkeypatch.setattr(bot_profile_cache, "_bot_id", 777)
monkeypatch.setattr(bot_profile_cache, "_bot_avatar_path", None)
monkeypatch.setattr(bot_profile_cache, "_bot_avatar_lock", asyncio.Lock())
monkeypatch.setattr(
bot_profile_cache, "_AUDIO_THUMB_PATH", tmp_path / "bot_audio_thumbnail.jpg"
)
photos = SimpleNamespace(
total_count=1,
photos=[
[
SimpleNamespace(
file_id="small", width=160, height=160, file_size=32_000
)
]
],
)
async def fake_download(_file_id, destination):
await asyncio.sleep(0)
destination.write(b"image-bytes")
def fake_normalize(source_path: Path, output_path: Path) -> bool:
output_path.write_bytes(source_path.read_bytes())
return True
bot = SimpleNamespace(
get_user_profile_photos=AsyncMock(return_value=photos),
download=AsyncMock(side_effect=fake_download),
)
monkeypatch.setattr(
bot_profile_cache, "_normalize_audio_thumbnail_file", fake_normalize
)
first, second = await asyncio.gather(
bot_profile_cache.get_bot_avatar_thumbnail(bot),
bot_profile_cache.get_bot_avatar_thumbnail(bot),
)
assert first is not None and second is not None
assert first.path == second.path == str(tmp_path / "bot_audio_thumbnail.jpg")
assert (tmp_path / "bot_audio_thumbnail.jpg").read_bytes() == b"image-bytes"
# Only one cold-cache caller may download; the other must reuse the result.
assert bot.download.await_count == 1
assert bot.get_user_profile_photos.await_count == 1