test(downloads): validate author_title stem generation and cache fallbacks

This commit is contained in:
2026-02-18 03:39:04 +01:00
parent 597e82dc20
commit 0cf2644f55

View File

@@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
from pathlib import Path from pathlib import Path
from typing import Any, cast
import pytest import pytest
@@ -17,9 +18,11 @@ def _manager_with_cache_dir(tmp_path: Path) -> DownloadManager:
def test_sanitize_filename_replaces_invalid_characters() -> None: def test_sanitize_filename_replaces_invalid_characters() -> None:
"""Ensure filesystem-invalid symbols are replaced with underscores.""" """Ensure filename normalization uses ASCII words and dashes."""
manager = DownloadManager.__new__(DownloadManager) manager = DownloadManager.__new__(DownloadManager)
assert manager._sanitize_filename('a<>:"/\\|?*b') == "a_________b" assert (
manager._sanitize_filename("Stephen King 11/22/63") == "Stephen-King-11-22-63"
)
def test_validate_download_url_accepts_only_http_schemes() -> None: def test_validate_download_url_accepts_only_http_schemes() -> None:
@@ -35,8 +38,12 @@ def test_get_cached_path_and_remove_cached(
) -> None: ) -> None:
"""Ensure cache lookup and cache deletion work for valid files.""" """Ensure cache lookup and cache deletion work for valid files."""
manager = _manager_with_cache_dir(tmp_path) manager = _manager_with_cache_dir(tmp_path)
monkeypatch.setattr(manager, "_get_name_from_asin", lambda asin: "My Book") monkeypatch.setattr(
cached_path = tmp_path / "My Book.aax" manager,
"_get_filename_stems_from_asin",
lambda asin: ["Stephen-King_11-22-63", "11-22-63"],
)
cached_path = tmp_path / "Stephen-King_11-22-63.aax"
cached_path.write_bytes(b"0" * MIN_FILE_SIZE) cached_path.write_bytes(b"0" * MIN_FILE_SIZE)
messages: list[str] = [] messages: list[str] = []
assert manager.get_cached_path("ASIN123") == cached_path assert manager.get_cached_path("ASIN123") == cached_path
@@ -51,7 +58,34 @@ def test_get_cached_path_ignores_small_files(
) -> None: ) -> None:
"""Ensure undersized files are not treated as valid cache entries.""" """Ensure undersized files are not treated as valid cache entries."""
manager = _manager_with_cache_dir(tmp_path) manager = _manager_with_cache_dir(tmp_path)
monkeypatch.setattr(manager, "_get_name_from_asin", lambda asin: "My Book") monkeypatch.setattr(
cached_path = tmp_path / "My Book.aax" manager,
"_get_filename_stems_from_asin",
lambda asin: ["Stephen-King_11-22-63", "11-22-63"],
)
cached_path = tmp_path / "Stephen-King_11-22-63.aax"
cached_path.write_bytes(b"0" * (MIN_FILE_SIZE - 1)) cached_path.write_bytes(b"0" * (MIN_FILE_SIZE - 1))
assert manager.get_cached_path("ASIN123") is None assert manager.get_cached_path("ASIN123") is None
def test_get_filename_stems_include_author_title_and_legacy_title() -> None:
"""Ensure filename candidates include new author_title and legacy title names."""
manager = DownloadManager.__new__(DownloadManager)
manager.client = cast(
Any,
type(
"Client",
(),
{
"get": lambda self, path, **kwargs: {
"product": {
"title": "11/22/63",
"authors": [{"name": "Stephen King"}],
}
}
},
)(),
)
stems = manager._get_filename_stems_from_asin("B00TEST")
assert stems[0] == "Stephen-King_11-22-63"
assert "11-22-63" in stems