Files
shelfmark/tests/prowlarr/test_failure_scenarios.py
T
Aidan AbbottandAidan Abbott f8d3f990ca fix: Prowlarr expired download link refresh (#1109)
## Summary

Fixes Prowlarr downloads that fail after a queued torrent result’s
tracker download link expires.

Prowlarr torrent results can expose a `downloadUrl` that is only a
short-lived proxy to the upstream tracker. Some trackers, including MAM,
include expiring credentials in that URL. Shelfmark was persisting that
URL as retry data and later treating it as durable. If the in-memory
Prowlarr cache was gone, or if qBittorrent tried to add a stale URL,
Shelfmark could fail with a misleading torrent-hash error instead of
refreshing the release.

## What changed

- Stop persisting Prowlarr `downloadUrl` values as durable retry data.
- Persist only source context needed to refresh the release later.
- On a Prowlarr cache miss, re-query Prowlarr using the queued task
context.
- Accept refreshed results only when the stable identity matches the
original release:
  - `guid == task.task_id`
  - or `infoUrl == task.task_id`
- Cache the fresh raw Prowlarr result and build the download request
from its current `downloadUrl` / `magnetUrl`.
- If qBittorrent add fails with `Could not determine torrent hash from
URL`, remove the stale cached Prowlarr result, refresh once, and retry
with the fresh URL.
- Preserve existing magnet handling: torrent results continue to use
`magnetUrl` first, so the refresh path only targets Prowlarr proxy
`downloadUrl` values that can expire.
- Improve the user-facing failure when refresh cannot find the same
release:
`The indexer download link expired and the release could not be
refreshed. Search again for a fresh result.`

## Why this approach

The important constraint is avoiding accidental downloads of a different
edition or format after the original tracker link expires. Re-running a
search by title can return many plausible results, so the refresh path
deliberately requires an exact stable identity match before using any
new URL.

This treats Prowlarr `downloadUrl` as a short-lived hint, while still
allowing retries to recover when Prowlarr can find the same release
again. The one-shot retry after qBittorrent add failure handles the case
where Shelfmark still has a cached Prowlarr result, but that cached
result contains an expired proxy URL.

The refresh hook is source-specific and defaults to no-op for other
external download handlers, so Newznab and other sources keep their
existing retry behavior.

## Bug

Fixes #1012

Context:
https://github.com/calibrain/shelfmark/issues/1012#issuecomment-4917148398

## Validation

- `uv run pytest tests/prowlarr -q`
- `uv run pytest tests/download/test_orchestrator_user_output_mode.py
-q`
- `uv run pytest tests/newznab/test_handler.py
tests/audiobookbay/test_handler.py -q`
- `uv run ruff check shelfmark/core/models.py
shelfmark/download/orchestrator.py
shelfmark/download/clients/base_handler.py
shelfmark/release_sources/prowlarr/handler.py
tests/prowlarr/test_handler.py
tests/prowlarr/test_integration_handler.py
tests/prowlarr/test_failure_scenarios.py tests/prowlarr/test_source.py
tests/download/test_orchestrator_user_output_mode.py`
- after resolving conflicts with latest `main`: `uv run pytest
tests/prowlarr -q`
- after resolving conflicts with latest `main`: `uv run ruff check
shelfmark/release_sources/prowlarr/handler.py`
- after resolving conflicts with latest `main`: `uv run pytest
tests/prowlarr/test_handler.py tests/prowlarr/test_source.py -q`

Co-authored-by: Aidan Abbott <aidanabbott@Aidans-MacBook-Pro.local>
2026-07-14 14:22:29 -04:00

996 lines
33 KiB
Python

"""
Failure scenario tests for Prowlarr handler and download clients.
These tests verify error handling behavior - what happens when things go wrong.
They use real clients where possible, with injected failures for edge cases.
Run with: uv run pytest tests/prowlarr/test_failure_scenarios.py -v
"""
import tempfile
import time
from pathlib import Path
from threading import Event, Thread
from unittest.mock import patch
import pytest
from shelfmark.core.models import DownloadTask
from shelfmark.download.clients import (
DownloadClient,
DownloadState,
DownloadStatus,
)
from shelfmark.release_sources.prowlarr.handler import ProwlarrHandler
# =============================================================================
# Test Fixtures and Helpers
# =============================================================================
class ProgressRecorder:
"""Records progress and status updates during download."""
def __init__(self):
self.progress_values: list[float] = []
self.status_updates: list[tuple[str, str | None]] = []
def progress_callback(self, progress: float):
self.progress_values.append(progress)
def status_callback(self, status: str, message: str | None):
self.status_updates.append((status, message))
@property
def last_status(self) -> str | None:
return self.status_updates[-1][0] if self.status_updates else None
@property
def last_message(self) -> str | None:
return self.status_updates[-1][1] if self.status_updates else None
@property
def statuses(self) -> list[str]:
return [s[0] for s in self.status_updates]
@property
def had_error(self) -> bool:
return "error" in self.statuses
class MockClient(DownloadClient):
"""Configurable mock client for testing failure scenarios."""
protocol = "torrent"
name = "mock"
def __init__(self):
self.downloads = {}
self.status_sequence = [] # List of DownloadStatus to return in order
self.status_index = 0
self.add_download_error = None # Exception to raise on add_download
self.get_status_error = None # Exception to raise on get_status
self.remove_called = False
self.remove_with_delete = False
@staticmethod
def is_configured() -> bool:
return True
def test_connection(self) -> tuple[bool, str]:
return True, "Mock client connected"
def add_download(
self,
url: str,
name: str,
category: str | None = None,
expected_hash: str | None = None,
**kwargs,
) -> str:
if self.add_download_error:
raise self.add_download_error
download_id = f"mock-{len(self.downloads)}"
self.downloads[download_id] = {"url": url, "name": name}
return download_id
def get_status(self, download_id: str) -> DownloadStatus:
if self.get_status_error:
raise self.get_status_error
if self.status_sequence:
if self.status_index < len(self.status_sequence):
status = self.status_sequence[self.status_index]
self.status_index += 1
return status
# Return last status if we've exhausted the sequence
return self.status_sequence[-1]
# Default: return downloading status
return DownloadStatus(
progress=50,
state=DownloadState.DOWNLOADING,
message=None,
complete=False,
file_path=None,
)
def remove(self, download_id: str, delete_files: bool = False) -> bool:
self.remove_called = True
self.remove_with_delete = delete_files
return True
def get_download_path(self, download_id: str) -> str | None:
return "/downloads/test-file.epub"
def find_existing(
self, url: str, category: str | None = None
) -> tuple[str, DownloadStatus] | None:
return None
@pytest.fixture
def handler():
return ProwlarrHandler()
@pytest.fixture
def mock_client():
return MockClient()
@pytest.fixture
def recorder():
return ProgressRecorder()
@pytest.fixture
def cancel_flag():
return Event()
@pytest.fixture
def sample_task():
return DownloadTask(
task_id="test-task-123",
source="prowlarr",
title="Test Book",
)
@pytest.fixture
def sample_release():
return {
"guid": "test-task-123",
"title": "Test Book",
"downloadUrl": "magnet:?xt=urn:btih:abc123",
"protocol": "torrent",
"indexer": "TestIndexer",
}
# =============================================================================
# Error State Tests - Client Reports Error
# =============================================================================
class TestClientErrorStates:
"""Tests for when download clients report error states."""
def test_client_returns_error_state_during_download(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Handler should abort when client reports error state."""
# Simulate: downloading -> downloading -> error
mock_client.status_sequence = [
DownloadStatus(
progress=10,
state=DownloadState.DOWNLOADING,
message=None,
complete=False,
file_path=None,
),
DownloadStatus(
progress=25,
state=DownloadState.DOWNLOADING,
message=None,
complete=False,
file_path=None,
),
DownloadStatus(
progress=0,
state=DownloadState.ERROR,
message="Tracker returned error: torrent not found",
complete=False,
file_path=None,
),
]
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert "Tracker returned error" in recorder.last_message
assert not mock_client.remove_called
def test_client_returns_error_with_complete_flag(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Edge case: complete=True but state=ERROR should be treated as error."""
mock_client.status_sequence = [
DownloadStatus(
progress=100,
state=DownloadState.ERROR,
message="Download corrupted",
complete=True,
file_path=None,
),
]
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert recorder.last_message == "Download corrupted"
assert not mock_client.remove_called
def test_error_without_message_uses_default(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Error state without message should use default error text."""
mock_client.status_sequence = [
DownloadStatus(
progress=0,
state=DownloadState.ERROR,
message=None,
complete=False,
file_path=None,
),
]
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert recorder.last_message == "Download failed"
assert not mock_client.remove_called
# =============================================================================
# Connection/Network Failure Tests
# =============================================================================
class TestConnectionFailures:
"""Tests for network and connection failures."""
def test_add_download_fails(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Handler should report error when add_download throws."""
mock_client.add_download_error = ConnectionError("Connection refused")
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert "Connection refused" in recorder.last_message
def test_get_status_fails_during_poll(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Handler should recover or error gracefully when get_status throws."""
call_count = 0
def failing_get_status(download_id):
nonlocal call_count
call_count += 1
if call_count <= 2:
return DownloadStatus(
progress=call_count * 10,
state=DownloadState.DOWNLOADING,
message=None,
complete=False,
file_path=None,
)
raise ConnectionError("Client went away")
mock_client.get_status = failing_get_status
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert "Client went away" in recorder.last_message
def test_no_client_configured(
self, handler, recorder, cancel_flag, sample_task, sample_release
):
"""Handler should report helpful error when no client is configured."""
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=None,
),
patch(
"shelfmark.release_sources.prowlarr.handler.list_configured_clients",
return_value=[],
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert "No download clients configured" in recorder.last_message
# =============================================================================
# Cancellation Tests
# =============================================================================
class TestCancellation:
"""Tests for download cancellation behavior."""
def test_cancel_during_download(
self, handler, mock_client, recorder, sample_task, sample_release
):
"""Cancellation should stop download and cleanup."""
cancel_flag = Event()
# Status sequence that keeps downloading
mock_client.status_sequence = [
DownloadStatus(
progress=i * 10,
state=DownloadState.DOWNLOADING,
message=None,
complete=False,
file_path=None,
)
for i in range(20)
]
def cancel_after_delay():
time.sleep(0.3)
cancel_flag.set()
cancel_thread = Thread(target=cancel_after_delay)
cancel_thread.start()
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
patch(
"shelfmark.release_sources.prowlarr.handler.POLL_INTERVAL",
0.1,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
cancel_thread.join()
assert result is None
assert "cancelled" in recorder.statuses
assert not mock_client.remove_called
def test_cancel_before_download_starts(
self, handler, mock_client, recorder, sample_task, sample_release
):
"""Pre-set cancel flag should abort immediately."""
cancel_flag = Event()
cancel_flag.set() # Already cancelled
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
# Should have been cancelled quickly
assert not mock_client.remove_called
# =============================================================================
# Cache/Release Not Found Tests
# =============================================================================
class TestCacheFailures:
"""Tests for release cache failures."""
def test_release_not_in_cache(self, handler, recorder, cancel_flag, sample_task):
"""Handler should error when release is not found in cache."""
with patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=None,
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert "could not be refreshed" in recorder.last_message
def test_release_missing_download_url(self, handler, recorder, cancel_flag, sample_task):
"""Handler should error when release has no download URL."""
release_no_url = {
"guid": "test-task-123",
"title": "Test Book",
# No downloadUrl or magnetUrl
"protocol": "torrent",
}
with patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=release_no_url,
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert "No download URL" in recorder.last_message
def test_unknown_protocol(self, handler, recorder, cancel_flag, sample_task):
"""Handler should error on unknown protocol."""
release_unknown_protocol = {
"guid": "test-task-123",
"title": "Test Book",
"downloadUrl": "ftp://example.com/book.epub",
"protocol": "ftp",
}
with patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=release_unknown_protocol,
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert "protocol" in recorder.last_message.lower()
# =============================================================================
# File Handling Failures
# =============================================================================
class TestFileHandlingFailures:
"""Tests for file staging/copying failures."""
def test_download_path_not_found(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Handler should error when completed file path is not available."""
mock_client.status_sequence = [
DownloadStatus(
progress=100,
state=DownloadState.COMPLETE,
message=None,
complete=True,
file_path=None,
),
]
mock_client.get_download_path = lambda x: None # No path available
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert "locate" in recorder.last_message.lower()
def test_usenet_returns_original_path(
self, handler, mock_client, recorder, cancel_flag, sample_task
):
"""Usenet downloads return the original client path without staging."""
usenet_release = {
"guid": "test-task-123",
"title": "Test Book",
"downloadUrl": "https://indexer.example.com/download/123",
"protocol": "usenet",
"indexer": "TestIndexer",
}
mock_client.status_sequence = [
DownloadStatus(
progress=100,
state=DownloadState.COMPLETE,
message=None,
complete=True,
file_path="/downloads/test.epub",
),
]
with tempfile.TemporaryDirectory() as tmpdir:
source_file = Path(tmpdir) / "source.epub"
source_file.write_text("test content")
mock_client.get_download_path = lambda x: str(source_file)
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=usenet_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
patch(
"shelfmark.release_sources.prowlarr.handler.remove_release",
),
patch(
"shelfmark.download.staging.get_staging_dir",
) as mock_get_staging,
patch(
"shelfmark.release_sources.prowlarr.handler.POLL_INTERVAL",
0.01,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result == str(source_file)
assert not recorder.had_error
mock_get_staging.assert_not_called()
# =============================================================================
# Progress Callback Tests
# =============================================================================
class TestProgressCallbacks:
"""Tests for progress reporting behavior."""
def test_progress_values_are_in_order(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Progress values should generally increase (allowing for client quirks)."""
mock_client.status_sequence = [
DownloadStatus(
progress=i,
state=DownloadState.DOWNLOADING if i < 100 else DownloadState.COMPLETE,
message=None,
complete=i >= 100,
file_path="/downloads/test.epub" if i >= 100 else None,
)
for i in [0, 10, 25, 50, 75, 90, 100]
]
with tempfile.TemporaryDirectory() as tmpdir:
source_file = Path(tmpdir) / "test.epub"
source_file.write_text("test content")
staging_dir = Path(tmpdir) / "staging"
staging_dir.mkdir()
mock_client.get_download_path = lambda x: str(source_file)
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
patch(
"shelfmark.download.staging.get_staging_dir",
return_value=staging_dir,
),
patch(
"shelfmark.release_sources.prowlarr.handler.POLL_INTERVAL",
0.01,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is not None
assert recorder.progress_values == [0, 10, 25, 50, 75, 90, 100]
def test_progress_clamps_to_valid_range(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""DownloadStatus should clamp progress to 0-100."""
# Test that invalid progress values are handled
status = DownloadStatus(
progress=150, # Over 100
state=DownloadState.DOWNLOADING,
message=None,
complete=False,
file_path=None,
)
assert status.progress == 100
status = DownloadStatus(
progress=-10, # Negative
state=DownloadState.DOWNLOADING,
message=None,
complete=False,
file_path=None,
)
assert status.progress == 0
# =============================================================================
# Status Message Tests
# =============================================================================
class TestStatusMessages:
"""Tests for status message formatting."""
def test_status_includes_speed_and_eta(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Status message should include speed and ETA when available."""
mock_client.status_sequence = [
DownloadStatus(
progress=50,
state=DownloadState.DOWNLOADING,
message=None, # No custom message
complete=False,
file_path=None,
download_speed=5 * 1024 * 1024, # 5 MB/s
eta=120, # 2 minutes
),
DownloadStatus(
progress=100,
state=DownloadState.COMPLETE,
message=None,
complete=True,
file_path="/downloads/test.epub",
),
]
with tempfile.TemporaryDirectory() as tmpdir:
source_file = Path(tmpdir) / "test.epub"
source_file.write_text("test content")
staging_dir = Path(tmpdir) / "staging"
staging_dir.mkdir()
mock_client.get_download_path = lambda x: str(source_file)
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
patch(
"shelfmark.download.staging.get_staging_dir",
return_value=staging_dir,
),
patch(
"shelfmark.release_sources.prowlarr.handler.POLL_INTERVAL",
0.01,
),
):
handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
# Find the downloading status message
downloading_msgs = [
msg for status, msg in recorder.status_updates if status == "downloading"
]
assert len(downloading_msgs) > 0
msg = downloading_msgs[0]
assert "50%" in msg
assert "MB/s" in msg
assert "2m" in msg
def test_client_message_takes_priority(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Client-provided message should override generated message."""
mock_client.status_sequence = [
DownloadStatus(
progress=0,
state=DownloadState.DOWNLOADING,
message="Fetching metadata from peers", # Custom message
complete=False,
file_path=None,
),
DownloadStatus(
progress=100,
state=DownloadState.COMPLETE,
message=None,
complete=True,
file_path="/downloads/test.epub",
),
]
with tempfile.TemporaryDirectory() as tmpdir:
source_file = Path(tmpdir) / "test.epub"
source_file.write_text("test content")
staging_dir = Path(tmpdir) / "staging"
staging_dir.mkdir()
mock_client.get_download_path = lambda x: str(source_file)
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
patch(
"shelfmark.download.staging.get_staging_dir",
return_value=staging_dir,
),
patch(
"shelfmark.release_sources.prowlarr.handler.POLL_INTERVAL",
0.01,
),
):
handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
# Check that custom message was used
downloading_msgs = [
msg for status, msg in recorder.status_updates if status == "downloading"
]
assert "Fetching metadata from peers" in downloading_msgs
# =============================================================================
# Cleanup After Error Tests
# =============================================================================
class TestErrorCleanup:
"""Tests verifying proper cleanup after errors."""
def test_cleanup_on_poll_exception(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Torrent downloads should not be removed after polling exception."""
call_count = 0
def exploding_get_status(download_id):
nonlocal call_count
call_count += 1
if call_count > 2:
raise RuntimeError("Client crashed")
return DownloadStatus(
progress=10,
state=DownloadState.DOWNLOADING,
message=None,
complete=False,
file_path=None,
)
mock_client.get_status = exploding_get_status
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=sample_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
patch(
"shelfmark.release_sources.prowlarr.handler.POLL_INTERVAL",
0.01,
),
):
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert not mock_client.remove_called
def test_cleanup_continues_even_if_remove_fails(
self, handler, mock_client, recorder, cancel_flag, sample_task, sample_release
):
"""Handler should not crash if cleanup removal fails."""
mock_client.status_sequence = [
DownloadStatus(
progress=0,
state=DownloadState.ERROR,
message="Download failed",
complete=False,
file_path=None,
),
]
remove_attempted = False
def failing_remove(download_id, delete_files=False):
nonlocal remove_attempted
remove_attempted = True
raise ConnectionError("Client not responding")
mock_client.remove = failing_remove
usenet_release = dict(sample_release)
usenet_release["protocol"] = "usenet"
usenet_release["downloadUrl"] = "https://indexer.example.com/download/123"
with (
patch(
"shelfmark.release_sources.prowlarr.handler.get_release",
return_value=usenet_release,
),
patch(
"shelfmark.release_sources.prowlarr.handler.get_client",
return_value=mock_client,
),
):
# Should not raise, even though remove() fails
result = handler.download(
task=sample_task,
cancel_flag=cancel_flag,
progress_callback=recorder.progress_callback,
status_callback=recorder.status_callback,
)
assert result is None
assert recorder.had_error
assert remove_attempted