mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-27 04:01:13 +00:00
Fixes six defects. Each one breaks downloads or deletes user data, and each has a regression test that fails on10710aband on77b80eb(current main). - **Completed-path wait (#1464)**: `_wait_for_completed_path` only returned a directory after two identical signatures of supported formats. - A directory holding only archives (a scene `book.rar` + `.nfo` + `.sfv`) never got a signature and failed with "Completed download has no stable supported files yet". Archives (`.zip`/`.rar`, via `is_archive`) now count toward the signature, so post-processing extracts them again, as in v1.4.0. - With `DOWNLOAD_CLIENT_COMPLETED_PATH_TIMEOUT=0`, `max_attempts` is 1, so every directory failed. One sample with book files or archives is now accepted when no retry is possible. - **Remove & Delete Files (#1401)**: `post_process_cleanup` called `remove(id, delete_files=True)` on torrents matched by `find_existing` that Shelfmark never added, deleting the user's seeding data. `_cleanup_refs` now records `added_by_shelfmark`. A pre-existing torrent (complete, or in progress and joined) is removed but keeps its files. - **Cancelling an unstarted torrent (#1421)**: `_remove_unstarted_torrent` deleted with files whenever `progress == 0`. That included `DownloadStatus.error()`, which always reports 0, and checking states that are re-verifying data already on disk. The torrent must now be QUEUED, PAUSED or DOWNLOADING. rTorrent's `remove(delete_files=True)` always returns False, so a new `DownloadClient.can_delete_files` flag (False on rTorrent) makes it remove without files instead. - **Queue grace overwritten**: when a QUEUED torrent's progress rose on the same poll, the 900 s queue grace was immediately replaced by a shorter stall window, because the orchestrator keeps one deadline. The stall window is now requested only when it is longer than what is left of the queue grace. - **`drop_ignored_files` (#1480)**: the suffix rule `w.endswith("/" + rel)` dropped a kept root `Book.epub` when `Extras/Book.epub` was skipped. Each skipped entry now drops at most one file: the exact path, else the file sharing the most trailing components, else a unique basename, else an AudiobookBay glued `"<folder> <file>"` name (the fallback `match_plan_to_files` uses, matched on folder plus file so chaptered `Book 2 01.mp3` picks the right folder). Ties drop nothing. - **Email and BookLore outputs** ignored `task.ignored_files` and sent every prepared file. Both now apply `drop_ignored_files` once after `prepare_output_files`, and report "No book files found" if the user skipped everything. It is applied in the outputs rather than in `prepare` because the folder output already filters in `transfer_book_files`, and filtering twice could match a kept file on the second pass. - `make python-checks`: ruff, ruff format, basedpyright (backend + tests), vulture all pass. - `uv run pytest tests/ -m "not integration and not e2e"`: 4054 passed on the rebased branch. - New tests run against unfixed77b80eband10710ab: all 18 new regression cases fail, and the 11 existing post-process-cleanup tests also fail there because they now build the 4-tuple cleanup ref.
220 lines
7.7 KiB
Python
220 lines
7.7 KiB
Python
"""Which torrents Shelfmark removes from the client, and when it deletes their files."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from threading import Event
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from shelfmark.core.models import DownloadTask
|
|
from shelfmark.download.clients import DownloadState, DownloadStatus
|
|
from shelfmark.release_sources.prowlarr.handler import ProwlarrHandler
|
|
|
|
|
|
def _status(state: DownloadState, progress: float = 0.0, **kw) -> DownloadStatus:
|
|
return DownloadStatus(
|
|
progress=progress,
|
|
state=state,
|
|
message="Queued" if state == DownloadState.QUEUED else None,
|
|
complete=kw.get("complete", False),
|
|
file_path=None,
|
|
)
|
|
|
|
|
|
class _Recorder:
|
|
def __init__(self) -> None:
|
|
self.events: list[tuple[str, str | None]] = []
|
|
|
|
def status(self, status: str, message: str | None) -> None:
|
|
self.events.append((status, message))
|
|
|
|
|
|
Q, D = DownloadState.QUEUED, DownloadState.DOWNLOADING
|
|
|
|
|
|
# --- removing a torrent that never started -----------------------------------------------
|
|
|
|
|
|
def _cancel(status: DownloadStatus, *, existing: bool, protocol: str = "torrent") -> MagicMock:
|
|
client = MagicMock()
|
|
client.name = "qbittorrent"
|
|
client.find_existing.return_value = ("dl", status) if existing else None
|
|
client.add_download.return_value = "dl"
|
|
client.remove.return_value = True
|
|
cancel = Event()
|
|
|
|
def get_status(_id: str) -> DownloadStatus:
|
|
cancel.set() # the user cancels while the download is being polled
|
|
return status
|
|
|
|
client.get_status.side_effect = get_status
|
|
release = {"protocol": protocol, "magnetUrl": "magnet:?xt=urn:btih:abc123"}
|
|
with (
|
|
patch("shelfmark.release_sources.prowlarr.handler.get_release", return_value=release),
|
|
patch("shelfmark.release_sources.prowlarr.handler.get_client", return_value=client),
|
|
patch("shelfmark.release_sources.prowlarr.handler.POLL_INTERVAL", 0.01),
|
|
):
|
|
ProwlarrHandler().download(
|
|
task=DownloadTask(task_id="c", source="prowlarr", title="Book"),
|
|
cancel_flag=cancel,
|
|
progress_callback=lambda _p: None,
|
|
status_callback=_Recorder().status,
|
|
)
|
|
return client
|
|
|
|
|
|
@pytest.mark.parametrize("state", [DownloadState.QUEUED, DownloadState.DOWNLOADING])
|
|
def test_a_cancelled_torrent_that_downloaded_nothing_is_removed(state: DownloadState) -> None:
|
|
client = _cancel(_status(state, 0.0), existing=False)
|
|
|
|
client.remove.assert_called_once_with("dl", delete_files=True)
|
|
|
|
|
|
def test_a_cancelled_torrent_with_data_is_left_for_seeding() -> None:
|
|
client = _cancel(_status(D, 12.0), existing=False)
|
|
|
|
client.remove.assert_not_called()
|
|
|
|
|
|
def test_a_cancelled_torrent_the_user_already_had_is_never_removed() -> None:
|
|
client = _cancel(_status(Q, 0.0), existing=True)
|
|
|
|
client.remove.assert_not_called()
|
|
|
|
|
|
def test_a_complete_or_seeding_torrent_is_not_removed() -> None:
|
|
client = _cancel(_status(DownloadState.SEEDING, 100.0, complete=True), existing=False)
|
|
|
|
client.remove.assert_not_called()
|
|
|
|
|
|
def test_a_failed_removal_does_not_break_the_cancel() -> None:
|
|
status = _status(Q, 0.0)
|
|
client = MagicMock()
|
|
client.remove.side_effect = OSError("boom")
|
|
client.get_status.return_value = status
|
|
|
|
ProwlarrHandler()._handle_cancelled_download(
|
|
client, "dl", "torrent", lambda *_a: None, remove_if_unstarted=True
|
|
) # does not raise
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"status",
|
|
[
|
|
# A failed lookup reports 0% whatever the torrent holds.
|
|
DownloadStatus.error("qbittorrent get_status failed (ConnectionError): offline"),
|
|
# Re-checking data already on disk starts from 0% too.
|
|
_status(DownloadState.CHECKING, 0.0),
|
|
_status(DownloadState.UNKNOWN, 0.0),
|
|
],
|
|
)
|
|
def test_a_torrent_at_zero_is_not_removed_unless_it_really_has_not_started(
|
|
status: DownloadStatus,
|
|
) -> None:
|
|
client = MagicMock()
|
|
client.name = "qbittorrent"
|
|
client.get_status.return_value = status # what the client reports once the user cancels
|
|
|
|
ProwlarrHandler()._handle_cancelled_download(
|
|
client, "dl", "torrent", lambda *_a: None, remove_if_unstarted=True
|
|
)
|
|
|
|
client.remove.assert_not_called()
|
|
|
|
|
|
def test_a_paused_torrent_that_downloaded_nothing_is_removed() -> None:
|
|
client = _cancel(_status(DownloadState.PAUSED, 0.0), existing=False)
|
|
|
|
client.remove.assert_called_once_with("dl", delete_files=True)
|
|
|
|
|
|
def test_an_unstarted_torrent_is_removed_without_files_where_the_client_cannot_delete_them() -> (
|
|
None
|
|
):
|
|
from shelfmark.download.clients.rtorrent import RTorrentClient
|
|
|
|
client = MagicMock()
|
|
client.name = "rtorrent"
|
|
client.can_delete_files = RTorrentClient.can_delete_files
|
|
client.get_status.return_value = _status(Q, 0.0)
|
|
client.remove.return_value = True
|
|
|
|
removed = ProwlarrHandler()._remove_unstarted_torrent(client, "dl")
|
|
|
|
assert removed is True
|
|
client.remove.assert_called_once_with("dl", delete_files=False)
|
|
|
|
|
|
# --- Remove & Delete Files only deletes what this task downloaded ------------------------
|
|
|
|
|
|
def _import_then_clean_up(existing: DownloadStatus | None, torrent_action: str) -> MagicMock:
|
|
"""Download through the handler, import successfully, then run the completion action."""
|
|
client = MagicMock()
|
|
client.name = "qbittorrent"
|
|
client.find_existing.return_value = ("dl", existing) if existing else None
|
|
client.add_download.return_value = "dl"
|
|
client.get_status.return_value = _status(DownloadState.COMPLETE, 100.0, complete=True)
|
|
client.remove.return_value = True
|
|
handler = ProwlarrHandler()
|
|
task = DownloadTask(task_id="r", source="prowlarr", title="Book")
|
|
release = {"protocol": "torrent", "magnetUrl": "magnet:?xt=urn:btih:abc123"}
|
|
with (
|
|
patch("shelfmark.release_sources.prowlarr.handler.get_release", return_value=release),
|
|
patch("shelfmark.release_sources.prowlarr.handler.get_client", return_value=client),
|
|
patch("shelfmark.release_sources.prowlarr.handler.remove_release"),
|
|
patch("shelfmark.release_sources.prowlarr.handler.POLL_INTERVAL", 0.01),
|
|
patch.object(
|
|
ProwlarrHandler,
|
|
"_wait_for_completed_path",
|
|
return_value=(Path("/downloads/Book"), None),
|
|
),
|
|
patch(
|
|
"shelfmark.download.clients.base_handler.config.get",
|
|
side_effect=lambda key, default=None, **_kw: (
|
|
torrent_action if key == "PROWLARR_TORRENT_ACTION" else default
|
|
),
|
|
),
|
|
):
|
|
result = handler.download(
|
|
task=task,
|
|
cancel_flag=Event(),
|
|
progress_callback=lambda _p: None,
|
|
status_callback=_Recorder().status,
|
|
)
|
|
assert result == str(Path("/downloads/Book"))
|
|
handler.post_process_cleanup(task, success=True)
|
|
return client
|
|
|
|
|
|
def test_remove_and_delete_deletes_the_files_of_a_torrent_this_task_added() -> None:
|
|
client = _import_then_clean_up(None, "remove_and_delete")
|
|
|
|
client.add_download.assert_called_once()
|
|
client.remove.assert_called_once_with("dl", delete_files=True)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"existing",
|
|
[
|
|
_status(DownloadState.SEEDING, 100.0, complete=True), # the user's own finished seed
|
|
_status(D, 40.0), # an in-progress torrent the task joined
|
|
],
|
|
)
|
|
def test_remove_and_delete_keeps_the_files_of_a_torrent_the_client_already_had(
|
|
existing: DownloadStatus,
|
|
) -> None:
|
|
client = _import_then_clean_up(existing, "remove_and_delete")
|
|
|
|
client.add_download.assert_not_called()
|
|
client.remove.assert_called_once_with("dl", delete_files=False)
|
|
|
|
|
|
def test_remove_still_removes_a_torrent_the_client_already_had() -> None:
|
|
client = _import_then_clean_up(_status(DownloadState.SEEDING, 100.0, complete=True), "remove")
|
|
|
|
client.remove.assert_called_once_with("dl", delete_files=False)
|