This document outlines a plan to refactor the codebase to address shallow modules and leaky interfaces. It defines new deep module boundaries to improve testability, maintainability, and extensibility for future development.
15 KiB
PRD 001: Deepen Architecture — Module Depth & Domain Alignment
Status: Draft (grilling complete, ready for issue breakdown) Date: 2026-06-04 Parent ADRs: ADR 0001, ADR 0006
Problem Statement
The librofm-downloader codebase has grown through 10+ issue-driven slices (Issues #2–#23). Each slice added correct code in the right place at the time, but the accumulated result is shallow modules with leaky interfaces:
cli.run()(246 lines) does the entire Sync Run pipeline — config resolution, auth, library fetch, history filtering, worker resolution, download orchestration, summary, and exit-code policy. Its interface is small (7 parameters) but its test surface is enormous: ~29 tests patch 6+ internal modules to exercise it.download_book()(9 parameters) exposes implementation details (transport override, cancel_event threading, format strategy branching, config for extras toggles) that every caller must understand.LibroFmClientrepeats auth-header assembly,httpx.Clientconstruction, authenticated-state checks, and test-onlytransport=parameters across 4 endpoint methods.- Book intake (
_raw_to_book()) lives in the orchestrator while theBookdataclass lives indownloader.py, splitting API-shape normalization knowledge across two files. - Output path logic is scattered across 4 functions in
downloader.pyplus theBookdataclass, and has already produced 2 real bugs (path doubling, cover placement). - Download reporting leaks task identity to callers; the parallel progress-bar bug (Issue #23 fix) was caused by callers needing to bind
task_idthrough a closure. - DownloadHistory writing happens inside
download_book()at two separate points, making it impossible for callers to control persistence policy without modifying download logic.
These shallow modules force tests to patch internals rather than exercising interfaces. They cause bugs when implementation details change. And they make every new feature touch more files than it should.
Solution
A coordinated deepening of 6 module boundaries, executed in dependency order. Each candidate introduces a deep module: a small interface behind which a lot of behaviour concentrates. The vocabulary comes from the existing domain glossary (CONTEXT.md) and respects both ADRs.
Execution Order
#2 Book intake ← foundation: everything works with Book objects
#3 Output Structure ← needs Book; consumed by download & session
#6 Libro.fm session ← independent of #3, can be done in parallel
#4 Download Attempt ← needs OutputPlan + Session + named results
#5 Download reporting ← needs Download Attempt shape
#1 Sync Run ← orchestrates all above
User Stories
- As a maintainer, I want
Bookintake and API-shape normalization to live in one place, so that Libro.fm API changes (new fields, nested structure shifts) require editing exactly one file. - As a maintainer, I want output path planning to return a coherent plan object, so that adding a new output type (e.g., chapter images, metadata sidecars) doesn't require touching 4+ functions.
- As a test author, I want to assert download outcomes through a
DownloadResultdataclass, so that I don't need to coordinate mocks forclient,history,progress,cancel_event, andconfigsimultaneously. - As a test author, I want the Librofm session to accept transport injection at construction time, so that I can create one mock session and pass it to any function that needs Librofm API access — instead of threading
transport=through 4 method signatures per test. - As a test author, I want the Sync Run pipeline to be callable as a function with clear parameters, so that I can test auth failure, empty library, mixed download results, and Ctrl+C handling without patching 6 internal imports inside
cli.py. - As a developer, I want
download_book()to take 4 domain-aligned parameters instead of 9, so that adding a new download concern (e.g., checksum verification) adds depth to an existing module rather than widening the interface again. - As a developer, I want progress reporting to give me a bound callback per book, so that parallel download wiring cannot accidentally update the wrong progress bar.
- As a future developer implementing Select Mode (ADR #6), I want the Sync Run pipeline to have a clear filter stage that I can replace with interactive selection — without restructuring the entire pipeline.
Implementation Decisions
Candidate #2: Book Intake Module
- New file:
librofm_downloader/book.py - Contains:
Bookdataclass (moved fromdownloader.py),from_library_row(raw: dict) -> Bookintake function - ISBN normalization:
from_library_row()coerces ISBN tostr()(API sometimes returnsint) - Narrator resolution: handles nested
audiobook_info.narratorsfallback to top-levelnarrators - PDF extras detection: handles nested
audiobook_info.pdf_extraswithbool()coercion - Default values: title defaults to
"Unknown", isbn defaults to"?"— preserved from current_raw_to_book() - Path helpers move to separate
path.py(not intobook.py):sanitize(),resolve_path(),needs_subdirectory(), token registry,_token_value()
Candidate #3: Output Structure Planning
- New file:
librofm_downloader/path.py - Contains: path helpers (from
downloader.py) + newOutputPlanfrozen dataclass OutputPlancarries:- Audio destination path (
.m4bor MP3 directory) - Partial-file path (
.m4b.partialor.zip.partial) - Cover art destination path (or
Noneif disabled/absent) - PDF extra destination path (or
Noneif disabled/absent) - Format strategy (from Config)
- Extras flags:
download_covers,download_extras(from Config)
- Audio destination path (
- Resolution function:
resolve_output_plan(book: Book, output_base: Path | str, config: Config) -> OutputPlan - Replaces:
_resolve_output_dir()+ scattered path computation indownload_book(),_download_cover(),_download_pdf() - Download functions consume plan fields instead of computing paths internally
Candidate #6: Libro.fm Session
- Rename:
LibroFmClient→LibroFmSession - New file:
librofm_downloader/session.py(renamed fromclient.py; keep alias for transition) - Constructor injection:
LibroFmSession(base_url, username, password, *, transport=None) - All 4 endpoint methods drop their
transportparameter - Internal: single
httpx.Clientconstructed once with the injected transport (or real HTTP) - Auth headers assembled once and stored on the instance (not recomputed per call)
- API semaphore (
threading.Semaphore(3)) stays on the instance - Backward compatibility:
librofm_downloader/client.pyre-exports fromsession.pyas deprecated aliases
Candidate #4: Download Attempt
- Stays in:
librofm_downloader/downloader.py(but slimmer — only download logic) - New interface:
download_book(book: Book, session: LibroFmSession, plan: OutputPlan, reporter: DownloadReporter) -> DownloadResult - New result type:
DownloadResultfrozen dataclass with fields:status:Literal["downloaded", "skipped", "failed"]path:Path | None(output path on success, None on skip/fail)format:str | None(actual format used:"m4b"or"mp3")error:str | None(error message on failure)
- History writing moves to caller:
download_book()no longer takeshistoryor calls_write_history(). The orchestrator/Sync Run inspectsDownloadResultand writes history for successful downloads. - Format strategy and extras flags come from
OutputPlan(not from raw Config) - Progress callback comes from reporter (see #5)
- Cancel event handled internally or via reporter contract (TBD during implementation)
Candidate #5: Download Reporting
- Stays in:
librofm_downloader/progress.py - Change:
start_download(book)returns a bound callableCallable[[int], None](for TTY/ProgressReporter) orNone(for PlainTextReporter) - Task identity is fully internal to
ProgressReporter; callers never seetask_id complete(book)andfail(book, reason)stay on the reporter (they look up task id internally viaid(book))- Orchestrator no longer creates closures — it just passes the bound callback to
download_fn - Fallback behavior removed:
update()withouttask_idraises or is a no-op; the bound callback always targets the right bar
Candidate #1: Sync Run Pipeline
- New file:
librofm_downloader/sync_run.py - New function:
sync_run(config, secrets_path, history_path, *, verbose, limit, workers, select_mode) -> SyncRunResult - Pipeline stages (sequential, early-return on fatal error):
- Resolve paths (XDG/CWD/history)
- Load config
- Authenticate →
LibroFmSession - Fetch library →
list[dict] - Intake: convert to
list[Book]viafrom_library_row() - Filter: remove downloaded books (unless select_mode)
- Select: if
select_mode, open TUI (ADR #6); else apply limit - Resolve
OutputPlanfor each book - Download all (via orchestrator or direct call)
- Write history for successful downloads
- Report summary
- Return
SyncRunResult
cli.pybecomes thin adapter: parse args → callsync_run()→ translate exit code →sys.exit()- Verbose output: printed by
sync_run()itself (it owns the pipeline, it owns the output) SyncRunResultabsorbs/replacesOrchestratorResult:downloaded_count,skipped_count,failed_countfailed_books: list[tuple[Book, str]]skipped_books: list[Book]interrupted: bool
Cross-Cutting: Named Result Types
DownloadResult— per-book outcome fromdownload_book()SyncRunResult— pipeline outcome (replacesOrchestratorResult)OrchestratorResult— deprecated;download_all_books()returnsSyncRunResultdirectly- All frozen dataclasses for immutability
Cross-Cutting: Import Migration
| Old import | New import |
|---|---|
from librofm_downloader.downloader import Book |
from librofm_downloader.book import Book |
from librofm_downloader.downloader import sanitize, resolve_path |
from librofm_downloader.path import sanitize, resolve_path |
from librofm_downloader.client import LibroFmClient |
from librofm_downloader.session import LibroFmSession |
from librofm_downloader.client import AuthError, M4BUnavailableError |
from librofm_downloader.session import AuthError, M4BUnavailableError |
Testing Decisions
Test Philosophy
- Test through interfaces, not implementation. After deepening, tests should construct real objects (Book, OutputPlan, fake LibroFmSession) rather than patching internal imports.
- Each deep module gets its own test file exercising its interface boundary.
- Integration tests verify pipeline stages connect correctly (sync_run tests use faked session + filesystem history).
Test File Map
| New/Modified Test File | Tests What |
|---|---|
tests/test_book.py (NEW) |
Book dataclass, from_library_row() edge cases: missing fields, int ISBN, nested narrators, PDF extras bool coercion, default values |
tests/test_path.py (NEW/MOVED) |
sanitize(), resolve_path() (default + custom pattern), needs_subdirectory(), OutputPlan resolution, subdirectory rules, cover/PDF path inclusion |
tests/test_session.py (RENAMED) |
Constructor injection, auth, all 4 endpoints via one transport, semaphore, error translation. Renamed from test_client.py |
tests/test_downloader.py (SHRUNK) |
Only download functions: download_m4b, download_zip_part, _download_mp3, download_accompanying_files, download_book (new 4-arg interface), DownloadResult shape |
tests/test_progress.py (UPDATED) |
Bound callable from start_download(), task identity internal, no more closure wiring tests needed |
tests/test_sync_run.py (NEW) |
Pipeline stages: config resolution, auth failure, empty library, filtering, limit, summary, exit codes, Ctrl+C |
tests/test_cli.py (SHRUNK) |
Only argparse parsing + exit-code translation. No more pipeline testing in CLI tests. Most current CLI test classes migrate to test_sync_run.py. |
tests/test_orchestrator.py (UPDATED) |
Takes list[Book] not list[dict]. Uses DownloadResult. History writing verified at caller level. |
Estimated Test Impact
| File | Current tests | Post-refactor estimate | Change |
|---|---|---|---|
test_cli.py |
~29 classes | ~5 classes (argparse + exit code only) | -24 classes migrate out |
test_downloader.py |
~20 classes | ~14 classes (Book/sanitize/path tests move out) | -6 classes migrate out |
test_client.py |
~6 classes | ~6 classes (renamed, signature updates) | Same count, rewrites |
test_book.py (NEW) |
0 | ~8 classes | +8 classes |
test_path.py (NEW) |
0 | ~12 classes | +12 classes |
test_sync_run.py (NEW) |
0 | ~15 classes | +15 classes (migrated from cli) |
Out of Scope
- Select Mode implementation (ADR #6) — this PRD defines where select mode plugs into the pipeline (stage 7), but does not implement the TUI itself.
- Async/multiprocessing migration — ADR 0001 explicitly rejects this; this PRD respects that decision.
- Configuration schema changes — no new config fields or validation rules.
- Database or persistent queue — beyond the tool's scope (cron-invoked CLI).
- Retry logic — not part of current requirements; would be a future addition to the Download Attempt module.
- Checksum/integrity verification — would consume
OutputPlanbut is not in scope.
Further Notes
Risks
- Import churn is the largest risk. ~97 files import
Bookfromdownloader.py. The aggressive profile embraces this, but a mechanical find-and-replace + test run cycle is essential. Git's rename detection will help track the move. LibroFmClientrename breaks external references if any exist (docs, user configs). The backward-compat alias inclient.pymitigates this for one release cycle.- History-writing relocation is the most subtle behavioral change. Currently
download_book()writes history after M4B success AND after MP3 success. Moving this to the caller means there's exactly one write point — but any caller ofdownload_book()(not just the orchestrator) must remember to write history. Mitigation: makedownload_all_books()/sync_run()the only callers; treatdownload_book()as internal API. - Orchestrator taking
list[Book]means orchestrator tests must construct Book objects instead of raw dicts. This is actually simpler (fewer dict keys to remember) but requires updating ~21 test helper functions.
Success Criteria
- Every new module has ≤5 public symbols in its interface (depth measure)
- No test in
test_cli.pypatchesload_config,LibroFmClient,DownloadHistory,download_book, ORDownloadReporter— those belong intest_sync_run.py download_book()has ≤5 parameters (down from 9)LibroFmSessionendpoint methods have ≤3 parameters each (down from 5-6)- All 259+ tests pass (including 3 known-hangy Ctrl+C tests)
cli.py≤80 lines (down from 246)- Zero knowledge of raw Libro.fm API dict shape exists outside
book.py
PRD written from architecture review grilling session. Ready for issue breakdown via /to-issues.