| 1 | import pytest |
| 2 | |
| 3 | from auth import CookieManager |
| 4 | from config import ConfigLoader |
| 5 | from control import QueueManager, RateLimiter, RetryHandler |
| 6 | from core.mix_downloader import MixDownloader |
| 7 | from storage import FileManager |
| 8 | |
| 9 | |
| 10 | class _FakeAPIClient: |
| 11 | async def get_mix_aweme(self, _mix_id: str, cursor: int = 0, count: int = 20): |
| 12 | if cursor > 0: |
| 13 | return {"items": [], "has_more": False, "max_cursor": cursor, "status_code": 0} |
| 14 | return { |
| 15 | "items": [ |
| 16 | { |
| 17 | "aweme_id": "7600224486650121888", |
| 18 | "desc": "mix-item", |
| 19 | "author": {"nickname": "mix-author"}, |
| 20 | "video": {"play_addr": {"url_list": ["https://example.com/video.mp4"]}}, |
| 21 | } |
| 22 | ], |
| 23 | "has_more": False, |
| 24 | "max_cursor": 0, |
| 25 | "status_code": 0, |
| 26 | } |
| 27 | |
| 28 | async def get_mix_detail(self, _mix_id: str): |
| 29 | return {"author": {"nickname": "mix-author"}} |
| 30 | |
| 31 | |
| 32 | @pytest.mark.asyncio |
| 33 | async def test_mix_downloader_downloads_mix_items(tmp_path, monkeypatch): |
| 34 | config = ConfigLoader() |
| 35 | config.update(path=str(tmp_path), number={"mix": 0}) |
| 36 | file_manager = FileManager(str(tmp_path)) |
| 37 | downloader = MixDownloader( |
| 38 | config=config, |
| 39 | api_client=_FakeAPIClient(), |
| 40 | file_manager=file_manager, |
| 41 | cookie_manager=CookieManager(str(tmp_path / ".cookies.json")), |
| 42 | database=None, |
| 43 | rate_limiter=RateLimiter(max_per_second=10), |
| 44 | retry_handler=RetryHandler(max_retries=1), |
| 45 | queue_manager=QueueManager(max_workers=1), |
| 46 | ) |
| 47 | |
| 48 | async def _always_true(*_args, **_kwargs): |
| 49 | return True |
| 50 | |
| 51 | monkeypatch.setattr(downloader, "_should_download", _always_true) |
| 52 | monkeypatch.setattr(downloader, "_download_aweme_assets", _always_true) |
| 53 | |
| 54 | result = await downloader.download({"mix_id": "123"}) |
| 55 | |
| 56 | assert result.total == 1 |
| 57 | assert result.success == 1 |
| 58 | assert result.failed == 0 |
| 59 | |
| 60 | |
| 61 | @pytest.mark.asyncio |
| 62 | async def test_mix_downloader_does_not_apply_redundant_limit_count(tmp_path, monkeypatch): |
| 63 | config = ConfigLoader() |
| 64 | config.update(path=str(tmp_path), number={"mix": 0}) |
| 65 | file_manager = FileManager(str(tmp_path)) |
| 66 | downloader = MixDownloader( |
| 67 | config=config, |
| 68 | api_client=_FakeAPIClient(), |
| 69 | file_manager=file_manager, |
| 70 | cookie_manager=CookieManager(str(tmp_path / ".cookies.json")), |
| 71 | database=None, |
| 72 | rate_limiter=RateLimiter(max_per_second=10), |
| 73 | retry_handler=RetryHandler(max_retries=1), |
| 74 | queue_manager=QueueManager(max_workers=1), |
| 75 | ) |
| 76 | |
| 77 | async def _always_true(*_args, **_kwargs): |
| 78 | return True |
| 79 | |
| 80 | call_count = {"limit": 0} |
| 81 | |
| 82 | def _track_limit(items, _mode): |
| 83 | call_count["limit"] += 1 |
| 84 | return items |
| 85 | |
| 86 | monkeypatch.setattr(downloader, "_should_download", _always_true) |
| 87 | monkeypatch.setattr(downloader, "_download_aweme_assets", _always_true) |
| 88 | monkeypatch.setattr(downloader, "_limit_count", _track_limit) |
| 89 | |
| 90 | result = await downloader.download({"mix_id": "123"}) |
| 91 | |
| 92 | assert result.total == 1 |
| 93 | assert call_count["limit"] == 0 |
| 94 |