feat(readarr): add the delete book handler
Handles the delete book prompt opened from the author details view, toggling whether to delete files on disk and whether to add a list exclusion before confirming. Backing out of the prompt resets both toggles, so preferences from an abandoned delete cannot leak into the next one.
This commit is contained in:
4 files changed
+636
-2
No files matched your search
@@ -0,0 +1,147 @@
|
|||||||
|
use crate::app::App;
|
||||||
|
use crate::event::Key;
|
||||||
|
use crate::handlers::{KeyEventHandler, handle_prompt_toggle};
|
||||||
|
use crate::matches_key;
|
||||||
|
use crate::models::Route;
|
||||||
|
use crate::models::readarr_models::DeleteParams;
|
||||||
|
use crate::models::servarr_data::readarr::readarr_data::{ActiveReadarrBlock, DELETE_BOOK_BLOCKS};
|
||||||
|
use crate::network::readarr_network::ReadarrEvent;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[path = "delete_book_handler_tests.rs"]
|
||||||
|
mod delete_book_handler_tests;
|
||||||
|
|
||||||
|
pub(in crate::handlers::readarr_handlers) struct DeleteBookHandler<'a, 'b> {
|
||||||
|
key: Key,
|
||||||
|
app: &'a mut App<'b>,
|
||||||
|
active_readarr_block: ActiveReadarrBlock,
|
||||||
|
_context: Option<ActiveReadarrBlock>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeleteBookHandler<'_, '_> {
|
||||||
|
fn build_delete_book_params(&mut self) -> DeleteParams {
|
||||||
|
let id = self.app.data.readarr_data.books.current_selection().id;
|
||||||
|
let delete_files = self.app.data.readarr_data.delete_files;
|
||||||
|
let add_import_list_exclusion = self.app.data.readarr_data.add_import_list_exclusion;
|
||||||
|
self.app.data.readarr_data.reset_delete_preferences();
|
||||||
|
|
||||||
|
DeleteParams {
|
||||||
|
id,
|
||||||
|
delete_files,
|
||||||
|
add_import_list_exclusion,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveReadarrBlock> for DeleteBookHandler<'a, 'b> {
|
||||||
|
fn accepts(active_block: ActiveReadarrBlock) -> bool {
|
||||||
|
DELETE_BOOK_BLOCKS.contains(&active_block)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ignore_special_keys(&self) -> bool {
|
||||||
|
self.app.ignore_special_keys_for_textbox_input
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new(
|
||||||
|
key: Key,
|
||||||
|
app: &'a mut App<'b>,
|
||||||
|
active_block: ActiveReadarrBlock,
|
||||||
|
_context: Option<ActiveReadarrBlock>,
|
||||||
|
) -> DeleteBookHandler<'a, 'b> {
|
||||||
|
DeleteBookHandler {
|
||||||
|
key,
|
||||||
|
app,
|
||||||
|
active_readarr_block: active_block,
|
||||||
|
_context,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_key(&self) -> Key {
|
||||||
|
self.key
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_ready(&self) -> bool {
|
||||||
|
!self.app.is_loading
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_scroll_up(&mut self) {
|
||||||
|
if self.active_readarr_block == ActiveReadarrBlock::DeleteBookPrompt {
|
||||||
|
self.app.data.readarr_data.selected_block.up();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_scroll_down(&mut self) {
|
||||||
|
if self.active_readarr_block == ActiveReadarrBlock::DeleteBookPrompt {
|
||||||
|
self.app.data.readarr_data.selected_block.down();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_home(&mut self) {}
|
||||||
|
|
||||||
|
fn handle_end(&mut self) {}
|
||||||
|
|
||||||
|
fn handle_delete(&mut self) {}
|
||||||
|
|
||||||
|
fn handle_left_right_action(&mut self) {
|
||||||
|
if self.active_readarr_block == ActiveReadarrBlock::DeleteBookPrompt {
|
||||||
|
handle_prompt_toggle(self.app, self.key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_submit(&mut self) {
|
||||||
|
if self.active_readarr_block == ActiveReadarrBlock::DeleteBookPrompt {
|
||||||
|
match self.app.data.readarr_data.selected_block.get_active_block() {
|
||||||
|
ActiveReadarrBlock::DeleteBookConfirmPrompt => {
|
||||||
|
if self.app.data.readarr_data.prompt_confirm {
|
||||||
|
self.app.data.readarr_data.prompt_confirm_action =
|
||||||
|
Some(ReadarrEvent::DeleteBook(self.build_delete_book_params()));
|
||||||
|
self.app.should_refresh = true;
|
||||||
|
} else {
|
||||||
|
self.app.data.readarr_data.reset_delete_preferences();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.app.pop_navigation_stack();
|
||||||
|
}
|
||||||
|
ActiveReadarrBlock::DeleteBookToggleDeleteFile => {
|
||||||
|
self.app.data.readarr_data.delete_files = !self.app.data.readarr_data.delete_files;
|
||||||
|
}
|
||||||
|
ActiveReadarrBlock::DeleteBookToggleAddListExclusion => {
|
||||||
|
self.app.data.readarr_data.add_import_list_exclusion =
|
||||||
|
!self.app.data.readarr_data.add_import_list_exclusion;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_esc(&mut self) {
|
||||||
|
if self.active_readarr_block == ActiveReadarrBlock::DeleteBookPrompt {
|
||||||
|
self.app.pop_navigation_stack();
|
||||||
|
self.app.data.readarr_data.reset_delete_preferences();
|
||||||
|
self.app.data.readarr_data.prompt_confirm = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_char_key_event(&mut self) {
|
||||||
|
if self.active_readarr_block == ActiveReadarrBlock::DeleteBookPrompt
|
||||||
|
&& self.app.data.readarr_data.selected_block.get_active_block()
|
||||||
|
== ActiveReadarrBlock::DeleteBookConfirmPrompt
|
||||||
|
&& matches_key!(confirm, self.key)
|
||||||
|
{
|
||||||
|
self.app.data.readarr_data.prompt_confirm = true;
|
||||||
|
self.app.data.readarr_data.prompt_confirm_action =
|
||||||
|
Some(ReadarrEvent::DeleteBook(self.build_delete_book_params()));
|
||||||
|
self.app.should_refresh = true;
|
||||||
|
|
||||||
|
self.app.pop_navigation_stack();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn app_mut(&mut self) -> &mut App<'b> {
|
||||||
|
self.app
|
||||||
|
}
|
||||||
|
|
||||||
|
fn current_route(&self) -> Route {
|
||||||
|
self.app.get_current_route()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,470 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use rstest::rstest;
|
||||||
|
use strum::IntoEnumIterator;
|
||||||
|
|
||||||
|
use crate::app::App;
|
||||||
|
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
|
||||||
|
use crate::event::Key;
|
||||||
|
use crate::handlers::KeyEventHandler;
|
||||||
|
use crate::handlers::readarr_handlers::library::delete_book_handler::DeleteBookHandler;
|
||||||
|
use crate::models::readarr_models::{Book, DeleteParams};
|
||||||
|
use crate::models::servarr_data::readarr::readarr_data::{
|
||||||
|
ActiveReadarrBlock, DELETE_BOOK_BLOCKS,
|
||||||
|
};
|
||||||
|
|
||||||
|
mod test_handle_scroll_up_and_down {
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
use crate::models::BlockSelectionState;
|
||||||
|
use crate::models::servarr_data::readarr::readarr_data::DELETE_BOOK_SELECTION_BLOCKS;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn test_delete_book_prompt_scroll(#[values(Key::Up, Key::Down)] key: Key) {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.data.readarr_data.selected_block = BlockSelectionState::new(DELETE_BOOK_SELECTION_BLOCKS);
|
||||||
|
app.data.readarr_data.selected_block.down();
|
||||||
|
|
||||||
|
DeleteBookHandler::new(key, &mut app, ActiveReadarrBlock::DeleteBookPrompt, None).handle();
|
||||||
|
|
||||||
|
if key == Key::Up {
|
||||||
|
assert_eq!(
|
||||||
|
app.data.readarr_data.selected_block.get_active_block(),
|
||||||
|
ActiveReadarrBlock::DeleteBookToggleDeleteFile
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
assert_eq!(
|
||||||
|
app.data.readarr_data.selected_block.get_active_block(),
|
||||||
|
ActiveReadarrBlock::DeleteBookConfirmPrompt
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn test_delete_book_prompt_scroll_no_op_when_not_ready(#[values(Key::Up, Key::Down)] key: Key) {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.is_loading = true;
|
||||||
|
app.data.readarr_data.selected_block = BlockSelectionState::new(DELETE_BOOK_SELECTION_BLOCKS);
|
||||||
|
app.data.readarr_data.selected_block.down();
|
||||||
|
|
||||||
|
DeleteBookHandler::new(key, &mut app, ActiveReadarrBlock::DeleteBookPrompt, None).handle();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
app.data.readarr_data.selected_block.get_active_block(),
|
||||||
|
ActiveReadarrBlock::DeleteBookToggleAddListExclusion
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod test_handle_left_right_action {
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn test_left_right_prompt_toggle(#[values(Key::Left, Key::Right)] key: Key) {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::DeleteBookPrompt.into());
|
||||||
|
|
||||||
|
DeleteBookHandler::new(key, &mut app, ActiveReadarrBlock::DeleteBookPrompt, None).handle();
|
||||||
|
|
||||||
|
assert!(app.data.readarr_data.prompt_confirm);
|
||||||
|
|
||||||
|
DeleteBookHandler::new(key, &mut app, ActiveReadarrBlock::DeleteBookPrompt, None).handle();
|
||||||
|
|
||||||
|
assert!(!app.data.readarr_data.prompt_confirm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod test_handle_submit {
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
use crate::assert_navigation_popped;
|
||||||
|
use crate::models::BlockSelectionState;
|
||||||
|
use crate::models::servarr_data::readarr::readarr_data::DELETE_BOOK_SELECTION_BLOCKS;
|
||||||
|
use crate::network::readarr_network::ReadarrEvent;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const SUBMIT_KEY: Key = DEFAULT_KEYBINDINGS.submit.key;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_prompt_prompt_decline_submit() {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::DeleteBookPrompt.into());
|
||||||
|
app.data.readarr_data.selected_block = BlockSelectionState::new(DELETE_BOOK_SELECTION_BLOCKS);
|
||||||
|
app
|
||||||
|
.data
|
||||||
|
.readarr_data
|
||||||
|
.selected_block
|
||||||
|
.set_index(0, DELETE_BOOK_SELECTION_BLOCKS.len() - 1);
|
||||||
|
app.data.readarr_data.delete_files = true;
|
||||||
|
app.data.readarr_data.add_import_list_exclusion = true;
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
SUBMIT_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_navigation_popped!(app, ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
assert_none!(app.data.readarr_data.prompt_confirm_action);
|
||||||
|
assert!(!app.data.readarr_data.prompt_confirm);
|
||||||
|
assert!(!app.data.readarr_data.delete_files);
|
||||||
|
assert!(!app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_confirm_prompt_prompt_confirmation_submit() {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::DeleteBookPrompt.into());
|
||||||
|
app.data.readarr_data.prompt_confirm = true;
|
||||||
|
app.data.readarr_data.delete_files = false;
|
||||||
|
app.data.readarr_data.add_import_list_exclusion = true;
|
||||||
|
app.data.readarr_data.books.set_items(vec![book()]);
|
||||||
|
let expected_delete_book_params = DeleteParams {
|
||||||
|
id: 42,
|
||||||
|
delete_files: false,
|
||||||
|
add_import_list_exclusion: true,
|
||||||
|
};
|
||||||
|
app.data.readarr_data.selected_block = BlockSelectionState::new(DELETE_BOOK_SELECTION_BLOCKS);
|
||||||
|
app
|
||||||
|
.data
|
||||||
|
.readarr_data
|
||||||
|
.selected_block
|
||||||
|
.set_index(0, DELETE_BOOK_SELECTION_BLOCKS.len() - 1);
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
SUBMIT_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_navigation_popped!(app, ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
assert_eq!(
|
||||||
|
app.data.readarr_data.prompt_confirm_action,
|
||||||
|
Some(ReadarrEvent::DeleteBook(expected_delete_book_params))
|
||||||
|
);
|
||||||
|
assert!(app.should_refresh);
|
||||||
|
assert!(app.data.readarr_data.prompt_confirm);
|
||||||
|
assert!(!app.data.readarr_data.delete_files);
|
||||||
|
assert!(!app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_confirm_prompt_prompt_confirmation_submit_no_op_when_not_ready() {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.is_loading = true;
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::DeleteBookPrompt.into());
|
||||||
|
app.data.readarr_data.prompt_confirm = true;
|
||||||
|
app.data.readarr_data.delete_files = true;
|
||||||
|
app.data.readarr_data.add_import_list_exclusion = false;
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
SUBMIT_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
app.get_current_route(),
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt.into()
|
||||||
|
);
|
||||||
|
assert_none!(app.data.readarr_data.prompt_confirm_action);
|
||||||
|
assert!(!app.should_refresh);
|
||||||
|
assert!(app.data.readarr_data.prompt_confirm);
|
||||||
|
assert!(app.data.readarr_data.delete_files);
|
||||||
|
assert!(!app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_toggle_delete_files_submit() {
|
||||||
|
let current_route = ActiveReadarrBlock::DeleteBookPrompt.into();
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.data.readarr_data.selected_block = BlockSelectionState::new(DELETE_BOOK_SELECTION_BLOCKS);
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::DeleteBookPrompt.into());
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
SUBMIT_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_eq!(app.get_current_route(), current_route);
|
||||||
|
assert!(app.data.readarr_data.delete_files);
|
||||||
|
assert!(!app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
SUBMIT_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_eq!(app.get_current_route(), current_route);
|
||||||
|
assert!(!app.data.readarr_data.delete_files);
|
||||||
|
assert!(!app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_toggle_add_list_exclusion_submit() {
|
||||||
|
let current_route = ActiveReadarrBlock::DeleteBookPrompt.into();
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.data.readarr_data.selected_block = BlockSelectionState::new(DELETE_BOOK_SELECTION_BLOCKS);
|
||||||
|
app.data.readarr_data.selected_block.down();
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::DeleteBookPrompt.into());
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
SUBMIT_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_eq!(app.get_current_route(), current_route);
|
||||||
|
assert!(app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
assert!(!app.data.readarr_data.delete_files);
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
SUBMIT_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_eq!(app.get_current_route(), current_route);
|
||||||
|
assert!(!app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
assert!(!app.data.readarr_data.delete_files);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod test_handle_esc {
|
||||||
|
use rstest::rstest;
|
||||||
|
|
||||||
|
use crate::assert_navigation_popped;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const ESC_KEY: Key = DEFAULT_KEYBINDINGS.esc.key;
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn test_delete_book_prompt_esc(#[values(true, false)] is_loading: bool) {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.is_loading = is_loading;
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::DeleteBookPrompt.into());
|
||||||
|
app.data.readarr_data.prompt_confirm = true;
|
||||||
|
app.data.readarr_data.delete_files = true;
|
||||||
|
app.data.readarr_data.add_import_list_exclusion = true;
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
ESC_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_navigation_popped!(app, ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
assert!(!app.data.readarr_data.prompt_confirm);
|
||||||
|
assert!(!app.data.readarr_data.delete_files);
|
||||||
|
assert!(!app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod test_handle_key_char {
|
||||||
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
|
use crate::assert_navigation_popped;
|
||||||
|
use crate::models::BlockSelectionState;
|
||||||
|
use crate::models::servarr_data::readarr::readarr_data::DELETE_BOOK_SELECTION_BLOCKS;
|
||||||
|
use crate::network::readarr_network::ReadarrEvent;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const CONFIRM_KEY: Key = DEFAULT_KEYBINDINGS.confirm.key;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_confirm_prompt_prompt_confirm() {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::DeleteBookPrompt.into());
|
||||||
|
app.data.readarr_data.delete_files = true;
|
||||||
|
app.data.readarr_data.add_import_list_exclusion = false;
|
||||||
|
app.data.readarr_data.books.set_items(vec![book()]);
|
||||||
|
let expected_delete_book_params = DeleteParams {
|
||||||
|
id: 42,
|
||||||
|
delete_files: true,
|
||||||
|
add_import_list_exclusion: false,
|
||||||
|
};
|
||||||
|
app.data.readarr_data.selected_block = BlockSelectionState::new(DELETE_BOOK_SELECTION_BLOCKS);
|
||||||
|
app
|
||||||
|
.data
|
||||||
|
.readarr_data
|
||||||
|
.selected_block
|
||||||
|
.set_index(0, DELETE_BOOK_SELECTION_BLOCKS.len() - 1);
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
CONFIRM_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_navigation_popped!(app, ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
assert_eq!(
|
||||||
|
app.data.readarr_data.prompt_confirm_action,
|
||||||
|
Some(ReadarrEvent::DeleteBook(expected_delete_book_params))
|
||||||
|
);
|
||||||
|
assert!(app.should_refresh);
|
||||||
|
assert!(app.data.readarr_data.prompt_confirm);
|
||||||
|
assert!(!app.data.readarr_data.delete_files);
|
||||||
|
assert!(!app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_confirm_prompt_prompt_confirm_no_op_when_not_ready() {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.is_loading = true;
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::AuthorDetails.into());
|
||||||
|
app.push_navigation_stack(ActiveReadarrBlock::DeleteBookPrompt.into());
|
||||||
|
app.data.readarr_data.delete_files = true;
|
||||||
|
app.data.readarr_data.add_import_list_exclusion = false;
|
||||||
|
app.data.readarr_data.books.set_items(vec![book()]);
|
||||||
|
app.data.readarr_data.selected_block = BlockSelectionState::new(DELETE_BOOK_SELECTION_BLOCKS);
|
||||||
|
app
|
||||||
|
.data
|
||||||
|
.readarr_data
|
||||||
|
.selected_block
|
||||||
|
.set_index(0, DELETE_BOOK_SELECTION_BLOCKS.len() - 1);
|
||||||
|
|
||||||
|
DeleteBookHandler::new(
|
||||||
|
CONFIRM_KEY,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.handle();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
app.get_current_route(),
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt.into()
|
||||||
|
);
|
||||||
|
assert_none!(app.data.readarr_data.prompt_confirm_action);
|
||||||
|
assert!(!app.should_refresh);
|
||||||
|
assert!(!app.data.readarr_data.prompt_confirm);
|
||||||
|
assert!(app.data.readarr_data.delete_files);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_handler_accepts() {
|
||||||
|
ActiveReadarrBlock::iter().for_each(|active_readarr_block| {
|
||||||
|
if DELETE_BOOK_BLOCKS.contains(&active_readarr_block) {
|
||||||
|
assert!(DeleteBookHandler::accepts(active_readarr_block));
|
||||||
|
} else {
|
||||||
|
assert!(!DeleteBookHandler::accepts(active_readarr_block));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
fn test_delete_book_handler_ignore_special_keys(
|
||||||
|
#[values(true, false)] ignore_special_keys_for_textbox_input: bool,
|
||||||
|
) {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.ignore_special_keys_for_textbox_input = ignore_special_keys_for_textbox_input;
|
||||||
|
let handler = DeleteBookHandler::new(
|
||||||
|
DEFAULT_KEYBINDINGS.esc.key,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::default(),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
handler.ignore_special_keys(),
|
||||||
|
ignore_special_keys_for_textbox_input
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build_delete_book_params() {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.data.readarr_data.books.set_items(vec![book()]);
|
||||||
|
app.data.readarr_data.delete_files = true;
|
||||||
|
app.data.readarr_data.add_import_list_exclusion = false;
|
||||||
|
let expected_delete_book_params = DeleteParams {
|
||||||
|
id: 42,
|
||||||
|
delete_files: true,
|
||||||
|
add_import_list_exclusion: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
let delete_book_params = DeleteBookHandler::new(
|
||||||
|
DEFAULT_KEYBINDINGS.esc.key,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.build_delete_book_params();
|
||||||
|
|
||||||
|
assert_eq!(delete_book_params, expected_delete_book_params);
|
||||||
|
assert!(!app.data.readarr_data.delete_files);
|
||||||
|
assert!(!app.data.readarr_data.add_import_list_exclusion);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_handler_not_ready_when_loading() {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.is_loading = true;
|
||||||
|
|
||||||
|
let handler = DeleteBookHandler::new(
|
||||||
|
DEFAULT_KEYBINDINGS.esc.key,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(!handler.is_ready());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_book_handler_ready_when_not_loading() {
|
||||||
|
let mut app = App::test_default();
|
||||||
|
app.is_loading = false;
|
||||||
|
|
||||||
|
let handler = DeleteBookHandler::new(
|
||||||
|
DEFAULT_KEYBINDINGS.esc.key,
|
||||||
|
&mut app,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt,
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(handler.is_ready());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn book() -> Book {
|
||||||
|
Book {
|
||||||
|
id: 42,
|
||||||
|
..Book::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,8 @@ mod tests {
|
|||||||
use crate::models::readarr_models::{Author, AuthorStatistics, AuthorStatus};
|
use crate::models::readarr_models::{Author, AuthorStatistics, AuthorStatus};
|
||||||
use crate::models::servarr_data::readarr::readarr_data::{
|
use crate::models::servarr_data::readarr::readarr_data::{
|
||||||
ADD_AUTHOR_BLOCKS, AUTHOR_DETAILS_BLOCKS, ActiveReadarrBlock, BOOK_DETAILS_BLOCKS,
|
ADD_AUTHOR_BLOCKS, AUTHOR_DETAILS_BLOCKS, ActiveReadarrBlock, BOOK_DETAILS_BLOCKS,
|
||||||
DELETE_AUTHOR_BLOCKS, EDIT_AUTHOR_BLOCKS, EDITION_DETAILS_BLOCKS, LIBRARY_BLOCKS,
|
DELETE_AUTHOR_BLOCKS, DELETE_BOOK_BLOCKS, EDIT_AUTHOR_BLOCKS, EDITION_DETAILS_BLOCKS,
|
||||||
|
LIBRARY_BLOCKS,
|
||||||
};
|
};
|
||||||
use crate::test_handler_delegation;
|
use crate::test_handler_delegation;
|
||||||
|
|
||||||
@@ -907,6 +908,15 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delegates_delete_book_blocks_to_delete_book_handler() {
|
||||||
|
test_handler_delegation!(
|
||||||
|
LibraryHandler,
|
||||||
|
ActiveReadarrBlock::Authors,
|
||||||
|
ActiveReadarrBlock::DeleteBookPrompt
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_delegates_edit_author_blocks_to_edit_author_handler() {
|
fn test_delegates_edit_author_blocks_to_edit_author_handler() {
|
||||||
test_handler_delegation!(
|
test_handler_delegation!(
|
||||||
@@ -925,6 +935,7 @@ mod tests {
|
|||||||
library_handler_blocks.extend(EDITION_DETAILS_BLOCKS);
|
library_handler_blocks.extend(EDITION_DETAILS_BLOCKS);
|
||||||
library_handler_blocks.extend(ADD_AUTHOR_BLOCKS);
|
library_handler_blocks.extend(ADD_AUTHOR_BLOCKS);
|
||||||
library_handler_blocks.extend(DELETE_AUTHOR_BLOCKS);
|
library_handler_blocks.extend(DELETE_AUTHOR_BLOCKS);
|
||||||
|
library_handler_blocks.extend(DELETE_BOOK_BLOCKS);
|
||||||
library_handler_blocks.extend(EDIT_AUTHOR_BLOCKS);
|
library_handler_blocks.extend(EDIT_AUTHOR_BLOCKS);
|
||||||
|
|
||||||
ActiveReadarrBlock::iter().for_each(|readarr_block| {
|
ActiveReadarrBlock::iter().for_each(|readarr_block| {
|
||||||
|
|||||||
@@ -23,13 +23,14 @@ mod add_author_handler;
|
|||||||
mod author_details_handler;
|
mod author_details_handler;
|
||||||
mod book_details_handler;
|
mod book_details_handler;
|
||||||
mod delete_author_handler;
|
mod delete_author_handler;
|
||||||
|
mod delete_book_handler;
|
||||||
mod edit_author_handler;
|
mod edit_author_handler;
|
||||||
mod edition_details_handler;
|
mod edition_details_handler;
|
||||||
|
|
||||||
pub(in crate::handlers::readarr_handlers) use {
|
pub(in crate::handlers::readarr_handlers) use {
|
||||||
add_author_handler::AddAuthorHandler, author_details_handler::AuthorDetailsHandler,
|
add_author_handler::AddAuthorHandler, author_details_handler::AuthorDetailsHandler,
|
||||||
book_details_handler::BookDetailsHandler, delete_author_handler::DeleteAuthorHandler,
|
book_details_handler::BookDetailsHandler, delete_author_handler::DeleteAuthorHandler,
|
||||||
edit_author_handler::EditAuthorHandler,
|
delete_book_handler::DeleteBookHandler, edit_author_handler::EditAuthorHandler,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -84,6 +85,10 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveReadarrBlock> for LibraryHandler<'a,
|
|||||||
AuthorDetailsHandler::new(self.key, self.app, self.active_readarr_block, self.context)
|
AuthorDetailsHandler::new(self.key, self.app, self.active_readarr_block, self.context)
|
||||||
.handle();
|
.handle();
|
||||||
}
|
}
|
||||||
|
_ if DeleteBookHandler::accepts(self.active_readarr_block) => {
|
||||||
|
DeleteBookHandler::new(self.key, self.app, self.active_readarr_block, self.context)
|
||||||
|
.handle();
|
||||||
|
}
|
||||||
_ if BookDetailsHandler::accepts(self.active_readarr_block) => {
|
_ if BookDetailsHandler::accepts(self.active_readarr_block) => {
|
||||||
BookDetailsHandler::new(self.key, self.app, self.active_readarr_block, self.context)
|
BookDetailsHandler::new(self.key, self.app, self.active_readarr_block, self.context)
|
||||||
.handle();
|
.handle();
|
||||||
@@ -96,6 +101,7 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveReadarrBlock> for LibraryHandler<'a,
|
|||||||
fn accepts(active_block: ActiveReadarrBlock) -> bool {
|
fn accepts(active_block: ActiveReadarrBlock) -> bool {
|
||||||
AddAuthorHandler::accepts(active_block)
|
AddAuthorHandler::accepts(active_block)
|
||||||
|| DeleteAuthorHandler::accepts(active_block)
|
|| DeleteAuthorHandler::accepts(active_block)
|
||||||
|
|| DeleteBookHandler::accepts(active_block)
|
||||||
|| EditAuthorHandler::accepts(active_block)
|
|| EditAuthorHandler::accepts(active_block)
|
||||||
|| AuthorDetailsHandler::accepts(active_block)
|
|| AuthorDetailsHandler::accepts(active_block)
|
||||||
|| BookDetailsHandler::accepts(active_block)
|
|| BookDetailsHandler::accepts(active_block)
|
||||||
|
|||||||
Reference in new issue
Block a user