Compare commits
8 Commits
196de92959
...
66681a7ea9
Author | SHA1 | Date | |
---|---|---|---|
66681a7ea9 | |||
b1e764ffb9 | |||
f5f2cc478f | |||
5a07106793 | |||
616f4c9f35 | |||
405d16cbf9 | |||
669d4c9da9 | |||
c6a6d21199 |
@ -1,9 +1,26 @@
|
||||
use crate::tui::app::{
|
||||
use std::{
|
||||
sync::{mpsc, Arc, Mutex},
|
||||
thread, time,
|
||||
};
|
||||
|
||||
use musichoard::collection::{
|
||||
album::AlbumMeta,
|
||||
artist::{Artist, ArtistMeta},
|
||||
musicbrainz::{IMusicBrainzRef, Mbid},
|
||||
};
|
||||
|
||||
use crate::tui::{
|
||||
app::{
|
||||
machine::{App, AppInner, AppMachine},
|
||||
selection::{Delta, ListSelection},
|
||||
AppPublic, AppState, IAppInteractBrowse,
|
||||
AppMatchesInfo, AppPublic, AppState, IAppInteractBrowse,
|
||||
},
|
||||
event::{Event, EventSender},
|
||||
lib::interface::musicbrainz::{self, IMusicBrainz},
|
||||
};
|
||||
|
||||
use super::fetch::{FetchResult, FetchSender};
|
||||
|
||||
pub struct AppBrowse;
|
||||
|
||||
impl AppMachine<AppBrowse> {
|
||||
@ -13,6 +30,80 @@ impl AppMachine<AppBrowse> {
|
||||
state: AppBrowse,
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_fetch_thread(
|
||||
inner: &AppInner,
|
||||
artist: &Artist,
|
||||
tx: FetchSender,
|
||||
) -> thread::JoinHandle<()> {
|
||||
match artist.meta.musicbrainz {
|
||||
Some(ref arid) => {
|
||||
let musicbrainz = Arc::clone(&inner.musicbrainz);
|
||||
let events = inner.events.clone();
|
||||
let arid = arid.mbid().clone();
|
||||
let albums = artist.albums.iter().map(|a| &a.meta).cloned().collect();
|
||||
thread::spawn(|| Self::fetch_albums(musicbrainz, tx, events, arid, albums))
|
||||
}
|
||||
None => {
|
||||
let musicbrainz = Arc::clone(&inner.musicbrainz);
|
||||
let events = inner.events.clone();
|
||||
let artist = artist.meta.clone();
|
||||
thread::spawn(|| Self::fetch_artist(musicbrainz, tx, events, artist))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch_artist(
|
||||
musicbrainz: Arc<Mutex<dyn IMusicBrainz + Send>>,
|
||||
fetch_tx: FetchSender,
|
||||
events: EventSender,
|
||||
artist: ArtistMeta,
|
||||
) {
|
||||
let result = musicbrainz.lock().unwrap().search_artist(&artist);
|
||||
let result = result.map(|list| AppMatchesInfo::artist(artist, list));
|
||||
Self::send_fetch_result(&fetch_tx, &events, result).ok();
|
||||
}
|
||||
|
||||
fn fetch_albums(
|
||||
musicbrainz: Arc<Mutex<dyn IMusicBrainz + Send>>,
|
||||
fetch_tx: FetchSender,
|
||||
events: EventSender,
|
||||
arid: Mbid,
|
||||
albums: Vec<AlbumMeta>,
|
||||
) {
|
||||
let mut musicbrainz = musicbrainz.lock().unwrap();
|
||||
let mut album_iter = albums.into_iter().peekable();
|
||||
while let Some(album) = album_iter.next() {
|
||||
if album.musicbrainz.is_some() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let result = musicbrainz.search_release_group(&arid, &album);
|
||||
let result = result.map(|list| AppMatchesInfo::album(album, list));
|
||||
if Self::send_fetch_result(&fetch_tx, &events, result).is_err() {
|
||||
return;
|
||||
};
|
||||
|
||||
if album_iter.peek().is_some() {
|
||||
thread::sleep(time::Duration::from_secs(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn send_fetch_result(
|
||||
fetch_tx: &FetchSender,
|
||||
events: &EventSender,
|
||||
result: Result<AppMatchesInfo, musicbrainz::Error>,
|
||||
) -> Result<(), ()> {
|
||||
// If receiver disconnects just drop the rest.
|
||||
fetch_tx.send(result).map_err(|_| ())?;
|
||||
|
||||
// If this send fails the event listener is dead. Don't panic as this function runs in a
|
||||
// detached thread so this might be happening during normal shut down.
|
||||
events.send(Event::FetchResultReady).map_err(|_| ())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AppMachine<AppBrowse>> for App {
|
||||
@ -79,7 +170,18 @@ impl IAppInteractBrowse for AppMachine<AppBrowse> {
|
||||
}
|
||||
|
||||
fn fetch_musicbrainz(self) -> Self::APP {
|
||||
AppMachine::app_fetch_new(self.inner)
|
||||
let coll = self.inner.music_hoard.get_collection();
|
||||
let artist = match self.inner.selection.state_artist(coll) {
|
||||
Some(artist_state) => &coll[artist_state.index],
|
||||
None => {
|
||||
return AppMachine::error(self.inner, "cannot fetch: no artist selected").into()
|
||||
}
|
||||
};
|
||||
|
||||
let (fetch_tx, fetch_rx) = mpsc::channel::<FetchResult>();
|
||||
Self::spawn_fetch_thread(&self.inner, artist, fetch_tx);
|
||||
|
||||
AppMachine::app_fetch_new(self.inner, fetch_rx)
|
||||
}
|
||||
|
||||
fn no_op(self) -> Self::APP {
|
||||
@ -90,16 +192,17 @@ impl IAppInteractBrowse for AppMachine<AppBrowse> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use mockall::{predicate, Sequence};
|
||||
use musichoard::collection::{album::AlbumMeta, artist::ArtistMeta, musicbrainz::Mbid};
|
||||
use mpsc::TryRecvError;
|
||||
|
||||
use crate::tui::{
|
||||
app::{
|
||||
machine::tests::{inner, inner_with_mb, music_hoard},
|
||||
AppAlbumMatches, AppArtistMatches, AppMatchesInfo, Category, IAppAccess, IAppInteract,
|
||||
IAppInteractMatches, MatchOption,
|
||||
AppAlbumMatches, AppArtistMatches, Category, IAppAccess, IAppInteract,
|
||||
},
|
||||
lib::interface::musicbrainz::{self, Match, MockIMusicBrainz},
|
||||
event::EventReceiver,
|
||||
lib::interface::musicbrainz::{Match, MockIMusicBrainz},
|
||||
testmod::COLLECTION,
|
||||
EventChannel,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
@ -174,208 +277,215 @@ mod tests {
|
||||
app.unwrap_search();
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn fetch_musicbrainz() {
|
||||
// let mut mb_api = MockIMusicBrainz::new();
|
||||
#[test]
|
||||
fn fetch_musicbrainz_no_artist() {
|
||||
let browse = AppMachine::browse(inner(music_hoard(vec![])));
|
||||
let app = browse.fetch_musicbrainz();
|
||||
app.unwrap_error();
|
||||
}
|
||||
|
||||
// let arid: Mbid = "11111111-1111-1111-1111-111111111111".try_into().unwrap();
|
||||
// let album_1 = COLLECTION[1].albums[0].meta.clone();
|
||||
// let album_4 = COLLECTION[1].albums[3].meta.clone();
|
||||
#[test]
|
||||
fn fetch_musicbrainz() {
|
||||
let mb_api = MockIMusicBrainz::new();
|
||||
let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
|
||||
|
||||
// let album_match_1_1 = Match::new(100, album_1.clone());
|
||||
// let album_match_1_2 = Match::new(50, album_4.clone());
|
||||
// let album_match_4_1 = Match::new(100, album_4.clone());
|
||||
// let album_match_4_2 = Match::new(30, album_1.clone());
|
||||
// let matches_1 = vec![album_match_1_1.clone(), album_match_1_2.clone()];
|
||||
// let matches_4 = vec![album_match_4_1.clone(), album_match_4_2.clone()];
|
||||
// Use the second artist for this test.
|
||||
let browse = browse.increment_selection(Delta::Line).unwrap_browse();
|
||||
let mut app = browse.fetch_musicbrainz();
|
||||
|
||||
// let result_1: Result<Vec<Match<AlbumMeta>>, musicbrainz::Error> = Ok(matches_1.clone());
|
||||
// let result_4: Result<Vec<Match<AlbumMeta>>, musicbrainz::Error> = Ok(matches_4.clone());
|
||||
let public = app.get();
|
||||
|
||||
// // Other albums have an MBID and so they will be skipped.
|
||||
// let mut seq = Sequence::new();
|
||||
// Because of threaded behaviour, this unit test cannot expect one or the other.
|
||||
assert!(
|
||||
matches!(public.state, AppState::Matches(_))
|
||||
|| matches!(public.state, AppState::Fetch(_))
|
||||
);
|
||||
}
|
||||
|
||||
// mb_api
|
||||
// .expect_search_release_group()
|
||||
// .with(predicate::eq(arid.clone()), predicate::eq(album_1.clone()))
|
||||
// .times(1)
|
||||
// .in_sequence(&mut seq)
|
||||
// .return_once(|_, _| result_1);
|
||||
// mb_api
|
||||
// .expect_search_release_group()
|
||||
// .with(predicate::eq(arid.clone()), predicate::eq(album_4.clone()))
|
||||
// .times(1)
|
||||
// .in_sequence(&mut seq)
|
||||
// .return_once(|_, _| result_4);
|
||||
fn event_channel() -> (EventSender, EventReceiver) {
|
||||
let event_channel = EventChannel::new();
|
||||
let events_tx = event_channel.sender();
|
||||
let events_rx = event_channel.receiver();
|
||||
(events_tx, events_rx)
|
||||
}
|
||||
|
||||
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
|
||||
fn album_expectations_1() -> (AlbumMeta, Vec<Match<AlbumMeta>>) {
|
||||
let album_1 = COLLECTION[1].albums[0].meta.clone();
|
||||
let album_4 = COLLECTION[1].albums[3].meta.clone();
|
||||
|
||||
// // Use the second artist for this test.
|
||||
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
|
||||
// let mut app = browse.fetch_musicbrainz();
|
||||
let album_match_1_1 = Match::new(100, album_1.clone());
|
||||
let album_match_1_2 = Match::new(50, album_4.clone());
|
||||
let matches_1 = vec![album_match_1_1.clone(), album_match_1_2.clone()];
|
||||
|
||||
// let public = app.get();
|
||||
// assert!(matches!(public.state, AppState::Matches(_)));
|
||||
(album_1, matches_1)
|
||||
}
|
||||
|
||||
// let public_matches = public.state.unwrap_matches();
|
||||
fn album_expectations_4() -> (AlbumMeta, Vec<Match<AlbumMeta>>) {
|
||||
let album_1 = COLLECTION[1].albums[0].meta.clone();
|
||||
let album_4 = COLLECTION[1].albums[3].meta.clone();
|
||||
|
||||
// let mut matches_1: Vec<MatchOption<AlbumMeta>> =
|
||||
// matches_1.into_iter().map(Into::into).collect();
|
||||
// matches_1.push(MatchOption::CannotHaveMbid);
|
||||
// let expected = Some(AppMatchesInfo::Album(AppAlbumMatches {
|
||||
// matching: album_1.clone(),
|
||||
// list: matches_1.clone(),
|
||||
// }));
|
||||
// assert_eq!(public_matches.matches, expected.as_ref());
|
||||
let album_match_4_1 = Match::new(100, album_4.clone());
|
||||
let album_match_4_2 = Match::new(30, album_1.clone());
|
||||
let matches_4 = vec![album_match_4_1.clone(), album_match_4_2.clone()];
|
||||
|
||||
// let mut app = app.unwrap_matches().select();
|
||||
(album_4, matches_4)
|
||||
}
|
||||
|
||||
// let public = app.get();
|
||||
// assert!(matches!(public.state, AppState::Matches(_)));
|
||||
fn search_release_group_expectation(
|
||||
api: &mut MockIMusicBrainz,
|
||||
seq: &mut Sequence,
|
||||
arid: &Mbid,
|
||||
album: &AlbumMeta,
|
||||
matches: &[Match<AlbumMeta>],
|
||||
) {
|
||||
let result = Ok(matches.to_owned());
|
||||
api.expect_search_release_group()
|
||||
.with(predicate::eq(arid.clone()), predicate::eq(album.clone()))
|
||||
.times(1)
|
||||
.in_sequence(seq)
|
||||
.return_once(|_, _| result);
|
||||
}
|
||||
|
||||
// let public_matches = public.state.unwrap_matches();
|
||||
#[test]
|
||||
fn fetch_albums() {
|
||||
let mut mb_api = MockIMusicBrainz::new();
|
||||
|
||||
// let mut matches_4: Vec<MatchOption<AlbumMeta>> =
|
||||
// matches_4.into_iter().map(Into::into).collect();
|
||||
// matches_4.push(MatchOption::CannotHaveMbid);
|
||||
// let expected = Some(AppMatchesInfo::Album(AppAlbumMatches {
|
||||
// matching: album_4.clone(),
|
||||
// list: matches_4.clone(),
|
||||
// }));
|
||||
// assert_eq!(public_matches.matches, expected.as_ref());
|
||||
let arid: Mbid = "11111111-1111-1111-1111-111111111111".try_into().unwrap();
|
||||
|
||||
// let app = app.unwrap_matches().select();
|
||||
// app.unwrap_browse();
|
||||
// }
|
||||
let (album_1, matches_1) = album_expectations_1();
|
||||
let (album_4, matches_4) = album_expectations_4();
|
||||
|
||||
// #[test]
|
||||
// fn fetch_musicbrainz_no_artist() {
|
||||
// let browse = AppMachine::browse(inner(music_hoard(vec![])));
|
||||
// let app = browse.fetch_musicbrainz();
|
||||
// app.unwrap_error();
|
||||
// }
|
||||
// Other albums have an MBID and so they will be skipped.
|
||||
let mut seq = Sequence::new();
|
||||
|
||||
// #[test]
|
||||
// fn fetch_musicbrainz_no_artist_mbid() {
|
||||
// let mut mb_api = MockIMusicBrainz::new();
|
||||
search_release_group_expectation(&mut mb_api, &mut seq, &arid, &album_1, &matches_1);
|
||||
search_release_group_expectation(&mut mb_api, &mut seq, &arid, &album_4, &matches_4);
|
||||
|
||||
// let artist = COLLECTION[3].meta.clone();
|
||||
let music_hoard = music_hoard(COLLECTION.to_owned());
|
||||
let (events_tx, events_rx) = event_channel();
|
||||
let inner = AppInner::new(music_hoard, mb_api, events_tx);
|
||||
|
||||
// let artist_match_1 = Match::new(100, artist.clone());
|
||||
// let artist_match_2 = Match::new(50, artist.clone());
|
||||
// let matches = vec![artist_match_1.clone(), artist_match_2.clone()];
|
||||
let (fetch_tx, fetch_rx) = mpsc::channel();
|
||||
// Use the second artist for this test.
|
||||
let handle = AppMachine::spawn_fetch_thread(&inner, &COLLECTION[1], fetch_tx);
|
||||
handle.join().unwrap();
|
||||
|
||||
// let result: Result<Vec<Match<ArtistMeta>>, musicbrainz::Error> = Ok(matches.clone());
|
||||
assert_eq!(events_rx.try_recv().unwrap(), Event::FetchResultReady);
|
||||
let result = fetch_rx.try_recv().unwrap();
|
||||
let expected = Ok(AppMatchesInfo::Album(AppAlbumMatches {
|
||||
matching: album_1.clone(),
|
||||
list: matches_1.iter().cloned().map(Into::into).collect(),
|
||||
}));
|
||||
assert_eq!(result, expected);
|
||||
|
||||
// mb_api
|
||||
// .expect_search_artist()
|
||||
// .with(predicate::eq(artist.clone()))
|
||||
// .times(1)
|
||||
// .return_once(|_| result);
|
||||
assert_eq!(events_rx.try_recv().unwrap(), Event::FetchResultReady);
|
||||
let result = fetch_rx.try_recv().unwrap();
|
||||
let expected = Ok(AppMatchesInfo::Album(AppAlbumMatches {
|
||||
matching: album_4.clone(),
|
||||
list: matches_4.iter().cloned().map(Into::into).collect(),
|
||||
}));
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
|
||||
fn artist_expectations() -> (ArtistMeta, Vec<Match<ArtistMeta>>) {
|
||||
let artist = COLLECTION[3].meta.clone();
|
||||
|
||||
// // 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();
|
||||
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
|
||||
// let mut app = browse.fetch_musicbrainz();
|
||||
let artist_match_1 = Match::new(100, artist.clone());
|
||||
let artist_match_2 = Match::new(50, artist.clone());
|
||||
let matches = vec![artist_match_1.clone(), artist_match_2.clone()];
|
||||
|
||||
// let public = app.get();
|
||||
// assert!(matches!(public.state, AppState::Matches(_)));
|
||||
(artist, matches)
|
||||
}
|
||||
|
||||
// let public_matches = public.state.unwrap_matches();
|
||||
fn search_artist_expectation(
|
||||
api: &mut MockIMusicBrainz,
|
||||
seq: &mut Sequence,
|
||||
artist: &ArtistMeta,
|
||||
matches: &[Match<ArtistMeta>],
|
||||
) {
|
||||
let result = Ok(matches.to_owned());
|
||||
api.expect_search_artist()
|
||||
.with(predicate::eq(artist.clone()))
|
||||
.times(1)
|
||||
.in_sequence(seq)
|
||||
.return_once(|_| result);
|
||||
}
|
||||
|
||||
// let mut matches: Vec<MatchOption<ArtistMeta>> =
|
||||
// matches.into_iter().map(Into::into).collect();
|
||||
// matches.push(MatchOption::CannotHaveMbid);
|
||||
// let expected = Some(AppMatchesInfo::Artist(AppArtistMatches {
|
||||
// matching: artist.clone(),
|
||||
// list: matches.clone(),
|
||||
// }));
|
||||
// assert_eq!(public_matches.matches, expected.as_ref());
|
||||
#[test]
|
||||
fn fetch_artist() {
|
||||
let mut mb_api = MockIMusicBrainz::new();
|
||||
|
||||
// let app = app.unwrap_matches().select();
|
||||
// app.unwrap_browse();
|
||||
// }
|
||||
let (artist, matches) = artist_expectations();
|
||||
let mut seq = Sequence::new();
|
||||
search_artist_expectation(&mut mb_api, &mut seq, &artist, &matches);
|
||||
|
||||
// #[test]
|
||||
// fn fetch_musicbrainz_artist_api_error() {
|
||||
// let mut mb_api = MockIMusicBrainz::new();
|
||||
let music_hoard = music_hoard(COLLECTION.to_owned());
|
||||
let (events_tx, events_rx) = event_channel();
|
||||
let inner = AppInner::new(music_hoard, mb_api, events_tx);
|
||||
|
||||
// let error = Err(musicbrainz::Error::RateLimit);
|
||||
let (fetch_tx, fetch_rx) = mpsc::channel();
|
||||
// Use the fourth artist for this test as they have no MBID.
|
||||
let handle = AppMachine::spawn_fetch_thread(&inner, &COLLECTION[3], fetch_tx);
|
||||
handle.join().unwrap();
|
||||
|
||||
// mb_api
|
||||
// .expect_search_artist()
|
||||
// .times(1)
|
||||
// .return_once(|_| error);
|
||||
assert_eq!(events_rx.try_recv().unwrap(), Event::FetchResultReady);
|
||||
let result = fetch_rx.try_recv().unwrap();
|
||||
let expected = Ok(AppMatchesInfo::Artist(AppArtistMatches {
|
||||
matching: artist.clone(),
|
||||
list: matches.iter().cloned().map(Into::into).collect(),
|
||||
}));
|
||||
assert_eq!(result, expected);
|
||||
}
|
||||
|
||||
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
|
||||
#[test]
|
||||
fn fetch_artist_fetch_disconnect() {
|
||||
let mut mb_api = MockIMusicBrainz::new();
|
||||
|
||||
// // 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();
|
||||
// let browse = browse.increment_selection(Delta::Line).unwrap_browse();
|
||||
let (artist, matches) = artist_expectations();
|
||||
let mut seq = Sequence::new();
|
||||
search_artist_expectation(&mut mb_api, &mut seq, &artist, &matches);
|
||||
|
||||
// let app = browse.fetch_musicbrainz();
|
||||
// app.unwrap_error();
|
||||
// }
|
||||
let music_hoard = music_hoard(COLLECTION.to_owned());
|
||||
let (events_tx, events_rx) = event_channel();
|
||||
let inner = AppInner::new(music_hoard, mb_api, events_tx);
|
||||
|
||||
// #[test]
|
||||
// fn fetch_musicbrainz_album_api_error() {
|
||||
// let mut mb_api = MockIMusicBrainz::new();
|
||||
let (fetch_tx, _) = mpsc::channel();
|
||||
// Use the fourth artist for this test as they have no MBID.
|
||||
let handle = AppMachine::spawn_fetch_thread(&inner, &COLLECTION[3], fetch_tx);
|
||||
handle.join().unwrap();
|
||||
|
||||
// let error = Err(musicbrainz::Error::RateLimit);
|
||||
assert!(events_rx.try_recv().is_err());
|
||||
}
|
||||
|
||||
// mb_api
|
||||
// .expect_search_release_group()
|
||||
// .times(1)
|
||||
// .return_once(|_, _| error);
|
||||
#[test]
|
||||
fn fetch_albums_event_disconnect() {
|
||||
let mut mb_api = MockIMusicBrainz::new();
|
||||
|
||||
// let browse = AppMachine::browse(inner_with_mb(music_hoard(COLLECTION.to_owned()), mb_api));
|
||||
let arid: Mbid = "11111111-1111-1111-1111-111111111111".try_into().unwrap();
|
||||
|
||||
// let app = browse.fetch_musicbrainz();
|
||||
// app.unwrap_error();
|
||||
// }
|
||||
let (album_1, matches_1) = album_expectations_1();
|
||||
|
||||
// #[test]
|
||||
// fn fetch_musicbrainz_artist_receiver_disconnect() {
|
||||
// let (tx, rx) = mpsc::channel::<FetchResult>();
|
||||
// drop(rx);
|
||||
let mut seq = Sequence::new();
|
||||
search_release_group_expectation(&mut mb_api, &mut seq, &arid, &album_1, &matches_1);
|
||||
|
||||
// let mut mb_api = MockIMusicBrainz::new();
|
||||
let music_hoard = music_hoard(COLLECTION.to_owned());
|
||||
let (events_tx, _) = event_channel();
|
||||
let inner = AppInner::new(music_hoard, mb_api, events_tx);
|
||||
|
||||
// mb_api
|
||||
// .expect_search_artist()
|
||||
// .times(1)
|
||||
// .return_once(|_| Ok(vec![]));
|
||||
let (fetch_tx, fetch_rx) = mpsc::channel();
|
||||
// Use the second artist for this test.
|
||||
let handle = AppMachine::spawn_fetch_thread(&inner, &COLLECTION[1], fetch_tx);
|
||||
handle.join().unwrap();
|
||||
|
||||
// // We only check it does not panic and that it doesn't call the API more than once.
|
||||
// AppMachine::fetch_artist(Arc::new(Mutex::new(mb_api)), tx, COLLECTION[3].meta.clone());
|
||||
// }
|
||||
let result = fetch_rx.try_recv().unwrap();
|
||||
let expected = Ok(AppMatchesInfo::Album(AppAlbumMatches {
|
||||
matching: album_1.clone(),
|
||||
list: matches_1.iter().cloned().map(Into::into).collect(),
|
||||
}));
|
||||
assert_eq!(result, expected);
|
||||
|
||||
// #[test]
|
||||
// fn fetch_musicbrainz_albums_receiver_disconnect() {
|
||||
// let (tx, rx) = mpsc::channel::<FetchResult>();
|
||||
// drop(rx);
|
||||
|
||||
// let mut mb_api = MockIMusicBrainz::new();
|
||||
|
||||
// mb_api
|
||||
// .expect_search_release_group()
|
||||
// .times(1)
|
||||
// .return_once(|_, _| Ok(vec![]));
|
||||
|
||||
// // We only check it does not panic and that it doesn't call the API more than once.
|
||||
// let mbref = &COLLECTION[1].meta.musicbrainz;
|
||||
// let albums = &COLLECTION[1].albums;
|
||||
// AppMachine::fetch_albums(
|
||||
// Arc::new(Mutex::new(mb_api)),
|
||||
// tx,
|
||||
// mbref.as_ref().unwrap().mbid().clone(),
|
||||
// albums.iter().map(|a| &a.meta).cloned().collect(),
|
||||
// );
|
||||
// }
|
||||
assert_eq!(fetch_rx.try_recv().unwrap_err(), TryRecvError::Disconnected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_op() {
|
||||
|
@ -57,4 +57,11 @@ mod tests {
|
||||
let app = error.dismiss_error();
|
||||
app.unwrap_browse();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_op() {
|
||||
let error = AppMachine::error(inner(music_hoard(vec![])), "get rekt");
|
||||
let app = error.no_op();
|
||||
app.unwrap_error();
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,11 @@
|
||||
use std::{
|
||||
sync::{
|
||||
mpsc::{self, TryRecvError},
|
||||
Arc, Mutex,
|
||||
},
|
||||
thread, time,
|
||||
};
|
||||
|
||||
use musichoard::collection::{
|
||||
album::AlbumMeta,
|
||||
artist::ArtistMeta,
|
||||
musicbrainz::{IMusicBrainzRef, Mbid},
|
||||
};
|
||||
use std::sync::mpsc::{self, TryRecvError};
|
||||
|
||||
use crate::tui::{
|
||||
app::{
|
||||
machine::{App, AppInner, AppMachine},
|
||||
AppMatchesInfo, AppPublic, AppState, IAppEventFetch, IAppInteractFetch,
|
||||
},
|
||||
event::{Event, EventSender},
|
||||
lib::interface::musicbrainz::{Error as MbError, IMusicBrainz},
|
||||
lib::interface::musicbrainz::Error as MbError,
|
||||
};
|
||||
|
||||
use super::matches::AppMatches;
|
||||
@ -27,45 +14,35 @@ pub struct AppFetch {
|
||||
fetch_rx: FetchReceiver,
|
||||
}
|
||||
|
||||
impl AppFetch {
|
||||
pub fn new(fetch_rx: FetchReceiver) -> Self {
|
||||
AppFetch { fetch_rx }
|
||||
}
|
||||
}
|
||||
|
||||
pub type FetchError = MbError;
|
||||
pub type FetchResult = Result<AppMatchesInfo, FetchError>;
|
||||
pub type FetchSender = mpsc::Sender<FetchResult>;
|
||||
pub type FetchReceiver = mpsc::Receiver<FetchResult>;
|
||||
|
||||
impl AppMachine<AppFetch> {
|
||||
fn fetch(inner: AppInner, state: AppFetch) -> Self {
|
||||
AppMachine { inner, state }
|
||||
}
|
||||
|
||||
pub fn app_fetch_new(inner: AppInner) -> App {
|
||||
let coll = inner.music_hoard.get_collection();
|
||||
let artist = match inner.selection.state_artist(coll) {
|
||||
Some(artist_state) => &coll[artist_state.index],
|
||||
None => return AppMachine::error(inner, "cannot fetch: no artist selected").into(),
|
||||
};
|
||||
|
||||
let (fetch_tx, fetch_rx) = mpsc::channel::<FetchResult>();
|
||||
|
||||
match artist.meta.musicbrainz {
|
||||
Some(ref arid) => {
|
||||
let musicbrainz = Arc::clone(&inner.musicbrainz);
|
||||
let events = inner.events.clone();
|
||||
let arid = arid.mbid().clone();
|
||||
let albums = artist.albums.iter().map(|a| &a.meta).cloned().collect();
|
||||
thread::spawn(|| Self::fetch_albums(musicbrainz, fetch_tx, events, arid, albums));
|
||||
}
|
||||
None => {
|
||||
let musicbrainz = Arc::clone(&inner.musicbrainz);
|
||||
let events = inner.events.clone();
|
||||
let artist = artist.meta.clone();
|
||||
thread::spawn(|| Self::fetch_artist(musicbrainz, fetch_tx, events, artist));
|
||||
}
|
||||
};
|
||||
|
||||
let fetch = AppFetch { fetch_rx };
|
||||
AppMachine::app_fetch_next(inner, fetch, true)
|
||||
pub fn app_fetch_new(inner: AppInner, fetch_rx: FetchReceiver) -> App {
|
||||
let fetch = AppFetch::new(fetch_rx);
|
||||
AppMachine::app_fetch(inner, fetch, true)
|
||||
}
|
||||
|
||||
pub fn app_fetch_next(inner: AppInner, fetch: AppFetch, first: bool) -> App {
|
||||
pub fn app_fetch_next(inner: AppInner, fetch: AppFetch) -> App {
|
||||
Self::app_fetch(inner, fetch, false)
|
||||
}
|
||||
|
||||
fn app_fetch(inner: AppInner, fetch: AppFetch, first: bool) -> App {
|
||||
match fetch.fetch_rx.try_recv() {
|
||||
Ok(fetch_result) => match fetch_result {
|
||||
Ok(mut next_match) => {
|
||||
next_match.push_cannot_have_mbid();
|
||||
Ok(next_match) => {
|
||||
let current = Some(next_match);
|
||||
AppMachine::matches(inner, AppMatches::new(current, fetch)).into()
|
||||
}
|
||||
@ -74,14 +51,14 @@ impl AppMachine<AppFetch> {
|
||||
}
|
||||
},
|
||||
Err(recv_err) => match recv_err {
|
||||
TryRecvError::Empty => AppMachine::fetch(inner, fetch).into(),
|
||||
TryRecvError::Disconnected => {
|
||||
if first {
|
||||
AppMachine::matches(inner, AppMatches::empty(fetch)).into()
|
||||
AppMachine::matches(inner, AppMatches::new(None, fetch)).into()
|
||||
} else {
|
||||
AppMachine::browse(inner).into()
|
||||
}
|
||||
}
|
||||
TryRecvError::Empty => AppMachine::fetch(inner, fetch).into(),
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -118,77 +95,104 @@ impl IAppEventFetch for AppMachine<AppFetch> {
|
||||
type APP = App;
|
||||
|
||||
fn fetch_result_ready(self) -> Self::APP {
|
||||
Self::app_fetch_next(self.inner, self.state, false)
|
||||
Self::app_fetch_next(self.inner, self.state)
|
||||
}
|
||||
}
|
||||
|
||||
type FetchError = MbError;
|
||||
type FetchResult = Result<AppMatchesInfo, FetchError>;
|
||||
type FetchSender = mpsc::Sender<FetchResult>;
|
||||
type FetchReceiver = mpsc::Receiver<FetchResult>;
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use musichoard::collection::artist::ArtistMeta;
|
||||
|
||||
trait IAppInteractFetchPrivate {
|
||||
fn fetch_artist(
|
||||
musicbrainz: Arc<Mutex<dyn IMusicBrainz + Send>>,
|
||||
fetch_tx: FetchSender,
|
||||
events: EventSender,
|
||||
artist: ArtistMeta,
|
||||
);
|
||||
fn fetch_albums(
|
||||
musicbrainz: Arc<Mutex<dyn IMusicBrainz + Send>>,
|
||||
fetch_tx: FetchSender,
|
||||
events: EventSender,
|
||||
arid: Mbid,
|
||||
albums: Vec<AlbumMeta>,
|
||||
);
|
||||
use crate::tui::{
|
||||
app::machine::tests::{inner, music_hoard},
|
||||
lib::interface::musicbrainz::{self, Match},
|
||||
testmod::COLLECTION,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn recv_ok_fetch_ok() {
|
||||
let (tx, rx) = mpsc::channel::<FetchResult>();
|
||||
|
||||
let artist = COLLECTION[3].meta.clone();
|
||||
let fetch_result = Ok(AppMatchesInfo::artist::<Match<ArtistMeta>>(artist, vec![]));
|
||||
tx.send(fetch_result).unwrap();
|
||||
|
||||
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
|
||||
assert!(matches!(app, AppState::Matches(_)));
|
||||
}
|
||||
|
||||
impl IAppInteractFetchPrivate for AppMachine<AppFetch> {
|
||||
fn fetch_artist(
|
||||
musicbrainz: Arc<Mutex<dyn IMusicBrainz + Send>>,
|
||||
fetch_tx: FetchSender,
|
||||
events: EventSender,
|
||||
artist: ArtistMeta,
|
||||
) {
|
||||
let result = musicbrainz.lock().unwrap().search_artist(&artist);
|
||||
let result = result.map(|list| AppMatchesInfo::artist(artist, list));
|
||||
if fetch_tx.send(result).is_ok() {
|
||||
// If this send fails the event listener is dead. Don't panic as this function runs
|
||||
// in a detached thread so this might be happening during normal shut down.
|
||||
events.send(Event::FetchResultReady).ok();
|
||||
}
|
||||
#[test]
|
||||
fn recv_ok_fetch_err() {
|
||||
let (tx, rx) = mpsc::channel::<FetchResult>();
|
||||
|
||||
let fetch_result = Err(musicbrainz::Error::RateLimit);
|
||||
tx.send(fetch_result).unwrap();
|
||||
|
||||
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
|
||||
assert!(matches!(app, AppState::Error(_)));
|
||||
}
|
||||
|
||||
fn fetch_albums(
|
||||
musicbrainz: Arc<Mutex<dyn IMusicBrainz + Send>>,
|
||||
fetch_tx: FetchSender,
|
||||
events: EventSender,
|
||||
arid: Mbid,
|
||||
albums: Vec<AlbumMeta>,
|
||||
) {
|
||||
let mut musicbrainz = musicbrainz.lock().unwrap();
|
||||
let mut album_iter = albums.into_iter().peekable();
|
||||
while let Some(album) = album_iter.next() {
|
||||
if album.musicbrainz.is_some() {
|
||||
continue;
|
||||
#[test]
|
||||
fn recv_err_empty() {
|
||||
let (_tx, rx) = mpsc::channel::<FetchResult>();
|
||||
|
||||
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
|
||||
assert!(matches!(app, AppState::Fetch(_)));
|
||||
}
|
||||
|
||||
let result = musicbrainz.search_release_group(&arid, &album);
|
||||
let result = result.map(|list| AppMatchesInfo::album(album, list));
|
||||
if fetch_tx.send(result).is_err() {
|
||||
// If receiver disconnects just drop the rest.
|
||||
return;
|
||||
#[test]
|
||||
fn recv_err_disconnected_first() {
|
||||
let (_, rx) = mpsc::channel::<FetchResult>();
|
||||
|
||||
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
|
||||
assert!(matches!(app, AppState::Matches(_)));
|
||||
}
|
||||
|
||||
if events.send(Event::FetchResultReady).is_err() {
|
||||
// If this send fails the event listener is dead. Don't panic as this function runs
|
||||
// in a detached thread so this might be happening during normal shut down.
|
||||
return;
|
||||
#[test]
|
||||
fn recv_err_disconnected_next() {
|
||||
let (_, rx) = mpsc::channel::<FetchResult>();
|
||||
|
||||
let fetch = AppFetch::new(rx);
|
||||
let app = AppMachine::app_fetch_next(inner(music_hoard(COLLECTION.clone())), fetch);
|
||||
assert!(matches!(app, AppState::Browse(_)));
|
||||
}
|
||||
|
||||
if album_iter.peek().is_some() {
|
||||
thread::sleep(time::Duration::from_secs(1));
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn empty_first_then_ready() {
|
||||
let (tx, rx) = mpsc::channel::<FetchResult>();
|
||||
|
||||
let app = AppMachine::app_fetch_new(inner(music_hoard(COLLECTION.clone())), rx);
|
||||
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();
|
||||
|
||||
let app = app.unwrap_fetch().fetch_result_ready();
|
||||
assert!(matches!(app, AppState::Matches(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn abort() {
|
||||
let (_, rx) = mpsc::channel::<FetchResult>();
|
||||
|
||||
let fetch = AppFetch::new(rx);
|
||||
let app = AppMachine::fetch(inner(music_hoard(COLLECTION.clone())), fetch);
|
||||
|
||||
let app = app.abort();
|
||||
assert!(matches!(app, AppState::Browse(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_op() {
|
||||
let (_, rx) = mpsc::channel::<FetchResult>();
|
||||
|
||||
let fetch = AppFetch::new(rx);
|
||||
let app = AppMachine::fetch(inner(music_hoard(COLLECTION.clone())), fetch);
|
||||
|
||||
let app = app.no_op();
|
||||
assert!(matches!(app, AppState::Fetch(_)));
|
||||
}
|
||||
}
|
||||
|
@ -51,14 +51,11 @@ pub struct AppMatches {
|
||||
}
|
||||
|
||||
impl AppMatches {
|
||||
pub fn empty(fetch: AppFetch) -> Self {
|
||||
Self::new(None, fetch)
|
||||
}
|
||||
|
||||
pub fn new(current: Option<AppMatchesInfo>, fetch: AppFetch) -> Self {
|
||||
pub fn new(mut current: Option<AppMatchesInfo>, fetch: AppFetch) -> Self {
|
||||
let mut state = WidgetState::default();
|
||||
if current.is_some() {
|
||||
if let Some(ref mut current) = current {
|
||||
state.list.select(Some(0));
|
||||
current.push_cannot_have_mbid();
|
||||
}
|
||||
AppMatches {
|
||||
current,
|
||||
@ -119,7 +116,7 @@ impl IAppInteractMatches for AppMachine<AppMatches> {
|
||||
}
|
||||
|
||||
fn select(self) -> Self::APP {
|
||||
AppMachine::app_fetch_next(self.inner, self.state.fetch, false)
|
||||
AppMachine::app_fetch_next(self.inner, self.state.fetch)
|
||||
}
|
||||
|
||||
fn abort(self) -> Self::APP {
|
||||
@ -160,209 +157,169 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn artist_matches_info_vec() -> Vec<AppMatchesInfo> {
|
||||
let artist_1 = ArtistMeta::new(ArtistId::new("Artist 1"));
|
||||
fn artist_match() -> AppMatchesInfo {
|
||||
let artist = ArtistMeta::new(ArtistId::new("Artist"));
|
||||
|
||||
let artist_1_1 = artist_1.clone();
|
||||
let artist_match_1_1 = Match::new(100, artist_1_1);
|
||||
let artist_1 = artist.clone();
|
||||
let artist_match_1 = Match::new(100, artist_1);
|
||||
|
||||
let artist_1_2 = artist_1.clone();
|
||||
let mut artist_match_1_2 = Match::new(100, artist_1_2);
|
||||
artist_match_1_2.disambiguation = Some(String::from("some disambiguation"));
|
||||
let artist_2 = artist.clone();
|
||||
let mut artist_match_2 = Match::new(100, artist_2);
|
||||
artist_match_2.disambiguation = Some(String::from("some disambiguation"));
|
||||
|
||||
let list = vec![artist_match_1_1.clone(), artist_match_1_2.clone()];
|
||||
let matches_info_1 = AppMatchesInfo::artist(artist_1.clone(), list);
|
||||
|
||||
let artist_2 = ArtistMeta::new(ArtistId::new("Artist 2"));
|
||||
|
||||
let artist_2_1 = artist_1.clone();
|
||||
let album_match_2_1 = Match::new(100, artist_2_1);
|
||||
|
||||
let list = vec![album_match_2_1.clone()];
|
||||
let matches_info_2 = AppMatchesInfo::artist(artist_2.clone(), list);
|
||||
|
||||
vec![matches_info_1, matches_info_2]
|
||||
let list = vec![artist_match_1.clone(), artist_match_2.clone()];
|
||||
AppMatchesInfo::artist(artist, list)
|
||||
}
|
||||
|
||||
fn album_matches_info_vec() -> Vec<AppMatchesInfo> {
|
||||
let album_1 = AlbumMeta::new(
|
||||
AlbumId::new("Album 1"),
|
||||
fn album_match() -> AppMatchesInfo {
|
||||
let album = AlbumMeta::new(
|
||||
AlbumId::new("Album"),
|
||||
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 album_1 = album.clone();
|
||||
let album_match_1 = Match::new(100, album_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 mut album_2 = album.clone();
|
||||
album_2.id.title.push_str(" extra title part");
|
||||
album_2.secondary_types.pop();
|
||||
let album_match_2 = Match::new(100, album_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 list = vec![album_match_1.clone(), album_match_2.clone()];
|
||||
AppMatchesInfo::album(album, list)
|
||||
}
|
||||
|
||||
let album_2 = AlbumMeta::new(
|
||||
AlbumId::new("Album 2"),
|
||||
AlbumDate::new(Some(2001), None, None),
|
||||
Some(AlbumPrimaryType::Album),
|
||||
vec![],
|
||||
fn fetch() -> AppFetch {
|
||||
let (_, rx) = mpsc::channel();
|
||||
AppFetch::new(rx)
|
||||
}
|
||||
|
||||
fn matches(matches_info: Option<AppMatchesInfo>) -> AppMatches {
|
||||
AppMatches::new(matches_info, fetch())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_empty() {
|
||||
let matches = AppMachine::matches(inner(music_hoard(vec![])), matches(None));
|
||||
|
||||
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 album_match = album_match();
|
||||
let matches = AppMachine::matches(
|
||||
inner(music_hoard(vec![])),
|
||||
matches(Some(album_match.clone())),
|
||||
);
|
||||
album_match.push_cannot_have_mbid();
|
||||
|
||||
let album_2_1 = album_1.clone();
|
||||
let album_match_2_1 = Match::new(100, album_2_1);
|
||||
let mut widget_state = WidgetState::default();
|
||||
widget_state.list.select(Some(0));
|
||||
|
||||
let list = vec![album_match_2_1.clone()];
|
||||
let matches_info_2 = AppMatchesInfo::album(album_2.clone(), list);
|
||||
assert_eq!(matches.state.current.as_ref(), Some(&album_match));
|
||||
assert_eq!(matches.state.state, widget_state);
|
||||
|
||||
vec![matches_info_1, matches_info_2]
|
||||
let mut app = matches.no_op();
|
||||
let public = app.get();
|
||||
let public_matches = public.state.unwrap_matches();
|
||||
|
||||
assert_eq!(public_matches.matches, Some(&album_match));
|
||||
assert_eq!(public_matches.state, &widget_state);
|
||||
}
|
||||
|
||||
// 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 matches_flow(mut matches_info: AppMatchesInfo) {
|
||||
// tx must exist for rx to return Empty rather than Disconnected.
|
||||
#[allow(unused_variables)]
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let app_matches = AppMatches::new(Some(matches_info.clone()), AppFetch::new(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();
|
||||
// }
|
||||
// }
|
||||
let matches = AppMachine::matches(inner(music_hoard(vec![])), app_matches);
|
||||
matches_info.push_cannot_have_mbid();
|
||||
|
||||
// #[test]
|
||||
// fn create_empty() {
|
||||
// let matches =
|
||||
// AppMachine::app_matches(inner(music_hoard(vec![])), receiver(vec![])).unwrap_matches();
|
||||
let mut widget_state = WidgetState::default();
|
||||
widget_state.list.select(Some(0));
|
||||
|
||||
// let widget_state = WidgetState::default();
|
||||
assert_eq!(matches.state.current.as_ref(), Some(&matches_info));
|
||||
assert_eq!(matches.state.state, widget_state);
|
||||
|
||||
// assert_eq!(matches.state.current, None);
|
||||
// assert_eq!(matches.state.state, widget_state);
|
||||
let matches = matches.prev_match().unwrap_matches();
|
||||
|
||||
// let mut app = matches.no_op();
|
||||
// let public = app.get();
|
||||
// let public_matches = public.state.unwrap_matches();
|
||||
assert_eq!(matches.state.current.as_ref(), Some(&matches_info));
|
||||
assert_eq!(matches.state.state.list.selected(), Some(0));
|
||||
|
||||
// assert_eq!(public_matches.matches, None);
|
||||
// assert_eq!(public_matches.state, &widget_state);
|
||||
// }
|
||||
let matches = matches.next_match().unwrap_matches();
|
||||
|
||||
// #[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);
|
||||
assert_eq!(matches.state.current.as_ref(), Some(&matches_info));
|
||||
assert_eq!(matches.state.state.list.selected(), Some(1));
|
||||
|
||||
// let mut widget_state = WidgetState::default();
|
||||
// widget_state.list.select(Some(0));
|
||||
// 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, widget_state);
|
||||
assert_eq!(matches.state.current.as_ref(), Some(&matches_info));
|
||||
assert_eq!(matches.state.state.list.selected(), Some(2));
|
||||
|
||||
// let mut app = matches.no_op();
|
||||
// let public = app.get();
|
||||
// let public_matches = public.state.unwrap_matches();
|
||||
let matches = matches.next_match().unwrap_matches();
|
||||
|
||||
// assert_eq!(public_matches.matches, Some(&matches_info_vec[0]));
|
||||
// assert_eq!(public_matches.state, &widget_state);
|
||||
// }
|
||||
assert_eq!(matches.state.current.as_ref(), Some(&matches_info));
|
||||
assert_eq!(matches.state.state.list.selected(), Some(2));
|
||||
|
||||
// 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();
|
||||
// }
|
||||
// And it's done
|
||||
matches.select().unwrap_fetch();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artist_matches_flow() {
|
||||
matches_flow(artist_match());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn album_matches_flow() {
|
||||
matches_flow(album_match());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matches_abort() {
|
||||
let mut album_match = album_match();
|
||||
let matches = AppMachine::matches(
|
||||
inner(music_hoard(vec![])),
|
||||
matches(Some(album_match.clone())),
|
||||
);
|
||||
album_match.push_cannot_have_mbid();
|
||||
|
||||
let mut widget_state = WidgetState::default();
|
||||
widget_state.list.select(Some(0));
|
||||
|
||||
assert_eq!(matches.state.current.as_ref(), Some(&album_match));
|
||||
assert_eq!(matches.state.state, widget_state);
|
||||
|
||||
matches.abort().unwrap_browse();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matches_select_empty() {
|
||||
// Note that what really matters in this test is actually that the transmit channel has
|
||||
// disconnected and so the receive within AppFetch concludes there are no more matches.
|
||||
let matches = AppMachine::matches(inner(music_hoard(vec![])), matches(None));
|
||||
matches.select().unwrap_browse();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_op() {
|
||||
let matches = AppMachine::matches(inner(music_hoard(vec![])), matches(None));
|
||||
let app = matches.no_op();
|
||||
app.unwrap_matches();
|
||||
}
|
||||
}
|
||||
|
@ -170,7 +170,8 @@ mod tests {
|
||||
|
||||
use crate::tui::{
|
||||
app::{AppState, IAppInteract, IAppInteractBrowse},
|
||||
lib::{interface::musicbrainz::MockIMusicBrainz, MockIMusicHoard}, EventChannel,
|
||||
lib::{interface::musicbrainz::MockIMusicBrainz, MockIMusicHoard},
|
||||
EventChannel,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
@ -337,24 +338,46 @@ mod tests {
|
||||
assert!(!app.is_running());
|
||||
}
|
||||
|
||||
// #[test]
|
||||
// fn state_matches() {
|
||||
// let mut app = App::new(music_hoard_init(vec![]), mb_api());
|
||||
// assert!(app.is_running());
|
||||
#[test]
|
||||
fn state_fetch() {
|
||||
let mut app = App::new(music_hoard_init(vec![]), mb_api(), events());
|
||||
assert!(app.is_running());
|
||||
|
||||
// let (_, rx) = mpsc::channel();
|
||||
// app = AppMachine::app_matches(app.unwrap_browse().inner, rx);
|
||||
let (_, rx) = mpsc::channel();
|
||||
let inner = app.unwrap_browse().inner;
|
||||
let state = AppFetch::new(rx);
|
||||
app = AppMachine { inner, state }.into();
|
||||
|
||||
// let state = app.state();
|
||||
// assert!(matches!(state, AppState::Matches(_)));
|
||||
// app = state;
|
||||
let state = app.state();
|
||||
assert!(matches!(state, AppState::Fetch(_)));
|
||||
app = state;
|
||||
|
||||
// let public = app.get();
|
||||
// assert!(matches!(public.state, AppState::Matches(_)));
|
||||
let public = app.get();
|
||||
assert!(matches!(public.state, AppState::Fetch(_)));
|
||||
|
||||
// let app = app.force_quit();
|
||||
// assert!(!app.is_running());
|
||||
// }
|
||||
let app = app.force_quit();
|
||||
assert!(!app.is_running());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn state_matches() {
|
||||
let mut app = App::new(music_hoard_init(vec![]), mb_api(), events());
|
||||
assert!(app.is_running());
|
||||
|
||||
let (_, rx) = mpsc::channel();
|
||||
let fetch = AppFetch::new(rx);
|
||||
app = AppMachine::matches(app.unwrap_browse().inner, AppMatches::new(None, fetch)).into();
|
||||
|
||||
let state = app.state();
|
||||
assert!(matches!(state, AppState::Matches(_)));
|
||||
app = state;
|
||||
|
||||
let public = app.get();
|
||||
assert!(matches!(public.state, AppState::Matches(_)));
|
||||
|
||||
let app = app.force_quit();
|
||||
assert!(!app.is_running());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn state_error() {
|
||||
|
@ -33,7 +33,13 @@ impl From<mpsc::RecvError> for EventError {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
impl From<mpsc::TryRecvError> for EventError {
|
||||
fn from(_: mpsc::TryRecvError) -> EventError {
|
||||
EventError::Recv
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Event {
|
||||
Key(KeyEvent),
|
||||
FetchResultReady,
|
||||
@ -82,6 +88,11 @@ impl EventReceiver {
|
||||
pub fn recv(&self) -> Result<Event, EventError> {
|
||||
Ok(self.receiver.recv()?)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn try_recv(&self) -> Result<Event, EventError> {
|
||||
Ok(self.receiver.try_recv()?)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -123,6 +134,25 @@ mod tests {
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn event_receiver_try() {
|
||||
let channel = EventChannel::new();
|
||||
let sender = channel.sender();
|
||||
let receiver = channel.receiver();
|
||||
let event = Event::FetchResultReady;
|
||||
|
||||
let result = receiver.try_recv();
|
||||
assert!(result.is_err());
|
||||
|
||||
sender.send(event).unwrap();
|
||||
let result = receiver.try_recv();
|
||||
assert!(result.is_ok());
|
||||
|
||||
drop(sender);
|
||||
let result = receiver.try_recv();
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors() {
|
||||
let send_err = EventError::Send(Event::Key(KeyEvent {
|
||||
|
9
src/tui/ui/fetch.rs
Normal file
9
src/tui/ui/fetch.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use ratatui::widgets::Paragraph;
|
||||
|
||||
pub struct FetchOverlay;
|
||||
|
||||
impl FetchOverlay {
|
||||
pub fn paragraph<'a>() -> Paragraph<'a> {
|
||||
Paragraph::new(" -- fetching --")
|
||||
}
|
||||
}
|
@ -57,10 +57,7 @@ impl Minibuffer<'_> {
|
||||
columns,
|
||||
},
|
||||
AppState::Fetch(()) => Minibuffer {
|
||||
paragraphs: vec![
|
||||
Paragraph::new(format!("fetching...")),
|
||||
Paragraph::new(format!("q: abort")),
|
||||
],
|
||||
paragraphs: vec![Paragraph::new("fetching..."), Paragraph::new("q: abort")],
|
||||
columns: 2,
|
||||
},
|
||||
AppState::Matches(public) => Minibuffer {
|
||||
|
@ -1,6 +1,7 @@
|
||||
mod browse;
|
||||
mod display;
|
||||
mod error;
|
||||
mod fetch;
|
||||
mod info;
|
||||
mod matches;
|
||||
mod minibuffer;
|
||||
@ -9,6 +10,7 @@ mod reload;
|
||||
mod style;
|
||||
mod widgets;
|
||||
|
||||
use fetch::FetchOverlay;
|
||||
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
|
||||
|
||||
use musichoard::collection::{album::Album, Collection};
|
||||
@ -125,21 +127,14 @@ impl Ui {
|
||||
.with_width(OverlaySize::Value(39))
|
||||
.with_height(OverlaySize::Value(4))
|
||||
.build(frame.size());
|
||||
|
||||
let reload_text = ReloadOverlay::paragraph();
|
||||
|
||||
UiWidget::render_overlay_widget("Reload", reload_text, area, false, frame);
|
||||
}
|
||||
|
||||
fn render_fetch_overlay(frame: &mut Frame) {
|
||||
let area = OverlayBuilder::default().build(frame.size());
|
||||
UiWidget::render_overlay_widget(
|
||||
"Fetching",
|
||||
Paragraph::new(" -- fetching --"),
|
||||
area,
|
||||
false,
|
||||
frame,
|
||||
)
|
||||
let fetch_text = FetchOverlay::paragraph();
|
||||
UiWidget::render_overlay_widget("Fetching", fetch_text, area, false, frame)
|
||||
}
|
||||
|
||||
fn render_matches_overlay(
|
||||
@ -156,9 +151,7 @@ impl Ui {
|
||||
let area = OverlayBuilder::default()
|
||||
.with_height(OverlaySize::Value(4))
|
||||
.build(frame.size());
|
||||
|
||||
let error_text = ErrorOverlay::paragraph(msg.as_ref());
|
||||
|
||||
UiWidget::render_overlay_widget(title.as_ref(), error_text, area, true, frame);
|
||||
}
|
||||
}
|
||||
@ -267,6 +260,9 @@ mod tests {
|
||||
app.state = AppState::Search("");
|
||||
terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap();
|
||||
|
||||
app.state = AppState::Fetch(());
|
||||
terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap();
|
||||
|
||||
app.state = AppState::Error("get rekt scrub");
|
||||
terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user