Add inline comment in _finalize_download() explaining that the
rename_chapters bool param shadows the module-level rename_chapters()
function. This is safe because the actual call is delegated to
_rename_and_log(), which has its own clean scope.
Two bugs found by code review:
1. Progress bar exceeds 100% on resumed downloads because
Content-Length from a Range response covers only remaining
bytes. Now adds resume_from for the true total.
2. session.transport reached into httpx.Client._transport, a
private attribute that may break on httpx upgrades. Transport
is now stored at construction time instead.
SyncRunResult now wraps OrchestratorResult instead of duplicating
its 6 fields. Delegating properties preserve the public interface,
eliminating field-by-field copy code in the bridge between
orchestrator and sync_run.
Closes#45
Add 4 injectable parameters (session, history, reporter,
download_all_fn) to sync_run() with backward-compatible defaults.
Extract _print_verbose_config() and _resolve_history_path() helpers.
Refactor _make_download_fn() to take explicit params instead of
closing over enclosing scope.
Tests can now pass fakes via parameters instead of mock.patch.
Closes#44
Delete 13 duplicate path test classes from test_downloader.py
(already exist in test_path.py). Move 2 sync_run integration
classes from test_progress.py to test_sync_run.py. Remove
duplicate TestParallelProgressCallbackWiring class. Rescue orphaned
3-book interleaved plain text test with proper class wrapper.
Net -469 test lines. Each test file now tests exactly one module.
The three format-strategy blocks in download_book() each duplicated
the same post-download success logic: rename chapters (MP3 only),
download accompanying files, and return a DownloadResult. Extract
a single _finalize_download() helper so each branch reduces to:
attempt download → return _finalize_download(...).
Adding a new format strategy now requires only the download attempt
plus one call to the existing success helper.
Closes#42
Eliminate the ~30-line duplicated chunked-download loop across
download_m4b(), download_zip_part(), _download_cover(), and
_download_pdf() by extracting a shared _stream_to_file() private
helper. Each caller is now a thin wrapper that delegates streaming
to the helper then performs its own post-processing step.
5 new TDD tests covering fresh download, resume, cancel mid-stream,
progress callback with content-length, and HTTP error propagation.
224 total tests pass. Net -22 lines in downloader.py.
Closes#41
Move the `_write_history()` function from `downloader.py` to `history.py`. This refactoring separates persistence logic from download orchestration, ensuring `downloader.py` focuses solely on its core responsibility.
Thread cancel_event explicitly through the parameter chain
(sync_run closure → download_book() → streaming calls) instead of
stashing it on reporter objects as shared state. Remove the
cancel_event attribute from both PlainTextReporter and
ProgressReporter — cancellation is not a reporting concern.
Closes#39
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add @property transport getter that returns the underlying httpx
transport, replacing the single call site in downloader.py that
reached through session._client._transport. Encapsulates private
attribute access behind a public interface. 2 new tests.
Closes#38
Remove the 32-line re-export shim (Issue #27 leftover) and its 588-line
duplicate test file. Migrate 4 unique rate-limiter tests into
test_session.py. Zero production imports of client.py confirmed.
Net: -337 lines dead code, 425→413 tests collected.
Closes#37
Add 4 end-to-end integration tests proving rename_chapters() fires
correctly through the full download pipeline for each format strategy,
and is properly suppressed when disabled.
Closes#35
_download_mp3() returns (output_dir, tracks) tuple so manifest
track data flows to callers. download_book() accepts rename_chapters
kwarg; after MP3 extraction succeeds, calls rename_chapters() via
_rename_and_log() helper which also reports to user via reporter.
Both PlainTextReporter and RichProgressReporter show visible feedback:
'Renamed N chapter(s) for Book → example.mp3'. sync_run.py closure
threads resolved_rename_chapters flag through to download_book().
6 new integration tests covering all format strategies and edge cases.
~229 total tests. Closes#34.
Add rename_chapters boolean config field following the established
pattern from download_extras/download_covers. Config defaults to
True; YAML supports explicit false; --rename-chapters CLI flag forces
True overriding config. Threaded through sync_run() with resolved value
ready for pipeline wiring in slice 3.
Closes#33
Add standalone rename_chapters(output_dir, tracks, book_title) -> int
function that renames extracted MP3 files to include chapter titles
from the Libro.fm download manifest. Features: natural sort by numeric
prefix, auto zero-padding from track count, null/blank title fallback
to "Chapter N", sanitization via existing sanitize(). 11 tests, all
passing. Closes#32.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>