fix(sonarr): Construct and pass the add series body alongside AddSeries events when publishing to the networking channel

This commit is contained in:
2024-12-17 22:16:43 -07:00
parent 23971cbb76
commit 478b4ae3c0
10 changed files with 430 additions and 330 deletions
@@ -1,10 +1,11 @@
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::handlers::table_handler::TableHandlingConfig;
use crate::handlers::{handle_prompt_toggle, KeyEventHandler};
use crate::models::servarr_data::sonarr::modals::AddSeriesModal;
use crate::models::servarr_data::sonarr::sonarr_data::{
ActiveSonarrBlock, ADD_SERIES_BLOCKS, ADD_SERIES_SELECTION_BLOCKS,
};
use crate::models::sonarr_models::AddSeriesSearchResult;
use crate::models::sonarr_models::{AddSeriesBody, AddSeriesOptions, AddSeriesSearchResult};
use crate::models::{BlockSelectionState, Scrollable};
use crate::network::sonarr_network::SonarrEvent;
use crate::{handle_table_events, handle_text_box_keys, handle_text_box_left_right_keys, App, Key};
@@ -33,6 +34,87 @@ impl<'a, 'b> AddSeriesHandler<'a, 'b> {
.unwrap(),
AddSeriesSearchResult
);
fn build_add_series_body(&mut self) -> AddSeriesBody {
let tags = self
.app
.data
.sonarr_data
.add_series_modal
.as_ref()
.unwrap()
.tags
.text
.clone();
let AddSeriesModal {
root_folder_list,
monitor_list,
quality_profile_list,
language_profile_list,
series_type_list,
use_season_folder,
..
} = self.app.data.sonarr_data.add_series_modal.as_ref().unwrap();
let season_folder = *use_season_folder;
let (tvdb_id, title) = {
let AddSeriesSearchResult { tvdb_id, title, .. } = self
.app
.data
.sonarr_data
.add_searched_series
.as_ref()
.unwrap()
.current_selection()
.clone();
(tvdb_id, title.text)
};
let quality_profile = quality_profile_list.current_selection();
let quality_profile_id = *self
.app
.data
.sonarr_data
.quality_profile_map
.iter()
.filter(|(_, value)| *value == quality_profile)
.map(|(key, _)| key)
.next()
.unwrap();
let language_profile = language_profile_list.current_selection();
let language_profile_id = *self
.app
.data
.sonarr_data
.language_profiles_map
.iter()
.filter(|(_, value)| *value == language_profile)
.map(|(key, _)| key)
.next()
.unwrap();
let path = root_folder_list.current_selection().path.clone();
let monitor = monitor_list.current_selection().to_string();
let series_type = series_type_list.current_selection().to_string();
self.app.data.sonarr_data.add_series_modal = None;
AddSeriesBody {
tvdb_id,
title,
monitored: true,
root_folder_path: path,
quality_profile_id,
language_profile_id,
series_type,
season_folder,
tags: Vec::new(),
tag_input_string: Some(tags),
add_options: AddSeriesOptions {
monitor,
search_for_cutoff_unmet_episodes: true,
search_for_missing_episodes: true,
},
}
}
}
impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for AddSeriesHandler<'a, 'b> {
@@ -403,7 +485,8 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for AddSeriesHandler<'a,
match self.app.data.sonarr_data.selected_block.get_active_block() {
ActiveSonarrBlock::AddSeriesConfirmPrompt => {
if self.app.data.sonarr_data.prompt_confirm {
self.app.data.sonarr_data.prompt_confirm_action = Some(SonarrEvent::AddSeries(None));
self.app.data.sonarr_data.prompt_confirm_action =
Some(SonarrEvent::AddSeries(self.build_add_series_body()));
}
self.app.pop_navigation_stack();
@@ -534,7 +617,8 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for AddSeriesHandler<'a,
&& key == DEFAULT_KEYBINDINGS.confirm.key
{
self.app.data.sonarr_data.prompt_confirm = true;
self.app.data.sonarr_data.prompt_confirm_action = Some(SonarrEvent::AddSeries(None));
self.app.data.sonarr_data.prompt_confirm_action =
Some(SonarrEvent::AddSeries(self.build_add_series_body()));
self.app.pop_navigation_stack();
}
}
@@ -1,16 +1,22 @@
#[cfg(test)]
mod tests {
use pretty_assertions::assert_str_eq;
use bimap::BiMap;
use pretty_assertions::{assert_eq, assert_str_eq};
use strum::IntoEnumIterator;
use crate::app::key_binding::DEFAULT_KEYBINDINGS;
use crate::app::App;
use crate::event::Key;
use crate::handlers::sonarr_handlers::library::add_series_handler::AddSeriesHandler;
use crate::handlers::sonarr_handlers::sonarr_handler_test_utils::utils::add_series_search_result;
use crate::handlers::KeyEventHandler;
use crate::models::servarr_data::sonarr::modals::AddSeriesModal;
use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, ADD_SERIES_BLOCKS};
use crate::models::servarr_models::RootFolder;
use crate::models::sonarr_models::{AddSeriesSearchResult, SeriesMonitor, SeriesType};
use crate::models::sonarr_models::{
AddSeriesBody, AddSeriesOptions, AddSeriesSearchResult, SeriesMonitor, SeriesType,
};
use crate::models::stateful_table::StatefulTable;
use crate::models::HorizontallyScrollableText;
mod test_handle_scroll_up_and_down {
@@ -366,6 +372,7 @@ mod tests {
}
mod test_handle_home_end {
use pretty_assertions::assert_eq;
use std::sync::atomic::Ordering;
use strum::IntoEnumIterator;
@@ -763,6 +770,7 @@ mod tests {
}
mod test_handle_left_right_action {
use pretty_assertions::assert_eq;
use std::sync::atomic::Ordering;
use crate::models::servarr_data::sonarr::modals::AddSeriesModal;
@@ -1109,10 +1117,67 @@ mod tests {
#[test]
fn test_add_series_confirm_prompt_prompt_confirmation_submit() {
let mut app = App::default();
app.data.sonarr_data.add_series_modal = Some(AddSeriesModal::default());
app.push_navigation_stack(ActiveSonarrBlock::Series.into());
app.push_navigation_stack(ActiveSonarrBlock::AddSeriesPrompt.into());
app.data.sonarr_data.prompt_confirm = true;
let mut add_series_modal = AddSeriesModal {
use_season_folder: true,
tags: "usenet, testing".into(),
..AddSeriesModal::default()
};
add_series_modal.root_folder_list.set_items(vec![
RootFolder {
id: 1,
path: "/nfs".to_owned(),
accessible: true,
free_space: 219902325555200,
unmapped_folders: None,
},
RootFolder {
id: 2,
path: "/nfs2".to_owned(),
accessible: true,
free_space: 21990232555520,
unmapped_folders: None,
},
]);
add_series_modal.root_folder_list.state.select(Some(1));
add_series_modal
.quality_profile_list
.set_items(vec!["HD - 1080p".to_owned()]);
add_series_modal
.language_profile_list
.set_items(vec!["English".to_owned()]);
add_series_modal
.monitor_list
.set_items(Vec::from_iter(SeriesMonitor::iter()));
add_series_modal
.series_type_list
.set_items(Vec::from_iter(SeriesType::iter()));
app.data.sonarr_data.add_series_modal = Some(add_series_modal);
app.data.sonarr_data.quality_profile_map =
BiMap::from_iter([(2222, "HD - 1080p".to_owned())]);
app.data.sonarr_data.language_profiles_map = BiMap::from_iter([(2222, "English".to_owned())]);
let mut add_searched_series = StatefulTable::default();
add_searched_series.set_items(vec![add_series_search_result()]);
app.data.sonarr_data.add_searched_series = Some(add_searched_series);
let expected_add_series_body = AddSeriesBody {
tvdb_id: 1234,
title: "Test".to_owned(),
monitored: true,
root_folder_path: "/nfs2".to_owned(),
quality_profile_id: 2222,
language_profile_id: 2222,
series_type: "standard".to_owned(),
season_folder: true,
tags: Vec::default(),
tag_input_string: Some("usenet, testing".to_owned()),
add_options: AddSeriesOptions {
monitor: "all".to_owned(),
search_for_cutoff_unmet_episodes: true,
search_for_missing_episodes: true,
},
};
app.data.sonarr_data.selected_block = BlockSelectionState::new(ADD_SERIES_SELECTION_BLOCKS);
app
.data
@@ -1131,9 +1196,9 @@ mod tests {
assert_eq!(app.get_current_route(), ActiveSonarrBlock::Series.into());
assert_eq!(
app.data.sonarr_data.prompt_confirm_action,
Some(SonarrEvent::AddSeries(None))
Some(SonarrEvent::AddSeries(expected_add_series_body))
);
assert!(app.data.sonarr_data.add_series_modal.is_some());
assert!(app.data.sonarr_data.add_series_modal.is_none());
}
#[rstest]
@@ -1440,6 +1505,7 @@ mod tests {
},
network::sonarr_network::SonarrEvent,
};
use pretty_assertions::assert_eq;
#[test]
fn test_add_series_search_input_backspace() {
@@ -1553,7 +1619,64 @@ mod tests {
#[test]
fn test_add_series_confirm_prompt_prompt_confirmation_confirm() {
let mut app = App::default();
app.data.sonarr_data.add_series_modal = Some(AddSeriesModal::default());
let mut add_series_modal = AddSeriesModal {
use_season_folder: true,
tags: "usenet, testing".into(),
..AddSeriesModal::default()
};
add_series_modal.root_folder_list.set_items(vec![
RootFolder {
id: 1,
path: "/nfs".to_owned(),
accessible: true,
free_space: 219902325555200,
unmapped_folders: None,
},
RootFolder {
id: 2,
path: "/nfs2".to_owned(),
accessible: true,
free_space: 21990232555520,
unmapped_folders: None,
},
]);
add_series_modal.root_folder_list.state.select(Some(1));
add_series_modal
.quality_profile_list
.set_items(vec!["HD - 1080p".to_owned()]);
add_series_modal
.language_profile_list
.set_items(vec!["English".to_owned()]);
add_series_modal
.monitor_list
.set_items(Vec::from_iter(SeriesMonitor::iter()));
add_series_modal
.series_type_list
.set_items(Vec::from_iter(SeriesType::iter()));
app.data.sonarr_data.add_series_modal = Some(add_series_modal);
app.data.sonarr_data.quality_profile_map =
BiMap::from_iter([(2222, "HD - 1080p".to_owned())]);
app.data.sonarr_data.language_profiles_map = BiMap::from_iter([(2222, "English".to_owned())]);
let mut add_searched_series = StatefulTable::default();
add_searched_series.set_items(vec![add_series_search_result()]);
app.data.sonarr_data.add_searched_series = Some(add_searched_series);
let expected_add_series_body = AddSeriesBody {
tvdb_id: 1234,
title: "Test".to_owned(),
monitored: true,
root_folder_path: "/nfs2".to_owned(),
quality_profile_id: 2222,
language_profile_id: 2222,
series_type: "standard".to_owned(),
season_folder: true,
tags: Vec::default(),
tag_input_string: Some("usenet, testing".to_owned()),
add_options: AddSeriesOptions {
monitor: "all".to_owned(),
search_for_cutoff_unmet_episodes: true,
search_for_missing_episodes: true,
},
};
app.push_navigation_stack(ActiveSonarrBlock::Series.into());
app.push_navigation_stack(ActiveSonarrBlock::AddSeriesPrompt.into());
app.data.sonarr_data.selected_block = BlockSelectionState::new(ADD_SERIES_SELECTION_BLOCKS);
@@ -1574,9 +1697,9 @@ mod tests {
assert_eq!(app.get_current_route(), ActiveSonarrBlock::Series.into());
assert_eq!(
app.data.sonarr_data.prompt_confirm_action,
Some(SonarrEvent::AddSeries(None))
Some(SonarrEvent::AddSeries(expected_add_series_body))
);
assert!(app.data.sonarr_data.add_series_modal.is_some());
assert!(app.data.sonarr_data.add_series_modal.is_none());
}
}
@@ -1591,6 +1714,79 @@ mod tests {
});
}
#[test]
fn test_build_add_series_body() {
let mut app = App::default();
let mut add_series_modal = AddSeriesModal {
use_season_folder: true,
tags: "usenet, testing".into(),
..AddSeriesModal::default()
};
add_series_modal.root_folder_list.set_items(vec![
RootFolder {
id: 1,
path: "/nfs".to_owned(),
accessible: true,
free_space: 219902325555200,
unmapped_folders: None,
},
RootFolder {
id: 2,
path: "/nfs2".to_owned(),
accessible: true,
free_space: 21990232555520,
unmapped_folders: None,
},
]);
add_series_modal.root_folder_list.state.select(Some(1));
add_series_modal
.quality_profile_list
.set_items(vec!["HD - 1080p".to_owned()]);
add_series_modal
.language_profile_list
.set_items(vec!["English".to_owned()]);
add_series_modal
.monitor_list
.set_items(Vec::from_iter(SeriesMonitor::iter()));
add_series_modal
.series_type_list
.set_items(Vec::from_iter(SeriesType::iter()));
app.data.sonarr_data.add_series_modal = Some(add_series_modal);
app.data.sonarr_data.quality_profile_map = BiMap::from_iter([(2222, "HD - 1080p".to_owned())]);
app.data.sonarr_data.language_profiles_map = BiMap::from_iter([(2222, "English".to_owned())]);
let mut add_searched_series = StatefulTable::default();
add_searched_series.set_items(vec![add_series_search_result()]);
app.data.sonarr_data.add_searched_series = Some(add_searched_series);
let expected_add_series_body = AddSeriesBody {
tvdb_id: 1234,
title: "Test".to_owned(),
monitored: true,
root_folder_path: "/nfs2".to_owned(),
quality_profile_id: 2222,
language_profile_id: 2222,
series_type: "standard".to_owned(),
season_folder: true,
tags: Vec::default(),
tag_input_string: Some("usenet, testing".to_owned()),
add_options: AddSeriesOptions {
monitor: "all".to_owned(),
search_for_cutoff_unmet_episodes: true,
search_for_missing_episodes: true,
},
};
let add_series_body = AddSeriesHandler::with(
DEFAULT_KEYBINDINGS.esc.key,
&mut app,
ActiveSonarrBlock::AddSeriesPrompt,
None,
)
.build_add_series_body();
assert_eq!(add_series_body, expected_add_series_body);
assert!(app.data.sonarr_data.add_series_modal.is_none());
}
#[test]
fn test_add_series_handler_is_not_ready_when_loading() {
let mut app = App::default();
@@ -28,9 +28,17 @@ impl<'a, 'b> RootFoldersHandler<'a, 'b> {
self.app.data.sonarr_data.root_folders,
RootFolder
);
fn build_add_root_folder_body(&mut self) -> AddRootFolderBody {
let root_folder = self.app.data.sonarr_data.edit_root_folder.as_ref().unwrap().text.clone();
let root_folder = self
.app
.data
.sonarr_data
.edit_root_folder
.as_ref()
.unwrap()
.text
.clone();
self.app.data.sonarr_data.edit_root_folder = None;
AddRootFolderBody { path: root_folder }
}
@@ -146,7 +154,9 @@ impl<'a, 'b> KeyEventHandler<'a, 'b, ActiveSonarrBlock> for RootFoldersHandler<'
.text
.is_empty() =>
{
self.app.data.sonarr_data.prompt_confirm_action = Some(SonarrEvent::AddRootFolder(self.build_add_root_folder_body()));
self.app.data.sonarr_data.prompt_confirm_action = Some(SonarrEvent::AddRootFolder(
self.build_add_root_folder_body(),
));
self.app.data.sonarr_data.prompt_confirm = true;
self.app.should_ignore_quit_key = false;
self.app.pop_navigation_stack();
@@ -258,7 +258,9 @@ mod tests {
#[test]
fn test_add_root_folder_prompt_confirm_submit() {
let mut app = App::default();
let expected_add_root_folder_body = AddRootFolderBody { path: "Test".to_owned() };
let expected_add_root_folder_body = AddRootFolderBody {
path: "Test".to_owned(),
};
app
.data
.sonarr_data
@@ -656,14 +658,17 @@ mod tests {
fn test_build_add_root_folder_body() {
let mut app = App::default();
app.data.sonarr_data.edit_root_folder = Some("/nfs/test".into());
let expected_add_root_folder_body = AddRootFolderBody { path: "/nfs/test".to_owned() };
let expected_add_root_folder_body = AddRootFolderBody {
path: "/nfs/test".to_owned(),
};
let root_folder = RootFoldersHandler::with(
DEFAULT_KEYBINDINGS.esc.key,
&mut app,
ActiveSonarrBlock::AddRootFolderPrompt,
None,
).build_add_root_folder_body();
)
.build_add_root_folder_body();
assert_eq!(root_folder, expected_add_root_folder_body);
assert!(app.data.sonarr_data.edit_root_folder.is_none());
@@ -1,8 +1,15 @@
#[cfg(test)]
#[macro_use]
pub(in crate::handlers::sonarr_handlers) mod utils {
use crate::models::servarr_models::{Indexer, IndexerField, Language, Quality, QualityWrapper, RootFolder};
use crate::models::sonarr_models::{AddSeriesSearchResult, AddSeriesSearchResultStatistics, BlocklistItem, DownloadRecord, DownloadStatus, DownloadsResponse, Episode, EpisodeFile, IndexerSettings, MediaInfo, Rating, Season, SeasonStatistics, Series, SeriesStatistics, SeriesStatus, SeriesType, SonarrHistoryData, SonarrHistoryEventType, SonarrHistoryItem, SonarrRelease};
use crate::models::servarr_models::{
Indexer, IndexerField, Language, Quality, QualityWrapper, RootFolder,
};
use crate::models::sonarr_models::{
AddSeriesSearchResult, AddSeriesSearchResultStatistics, BlocklistItem, DownloadRecord,
DownloadStatus, DownloadsResponse, Episode, EpisodeFile, IndexerSettings, MediaInfo, Rating,
Season, SeasonStatistics, Series, SeriesStatistics, SeriesStatus, SeriesType,
SonarrHistoryData, SonarrHistoryEventType, SonarrHistoryItem, SonarrRelease,
};
use crate::models::HorizontallyScrollableText;
use chrono::DateTime;
use serde_json::{json, Number};
@@ -160,7 +167,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
};
}
fn add_series_search_result() -> AddSeriesSearchResult {
pub fn add_series_search_result() -> AddSeriesSearchResult {
AddSeriesSearchResult {
tvdb_id: 1234,
title: HorizontallyScrollableText::from("Test"),
@@ -176,11 +183,11 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn add_series_search_result_statistics() -> AddSeriesSearchResultStatistics {
pub fn add_series_search_result_statistics() -> AddSeriesSearchResultStatistics {
AddSeriesSearchResultStatistics { season_count: 3 }
}
fn blocklist_item() -> BlocklistItem {
pub fn blocklist_item() -> BlocklistItem {
BlocklistItem {
id: 1,
series_id: 1,
@@ -196,7 +203,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn download_record() -> DownloadRecord {
pub fn download_record() -> DownloadRecord {
DownloadRecord {
title: "Test Download Title".to_owned(),
status: DownloadStatus::Downloading,
@@ -212,13 +219,13 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn downloads_response() -> DownloadsResponse {
pub fn downloads_response() -> DownloadsResponse {
DownloadsResponse {
records: vec![download_record()],
}
}
fn episode() -> Episode {
pub fn episode() -> Episode {
Episode {
id: 1,
series_id: 1,
@@ -237,7 +244,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn episode_file() -> EpisodeFile {
pub fn episode_file() -> EpisodeFile {
EpisodeFile {
id: 1,
relative_path: "/season 1/episode 1.mkv".to_owned(),
@@ -250,11 +257,11 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn genres() -> Vec<String> {
pub fn genres() -> Vec<String> {
vec!["cool".to_owned(), "family".to_owned(), "fun".to_owned()]
}
fn history_data() -> SonarrHistoryData {
pub fn history_data() -> SonarrHistoryData {
SonarrHistoryData {
dropped_path: Some("/nfs/nzbget/completed/series/Coolness/something.cool.mkv".to_owned()),
imported_path: Some(
@@ -264,7 +271,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn history_item() -> SonarrHistoryItem {
pub fn history_item() -> SonarrHistoryItem {
SonarrHistoryItem {
id: 1,
source_title: "Test source".into(),
@@ -277,7 +284,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn indexer() -> Indexer {
pub fn indexer() -> Indexer {
Indexer {
enable_rss: true,
enable_automatic_search: true,
@@ -310,7 +317,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn indexer_settings() -> IndexerSettings {
pub fn indexer_settings() -> IndexerSettings {
IndexerSettings {
id: 1,
minimum_age: 1,
@@ -320,14 +327,14 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn language() -> Language {
pub fn language() -> Language {
Language {
id: 1,
name: "English".to_owned(),
}
}
fn media_info() -> MediaInfo {
pub fn media_info() -> MediaInfo {
MediaInfo {
audio_bitrate: 0,
audio_channels: Number::from_f64(7.1).unwrap(),
@@ -344,24 +351,24 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
subtitles: Some("English".to_owned()),
}
}
fn quality() -> Quality {
pub fn quality() -> Quality {
Quality {
name: "Bluray-1080p".to_owned(),
}
}
fn quality_wrapper() -> QualityWrapper {
pub fn quality_wrapper() -> QualityWrapper {
QualityWrapper { quality: quality() }
}
fn rating() -> Rating {
pub fn rating() -> Rating {
Rating {
votes: 406744,
value: 8.4,
}
}
fn season() -> Season {
pub fn season() -> Season {
Season {
title: None,
season_number: 1,
@@ -370,7 +377,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn season_statistics() -> SeasonStatistics {
pub fn season_statistics() -> SeasonStatistics {
SeasonStatistics {
previous_airing: Some(DateTime::from(
DateTime::parse_from_rfc3339("2022-10-24T01:00:00Z").unwrap(),
@@ -384,7 +391,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn series() -> Series {
pub fn series() -> Series {
Series {
title: "Test".to_owned().into(),
status: SeriesStatus::Continuing,
@@ -410,7 +417,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn series_statistics() -> SeriesStatistics {
pub fn series_statistics() -> SeriesStatistics {
SeriesStatistics {
season_count: 2,
episode_file_count: 18,
@@ -421,14 +428,14 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn rejections() -> Vec<String> {
pub fn rejections() -> Vec<String> {
vec![
"Unknown quality profile".to_owned(),
"Release is already mapped".to_owned(),
]
}
fn release() -> SonarrRelease {
pub fn release() -> SonarrRelease {
SonarrRelease {
guid: "1234".to_owned(),
protocol: "torrent".to_owned(),
@@ -447,7 +454,7 @@ pub(in crate::handlers::sonarr_handlers) mod utils {
}
}
fn root_folder() -> RootFolder {
pub fn root_folder() -> RootFolder {
RootFolder {
id: 1,
path: "/nfs".to_owned(),