Refactor code structure for improved readability and maintainability
CI / Quality Checks (push) Failing after 9s
CI / Test Suite (push) Successful in 10s

This commit is contained in:
ВяткинАртём
2026-05-27 17:06:39 +03:00
parent d06bd989f3
commit 59aaa9c4d7
31 changed files with 1416 additions and 543 deletions
+48 -18
View File
@@ -6,26 +6,56 @@ import pytest
from yt_shorts_downloader import api
from yt_shorts_downloader.exceptions import InvalidSessionError, InvalidUrlError
from yt_shorts_downloader.models import SessionValidation
from yt_shorts_downloader.models import DownloadedVideo, SessionValidation
def test_download_returns_path_from_downloader(
def _build_valid_session_validation(path: Path) -> SessionValidation:
return SessionValidation(True, True, True, True, str(path))
def test_download_returns_binary_mp4(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
expected_content = b"mp4-binary-content"
def fake_validate_session_file(path: Path) -> SessionValidation:
return _build_valid_session_validation(path)
def fake_download_video(*, url: str, output_dir: Path, session_path: Path) -> Path:
del url, session_path
downloaded_file = output_dir / "video.mp4"
downloaded_file.write_bytes(expected_content)
return downloaded_file
monkeypatch.setattr(api, "validate_session_file", fake_validate_session_file)
monkeypatch.setattr(api, "download_video", fake_download_video)
downloaded_video = api.download(
"https://youtube.com/shorts/example",
session_path="cookies.txt",
)
assert downloaded_video == DownloadedVideo(
filename="video.mp4",
content=expected_content,
)
def test_download_to_path_returns_path_from_downloader(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
expected_path = tmp_path / "video.mp4"
monkeypatch.setattr(
api,
"validate_session_file",
lambda path: SessionValidation(True, True, True, True, str(path)),
)
monkeypatch.setattr(
api,
"download_video",
lambda *, url, output_dir, session_path: expected_path,
)
def fake_validate_session_file(path: Path) -> SessionValidation:
return _build_valid_session_validation(path)
downloaded_path = api.download(
def fake_download_video(*, url: str, output_dir: Path, session_path: Path) -> Path:
del url, session_path
expected_path.write_bytes(b"mp4")
return output_dir / expected_path.name
monkeypatch.setattr(api, "validate_session_file", fake_validate_session_file)
monkeypatch.setattr(api, "download_video", fake_download_video)
downloaded_path = api.download_to_path(
"https://youtube.com/shorts/example",
session_path="cookies.txt",
output_dir=tmp_path,
@@ -40,11 +70,11 @@ def test_download_rejects_invalid_url() -> None:
def test_download_rejects_invalid_session(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
api,
"validate_session_file",
lambda path: SessionValidation(False, False, False, False, "bad session"),
)
def fake_validate_session_file(path: Path) -> SessionValidation:
del path
return SessionValidation(False, False, False, False, "bad session")
monkeypatch.setattr(api, "validate_session_file", fake_validate_session_file)
with pytest.raises(InvalidSessionError, match="bad session"):
api.download("https://youtube.com/shorts/example", session_path="cookies.txt")
+8 -2
View File
@@ -14,7 +14,12 @@ def test_cli_prints_downloaded_path(
tmp_path: Path,
) -> None:
expected_path = tmp_path / "video.mp4"
monkeypatch.setattr(cli, "download", lambda **kwargs: expected_path)
def fake_download_to_path(**kwargs: object) -> Path:
del kwargs
return expected_path
monkeypatch.setattr(cli, "download_to_path", fake_download_to_path)
exit_code = cli.main(
[
@@ -33,9 +38,10 @@ def test_cli_prints_downloaded_path(
def test_cli_exits_with_error(monkeypatch: pytest.MonkeyPatch) -> None:
def raise_error(**kwargs: object) -> Path:
del kwargs
raise InvalidSessionError("session error")
monkeypatch.setattr(cli, "download", raise_error)
monkeypatch.setattr(cli, "download_to_path", raise_error)
with pytest.raises(SystemExit) as exc_info:
cli.main(["https://youtube.com/shorts/example", "cookies.txt"])
+2 -4
View File
@@ -4,7 +4,7 @@ from pathlib import Path
import pytest
from yt_shorts_downloader import runtime
from yt_shorts_downloader.core import runtime
def test_find_supported_js_runtimes_prefers_deno(
@@ -37,9 +37,7 @@ def test_find_supported_js_runtimes_uses_best_supported_node(
monkeypatch.setattr(
runtime,
"_probe_executable_version",
lambda executable: (22, 9, 0)
if executable.endswith("v22.9.0/bin/node")
else (22, 1, 0),
lambda executable: (22, 9, 0) if executable.endswith("v22.9.0/bin/node") else (22, 1, 0),
)
runtimes = runtime.find_supported_js_runtimes()