Files
Downloader-Bot/tests/test_download_worker_cli.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

149 lines
4.6 KiB
Python

import io
import json
from types import SimpleNamespace
import pytest
from services import download_worker_cli
from utils.download_manager import DownloadMetrics
def test_read_payload_raises_when_stdin_is_empty(monkeypatch):
monkeypatch.setattr(download_worker_cli.sys, "stdin", SimpleNamespace(buffer=io.BytesIO(b"")))
with pytest.raises(ValueError, match="No payload provided"):
download_worker_cli._read_payload()
def test_build_config_converts_stream_timeout_list_to_tuple():
config = download_worker_cli._build_config(
{
"chunk_size": 123,
"stream_timeout": [1.5, 8.0],
}
)
assert config.chunk_size == 123
assert config.stream_timeout == (1.5, 8.0)
def test_main_downloads_payload_and_writes_json(monkeypatch):
payload = {
"output_dir": "downloads",
"url": "https://example.com/video",
"filename": "video.mp4",
"headers": {"User-Agent": "pytest"},
"skip_if_exists": True,
"max_size_bytes": 4096,
"source": "pytest-worker",
"config": {"stream_timeout": [3.0, 12.0]},
}
stdout = io.StringIO()
stderr = io.StringIO()
captured = {}
class FakeDownloader:
def __init__(self, *, output_dir, config, source):
captured["output_dir"] = output_dir
captured["config"] = config
captured["source"] = source
def _download_sync(
self,
url,
filename,
headers,
skip_if_exists,
_progress_callback,
max_size_bytes,
):
captured["call"] = {
"url": url,
"filename": filename,
"headers": headers,
"skip_if_exists": skip_if_exists,
"max_size_bytes": max_size_bytes,
}
return DownloadMetrics(
url=url,
path=f"downloads/{filename}",
size=2048,
elapsed=1.25,
used_multipart=False,
resumed=True,
)
monkeypatch.setattr(
download_worker_cli.sys,
"stdin",
SimpleNamespace(buffer=io.BytesIO(json.dumps(payload).encode("utf-8"))),
)
monkeypatch.setattr(download_worker_cli.sys, "stdout", stdout)
monkeypatch.setattr(download_worker_cli.sys, "stderr", stderr)
monkeypatch.setattr(download_worker_cli, "ResilientDownloader", FakeDownloader)
monkeypatch.setattr(download_worker_cli, "disable_log_sinks", lambda: None)
assert download_worker_cli.main() == 0
assert stderr.getvalue() == ""
assert captured["output_dir"] == "downloads"
assert captured["source"] == "pytest-worker"
assert captured["config"].stream_timeout == (3.0, 12.0)
assert captured["call"]["skip_if_exists"] is True
assert captured["call"]["max_size_bytes"] == 4096
assert json.loads(stdout.getvalue()) == {
"url": "https://example.com/video",
"path": "downloads/video.mp4",
"size": 2048,
"elapsed": 1.25,
"used_multipart": False,
"resumed": True,
}
def test_main_writes_error_to_stderr(monkeypatch):
payload = {
"output_dir": "downloads",
"url": "https://example.com/video",
"filename": "video.mp4",
"config": {},
}
stdout = io.StringIO()
stderr = io.StringIO()
class BrokenDownloader:
def __init__(self, *, output_dir, config, source):
pass
def _download_sync(self, *args, **kwargs):
raise RuntimeError("boom")
monkeypatch.setattr(
download_worker_cli.sys,
"stdin",
SimpleNamespace(buffer=io.BytesIO(json.dumps(payload).encode("utf-8"))),
)
monkeypatch.setattr(download_worker_cli.sys, "stdout", stdout)
monkeypatch.setattr(download_worker_cli.sys, "stderr", stderr)
monkeypatch.setattr(download_worker_cli, "ResilientDownloader", BrokenDownloader)
monkeypatch.setattr(download_worker_cli, "disable_log_sinks", lambda: None)
assert download_worker_cli.main() == 1
assert stdout.getvalue() == ""
assert "boom" in stderr.getvalue()
def test_main_disables_log_sinks_before_running(monkeypatch):
calls: list[bool] = []
monkeypatch.setattr(download_worker_cli, "disable_log_sinks", lambda: calls.append(True))
monkeypatch.setattr(
download_worker_cli.sys,
"stdin",
SimpleNamespace(buffer=io.BytesIO(b"")),
)
stderr = io.StringIO()
monkeypatch.setattr(download_worker_cli.sys, "stderr", stderr)
assert download_worker_cli.main() == 1
assert calls == [True]
assert "No payload provided" in stderr.getvalue()