Refactored to require handlers to specify the components they rely on and to specify when they are ready. This fixes a lot of bugs with the UI when users try to press buttons while the application is still loading.

This commit is contained in:
2024-07-17 19:55:10 -06:00
parent 9104b7c356
commit d84e7dfcab
49 changed files with 5143 additions and 265 deletions
@@ -12,6 +12,7 @@ mod tests {
use crate::models::servarr_data::radarr::radarr_data::{
ActiveRadarrBlock, SYSTEM_DETAILS_BLOCKS,
};
use crate::models::{HorizontallyScrollableText, ScrollableText};
mod test_handle_scroll_up_and_down {
use rstest::rstest;
@@ -31,25 +32,170 @@ mod tests {
text
);
test_iterable_scroll!(
test_tasks_scroll,
SystemDetailsHandler,
tasks,
simple_stateful_iterable_vec!(Task, String, name),
ActiveRadarrBlock::SystemTasks,
None,
name
);
#[rstest]
fn test_log_details_scroll_no_op_when_not_ready(
#[values(DEFAULT_KEYBINDINGS.up.key, DEFAULT_KEYBINDINGS.down.key)] key: Key,
) {
let mut app = App::default();
app.is_loading = true;
app
.data
.radarr_data
.log_details
.set_items(simple_stateful_iterable_vec!(
HorizontallyScrollableText,
String,
text
));
test_iterable_scroll!(
test_queued_events_scroll,
SystemDetailsHandler,
queued_events,
simple_stateful_iterable_vec!(QueueEvent, String, name),
ActiveRadarrBlock::SystemQueuedEvents,
None,
name
);
SystemDetailsHandler::with(&key, &mut app, &ActiveRadarrBlock::SystemLogs, &None).handle();
assert_str_eq!(
app.data.radarr_data.log_details.current_selection().text,
"Test 1"
);
SystemDetailsHandler::with(&key, &mut app, &ActiveRadarrBlock::SystemLogs, &None).handle();
assert_str_eq!(
app.data.radarr_data.log_details.current_selection().text,
"Test 1"
);
}
#[rstest]
fn test_tasks_scroll(
#[values(DEFAULT_KEYBINDINGS.up.key, DEFAULT_KEYBINDINGS.down.key)] key: Key,
) {
let mut app = App::default();
app.data.radarr_data.updates = ScrollableText::with_string("Test 1\nTest 2".to_owned());
app
.data
.radarr_data
.tasks
.set_items(simple_stateful_iterable_vec!(Task, String, name));
SystemDetailsHandler::with(&key, &mut app, &ActiveRadarrBlock::SystemTasks, &None).handle();
assert_str_eq!(
app.data.radarr_data.tasks.current_selection().name,
"Test 2"
);
SystemDetailsHandler::with(&key, &mut app, &ActiveRadarrBlock::SystemTasks, &None).handle();
assert_str_eq!(
app.data.radarr_data.tasks.current_selection().name,
"Test 1"
);
}
#[rstest]
fn test_tasks_scroll_no_op_when_not_ready(
#[values(DEFAULT_KEYBINDINGS.up.key, DEFAULT_KEYBINDINGS.down.key)] key: Key,
) {
let mut app = App::default();
app.is_loading = true;
app.data.radarr_data.updates = ScrollableText::with_string("Test 1\nTest 2".to_owned());
app
.data
.radarr_data
.tasks
.set_items(simple_stateful_iterable_vec!(Task, String, name));
SystemDetailsHandler::with(&key, &mut app, &ActiveRadarrBlock::SystemTasks, &None).handle();
assert_str_eq!(
app.data.radarr_data.tasks.current_selection().name,
"Test 1"
);
SystemDetailsHandler::with(&key, &mut app, &ActiveRadarrBlock::SystemTasks, &None).handle();
assert_str_eq!(
app.data.radarr_data.tasks.current_selection().name,
"Test 1"
);
}
#[rstest]
fn test_queued_events_scroll(
#[values(DEFAULT_KEYBINDINGS.up.key, DEFAULT_KEYBINDINGS.down.key)] key: Key,
) {
let mut app = App::default();
app.data.radarr_data.updates = ScrollableText::with_string("Test 1\nTest 2".to_owned());
app
.data
.radarr_data
.queued_events
.set_items(simple_stateful_iterable_vec!(QueueEvent, String, name));
SystemDetailsHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::SystemQueuedEvents,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.queued_events.current_selection().name,
"Test 2"
);
SystemDetailsHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::SystemQueuedEvents,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.queued_events.current_selection().name,
"Test 1"
);
}
#[rstest]
fn test_queued_events_scroll_no_op_when_not_ready(
#[values(DEFAULT_KEYBINDINGS.up.key, DEFAULT_KEYBINDINGS.down.key)] key: Key,
) {
let mut app = App::default();
app.is_loading = true;
app.data.radarr_data.updates = ScrollableText::with_string("Test 1\nTest 2".to_owned());
app
.data
.radarr_data
.queued_events
.set_items(simple_stateful_iterable_vec!(QueueEvent, String, name));
SystemDetailsHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::SystemQueuedEvents,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.queued_events.current_selection().name,
"Test 1"
);
SystemDetailsHandler::with(
&key,
&mut app,
&ActiveRadarrBlock::SystemQueuedEvents,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.queued_events.current_selection().name,
"Test 1"
);
}
#[test]
fn test_system_updates_scroll() {
@@ -76,6 +222,33 @@ mod tests {
assert_eq!(app.data.radarr_data.updates.offset, 1);
}
#[test]
fn test_system_updates_scroll_no_op_when_not_ready() {
let mut app = App::default();
app.is_loading = true;
app.data.radarr_data.updates = ScrollableText::with_string("Test 1\nTest 2".to_owned());
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.up.key,
&mut app,
&ActiveRadarrBlock::SystemUpdates,
&None,
)
.handle();
assert_eq!(app.data.radarr_data.updates.offset, 0);
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.down.key,
&mut app,
&ActiveRadarrBlock::SystemUpdates,
&None,
)
.handle();
assert_eq!(app.data.radarr_data.updates.offset, 0);
}
}
mod test_handle_home_end {
@@ -94,25 +267,200 @@ mod tests {
text
);
test_iterable_home_and_end!(
test_tasks_home_end,
SystemDetailsHandler,
tasks,
extended_stateful_iterable_vec!(Task, String, name),
ActiveRadarrBlock::SystemTasks,
None,
name
);
#[test]
fn test_log_details_home_end_no_op_when_not_ready() {
let mut app = App::default();
app.is_loading = true;
app
.data
.radarr_data
.log_details
.set_items(extended_stateful_iterable_vec!(
HorizontallyScrollableText,
String,
text
));
test_iterable_home_and_end!(
test_queued_events_home_end,
SystemDetailsHandler,
queued_events,
extended_stateful_iterable_vec!(QueueEvent, String, name),
ActiveRadarrBlock::SystemQueuedEvents,
None,
name
);
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.end.key,
&mut app,
&ActiveRadarrBlock::SystemLogs,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.log_details.current_selection().text,
"Test 1"
);
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.home.key,
&mut app,
&ActiveRadarrBlock::SystemLogs,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.log_details.current_selection().text,
"Test 1"
);
}
#[test]
fn test_tasks_home_end() {
let mut app = App::default();
app.data.radarr_data.updates =
ScrollableText::with_string("Test 1\nTest 2\nTest 3".to_owned());
app
.data
.radarr_data
.tasks
.set_items(extended_stateful_iterable_vec!(Task, String, name));
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.end.key,
&mut app,
&ActiveRadarrBlock::SystemTasks,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.tasks.current_selection().name,
"Test 3"
);
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.home.key,
&mut app,
&ActiveRadarrBlock::SystemTasks,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.tasks.current_selection().name,
"Test 1"
);
}
#[test]
fn test_tasks_home_end_no_op_when_not_ready() {
let mut app = App::default();
app.is_loading = true;
app.data.radarr_data.updates =
ScrollableText::with_string("Test 1\nTest 2\nTest 3".to_owned());
app
.data
.radarr_data
.tasks
.set_items(extended_stateful_iterable_vec!(Task, String, name));
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.end.key,
&mut app,
&ActiveRadarrBlock::SystemTasks,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.tasks.current_selection().name,
"Test 1"
);
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.home.key,
&mut app,
&ActiveRadarrBlock::SystemTasks,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.tasks.current_selection().name,
"Test 1"
);
}
#[test]
fn test_queued_events_home_end() {
let mut app = App::default();
app.data.radarr_data.updates =
ScrollableText::with_string("Test 1\nTest 2\nTest 3".to_owned());
app
.data
.radarr_data
.queued_events
.set_items(extended_stateful_iterable_vec!(QueueEvent, String, name));
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.end.key,
&mut app,
&ActiveRadarrBlock::SystemQueuedEvents,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.queued_events.current_selection().name,
"Test 3"
);
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.home.key,
&mut app,
&ActiveRadarrBlock::SystemQueuedEvents,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.queued_events.current_selection().name,
"Test 1"
);
}
#[test]
fn test_queued_events_home_end_no_op_when_not_ready() {
let mut app = App::default();
app.is_loading = true;
app.data.radarr_data.updates =
ScrollableText::with_string("Test 1\nTest 2\nTest 3".to_owned());
app
.data
.radarr_data
.queued_events
.set_items(extended_stateful_iterable_vec!(QueueEvent, String, name));
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.end.key,
&mut app,
&ActiveRadarrBlock::SystemQueuedEvents,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.queued_events.current_selection().name,
"Test 1"
);
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.home.key,
&mut app,
&ActiveRadarrBlock::SystemQueuedEvents,
&None,
)
.handle();
assert_str_eq!(
app.data.radarr_data.queued_events.current_selection().name,
"Test 1"
);
}
#[test]
fn test_system_updates_home_end() {
@@ -139,6 +487,33 @@ mod tests {
assert_eq!(app.data.radarr_data.updates.offset, 0);
}
#[test]
fn test_system_updates_home_end_no_op_when_not_ready() {
let mut app = App::default();
app.is_loading = true;
app.data.radarr_data.updates = ScrollableText::with_string("Test 1\nTest 2".to_owned());
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.end.key,
&mut app,
&ActiveRadarrBlock::SystemUpdates,
&None,
)
.handle();
assert_eq!(app.data.radarr_data.updates.offset, 0);
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.home.key,
&mut app,
&ActiveRadarrBlock::SystemUpdates,
&None,
)
.handle();
assert_eq!(app.data.radarr_data.updates.offset, 0);
}
}
mod test_handle_left_right_action {
@@ -286,6 +661,7 @@ mod tests {
#[test]
fn test_system_tasks_submit() {
let mut app = App::default();
app.data.radarr_data.updates = ScrollableText::with_string("Test".to_owned());
SystemDetailsHandler::with(
&SUBMIT_KEY,
@@ -301,9 +677,31 @@ mod tests {
);
}
#[test]
fn test_system_tasks_submit_no_op_when_not_ready() {
let mut app = App::default();
app.is_loading = true;
app.push_navigation_stack(ActiveRadarrBlock::SystemTasks.into());
app.data.radarr_data.updates = ScrollableText::with_string("Test".to_owned());
SystemDetailsHandler::with(
&SUBMIT_KEY,
&mut app,
&ActiveRadarrBlock::SystemTasks,
&None,
)
.handle();
assert_eq!(
app.get_current_route(),
&ActiveRadarrBlock::SystemTasks.into()
);
}
#[test]
fn test_system_tasks_start_task_prompt_confirm_submit() {
let mut app = App::default();
app.data.radarr_data.updates = ScrollableText::with_string("Test".to_owned());
app.data.radarr_data.prompt_confirm = true;
app.push_navigation_stack(ActiveRadarrBlock::SystemTasks.into());
app.push_navigation_stack(ActiveRadarrBlock::SystemTaskStartConfirmPrompt.into());
@@ -330,6 +728,7 @@ mod tests {
#[test]
fn test_system_tasks_start_task_prompt_decline_submit() {
let mut app = App::default();
app.data.radarr_data.updates = ScrollableText::with_string("Test".to_owned());
app.push_navigation_stack(ActiveRadarrBlock::SystemTasks.into());
app.push_navigation_stack(ActiveRadarrBlock::SystemTaskStartConfirmPrompt.into());
@@ -351,17 +750,18 @@ mod tests {
}
mod test_handle_esc {
use pretty_assertions::assert_eq;
use crate::models::HorizontallyScrollableText;
use pretty_assertions::assert_eq;
use rstest::rstest;
use super::*;
const ESC_KEY: Key = DEFAULT_KEYBINDINGS.esc.key;
#[test]
fn test_esc_system_logs() {
#[rstest]
fn test_esc_system_logs(#[values(true, false)] is_ready: bool) {
let mut app = App::default();
app.is_loading = is_ready;
app
.data
.radarr_data
@@ -382,9 +782,10 @@ mod tests {
assert!(app.data.radarr_data.log_details.items.is_empty());
}
#[test]
fn test_esc_system_tasks() {
#[rstest]
fn test_esc_system_tasks(#[values(true, false)] is_ready: bool) {
let mut app = App::default();
app.is_loading = is_ready;
app.push_navigation_stack(ActiveRadarrBlock::System.into());
app.push_navigation_stack(ActiveRadarrBlock::SystemTasks.into());
app.data.radarr_data.tasks.set_items(vec![Task::default()]);
@@ -395,9 +796,10 @@ mod tests {
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::System.into());
}
#[test]
fn test_esc_system_queued_events() {
#[rstest]
fn test_esc_system_queued_events(#[values(true, false)] is_ready: bool) {
let mut app = App::default();
app.is_loading = is_ready;
app.push_navigation_stack(ActiveRadarrBlock::System.into());
app.push_navigation_stack(ActiveRadarrBlock::SystemQueuedEvents.into());
app
@@ -417,9 +819,10 @@ mod tests {
assert_eq!(app.get_current_route(), &ActiveRadarrBlock::System.into());
}
#[test]
fn test_esc_system_updates() {
#[rstest]
fn test_esc_system_updates(#[values(true, false)] is_ready: bool) {
let mut app = App::default();
app.is_loading = is_ready;
app.push_navigation_stack(ActiveRadarrBlock::System.into());
app.push_navigation_stack(ActiveRadarrBlock::SystemUpdates.into());
@@ -468,6 +871,7 @@ mod tests {
active_radarr_block: ActiveRadarrBlock,
) {
let mut app = App::default();
app.data.radarr_data.updates = ScrollableText::with_string("Test".to_owned());
app.push_navigation_stack(active_radarr_block.into());
SystemDetailsHandler::with(
@@ -481,6 +885,33 @@ mod tests {
assert_eq!(app.get_current_route(), &active_radarr_block.into());
assert!(app.should_refresh);
}
#[rstest]
fn test_refresh_key_no_op_when_not_ready(
#[values(
ActiveRadarrBlock::SystemLogs,
ActiveRadarrBlock::SystemTasks,
ActiveRadarrBlock::SystemQueuedEvents,
ActiveRadarrBlock::SystemUpdates
)]
active_radarr_block: ActiveRadarrBlock,
) {
let mut app = App::default();
app.is_loading = true;
app.data.radarr_data.updates = ScrollableText::with_string("Test".to_owned());
app.push_navigation_stack(active_radarr_block.into());
SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.refresh.key,
&mut app,
&active_radarr_block,
&None,
)
.handle();
assert_eq!(app.get_current_route(), &active_radarr_block.into());
assert!(!app.should_refresh);
}
}
#[test]
@@ -493,4 +924,70 @@ mod tests {
}
})
}
#[test]
fn test_system_details_handler_not_ready_when_loading() {
let mut app = App::default();
app.is_loading = true;
let handler = SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.esc.key,
&mut app,
&ActiveRadarrBlock::SystemUpdates,
&None,
);
assert!(!handler.is_ready());
}
#[test]
fn test_system_details_handler_not_ready_when_both_log_details_and_updates_are_empty() {
let mut app = App::default();
app.is_loading = false;
let handler = SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.esc.key,
&mut app,
&ActiveRadarrBlock::SystemUpdates,
&None,
);
assert!(!handler.is_ready());
}
#[test]
fn test_system_details_handler_ready_when_not_loading_and_log_details_is_not_empty() {
let mut app = App::default();
app.is_loading = false;
app
.data
.radarr_data
.log_details
.set_items(vec![HorizontallyScrollableText::default()]);
let handler = SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.esc.key,
&mut app,
&ActiveRadarrBlock::SystemUpdates,
&None,
);
assert!(handler.is_ready());
}
#[test]
fn test_system_details_handler_ready_when_not_loading_and_updates_is_not_empty() {
let mut app = App::default();
app.is_loading = false;
app.data.radarr_data.updates = ScrollableText::with_string("Test".to_owned());
let handler = SystemDetailsHandler::with(
&DEFAULT_KEYBINDINGS.esc.key,
&mut app,
&ActiveRadarrBlock::SystemUpdates,
&None,
);
assert!(handler.is_ready());
}
}