109 lines
2.7 KiB
Python
109 lines
2.7 KiB
Python
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from favorite_stickers.bot import (
|
|
UnsupportedMediaError,
|
|
download_with_limit,
|
|
select_media,
|
|
)
|
|
|
|
|
|
def message(**values):
|
|
defaults = {
|
|
"sticker": None,
|
|
"photo": None,
|
|
"video": None,
|
|
"animation": None,
|
|
"video_note": None,
|
|
"document": None,
|
|
}
|
|
defaults.update(values)
|
|
return SimpleNamespace(**defaults)
|
|
|
|
|
|
def test_photo_selects_largest_size_as_static_media() -> None:
|
|
small = SimpleNamespace(file_unique_id="small", file_size=10)
|
|
large = SimpleNamespace(file_unique_id="large", file_size=20)
|
|
|
|
selected = select_media(message(photo=[small, large]))
|
|
|
|
assert selected.downloadable is large
|
|
assert selected.source_unique_id == "large"
|
|
assert selected.kind == "static"
|
|
assert selected.suffix == ".jpg"
|
|
|
|
|
|
def test_video_sticker_preserves_emoji() -> None:
|
|
sticker = SimpleNamespace(
|
|
file_unique_id="sticker",
|
|
file_size=42,
|
|
is_animated=False,
|
|
is_video=True,
|
|
emoji="🔥",
|
|
)
|
|
|
|
selected = select_media(message(sticker=sticker))
|
|
|
|
assert selected.kind == "video"
|
|
assert selected.emoji == "🔥"
|
|
assert selected.suffix == ".webm"
|
|
|
|
|
|
def test_tgs_sticker_reports_clear_limitation() -> None:
|
|
sticker = SimpleNamespace(
|
|
file_unique_id="animated",
|
|
file_size=42,
|
|
is_animated=True,
|
|
is_video=False,
|
|
emoji="🙂",
|
|
)
|
|
|
|
with pytest.raises(UnsupportedMediaError, match="TGS"):
|
|
select_media(message(sticker=sticker))
|
|
|
|
|
|
def test_image_document_lets_ffprobe_detect_animation() -> None:
|
|
document = SimpleNamespace(
|
|
file_unique_id="doc",
|
|
file_size=42,
|
|
mime_type="image/png",
|
|
file_name="picture.PNG",
|
|
)
|
|
|
|
selected = select_media(message(document=document))
|
|
|
|
assert selected.kind == "auto"
|
|
assert selected.suffix == ".png"
|
|
|
|
|
|
def test_gif_document_lets_ffprobe_detect_animation() -> None:
|
|
document = SimpleNamespace(
|
|
file_unique_id="gif",
|
|
file_size=42,
|
|
mime_type="image/gif",
|
|
file_name="animation.gif",
|
|
)
|
|
|
|
selected = select_media(message(document=document))
|
|
|
|
assert selected.kind == "auto"
|
|
|
|
|
|
class FakeDownloadBot:
|
|
async def download(self, downloadable, destination, seek=False):
|
|
destination.write(b"1234")
|
|
destination.flush()
|
|
destination.write(b"5678")
|
|
destination.flush()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_download_enforces_actual_byte_limit(tmp_path) -> None:
|
|
destination = tmp_path / "source.bin"
|
|
|
|
with pytest.raises(UnsupportedMediaError, match="слишком большой"):
|
|
await download_with_limit(FakeDownloadBot(), object(), destination, 6)
|
|
|
|
assert not destination.exists()
|