diff --git a/tests/downloads/test_download_manager_cache_and_validation.py b/tests/downloads/test_download_manager_cache_and_validation.py index 6c7a6ae..e78869d 100644 --- a/tests/downloads/test_download_manager_cache_and_validation.py +++ b/tests/downloads/test_download_manager_cache_and_validation.py @@ -1,6 +1,7 @@ from __future__ import annotations from pathlib import Path +from typing import Any, cast import pytest @@ -17,9 +18,11 @@ def _manager_with_cache_dir(tmp_path: Path) -> DownloadManager: 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) - 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: @@ -35,8 +38,12 @@ def test_get_cached_path_and_remove_cached( ) -> None: """Ensure cache lookup and cache deletion work for valid files.""" manager = _manager_with_cache_dir(tmp_path) - monkeypatch.setattr(manager, "_get_name_from_asin", lambda asin: "My Book") - cached_path = tmp_path / "My Book.aax" + monkeypatch.setattr( + 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) messages: list[str] = [] assert manager.get_cached_path("ASIN123") == cached_path @@ -51,7 +58,34 @@ def test_get_cached_path_ignores_small_files( ) -> None: """Ensure undersized files are not treated as valid cache entries.""" manager = _manager_with_cache_dir(tmp_path) - monkeypatch.setattr(manager, "_get_name_from_asin", lambda asin: "My Book") - cached_path = tmp_path / "My Book.aax" + monkeypatch.setattr( + 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)) 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