Handle idle time between fetch results #212

Merged
wojtek merged 18 commits from 211---handle-idle-time-between-fetch-results into main 2024-09-08 23:23:53 +02:00
Showing only changes of commit b1e764ffb9 - Show all commits

View File

@ -51,6 +51,7 @@ impl AppMachine<AppFetch> {
} }
}, },
Err(recv_err) => match recv_err { Err(recv_err) => match recv_err {
TryRecvError::Empty => AppMachine::fetch(inner, fetch).into(),
TryRecvError::Disconnected => { TryRecvError::Disconnected => {
if first { if first {
AppMachine::matches(inner, AppMatches::new(None, fetch)).into() AppMachine::matches(inner, AppMatches::new(None, fetch)).into()
@ -58,7 +59,6 @@ impl AppMachine<AppFetch> {
AppMachine::browse(inner).into() AppMachine::browse(inner).into()
} }
} }
TryRecvError::Empty => AppMachine::fetch(inner, fetch).into(),
}, },
} }
} }
@ -101,276 +101,98 @@ impl IAppEventFetch for AppMachine<AppFetch> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use musichoard::collection::artist::ArtistMeta;
// use std::sync::mpsc; use crate::tui::{
app::machine::tests::{inner, music_hoard},
lib::interface::musicbrainz::{self, Match},
testmod::COLLECTION,
};
// use musichoard::collection::{ use super::*;
// album::{AlbumDate, AlbumId, AlbumMeta, AlbumPrimaryType, AlbumSecondaryType},
// artist::{ArtistId, ArtistMeta},
// };
// use crate::tui::{ #[test]
// app::{ fn recv_ok_fetch_ok() {
// machine::tests::{inner, music_hoard}, let (tx, rx) = mpsc::channel::<FetchResult>();
// IAppAccess,
// },
// lib::interface::musicbrainz::Match,
// };
// use super::*; let artist = COLLECTION[3].meta.clone();
let fetch_result = Ok(AppMatchesInfo::artist::<Match<ArtistMeta>>(artist, vec![]));
tx.send(fetch_result).unwrap();
// #[test] let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
// fn fetch_musicbrainz_artist_api_error() { assert!(matches!(app, AppState::Matches(_)));
// let mut mb_api = MockIMusicBrainz::new(); }
// let error = Err(musicbrainz::Error::RateLimit); #[test]
fn recv_ok_fetch_err() {
// mb_api let (tx, rx) = mpsc::channel::<FetchResult>();
// .expect_search_artist()
// .times(1) let fetch_result = Err(musicbrainz::Error::RateLimit);
// .return_once(|_| error); tx.send(fetch_result).unwrap();
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api)); let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
assert!(matches!(app, AppState::Error(_)));
// // Use the fourth artist for this test as they have no MBID. }
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
// let browse = browse.increment_selection(Delta::Line).unwrap_browse(); #[test]
// let browse = browse.increment_selection(Delta::Line).unwrap_browse(); fn recv_err_empty() {
let (_tx, rx) = mpsc::channel::<FetchResult>();
// let app = browse.fetch_musicbrainz();
// app.unwrap_error(); let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
// } assert!(matches!(app, AppState::Fetch(_)));
}
// #[test]
// fn fetch_musicbrainz_album_api_error() { #[test]
// let mut mb_api = MockIMusicBrainz::new(); fn recv_err_disconnected_first() {
let (_, rx) = mpsc::channel::<FetchResult>();
// let error = Err(musicbrainz::Error::RateLimit);
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
// mb_api assert!(matches!(app, AppState::Matches(_)));
// .expect_search_release_group() }
// .times(1)
// .return_once(|_, _| error); #[test]
fn recv_err_disconnected_next() {
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api)); let (_, rx) = mpsc::channel::<FetchResult>();
// let app = browse.fetch_musicbrainz(); let fetch = AppFetch::new(rx);
// app.unwrap_error(); let app = AppMachine::app_fetch_next(inner(music_hoard(COLLECTION.clone())), fetch);
// } assert!(matches!(app, AppState::Browse(_)));
}
// impl<T> Match<T> { #[test]
// pub fn new(score: u8, item: T) -> Self { fn empty_first_then_ready() {
// Match { let (tx, rx) = mpsc::channel::<FetchResult>();
// score,
// item, let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
// disambiguation: None, assert!(matches!(app, AppState::Fetch(_)));
// }
// } let artist = COLLECTION[3].meta.clone();
// } let fetch_result = Ok(AppMatchesInfo::artist::<Match<ArtistMeta>>(artist, vec![]));
tx.send(fetch_result).unwrap();
// fn artist_matches_info_vec() -> Vec<AppMatchesInfo> {
// let artist_1 = ArtistMeta::new(ArtistId::new("Artist 1")); let app = app.unwrap_fetch().fetch_result_ready();
assert!(matches!(app, AppState::Matches(_)));
// let artist_1_1 = artist_1.clone(); }
// let artist_match_1_1 = Match::new(100, artist_1_1);
#[test]
// let artist_1_2 = artist_1.clone(); fn abort() {
// let mut artist_match_1_2 = Match::new(100, artist_1_2); let (_, rx) = mpsc::channel::<FetchResult>();
// artist_match_1_2.disambiguation = Some(String::from("some disambiguation"));
let fetch = AppFetch::new(rx);
// let list = vec![artist_match_1_1.clone(), artist_match_1_2.clone()]; let app = AppMachine::fetch(inner(music_hoard(COLLECTION.clone())), fetch);
// let matches_info_1 = AppMatchesInfo::artist(artist_1.clone(), list);
let app = app.abort();
// let artist_2 = ArtistMeta::new(ArtistId::new("Artist 2")); assert!(matches!(app, AppState::Browse(_)));
}
// let artist_2_1 = artist_1.clone();
// let album_match_2_1 = Match::new(100, artist_2_1); #[test]
fn no_op() {
// let list = vec![album_match_2_1.clone()]; let (_, rx) = mpsc::channel::<FetchResult>();
// let matches_info_2 = AppMatchesInfo::artist(artist_2.clone(), list);
let fetch = AppFetch::new(rx);
// vec![matches_info_1, matches_info_2] let app = AppMachine::fetch(inner(music_hoard(COLLECTION.clone())), fetch);
// }
let app = app.no_op();
// fn album_matches_info_vec() -> Vec<AppMatchesInfo> { assert!(matches!(app, AppState::Fetch(_)));
// let album_1 = AlbumMeta::new( }
// AlbumId::new("Album 1"),
// AlbumDate::new(Some(1990), Some(5), None),
// Some(AlbumPrimaryType::Album),
// vec![AlbumSecondaryType::Live, AlbumSecondaryType::Compilation],
// );
// let album_1_1 = album_1.clone();
// let album_match_1_1 = Match::new(100, album_1_1);
// let mut album_1_2 = album_1.clone();
// album_1_2.id.title.push_str(" extra title part");
// album_1_2.secondary_types.pop();
// let album_match_1_2 = Match::new(100, album_1_2);
// let list = vec![album_match_1_1.clone(), album_match_1_2.clone()];
// let matches_info_1 = AppMatchesInfo::album(album_1.clone(), list);
// let album_2 = AlbumMeta::new(
// AlbumId::new("Album 2"),
// AlbumDate::new(Some(2001), None, None),
// Some(AlbumPrimaryType::Album),
// vec![],
// );
// let album_2_1 = album_1.clone();
// let album_match_2_1 = Match::new(100, album_2_1);
// let list = vec![album_match_2_1.clone()];
// let matches_info_2 = AppMatchesInfo::album(album_2.clone(), list);
// vec![matches_info_1, matches_info_2]
// }
// fn receiver(matches_info_vec: Vec<AppMatchesInfo>) -> FetchReceiver {
// let (tx, rx) = mpsc::channel();
// for matches_info in matches_info_vec.into_iter() {
// tx.send(Ok(matches_info)).unwrap();
// }
// rx
// }
// fn push_cannot_have_mbid(matches_info_vec: &mut [AppMatchesInfo]) {
// for matches_info in matches_info_vec.iter_mut() {
// matches_info.push_cannot_have_mbid();
// }
// }
// #[test]
// fn create_empty() {
// let matches = AppMachine::matches(inner(music_hoard(vec![])), AppMatches::empty(fetch()));
// let widget_state = WidgetState::default();
// assert_eq!(matches.state.current, None);
// assert_eq!(matches.state.state, widget_state);
// let mut app = matches.no_op();
// let public = app.get();
// let public_matches = public.state.unwrap_matches();
// assert_eq!(public_matches.matches, None);
// assert_eq!(public_matches.state, &widget_state);
// }
// #[test]
// fn create_nonempty() {
// let mut matches_info_vec = album_matches_info_vec();
// let matches = AppMachine::app_matches(
// inner(music_hoard(vec![])),
// receiver(matches_info_vec.clone()),
// )
// .unwrap_matches();
// push_cannot_have_mbid(&mut matches_info_vec);
// let mut widget_state = WidgetState::default();
// widget_state.list.select(Some(0));
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state, widget_state);
// let mut app = matches.no_op();
// let public = app.get();
// let public_matches = public.state.unwrap_matches();
// assert_eq!(public_matches.matches, Some(&matches_info_vec[0]));
// assert_eq!(public_matches.state, &widget_state);
// }
// fn matches_flow(mut matches_info_vec: Vec<AppMatchesInfo>) {
// let matches = AppMachine::app_matches(
// inner(music_hoard(vec![])),
// receiver(matches_info_vec.clone()),
// )
// .unwrap_matches();
// push_cannot_have_mbid(&mut matches_info_vec);
// let mut widget_state = WidgetState::default();
// widget_state.list.select(Some(0));
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state, widget_state);
// let matches = matches.prev_match().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state.list.selected(), Some(0));
// let matches = matches.next_match().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state.list.selected(), Some(1));
// // Next is CannotHaveMBID
// let matches = matches.next_match().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state.list.selected(), Some(2));
// let matches = matches.next_match().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state.list.selected(), Some(2));
// let matches = matches.select().unwrap_matches();
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[1]));
// assert_eq!(matches.state.state.list.selected(), Some(0));
// // And it's done
// matches.select().unwrap_browse();
// }
// #[test]
// fn artist_matches_flow() {
// matches_flow(artist_matches_info_vec());
// }
// #[test]
// fn album_matches_flow() {
// matches_flow(album_matches_info_vec());
// }
// #[test]
// fn matches_abort() {
// let mut matches_info_vec = album_matches_info_vec();
// let matches = AppMachine::app_matches(
// inner(music_hoard(vec![])),
// receiver(matches_info_vec.clone()),
// )
// .unwrap_matches();
// push_cannot_have_mbid(&mut matches_info_vec);
// let mut widget_state = WidgetState::default();
// widget_state.list.select(Some(0));
// assert_eq!(matches.state.current.as_ref(), Some(&matches_info_vec[0]));
// assert_eq!(matches.state.state, widget_state);
// matches.abort().unwrap_browse();
// }
// #[test]
// fn matches_select_empty() {
// let matches =
// AppMachine::app_matches(inner(music_hoard(vec![])), receiver(vec![])).unwrap_matches();
// assert_eq!(matches.state.current, None);
// matches.select().unwrap_browse();
// }
// #[test]
// fn no_op() {
// let matches =
// AppMachine::app_matches(inner(music_hoard(vec![])), receiver(vec![])).unwrap_matches();
// let app = matches.no_op();
// app.unwrap_matches();
// }
} }