f503eb5de3
media_names_for_post derives <post_id>_<kind><index>.<ext> (img/vid/gif, per-kind counters) from media_urls; download_media(thread, output_dir) -> Result[list[Path]] downloads via stdlib urlopen, idempotent skip when target exists with size>0, URLError -> Result.err(HttpError). Authorized HTTP-boundary mock tests. TDD: red(no module) -> green(6 media + 16 regression pass).
82 lines
3.5 KiB
Python
82 lines
3.5 KiB
Python
"""Tests for scripts.twitter_threads.download_media: naming and HTTP download contract."""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from unittest.mock import patch, MagicMock
|
|
from urllib.error import URLError
|
|
|
|
import pytest
|
|
|
|
from scripts.twitter_threads.error_types import PostMetrics, PostData, ThreadData
|
|
from scripts.twitter_threads.download_media import media_names_for_post, download_media
|
|
|
|
|
|
class _FakeResp:
|
|
def __init__(self, data: bytes) -> None:
|
|
self._data = data
|
|
def read(self) -> bytes:
|
|
return self._data
|
|
def __enter__(self) -> "_FakeResp":
|
|
return self
|
|
def __exit__(self, *a: object) -> bool:
|
|
return False
|
|
|
|
|
|
def _post(post_id: str, media_urls: tuple[str, ...]) -> PostData:
|
|
metrics = PostMetrics(reply_count=0, repost_count=0, like_count=0, view_count=None)
|
|
return PostData(post_id=post_id, author="Tim", handle="NOTimothyLottes", text="t", timestamp="2024-02-01T00:00:00Z", media_urls=media_urls, reply_to_id=None, quote_of_id=None, metrics=metrics)
|
|
|
|
|
|
def test_media_names_for_post() -> None:
|
|
post = _post("123", ("https://pbs.twimg.com/media/A?format=jpg&name=large", "https://pbs.twimg.com/media/B?format=png&name=large"))
|
|
assert media_names_for_post(post) == ["123_img1.jpg", "123_img2.png"]
|
|
|
|
|
|
def test_media_names_video() -> None:
|
|
post = _post("123", ("https://video.twimg.com/ext_tw_video/1/pu/vid/1280x720/1234567890.mp4",))
|
|
assert media_names_for_post(post) == ["123_vid1.mp4"]
|
|
|
|
|
|
def test_download_naming(tmp_path: Path) -> None:
|
|
post = _post("123", ("https://pbs.twimg.com/media/A?format=jpg&name=large", "https://pbs.twimg.com/media/B?format=png&name=large"))
|
|
thread = ThreadData(root_post_id="123", posts=(post,), source_url="https://x.com/x/x/123")
|
|
with patch("scripts.twitter_threads.download_media.urlopen", return_value=_FakeResp(b"bytes")):
|
|
result = download_media(thread, tmp_path)
|
|
assert result.is_ok
|
|
assert (tmp_path / "123_img1.jpg").exists()
|
|
assert (tmp_path / "123_img2.png").exists()
|
|
assert result.data is not None
|
|
names = [p.name for p in result.data]
|
|
assert "123_img1.jpg" in names
|
|
assert "123_img2.png" in names
|
|
|
|
|
|
def test_download_video_naming(tmp_path: Path) -> None:
|
|
post = _post("123", ("https://video.twimg.com/ext_tw_video/1/pu/vid/1280x720/1234567890.mp4",))
|
|
thread = ThreadData(root_post_id="123", posts=(post,), source_url="https://x.com/x/x/123")
|
|
with patch("scripts.twitter_threads.download_media.urlopen", return_value=_FakeResp(b"vid")):
|
|
result = download_media(thread, tmp_path)
|
|
assert (tmp_path / "123_vid1.mp4").exists()
|
|
|
|
|
|
def test_download_idempotent(tmp_path: Path) -> None:
|
|
post = _post("123", ("https://pbs.twimg.com/media/A?format=jpg&name=large",))
|
|
thread = ThreadData(root_post_id="123", posts=(post,), source_url="https://x.com/x/x/123")
|
|
name = media_names_for_post(post)[0]
|
|
(tmp_path / name).write_bytes(b"already")
|
|
mock_urlopen = MagicMock()
|
|
with patch("scripts.twitter_threads.download_media.urlopen", mock_urlopen):
|
|
result = download_media(thread, tmp_path)
|
|
assert result.is_ok
|
|
mock_urlopen.assert_not_called()
|
|
assert (tmp_path / name).read_bytes() == b"already"
|
|
|
|
|
|
def test_download_http_error(tmp_path: Path) -> None:
|
|
post = _post("123", ("https://pbs.twimg.com/media/A?format=jpg&name=large",))
|
|
thread = ThreadData(root_post_id="123", posts=(post,), source_url="https://x.com/x/x/123")
|
|
with patch("scripts.twitter_threads.download_media.urlopen", side_effect=URLError("boom")):
|
|
result = download_media(thread, tmp_path)
|
|
assert result.is_ok is False
|
|
assert result.error is not None
|
|
assert result.error.kind == "HttpError" |