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