Slice 1: selector.py — select_books(), row formatting, confirmation, unit tests #47

Closed
opened 2026-06-06 10:44:59 +02:00 by AlexKucera · 1 comment
AlexKucera commented 2026-06-06 10:44:59 +02:00 (Migrated from github.com)

Parent

#20 (Feature: Interactive Book Selection --select)

What to build

Add questionary as a required runtime dependency in pyproject.toml. Create a new librofm_downloader/selector.py module with a clean public interface: select_books(books: list[Book]) -> list[Book].

The function:

  1. Formats each Book into a display string: "N. Title — First Author [Series #]" (series suffix omitted when absent, author falls back to "Unknown")
  2. Opens a questionary checkbox prompt with those choices, all unchecked by default (opt-in — user story 4)
  3. If the user selects zero books and presses Enter, prints "No books selected. Exiting." and returns an empty list
  4. If the user selects one or more books, shows a confirmation summary: N book(s) selected for download: with a bulleted list, then prompts y/N
  5. On confirmation (y/Y), returns the selected Book objects in their original input order (not the order checkboxes were toggled)
  6. On cancellation (N / bare Enter / Ctrl+C at confirm), returns an empty list
  7. Ctrl+C during the checkbox prompt itself propagates KeyboardInterrupt (handled upstream by sync_run)

Row format: N. Title — First Author [Series #]

  • N is 1-based index
  • [Series #] suffix only when both series and series_num are present
  • Duration excluded (per PRD — would require N extra API calls)

The module has no side effects beyond console I/O via questionary/Rich. It does not call any network APIs or touch the filesystem.

Unit test file: tests/test_selector.py. Tests should patch questionary prompt functions to return controlled values without rendering a real TUI. Test external behavior (inputs → outputs), not which questionary methods were called.

Acceptance criteria

  • questionary added to dependencies in pyproject.toml
  • selector.py exists with _format_row(book, index) -> str and select_books(books) -> list[Book]
  • _format_row produces correct output: with series, without series, single author, no authors → "Unknown"
  • select_books returns the correct subset of Books in original input order
  • select_books returns empty list on zero selection
  • select_books returns empty list on confirmation rejection (N / bare Enter)
  • select_books returns selected Books on confirmation acceptance (y/Y)
  • Ctrl+C during checkbox propagates as KeyboardInterrupt
  • All checkbox choices start unchecked by default
  • test_selector.py has unit tests covering: formatting variants, selection round-trip, empty selection, confirm yes, confirm no, order preservation, single-book library

Blocked by

None — can start immediately.

## Parent #20 (Feature: Interactive Book Selection `--select`) ## What to build Add `questionary` as a required runtime dependency in `pyproject.toml`. Create a new `librofm_downloader/selector.py` module with a clean public interface: `select_books(books: list[Book]) -> list[Book]`. The function: 1. Formats each Book into a display string: `"N. Title — First Author [Series #]"` (series suffix omitted when absent, author falls back to "Unknown") 2. Opens a questionary checkbox prompt with those choices, **all unchecked by default** (opt-in — user story 4) 3. If the user selects zero books and presses Enter, prints "No books selected. Exiting." and returns an empty list 4. If the user selects one or more books, shows a confirmation summary: `N book(s) selected for download:` with a bulleted list, then prompts y/N 5. On confirmation (y/Y), returns the selected Book objects in their **original input order** (not the order checkboxes were toggled) 6. On cancellation (N / bare Enter / Ctrl+C at confirm), returns an empty list 7. Ctrl+C during the checkbox prompt itself propagates `KeyboardInterrupt` (handled upstream by sync_run) Row format: `N. Title — First Author [Series #]` - N is 1-based index - `[Series #]` suffix only when both `series` and `series_num` are present - Duration excluded (per PRD — would require N extra API calls) The module has no side effects beyond console I/O via questionary/Rich. It does not call any network APIs or touch the filesystem. Unit test file: `tests/test_selector.py`. Tests should patch questionary prompt functions to return controlled values without rendering a real TUI. Test external behavior (inputs → outputs), not which questionary methods were called. ## Acceptance criteria - [ ] `questionary` added to `dependencies` in `pyproject.toml` - [ ] `selector.py` exists with `_format_row(book, index) -> str` and `select_books(books) -> list[Book]` - [ ] `_format_row` produces correct output: with series, without series, single author, no authors → "Unknown" - [ ] `select_books` returns the correct subset of Books in original input order - [ ] `select_books` returns empty list on zero selection - [ ] `select_books` returns empty list on confirmation rejection (N / bare Enter) - [ ] `select_books` returns selected Books on confirmation acceptance (y/Y) - [ ] Ctrl+C during checkbox propagates as `KeyboardInterrupt` - [ ] All checkbox choices start unchecked by default - [ ] `test_selector.py` has unit tests covering: formatting variants, selection round-trip, empty selection, confirm yes, confirm no, order preservation, single-book library ## Blocked by None — can start immediately.
AlexKucera commented 2026-06-06 11:53:31 +02:00 (Migrated from github.com)

Implemented in 922e7fe on feature/chooseBooks.

Implemented in `922e7fe` on `feature/chooseBooks`.
Sign in to join this conversation.