Skip to content

Commit

Permalink
fix(sonarr): Provide the task name directly alongside all StartTask e…
Browse files Browse the repository at this point in the history
…vents when publishing to the networking channel
  • Loading branch information
Dark-Alex-17 committed Dec 18, 2024
1 parent 1193b8c commit 42479ce
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/cli/sonarr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 252,7 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrCommand> for SonarrCliHandler<'a, '
SonarrCommand::StartTask { task_name } => {
let resp = self
.network
.handle_network_event(SonarrEvent::StartTask(Some(task_name)).into())
.handle_network_event(SonarrEvent::StartTask(task_name).into())
.await?;
serde_json::to_string_pretty(&resp)?
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/sonarr/sonarr_command_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 624,7 @@ mod tests {
mock_network
.expect_handle_network_event()
.with(eq::<NetworkEvent>(
SonarrEvent::StartTask(Some(expected_task_name)).into(),
SonarrEvent::StartTask(expected_task_name).into(),
))
.times(1)
.returning(|_| {
Expand Down
19 changes: 17 additions & 2 deletions src/handlers/sonarr_handlers/system/system_details_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 3,7 @@ use crate::app::App;
use crate::event::Key;
use crate::handlers::{handle_prompt_toggle, KeyEventHandler};
use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, SYSTEM_DETAILS_BLOCKS};
use crate::models::sonarr_models::SonarrTaskName;
use crate::models::stateful_list::StatefulList;
use crate::models::Scrollable;
use crate::network::sonarr_network::SonarrEvent;
Expand All @@ -18,6 19,18 @@ pub(super) struct SystemDetailsHandler<'a, 'b> {
_context: Option<ActiveSonarrBlock>,
}

impl<'a, 'b> SystemDetailsHandler<'a, 'b> {
fn extract_task_name(&self) -> SonarrTaskName {
self
.app
.data
.sonarr_data
.tasks
.current_selection()
.task_name
}
}

impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for SystemDetailsHandler<'a, 'b> {
fn accepts(active_block: ActiveSonarrBlock) -> bool {
SYSTEM_DETAILS_BLOCKS.contains(&active_block)
Expand Down Expand Up @@ -137,7 150,8 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for SystemDetailsHandler
}
ActiveSonarrBlock::SystemTaskStartConfirmPrompt => {
if self.app.data.sonarr_data.prompt_confirm {
self.app.data.sonarr_data.prompt_confirm_action = Some(SonarrEvent::StartTask(None));
self.app.data.sonarr_data.prompt_confirm_action =
Some(SonarrEvent::StartTask(self.extract_task_name()));
}

self.app.pop_navigation_stack();
Expand Down Expand Up @@ -174,7 188,8 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for SystemDetailsHandler
&& self.key == DEFAULT_KEYBINDINGS.confirm.key
{
self.app.data.sonarr_data.prompt_confirm = true;
self.app.data.sonarr_data.prompt_confirm_action = Some(SonarrEvent::StartTask(None));
self.app.data.sonarr_data.prompt_confirm_action =
Some(SonarrEvent::StartTask(self.extract_task_name()));
self.app.pop_navigation_stack();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use pretty_assertions::{assert_eq, assert_str_eq};
use strum::IntoEnumIterator;

use crate::app::key_binding::DEFAULT_KEYBINDINGS;
Expand All @@ -12,10 12,11 @@ mod tests {
ActiveSonarrBlock, SYSTEM_DETAILS_BLOCKS,
};
use crate::models::servarr_models::QueueEvent;
use crate::models::sonarr_models::SonarrTask;
use crate::models::sonarr_models::{SonarrTask, SonarrTaskName};
use crate::models::{HorizontallyScrollableText, ScrollableText};

mod test_handle_scroll_up_and_down {
use pretty_assertions::assert_eq;
use rstest::rstest;

use crate::models::{HorizontallyScrollableText, ScrollableText};
Expand Down Expand Up @@ -245,6 246,7 @@ mod tests {
use crate::{extended_stateful_iterable_vec, test_iterable_home_and_end};

use super::*;
use pretty_assertions::assert_eq;

test_iterable_home_and_end!(
test_log_details_home_end,
Expand Down Expand Up @@ -693,6 695,11 @@ mod tests {
let mut app = App::default();
app.data.sonarr_data.updates = ScrollableText::with_string("Test".to_owned());
app.data.sonarr_data.prompt_confirm = true;
app
.data
.sonarr_data
.tasks
.set_items(vec![SonarrTask::default()]);
app.push_navigation_stack(ActiveSonarrBlock::SystemTasks.into());
app.push_navigation_stack(ActiveSonarrBlock::SystemTaskStartConfirmPrompt.into());

Expand All @@ -707,7 714,7 @@ mod tests {
assert!(app.data.sonarr_data.prompt_confirm);
assert_eq!(
app.data.sonarr_data.prompt_confirm_action,
Some(SonarrEvent::StartTask(None))
Some(SonarrEvent::StartTask(SonarrTaskName::default()))
);
assert_eq!(
app.get_current_route(),
Expand Down Expand Up @@ -848,6 855,7 @@ mod tests {
}

mod test_handle_key_char {
use pretty_assertions::assert_eq;
use rstest::rstest;

use crate::network::sonarr_network::SonarrEvent;
Expand Down Expand Up @@ -914,6 922,11 @@ mod tests {
let mut app = App::default();
app.push_navigation_stack(ActiveSonarrBlock::System.into());
app.data.sonarr_data.updates = ScrollableText::with_string("Test".to_owned());
app
.data
.sonarr_data
.tasks
.set_items(vec![SonarrTask::default()]);
app.push_navigation_stack(ActiveSonarrBlock::SystemTasks.into());
app.push_navigation_stack(ActiveSonarrBlock::SystemTaskStartConfirmPrompt.into());

Expand All @@ -928,7 941,7 @@ mod tests {
assert!(app.data.sonarr_data.prompt_confirm);
assert_eq!(
app.data.sonarr_data.prompt_confirm_action,
Some(SonarrEvent::StartTask(None))
Some(SonarrEvent::StartTask(SonarrTaskName::default()))
);
assert_eq!(
app.get_current_route(),
Expand All @@ -948,6 961,26 @@ mod tests {
})
}

#[test]
fn test_extract_task_name() {
let mut app = App::default();
app
.data
.sonarr_data
.tasks
.set_items(vec![SonarrTask::default()]);

let task_name = SystemDetailsHandler::with(
DEFAULT_KEYBINDINGS.esc.key,
&mut app,
ActiveSonarrBlock::SystemTasks,
None,
)
.extract_task_name();

assert_eq!(task_name, SonarrTaskName::default());
}

#[test]
fn test_system_details_handler_not_ready_when_loading() {
let mut app = App::default();
Expand Down
22 changes: 4 additions & 18 deletions src/network/sonarr_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 83,7 @@ pub enum SonarrEvent {
ListSeries,
MarkHistoryItemAsFailed(i64),
SearchNewSeries(String),
StartTask(Option<SonarrTaskName>),
StartTask(SonarrTaskName),
TestIndexer(Option<i64>),
TestAllIndexers,
ToggleSeasonMonitoring(Option<(i64, i64)>),
Expand Down Expand Up @@ -2025,23 2025,9 @@ impl<'a, 'b> Network<'a, 'b> {
.await
}

async fn start_sonarr_task(&mut self, task: Option<SonarrTaskName>) -> Result<Value> {
let event = SonarrEvent::StartTask(None);
let task_name = if let Some(t_name) = task {
t_name
} else {
self
.app
.lock()
.await
.data
.sonarr_data
.tasks
.current_selection()
.task_name
}
.to_string();

async fn start_sonarr_task(&mut self, task: SonarrTaskName) -> Result<Value> {
let event = SonarrEvent::StartTask(task);
let task_name = task.to_string();
info!("Starting Sonarr task: {task_name}");

let body = CommandBody { name: task_name };
Expand Down
35 changes: 5 additions & 30 deletions src/network/sonarr_network_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 192,7 @@ mod test {
fn test_resource_command(
#[values(
SonarrEvent::GetQueuedEvents,
SonarrEvent::StartTask(None),
SonarrEvent::StartTask(SonarrTaskName::default()),
SonarrEvent::TriggerAutomaticEpisodeSearch(None),
SonarrEvent::TriggerAutomaticSeasonSearch(None),
SonarrEvent::TriggerAutomaticSeriesSearch(None),
Expand Down Expand Up @@ -4763,7 4763,7 @@ mod test {
})),
Some(response.clone()),
None,
SonarrEvent::StartTask(None),
SonarrEvent::StartTask(SonarrTaskName::ApplicationUpdateCheck),
None,
None,
)
Expand All @@ -4781,34 4781,9 @@ mod test {
let mut network = Network::new(&app_arc, CancellationToken::new(), Client::new());

if let SonarrSerdeable::Value(value) = network
.handle_sonarr_event(SonarrEvent::StartTask(None))
.await
.unwrap()
{
async_server.assert_async().await;
assert_eq!(value, response);
}
}

#[tokio::test]
async fn test_handle_start_sonarr_task_event_uses_provided_task_name() {
let response = json!({ "test": "test"});
let (async_server, app_arc, _server) = mock_servarr_api(
RequestMethod::Post,
Some(json!({
"name": "ApplicationUpdateCheck"
})),
Some(response.clone()),
None,
SonarrEvent::StartTask(None),
None,
None,
)
.await;
let mut network = Network::new(&app_arc, CancellationToken::new(), Client::new());

if let SonarrSerdeable::Value(value) = network
.handle_sonarr_event(SonarrEvent::StartTask(Some(SonarrTaskName::default())))
.handle_sonarr_event(SonarrEvent::StartTask(
SonarrTaskName::ApplicationUpdateCheck,
))
.await
.unwrap()
{
Expand Down

0 comments on commit 42479ce

Please sign in to comment.