use std::{ fs, path::PathBuf, sync::{Arc, Mutex}, }; use once_cell::sync::Lazy; use musichoard::{ library::{ beets::{executor::BeetsLibraryProcessExecutor, BeetsLibrary}, Field, ILibrary, Item, Query, }, Artist, }; use crate::COLLECTION; static BEETS_EMPTY_CONFIG: Lazy>>> = Lazy::new(|| { Arc::new(Mutex::new(BeetsLibrary::new( BeetsLibraryProcessExecutor::default(), ))) }); static BEETS_TEST_CONFIG: Lazy>>> = Lazy::new(|| { Arc::new(Mutex::new(BeetsLibrary::new( BeetsLibraryProcessExecutor::default().config(Some( &fs::canonicalize("./tests/files/library/config.yml").unwrap(), )), ))) }); fn artist_to_items(artist: &Artist) -> Vec { let mut items = vec![]; for album in artist.albums.iter() { for track in album.tracks.iter() { items.push(Item { album_artist: artist.id.name.clone(), album_year: album.id.year, album_title: album.id.title.clone(), track_number: track.id.number, track_title: track.id.title.clone(), track_artist: track.artist.clone(), track_format: track.quality.format, track_bitrate: track.quality.bitrate, }); } } items } fn artists_to_items(artists: &[Artist]) -> Vec { let mut items = vec![]; for artist in artists.iter() { items.append(&mut artist_to_items(artist)); } items } #[test] fn test_no_config_list() { let beets_arc = BEETS_EMPTY_CONFIG.clone(); let beets = &mut beets_arc.lock().unwrap(); let output = beets.list(&Query::new()).unwrap(); let expected: Vec = vec![]; assert_eq!(output, expected); } #[test] fn test_invalid_config() { let mut beets = BeetsLibrary::new(BeetsLibraryProcessExecutor::default().config(Some( &PathBuf::from("./tests/files/library/config-does-not-exist.yml"), ))); let result = beets.list(&Query::new()); assert!(result.is_err()); assert!(!result.unwrap_err().to_string().is_empty()); } #[test] fn test_full_list() { let beets_arc = BEETS_TEST_CONFIG.clone(); let beets = &mut beets_arc.lock().unwrap(); let output = beets.list(&Query::new()).unwrap(); let expected: Vec = artists_to_items(&COLLECTION); assert_eq!(output, expected); } #[test] fn test_album_artist_query() { let beets_arc = BEETS_TEST_CONFIG.clone(); let beets = &mut beets_arc.lock().unwrap(); let output = beets .list(Query::new().include(Field::AlbumArtist(String::from("Аркона")))) .unwrap(); let expected: Vec = artists_to_items(&COLLECTION[0..1]); assert_eq!(output, expected); } #[test] fn test_album_title_query() { let beets_arc = BEETS_TEST_CONFIG.clone(); let beets = &mut beets_arc.lock().unwrap(); let output = beets .list(&Query::new().include(Field::AlbumTitle(String::from("Slovo")))) .unwrap(); let expected: Vec = artists_to_items(&COLLECTION[0..1]); assert_eq!(output, expected); } #[test] fn test_exclude_query() { let beets_arc = BEETS_TEST_CONFIG.clone(); let beets = &mut beets_arc.lock().unwrap(); let output = beets .list(&Query::new().exclude(Field::AlbumArtist(String::from("Аркона")))) .unwrap(); let expected: Vec = artists_to_items(&COLLECTION[1..]); assert_eq!(output, expected); }