feat(cli): enhanced summary, graceful Ctrl+C, integration tests (closes #9)
Summary output now lists each failed book (ISBN + title + reason) and each skipped book (ISBN + title) in both plain-text and rich/TTY formats. Ctrl+C produces a clean shutdown message with exit code 130 instead of a Python traceback. Three integration tests exercise the full run() pipeline: happy path, mixed results, and fatal auth failure. 12 new tests (168 total), 15/15 acceptance criteria met.
This commit is contained in:
1 parent
351ded2478
commit
4aaae68a2e
7 files changed
+706
-128
No files matched your search
@@ -356,3 +356,253 @@ class TestCLIFormatWiring:
|
||||
# Reporter now outputs "Completed:" for successful downloads (Issue #8)
|
||||
assert "Completed" in captured.out
|
||||
assert "Skipped" not in captured.out or "Completed" in captured.out
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Issue #9: Graceful Ctrl+C / KeyboardInterrupt
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestGracefulShutdown:
|
||||
"""Ctrl+C produces a clean exit, not a Python traceback."""
|
||||
|
||||
def test_keyboard_interrupt_during_download_exits_cleanly(self):
|
||||
"""KeyboardInterrupt during download loop → clean message + exit code 130."""
|
||||
with (
|
||||
patch("librofm_downloader.cli.load_config") as mock_config,
|
||||
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
|
||||
patch("librofm_downloader.cli.DownloadHistory") as mock_history_cls,
|
||||
patch("librofm_downloader.cli.download_book") as mock_download,
|
||||
):
|
||||
mock_config.return_value.username = "alice"
|
||||
mock_config.return_value.password = "secret"
|
||||
mock_config.return_value.output_dir = "./audiobooks"
|
||||
|
||||
mock_instance = mock_client_cls.return_value
|
||||
mock_instance.authenticate.return_value = None
|
||||
mock_instance.fetch_library.return_value = [
|
||||
{"isbn": "978111", "title": "Book A", "authors": ["A"], "narrators": ["N"]},
|
||||
{"isbn": "978222", "title": "Book B", "authors": ["A"], "narrators": ["N"]},
|
||||
]
|
||||
|
||||
mock_history_cls.return_value.is_downloaded.return_value = False
|
||||
|
||||
from pathlib import Path
|
||||
mock_download.side_effect = [
|
||||
Path("/audiobooks/A/Book A.m4b"),
|
||||
KeyboardInterrupt,
|
||||
]
|
||||
|
||||
exit_code = run(
|
||||
config_path="/fake/config.yaml",
|
||||
secrets_path="/fake/secrets.yaml",
|
||||
history_path="/fake/history.json",
|
||||
)
|
||||
|
||||
assert exit_code == 130
|
||||
|
||||
def test_keyboard_interrupt_before_downloads_exits_cleanly(self):
|
||||
"""KeyboardInterrupt during library fetch → clean exit."""
|
||||
with (
|
||||
patch("librofm_downloader.cli.load_config") as mock_config,
|
||||
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
|
||||
):
|
||||
mock_config.return_value.username = "alice"
|
||||
mock_config.return_value.password = "secret"
|
||||
|
||||
mock_instance = mock_client_cls.return_value
|
||||
mock_instance.authenticate.return_value = None
|
||||
mock_instance.fetch_library.side_effect = KeyboardInterrupt
|
||||
|
||||
exit_code = run(
|
||||
config_path="/fake/config.yaml",
|
||||
secrets_path="/fake/secrets.yaml",
|
||||
history_path="/fake/history.json",
|
||||
)
|
||||
|
||||
assert exit_code == 130
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Issue #9: Integration tests (full run() pipeline with mocked HTTP)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestIntegrationHappyPath:
|
||||
"""Happy path: 3 new books → all download successfully."""
|
||||
|
||||
def test_three_books_all_download_successfully(self):
|
||||
"""Full pipeline: auth + library fetch + 3 downloads → exit 0 + summary."""
|
||||
with (
|
||||
patch("librofm_downloader.cli.load_config") as mock_config,
|
||||
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
|
||||
patch("librofm_downloader.cli.DownloadHistory") as mock_history_cls,
|
||||
patch("librofm_downloader.cli.download_book") as mock_download,
|
||||
):
|
||||
mock_config.return_value.username = "alice"
|
||||
mock_config.return_value.password = "secret"
|
||||
mock_config.return_value.output_dir = "./audiobooks"
|
||||
|
||||
mock_instance = mock_client_cls.return_value
|
||||
mock_instance.authenticate.return_value = None
|
||||
mock_instance.fetch_library.return_value = [
|
||||
{"isbn": "978111", "title": "Book A", "authors": ["Author A"], "narrators": ["N1"]},
|
||||
{"isbn": "978222", "title": "Book B", "authors": ["Author B"], "narrators": ["N2"]},
|
||||
{"isbn": "978333", "title": "Book C", "authors": ["Author C"], "narrators": ["N3"]},
|
||||
]
|
||||
|
||||
mock_history_cls.return_value.is_downloaded.return_value = False
|
||||
|
||||
from pathlib import Path
|
||||
mock_download.return_value = Path("/audiobooks/Author/Book.m4b")
|
||||
|
||||
exit_code = run(
|
||||
config_path="/fake/config.yaml",
|
||||
secrets_path="/fake/secrets.yaml",
|
||||
history_path="/fake/history.json",
|
||||
)
|
||||
|
||||
assert exit_code == 0
|
||||
assert mock_download.call_count == 3
|
||||
|
||||
|
||||
class TestIntegrationMixedResult:
|
||||
"""Mixed result: new downloads + skipped + failed → correct summary + exit 0."""
|
||||
|
||||
def test_mixed_result_shows_correct_summary_and_exit_code(self, capsys):
|
||||
"""2 download (1 M4B, 1 MP3), 1 skipped, 1 fails → summary lists all + exit 0."""
|
||||
with (
|
||||
patch("librofm_downloader.cli.load_config") as mock_config,
|
||||
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
|
||||
patch("librofm_downloader.cli.DownloadHistory") as mock_history_cls,
|
||||
patch("librofm_downloader.cli.download_book") as mock_download,
|
||||
):
|
||||
mock_config.return_value.username = "alice"
|
||||
mock_config.return_value.password = "secret"
|
||||
mock_config.return_value.output_dir = "./audiobooks"
|
||||
|
||||
mock_instance = mock_client_cls.return_value
|
||||
mock_instance.authenticate.return_value = None
|
||||
# 3 new books (none already downloaded — all enter download loop)
|
||||
mock_instance.fetch_library.return_value = [
|
||||
{"isbn": "978111", "title": "New Book A", "authors": ["Author A"], "narrators": ["N1"]},
|
||||
{"isbn": "978222", "title": "New Book B", "authors": ["Author B"], "narrators": ["N2"]},
|
||||
{"isbn": "978333", "title": "Skip Book C", "authors": ["Author C"], "narrators": ["N3"]},
|
||||
{"isbn": "978444", "title": "Fail Book D", "authors": ["Author D"], "narrators": ["N4"]},
|
||||
]
|
||||
|
||||
mock_history_cls.return_value.is_downloaded.return_value = False
|
||||
|
||||
from pathlib import Path
|
||||
import httpx
|
||||
# 2 succeed, 1 returns None (skipped/no format), 1 raises error
|
||||
mock_download.side_effect = [
|
||||
Path("/audiobooks/A/New Book A.m4b"),
|
||||
Path("/audiobooks/B/New Book B.mp3"),
|
||||
None, # skipped (no format available)
|
||||
httpx.HTTPStatusError(
|
||||
message="Not Found",
|
||||
request=httpx.Request("GET", "https://example.com/fail"),
|
||||
response=httpx.Response(404, request=httpx.Request("GET", "https://example.com/fail")),
|
||||
),
|
||||
]
|
||||
|
||||
exit_code = run(
|
||||
config_path="/fake/config.yaml",
|
||||
secrets_path="/fake/secrets.yaml",
|
||||
history_path="/fake/history.json",
|
||||
)
|
||||
|
||||
assert exit_code == 0
|
||||
assert mock_download.call_count == 4
|
||||
captured = capsys.readouterr()
|
||||
output = captured.out
|
||||
# Summary should show counts
|
||||
assert "Summary:" in output
|
||||
assert "2 downloaded" in output
|
||||
# Failed book should be listed with ISBN and title
|
||||
assert "978444" in output
|
||||
assert "Fail Book D" in output
|
||||
assert "404" in output or "Not Found" in output
|
||||
# Skipped book should be listed
|
||||
assert "978333" in output
|
||||
assert "Skip Book C" in output
|
||||
|
||||
|
||||
class TestIntegrationFatalAuthFailure:
|
||||
"""Fatal path: auth fails → exit 1, no downloads attempted."""
|
||||
|
||||
def test_auth_failure_returns_exit_code_1_no_downloads(self, capsys):
|
||||
"""Authentication failure → exit code 1, download_book never called."""
|
||||
from librofm_downloader.client import AuthError
|
||||
|
||||
with (
|
||||
patch("librofm_downloader.cli.load_config") as mock_config,
|
||||
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
|
||||
patch("librofm_downloader.cli.download_book") as mock_download,
|
||||
):
|
||||
mock_config.return_value.username = "baduser"
|
||||
mock_config.return_value.password = "wrongpass"
|
||||
|
||||
mock_instance = mock_client_cls.return_value
|
||||
mock_instance.authenticate.side_effect = AuthError("Invalid credentials")
|
||||
|
||||
exit_code = run(
|
||||
config_path="/fake/config.yaml",
|
||||
secrets_path="/fake/secrets.yaml",
|
||||
history_path="/fake/history.json",
|
||||
)
|
||||
|
||||
assert exit_code == 1
|
||||
mock_download.assert_not_called()
|
||||
|
||||
captured = capsys.readouterr()
|
||||
assert "Authentication failed" in captured.out
|
||||
|
||||
|
||||
class TestAllBooksFailExitCode:
|
||||
"""When every book fails individually, exit code is still 0 (non-fatal)."""
|
||||
|
||||
def test_all_books_fail_returns_exit_code_0(self):
|
||||
"""All 3 books fail with exceptions → exit 0, not 1."""
|
||||
import httpx
|
||||
|
||||
with (
|
||||
patch("librofm_downloader.cli.load_config") as mock_config,
|
||||
patch("librofm_downloader.cli.LibroFmClient") as mock_client_cls,
|
||||
patch("librofm_downloader.cli.DownloadHistory") as mock_history_cls,
|
||||
patch("librofm_downloader.cli.download_book") as mock_download,
|
||||
):
|
||||
mock_config.return_value.username = "alice"
|
||||
mock_config.return_value.password = "secret"
|
||||
mock_config.return_value.output_dir = "./audiobooks"
|
||||
|
||||
mock_instance = mock_client_cls.return_value
|
||||
mock_instance.authenticate.return_value = None
|
||||
mock_instance.fetch_library.return_value = [
|
||||
{"isbn": "978111", "title": "Fail A", "authors": ["A"], "narrators": ["N"]},
|
||||
{"isbn": "978222", "title": "Fail B", "authors": ["B"], "narrators": ["N"]},
|
||||
{"isbn": "978333", "title": "Fail C", "authors": ["C"], "narrators": ["N"]},
|
||||
]
|
||||
|
||||
mock_history_cls.return_value.is_downloaded.return_value = False
|
||||
|
||||
mock_download.side_effect = [
|
||||
httpx.HTTPStatusError(
|
||||
message="Server Error",
|
||||
request=httpx.Request("GET", "https://example.com/a"),
|
||||
response=httpx.Response(500, request=httpx.Request("GET", "https://example.com/a")),
|
||||
),
|
||||
httpx.ConnectError("Connection refused"),
|
||||
Exception("Download timed out"),
|
||||
]
|
||||
|
||||
exit_code = run(
|
||||
config_path="/fake/config.yaml",
|
||||
secrets_path="/fake/secrets.yaml",
|
||||
history_path="/fake/history.json",
|
||||
)
|
||||
|
||||
# Individual failures are NOT fatal → exit 0
|
||||
assert exit_code == 0
|
||||
assert mock_download.call_count == 3
|
||||
+167
-1
@@ -7,7 +7,7 @@ import tempfile
|
||||
import pytest
|
||||
|
||||
from librofm_downloader.downloader import Book
|
||||
from librofm_downloader.progress import DownloadReporter, PlainTextReporter
|
||||
from librofm_downloader.progress import DownloadReporter, PlainTextReporter, ProgressReporter
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -352,6 +352,115 @@ class TestSummaryLine:
|
||||
assert "0 failed" in output
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Test 8b: Enhanced summary — failed/skipped book details (Issue #9)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestEnhancedSummaryFailedBooks:
|
||||
"""Summary lists each failed book with ISBN, title, and error reason."""
|
||||
|
||||
def test_summary_lists_failed_books_with_isbn_title_and_reason(self):
|
||||
"""When books fail, summary shows each one: ISBN, title, reason."""
|
||||
fake_stdout = io.StringIO()
|
||||
reporter = PlainTextReporter(stdout=fake_stdout)
|
||||
|
||||
book1 = Book(
|
||||
title="Broken Book",
|
||||
authors=["Some Author"],
|
||||
narrators=["N"],
|
||||
isbn="9789999999999",
|
||||
)
|
||||
book2 = Book(
|
||||
title="Timeout Book",
|
||||
authors=["Other Author"],
|
||||
narrators=["N2"],
|
||||
isbn="9788888888888",
|
||||
)
|
||||
|
||||
reporter.summary(
|
||||
downloaded=1,
|
||||
skipped=0,
|
||||
failed=2,
|
||||
failed_books=[(book1, "HTTP 404"), (book2, "Connection timed out")],
|
||||
)
|
||||
|
||||
output = fake_stdout.getvalue()
|
||||
assert "Summary:" in output
|
||||
assert "1 downloaded" in output
|
||||
assert "2 failed" in output
|
||||
# Each failed book listed with ISBN, title, reason
|
||||
assert "9789999999999" in output
|
||||
assert "Broken Book" in output
|
||||
assert "HTTP 404" in output
|
||||
assert "9788888888888" in output
|
||||
assert "Timeout Book" in output
|
||||
assert "Connection timed out" in output
|
||||
|
||||
def test_summary_no_failed_books_omits_section(self):
|
||||
"""When no books failed, no failed-book section appears."""
|
||||
fake_stdout = io.StringIO()
|
||||
reporter = PlainTextReporter(stdout=fake_stdout)
|
||||
|
||||
reporter.summary(
|
||||
downloaded=3,
|
||||
skipped=0,
|
||||
failed=0,
|
||||
failed_books=[],
|
||||
)
|
||||
|
||||
output = fake_stdout.getvalue()
|
||||
assert "3 downloaded" in output
|
||||
assert "0 failed" in output
|
||||
# No individual failure lines
|
||||
assert " ✗" not in output
|
||||
|
||||
|
||||
class TestEnhancedSummarySkippedBooks:
|
||||
"""Summary lists each skipped book with ISBN and title."""
|
||||
|
||||
def test_summary_lists_skipped_books_with_isbn_and_title(self):
|
||||
"""When books are skipped, summary shows each one: ISBN, title."""
|
||||
fake_stdout = io.StringIO()
|
||||
reporter = PlainTextReporter(stdout=fake_stdout)
|
||||
|
||||
book = Book(
|
||||
title="No M4B Book",
|
||||
authors=["Author X"],
|
||||
narrators=["N"],
|
||||
isbn="9787777777777",
|
||||
)
|
||||
|
||||
reporter.summary(
|
||||
downloaded=2,
|
||||
skipped=1,
|
||||
failed=0,
|
||||
skipped_books=[book],
|
||||
)
|
||||
|
||||
output = fake_stdout.getvalue()
|
||||
assert "2 downloaded" in output
|
||||
assert "1 skipped" in output
|
||||
assert "9787777777777" in output
|
||||
assert "No M4B Book" in output
|
||||
|
||||
def test_summary_no_skipped_books_omits_section(self):
|
||||
"""When no books skipped, no skipped-book section appears."""
|
||||
fake_stdout = io.StringIO()
|
||||
reporter = PlainTextReporter(stdout=fake_stdout)
|
||||
|
||||
reporter.summary(
|
||||
downloaded=5,
|
||||
skipped=0,
|
||||
failed=0,
|
||||
skipped_books=[],
|
||||
)
|
||||
|
||||
output = fake_stdout.getvalue()
|
||||
assert "5 downloaded" in output
|
||||
assert "0 skipped" in output
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Test 9: Progress callback — download functions call back with bytes done
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -608,3 +717,60 @@ class TestM4BReportsContentLength:
|
||||
totals_with_cl = [t for _, t in calls if t is not None]
|
||||
assert len(totals_with_cl) >= 1
|
||||
assert content_length in totals_with_cl
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Issue #9: Enhanced summary — ProgressReporter (rich/TTY) variants
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestEnhancedSummaryProgressReporterFailed:
|
||||
"""ProgressReporter (TTY/rich) summary lists failed books with details."""
|
||||
|
||||
def test_rich_summary_lists_failed_books(self):
|
||||
"""TTY reporter's summary includes failed book ISBN, title, reason."""
|
||||
from io import StringIO
|
||||
|
||||
fake_stdout = StringIO()
|
||||
reporter = ProgressReporter(stdout=fake_stdout)
|
||||
|
||||
book = Book(
|
||||
title="Rich Fail",
|
||||
authors=["R Author"],
|
||||
narrators=["N"],
|
||||
isbn="9780000000099",
|
||||
)
|
||||
|
||||
# Should not raise; ProgressReporter prints via rich Console
|
||||
reporter.summary(
|
||||
downloaded=0,
|
||||
skipped=0,
|
||||
failed=1,
|
||||
failed_books=[(book, "HTTP 500")],
|
||||
)
|
||||
|
||||
|
||||
class TestEnhancedSummaryProgressReporterSkipped:
|
||||
"""ProgressReporter (TTY/rich) summary lists skipped books."""
|
||||
|
||||
def test_rich_summary_lists_skipped_books(self):
|
||||
"""TTY reporter's summary includes skipped book ISBN and title."""
|
||||
from io import StringIO
|
||||
|
||||
fake_stdout = StringIO()
|
||||
reporter = ProgressReporter(stdout=fake_stdout)
|
||||
|
||||
book = Book(
|
||||
title="Rich Skip",
|
||||
authors=["S Author"],
|
||||
narrators=["N"],
|
||||
isbn="9780000000088",
|
||||
)
|
||||
|
||||
# Should not raise; ProgressReporter prints via rich Console
|
||||
reporter.summary(
|
||||
downloaded=1,
|
||||
skipped=1,
|
||||
failed=0,
|
||||
skipped_books=[book],
|
||||
)
|
||||
Reference in new issue
Block a user