feat(cli): Support for adding a series to Sonarr

This commit is contained in:
2024-11-24 14:29:13 -07:00
parent 5ba3f2b1ba
commit 8125bd5ae0
12 changed files with 523 additions and 51 deletions
+93 -1
View File
@@ -1,12 +1,13 @@
use std::sync::Arc;
use anyhow::Result;
use clap::Subcommand;
use clap::{ArgAction, Subcommand};
use tokio::sync::Mutex;
use crate::{
app::App,
cli::{CliCommandHandler, Command},
models::sonarr_models::{AddSeriesBody, AddSeriesOptions, SeriesMonitor, SeriesType},
network::{sonarr_network::SonarrEvent, NetworkTrait},
};
@@ -18,6 +19,63 @@ mod add_command_handler_tests;
#[derive(Debug, Clone, PartialEq, Eq, Subcommand)]
pub enum SonarrAddCommand {
#[command(about = "Add a new series to your Sonarr library")]
Series {
#[arg(
long,
help = "The TVDB ID of the series you wish to add to your library",
required = true
)]
tvdb_id: i64,
#[arg(
long,
help = "The root folder path where all series data and metadata should live",
required = true
)]
root_folder_path: String,
#[arg(
long,
help = "The ID of the quality profile to use for this series",
required = true
)]
quality_profile_id: i64,
#[arg(
long,
help = "The ID of the language profile to use for this series",
required = true
)]
language_profile_id: i64,
#[arg(
long,
help = "The type of series",
value_enum,
default_value_t = SeriesType::default()
)]
series_type: SeriesType,
#[arg(long, help = "Disable monitoring for this series")]
disable_monitoring: bool,
#[arg(long, help = "Don't use season folders for this series")]
disable_season_folders: bool,
#[arg(
long,
help = "Tag IDs to tag the series with",
value_parser,
action = ArgAction::Append
)]
tag: Vec<i64>,
#[arg(
long,
help = "What Sonarr should monitor",
value_enum,
default_value_t = SeriesMonitor::default()
)]
monitor: SeriesMonitor,
#[arg(
long,
help = "Tell Sonarr to not start a search for this series once it's added to your library"
)]
no_search_for_series: bool,
},
#[command(about = "Add a new root folder")]
RootFolder {
#[arg(long, help = "The path of the new root folder", required = true)]
@@ -57,6 +115,40 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrAddCommand> for SonarrAddCommandHan
async fn handle(self) -> Result<String> {
let result = match self.command {
SonarrAddCommand::Series {
tvdb_id,
root_folder_path,
quality_profile_id,
language_profile_id,
series_type,
disable_monitoring,
disable_season_folders,
tag: tags,
monitor,
no_search_for_series,
} => {
let body = AddSeriesBody {
tvdb_id,
title: String::new(),
monitored: !disable_monitoring,
root_folder_path,
quality_profile_id,
language_profile_id,
series_type: series_type.to_string(),
season_folder: !disable_season_folders,
tags,
add_options: AddSeriesOptions {
monitor: monitor.to_string(),
search_for_cutoff_unmet_episodes: !no_search_for_series,
search_for_missing_episodes: !no_search_for_series,
},
};
let resp = self
.network
.handle_network_event(SonarrEvent::AddSeries(Some(body)).into())
.await?;
serde_json::to_string_pretty(&resp)?
}
SonarrAddCommand::RootFolder { root_folder_path } => {
let resp = self
.network
+372 -1
View File
@@ -23,8 +23,11 @@ mod tests {
}
mod cli {
use crate::models::sonarr_models::{SeriesMonitor, SeriesType};
use super::*;
use pretty_assertions::assert_eq;
use rstest::rstest;
#[test]
fn test_add_root_folder_requires_arguments() {
@@ -60,6 +63,318 @@ mod tests {
}
}
#[test]
fn test_add_series_requires_arguments() {
let result = Cli::command().try_get_matches_from(["managarr", "sonarr", "add", "series"]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_add_series_requires_tvdb_id() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_add_series_requires_root_folder_path() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--tvdb-id",
"1",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_add_series_requires_quality_profile_id() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--tvdb-id",
"1",
"--root-folder-path",
"test",
"--language-profile-id",
"1",
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_add_series_requires_language_profile_id() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--tvdb-id",
"1",
"--root-folder-path",
"test",
"--quality-profile-id",
"1",
]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[rstest]
fn test_add_series_assert_argument_flags_require_args(
#[values("--series-type", "--tag", "--monitor")] flag: &str,
) {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--tvdb-id",
"1",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
flag,
]);
assert!(result.is_err());
assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn test_add_series_all_arguments_satisfied() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
]);
assert!(result.is_ok());
}
#[test]
fn test_add_series_series_type_validation() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
"--series-type",
"test",
]);
assert!(result.is_err());
assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn test_add_series_monitor_validation() {
let result = Cli::command().try_get_matches_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
"--monitor",
"test",
]);
assert!(result.is_err());
assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue);
}
#[test]
fn test_add_series_defaults() {
let expected_args = SonarrAddCommand::Series {
tvdb_id: 1,
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: SeriesType::default(),
disable_monitoring: false,
disable_season_folders: false,
tag: vec![],
monitor: SeriesMonitor::default(),
no_search_for_series: false,
};
let result = Cli::try_parse_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
]);
assert!(result.is_ok());
if let Some(Command::Sonarr(SonarrCommand::Add(add_command))) = result.unwrap().command {
assert_eq!(add_command, expected_args);
}
}
#[test]
fn test_add_series_tags_is_repeatable() {
let expected_args = SonarrAddCommand::Series {
tvdb_id: 1,
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: SeriesType::default(),
disable_monitoring: false,
disable_season_folders: false,
tag: vec![1, 2],
monitor: SeriesMonitor::default(),
no_search_for_series: false,
};
let result = Cli::try_parse_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--tvdb-id",
"1",
"--tag",
"1",
"--tag",
"2",
]);
assert!(result.is_ok());
if let Some(Command::Sonarr(SonarrCommand::Add(add_command))) = result.unwrap().command {
assert_eq!(add_command, expected_args);
}
}
#[test]
fn test_add_series_all_args_defined() {
let expected_args = SonarrAddCommand::Series {
tvdb_id: 1,
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: SeriesType::Anime,
disable_monitoring: true,
disable_season_folders: true,
tag: vec![1, 2],
monitor: SeriesMonitor::Future,
no_search_for_series: true,
};
let result = Cli::try_parse_from([
"managarr",
"sonarr",
"add",
"series",
"--root-folder-path",
"/test",
"--quality-profile-id",
"1",
"--language-profile-id",
"1",
"--series-type",
"anime",
"--disable-monitoring",
"--disable-season-folders",
"--tvdb-id",
"1",
"--tag",
"1",
"--tag",
"2",
"--monitor",
"future",
"--no-search-for-series",
]);
assert!(result.is_ok());
if let Some(Command::Sonarr(SonarrCommand::Add(add_command))) = result.unwrap().command {
assert_eq!(add_command, expected_args);
}
}
#[test]
fn test_add_tag_requires_arguments() {
let result = Cli::command().try_get_matches_from(["managarr", "sonarr", "add", "tag"]);
@@ -93,7 +408,12 @@ mod tests {
use crate::{
app::App,
cli::{sonarr::add_command_handler::SonarrAddCommandHandler, CliCommandHandler},
models::{sonarr_models::SonarrSerdeable, Serdeable},
models::{
sonarr_models::{
AddSeriesBody, AddSeriesOptions, SeriesMonitor, SeriesType, SonarrSerdeable,
},
Serdeable,
},
network::{sonarr_network::SonarrEvent, MockNetworkTrait, NetworkEvent},
};
@@ -131,6 +451,57 @@ mod tests {
assert!(result.is_ok());
}
#[tokio::test]
async fn test_handle_add_series_command() {
let expected_add_series_body = AddSeriesBody {
tvdb_id: 1,
title: String::new(),
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: "anime".to_owned(),
monitored: false,
tags: vec![1, 2],
season_folder: false,
add_options: AddSeriesOptions {
monitor: "future".to_owned(),
search_for_cutoff_unmet_episodes: false,
search_for_missing_episodes: false,
},
};
let mut mock_network = MockNetworkTrait::new();
mock_network
.expect_handle_network_event()
.with(eq::<NetworkEvent>(
SonarrEvent::AddSeries(Some(expected_add_series_body)).into(),
))
.times(1)
.returning(|_| {
Ok(Serdeable::Sonarr(SonarrSerdeable::Value(
json!({"testResponse": "response"}),
)))
});
let app_arc = Arc::new(Mutex::new(App::default()));
let add_series_command = SonarrAddCommand::Series {
tvdb_id: 1,
root_folder_path: "/test".to_owned(),
quality_profile_id: 1,
language_profile_id: 1,
series_type: SeriesType::Anime,
disable_monitoring: true,
disable_season_folders: true,
tag: vec![1, 2],
monitor: SeriesMonitor::Future,
no_search_for_series: true,
};
let result = SonarrAddCommandHandler::with(&app_arc, add_series_command, &mut mock_network)
.handle()
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_handle_add_tag_command() {
let expected_tag_name = "test".to_owned();