use super::*; /// RULE (Music definition, 4 Nov 2026): the folder and the file's name fill only what the tags left empty: `Artist/Album (Year)/01 Title.ext`. #[test] fn a_path_says_its_artist_album_year_track_and_title() { let facts = path_facts("Imported/The Paper Lanterns/Harbour Lights (2019)/03 Low Tide.flac"); assert_eq!(facts.artist.as_deref(), Some("The Paper Lanterns")); assert_eq!(facts.album.as_deref(), Some("Harbour Lights")); assert_eq!(facts.year.as_deref(), Some("2019")); assert_eq!(facts.track, Some(3)); assert_eq!(facts.title.as_deref(), Some("Low Tide")); let disc = path_facts("Ana Vell/Two Rooms/CD2/1-04 - The Stair.mp3"); assert_eq!((disc.artist.as_deref(), disc.album.as_deref(), disc.disc, disc.track, disc.title.as_deref()), (Some("Ana Vell"), Some("Two Rooms"), Some(1), Some(4), Some("The Stair"))); let loose = path_facts("Ana Vell - Morning Bell.mp3"); assert_eq!((loose.artist.as_deref(), loose.album, loose.track, loose.title.as_deref()), (Some("Ana Vell"), None, None, Some("Morning Bell"))); assert_eq!(path_facts("2046.mp3").title.as_deref(), Some("2046"), "a name that is only a number is the title, never a track"); } /// RULE (Music definition, 4 Nov 2026): tags come first; a song with no tags takes its row from its path. #[test] fn tags_come_first_and_the_path_fills_only_what_they_left() { let path = "Ana Vell/Two Rooms (2021)/02 From The Folder.mp3"; let tagged = Tags { title: Some("From The Tags".into()), artist: Some("Ana Vell feat. Oru".into()), track: Some(7), duration: Some(201.4), bitrate: Some(320), ..Tags::default() }; let song = song_from(&tagged, path, "mp3").unwrap(); assert_eq!(song.title, "From The Tags"); assert_eq!(song.artist_name.as_deref(), Some("Ana Vell feat. Oru"), "the credit as tagged"); assert_eq!(song.album_artist.as_deref(), Some("Ana Vell"), "the folder names the album artist the tags left empty"); assert_eq!((song.album_title.as_deref(), song.year.as_deref(), song.track_number), (Some("Two Rooms"), Some("2021"), Some(7))); assert_eq!((song.source, song.format.as_str()), ("file", "mp3")); let bare = song_from(&Tags::default(), path, "mp3").unwrap(); assert_eq!((bare.title.as_str(), bare.artist_name.as_deref(), bare.track_number, bare.source), ("From The Folder", Some("Ana Vell"), Some(2), "folder")); assert!(song_from(&Tags::default(), "a.epub", "epub").is_none()); } /// RULE (coordinator, 5 Nov 2026): a folder of downloads is no `Artist/Album/`: when the tags name an album and the folder another, the path's artist, album and year say nothing, and the song's own tagged artist is its album artist. #[test] fn a_flat_dump_takes_its_album_and_album_artist_from_the_tags() { let tags = Tags { title: Some("Paper Moon".into()), artist: Some("Ana Vell".into()), album: Some("Two Rooms".into()), ..Tags::default() }; let song = song_from(&tags, "scratchpad/music-in/Paper Moon.mp3", "mp3").unwrap(); assert_eq!(song.artist_name.as_deref(), Some("Ana Vell")); assert_eq!(song.album_title.as_deref(), Some("Two Rooms")); assert_eq!(song.album_artist.as_deref(), Some("Ana Vell"), "never the folder two above"); let dated = song_from(&tags, "scratchpad/music-in (2020)/Paper Moon.mp3", "mp3").unwrap(); assert_eq!(dated.year, None, "a disagreeing folder's year says nothing either"); let bare = Tags { title: Some("Paper Moon".into()), album: Some("Two Rooms".into()), ..Tags::default() }; assert_eq!(song_from(&bare, "scratchpad/music-in/Paper Moon.mp3", "mp3").unwrap().artist_name, None); } /// RULE (coordinator, 5 Nov 2026): a proper `Artist/Album (Year)/` layout whose album folds equal to the tags' names the album artist and the year the tags left empty. #[test] fn an_agreeing_layout_names_the_album_artist_and_year() { let tags = Tags { title: Some("Low Tide".into()), artist: Some("Ana Vell feat. Oru".into()), album: Some("two ROOMS".into()), ..Tags::default() }; let song = song_from(&tags, "Ana Vell/Two Rooms (2019)/01 Low Tide.mp3", "mp3").unwrap(); assert_eq!(song.album_artist.as_deref(), Some("Ana Vell"), "the folder, since the album agrees"); assert_eq!(song.artist_name.as_deref(), Some("Ana Vell feat. Oru")); assert_eq!((song.album_title.as_deref(), song.year.as_deref()), (Some("two ROOMS"), Some("2019"))); } /// RULE (coordinator, 5 Nov 2026): tags silent on the album leave the path's artist and album standing. #[test] fn tags_silent_on_the_album_leave_the_path_standing() { let tags = Tags { title: Some("Low Tide".into()), ..Tags::default() }; let song = song_from(&tags, "The Paper Lanterns/Harbour Lights (2019)/03 Low Tide.flac", "flac").unwrap(); assert_eq!( (song.artist_name.as_deref(), song.album_title.as_deref(), song.album_artist.as_deref(), song.year.as_deref()), (Some("The Paper Lanterns"), Some("Harbour Lights"), Some("The Paper Lanterns"), Some("2019")) ); } /// RULE (Music definition, 4 Nov 2026): "Various Artists" is one album artist; a compilation's songs gather under it. #[test] fn a_compilation_gathers_under_various_artists_and_keys_on_album_artist_and_title() { let tags = Tags { title: Some("One".into()), artist: Some("Oru".into()), album: Some("Night Mix".into()), compilation: Some(true), ..Tags::default() }; let song = song_from(&tags, "x.m4a", "m4a").unwrap(); assert_eq!(song.album_artist.as_deref(), Some(VARIOUS_ARTISTS)); let filed = song_from(&tags, "Oru/Night Mix/01 One.m4a", "m4a").unwrap(); assert_eq!(filed.album_artist.as_deref(), Some(VARIOUS_ARTISTS), "a compilation beats an agreeing folder's artist"); let dumped = song_from(&tags, "scratchpad/music-in/One.m4a", "m4a").unwrap(); assert_eq!(dumped.album_artist.as_deref(), Some(VARIOUS_ARTISTS)); assert_eq!(album_key("Various Artists", "Night Mix "), album_key("various artists", "night mix")); assert_ne!(album_key("A", "Night Mix"), album_key("B", "Night Mix")); } /// RULE (Music definition, 4 Nov 2026): the same song in two formats is one song, the better file kept: lossless over lossy, then bitrate. #[test] fn the_better_of_two_files_of_one_song_is_kept() { assert!(same_song(("Ana Vell", "Low Tide", "Harbour Lights", Some(200.0)), ("ana vell", "low tide", "Harbour Lights", Some(201.9)))); assert!(!same_song(("Ana Vell", "Low Tide", "Harbour Lights", Some(200.0)), ("Ana Vell", "Low Tide", "Harbour Lights", Some(202.5)))); assert!(!same_song(("Ana Vell", "Low Tide", "Live", Some(200.0)), ("Ana Vell", "Low Tide", "Harbour Lights", Some(200.0)))); assert!(better("flac", Some(900), "mp3", Some(320))); assert!(!better("mp3", Some(320), "alac", None)); assert!(better("mp3", Some(320), "aac", Some(256))); assert!(!better("mp3", Some(256), "mp3", Some(256)), "a tie keeps the one already here"); assert_eq!(format_of("m4a", Some(true)), Some("alac")); assert_eq!(format_of("M4A", None), Some("aac")); assert_eq!(format_of("ogg", None), Some("vorbis")); } /// RULE (Music definition, 4 Nov 2026): an artist sorts without a leading "The". #[test] fn an_artist_sorts_without_a_leading_the() { assert_eq!(sort_name("The Paper Lanterns"), "Paper Lanterns"); assert_eq!(sort_name("the band"), "band"); assert_eq!(sort_name("Theo Lin"), "Theo Lin"); assert_eq!(sort_name("The"), "The"); } /// RULE (Music definition, 4 Nov 2026): timed lyrics (SYLT or a .lrc beside the song) go into synced_lyrics, and the plain words come from them when the file has none. #[test] fn timed_lyrics_keep_their_source_and_give_the_plain_words() { let lrc = "[ar:Ana Vell]\n[00:01.00]First line\n[00:04.50][00:20.00]Second line\n"; assert_eq!(plain_of_lrc(lrc), "First line\nSecond line"); let beside = Tags { synced_lyrics: Some(lrc.into()), lyrics_beside: Some(true), ..Tags::default() }; let song = song_from(&beside, "a/b/01 c.flac", "flac").unwrap(); assert_eq!((song.lyrics_source, song.lyrics.as_deref()), (Some("sidecar"), Some("First line\nSecond line"))); let embedded = Tags { lyrics: Some("Words".into()), ..Tags::default() }; assert_eq!(song_from(&embedded, "x.mp3", "mp3").unwrap().lyrics_source, Some("file")); } /// RULE (Music definition, 4 Nov 2026): the iTunes purchaser atoms are never read: a reader cannot even hand them over. #[test] fn a_reader_cannot_hand_over_the_purchaser_atoms() { let refused = serde_json::from_value::(serde_json::json!({"title": "x", "ownr": "someone"})); assert!(refused.is_err()); let refused = serde_json::from_value::(serde_json::json!({"title": "x", "purchaseDate": "2020"})); assert!(refused.is_err()); } /// RULE (open standards, 7 Nov 2026): the readers' field list is the shape a reader hands over: TAG_FIELDS names every field of Tags, in order, and nothing else. #[test] fn the_field_list_is_the_tags_shape() { let all = Tags { title: Some(String::new()), artist: Some(String::new()), album: Some(String::new()), album_artist: Some(String::new()), track: Some(1), disc: Some(1), discs: Some(1), year: Some(String::new()), genre: Some(String::new()), composer: Some(String::new()), compilation: Some(true), duration: Some(1.0), bitrate: Some(1), sample_rate: Some(1), lossless: Some(true), lyrics: Some(String::new()), synced_lyrics: Some(String::new()), lyrics_beside: Some(true), track_gain: Some(1.0), track_peak: Some(1.0), album_gain: Some(1.0), album_peak: Some(1.0), isrc: Some(String::new()), mb_recording: Some(String::new()), mb_release: Some(String::new()), mb_release_group: Some(String::new()), mb_artist: Some(String::new()), mb_album_artist: Some(String::new()), mb_release_track: Some(String::new()), artist_sort: Some(String::new()), album_artist_sort: Some(String::new()), original_date: Some(String::new()), label: Some(String::new()), catalog_number: Some(String::new()), barcode: Some(String::new()), release_type: Some(String::new()), }; let shape = serde_json::to_value(&all).unwrap(); let keys: Vec<&str> = shape.as_object().unwrap().keys().map(String::as_str).collect(); let mut listed = TAG_FIELDS.to_vec(); let mut sorted = keys.clone(); listed.sort_unstable(); sorted.sort_unstable(); assert_eq!(sorted, listed); } /// RULE (open standards, 7 Nov 2026): a tagged kind of release is lower-cased into album, single, ep, compilation, live, soundtrack or other; a secondary type says more than the primary. #[test] fn a_kind_of_release_reads_as_one_of_seven() { assert_eq!(release_type("Album"), Some("album")); assert_eq!(release_type("album; live"), Some("live")); assert_eq!(release_type("Album/Compilation"), Some("compilation")); assert_eq!(release_type("EP"), Some("ep")); assert_eq!(release_type("album\0soundtrack"), Some("soundtrack")); assert_eq!(release_type("broadcast"), Some("other")); assert_eq!(release_type(" "), None); for kind in ["album; live", "single", "spokenword"] { assert!(RELEASE_TYPES.contains(&release_type(kind).unwrap())); } assert_eq!(year_of("1997-03-02"), Some("1997".into())); assert_eq!(year_of("97"), None); } /// RULE (open standards, 7 Nov 2026): what a song's tags bring its album is one list for both doors: label, catalogue number, barcode, first release year and kind of release beside the year, discs, gains and MusicBrainz ids. #[test] fn a_songs_tags_bring_its_album_the_release_facts() { let tags = Tags { title: Some("Low Tide".into()), album: Some("Harbour Lights".into()), original_date: Some("1971-05-01".into()), release_type: Some("Album; Live".into()), label: Some(" Paper Records ".into()), catalog_number: Some("PR-001".into()), barcode: Some("0123456789012".into()), mb_release_track: Some("track-id".into()), mb_recording: Some("recording-id".into()), discs: Some(2), ..Tags::default() }; let facts = song_from(&tags, "x.flac", "flac").unwrap(); let fields: std::collections::BTreeMap<_, _> = album_fields(&facts, &tags).into_iter().collect(); assert_eq!(fields["original_year"], "1971"); assert_eq!(fields["release_type"], "live"); assert_eq!(fields["label"], "Paper Records"); assert_eq!(fields["catalog_number"], "PR-001"); assert_eq!(fields["barcode"], "0123456789012"); assert_eq!(fields["discs"], 2); assert!(fields.keys().all(|key| ALBUM_FIELDS.contains(key))); let row = song_row(&facts, "db/music/a.flac", None, None, "2026-11-07"); assert_eq!(row["mb_recording"], "recording-id"); assert_eq!(row["mb_release_track"], "track-id"); assert_eq!(sort_name_of("The Paper Lanterns", Some("Paper Lanterns, The")), "Paper Lanterns, The"); assert_eq!(sort_name_of("The Paper Lanterns", Some(" ")), "Paper Lanterns"); }