feat(orchestrator): add ThreadPoolExecutor parallel download engine (#16)

Replace the sequential for-loop in cli.py with download_all_books()
which submits all books as futures to a ThreadPoolExecutor. Each future
runs the full download pipeline (API → streaming → history) independently;
failures are isolated per book and siblings continue unaffected.

OrchestratorResult dataclass returns stable-sorted counts and per-book
details (failed_books as (Book, reason) tuples, skipped_books as Book
objects). The _raw_to_book() conversion is extracted from cli.py so the
orchestrator owns the dict→Book mapping. All 20 unit tests use an injected
download_fn — no network or filesystem required.

Co-Authored-By: pi <pi@earendil.works>
This commit is contained in:
AlexKuceraandpi committed 2026-06-03 18:00:52 +02:00
1 parent 6418c63c2f
commit e7e6c625b1
6 files changed
+894 -74

No files matched your search

+17 -24
View File
@@ -535,17 +535,21 @@ class TestIntegrationMixedResult:
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")),
),
]
# Deterministic mock based on ISBN — thread-safe for concurrent workers
def _download_side_effect(book, **kwargs):
if book.isbn == "978333":
return None # skipped (no format available)
if book.isbn == "978444":
raise 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")),
)
# 978111 and 978222 succeed
return Path(f"/audiobooks/{book.authors[0]}/{book.title}.m4b")
mock_download.side_effect = _download_side_effect
exit_code = run(
config_path="/fake/config.yaml",
@@ -553,21 +557,10 @@ class TestIntegrationMixedResult:
history_path="/fake/history.json",
)
# Exit code 0 even with failures (best-effort batch)
assert exit_code == 0
# All 4 books were attempted
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."""