use serde_json::{json, Value}; use super::*; fn ask(method: &str, request: Value) -> Value { call(method, &request).expect(method) } #[test] fn a_picked_folder_names_its_songs_their_lyrics_and_cover_and_its_lists() { let answer = ask( "music.picked", json!({ "paths": [ "Kelmoor/Harbour (2019)/01 Night Ferry.flac", "Kelmoor/Harbour (2019)/01 Night Ferry.lrc", "Kelmoor/Harbour (2019)/02 Lanterns.FLAC", "Kelmoor/Harbour (2019)/Folder.jpg", "Kelmoor/Harbour (2019)/cover.png", "Kelmoor/Harbour (2019)/notes.txt", "Kelmoor/Road songs.m3u8", "Kelmoor/03 Loose.mp3", ] }), ); assert_eq!( answer["songs"], json!([ { "path": "Kelmoor/Harbour (2019)/01 Night Ferry.flac", "lyrics": "Kelmoor/Harbour (2019)/01 Night Ferry.lrc", "cover": "Kelmoor/Harbour (2019)/cover.png" }, { "path": "Kelmoor/Harbour (2019)/02 Lanterns.FLAC", "cover": "Kelmoor/Harbour (2019)/cover.png" }, { "path": "Kelmoor/03 Loose.mp3" }, ]), "cover comes before folder whatever its kind; a folder with no picture names none" ); assert_eq!(answer["lists"], json!(["Kelmoor/Road songs.m3u8"])); assert_eq!( answer["stage"], json!(["Kelmoor/Harbour (2019)/01 Night Ferry.lrc", "Kelmoor/Harbour (2019)/cover.png", "Kelmoor/Road songs.m3u8"]), "only what the intake reads is staged: never the notes, never the unused folder picture" ); } #[test] fn files_picked_alone_are_songs_by_their_names() { let answer = ask("music.picked", json!({ "paths": ["one.mp3", "two.wav", "photo.jpg", "three.m4a"] })); assert_eq!(answer["songs"], json!([{ "path": "one.mp3" }, { "path": "two.wav" }, { "path": "three.m4a" }])); assert_eq!(answer["stage"], json!([])); assert!(call("music.picked", &json!({})).is_err()); } #[test] fn songs_gather_by_album_artist_and_album_in_the_order_they_were_met() { let answer = ask( "music.gathered", json!({ "songs": [ { "sourcePath": "A/One/01.flac", "tags": { "title": "x", "artist": "Kelmoor", "album": "Harbour" } }, { "sourcePath": "B/02.flac", "tags": { "title": "y", "artist": "Other", "album": "Lights" } }, { "sourcePath": "A/One/02.flac", "tags": { "title": "z", "albumArtist": "kelmoor ", "artist": "Guest", "album": "harbour" } }, { "sourcePath": "Mixes/Summer/01.mp3", "tags": { "title": "w", "artist": "Someone", "album": "Summer", "compilation": true } }, { "sourcePath": "Mixes/Summer/02.mp3", "tags": { "title": "v", "artist": "Another", "album": "Summer", "compilation": true } }, { "sourcePath": "Folder Artist/Folder Album/03.mp3", "tags": {} }, ] }), ); assert_eq!(answer["batches"], json!([[0, 2], [1], [3, 4], [5]])); } #[test] fn the_folders_name_an_album_its_tags_leave_out() { assert_eq!(gathering_key(&Tags::default(), "Kelmoor/Harbour/01.flac"), "kelmoor\u{1f}harbour"); assert_eq!(gathering_key(&Tags::default(), "01.flac"), "\u{1f}"); } #[test] fn an_album_of_more_than_one_write_is_split() { let songs: Vec = (0..MOST_IN_ONE + 3).map(|at| json!({ "sourcePath": format!("A/B/{at}.mp3"), "tags": {} })).collect(); let answer = ask("music.gathered", json!({ "songs": songs })); let batches = answer["batches"].as_array().unwrap(); assert_eq!(batches.len(), 2); assert_eq!(batches[0].as_array().unwrap().len(), MOST_IN_ONE); assert_eq!(batches[1], json!([MOST_IN_ONE, MOST_IN_ONE + 1, MOST_IN_ONE + 2])); } #[test] fn timed_lyrics_go_where_they_go() { let lrc = "[00:01.20] Night ferry\n[00:05.00] over the water\n\n[00:09.40] home"; let answer = ask( "music.gathered", json!({ "songs": [ { "sourcePath": "a.mp3", "tags": { "lyrics": lrc } }, { "sourcePath": "b.mp3", "tags": { "lyrics": "Plain words" }, "lrc": lrc }, { "sourcePath": "c.mp3", "tags": { "syncedLyrics": "[00:00] kept" }, "lrc": lrc }, { "sourcePath": "d.mp3", "tags": {}, "lrc": "just\nsome\nwords [00:01]" }, ] }), ); let tags = answer["tags"].as_array().unwrap(); assert_eq!(tags[0]["syncedLyrics"], json!(lrc)); assert_eq!(tags[0]["lyrics"], Value::Null, "LRC in the lyrics tag moves to the timed lyrics"); assert_eq!(tags[1]["lyrics"], json!("Plain words")); assert_eq!(tags[1]["syncedLyrics"], json!(lrc)); assert_eq!(tags[1]["lyricsBeside"], json!(true)); assert_eq!(tags[2]["syncedLyrics"], json!("[00:00] kept"), "the tags' own timed lyrics stand"); assert_eq!(tags[2]["lyricsBeside"], Value::Null); assert_eq!(tags[3]["syncedLyrics"], Value::Null, "an .lrc that is mostly untimed is no timed lyrics"); } #[test] fn a_time_mark_is_minutes_and_two_digit_seconds() { assert!(looks_timed("[1:02] a")); assert!(looks_timed("[123:02.5] a")); assert!(looks_timed("[01:02:345] a")); assert!(!looks_timed("[1:2] a")); assert!(!looks_timed("[ar:Someone]\n[ti:Song]")); assert!(!looks_timed("")); } #[test] fn an_intake_lands_on_the_last_song_that_came_in_and_counts_many() { let one = json!({ "songs": [{ "outcome": "added", "id": "s1" }, { "outcome": "here", "id": "s2" }], "said": "Night Ferry is in Music." }); let two = json!({ "songs": [{ "outcome": "worse", "id": "s3" }, { "outcome": "added", "id": "s4" }], "said": "Lanterns is in Music." }); let landed = ask("music.landed", json!({ "answers": [one.clone()], "albums": 1 })); assert_eq!(landed, json!({ "said": "Night Ferry is in Music.", "landing": "s1" })); let many = ask("music.landed", json!({ "answers": [one, two], "albums": 2 })); assert_eq!(many, json!({ "said": "2 songs came in.", "landing": "s4" })); let here = ask("music.landed", json!({ "answers": [{ "songs": [{ "outcome": "here", "id": "s2" }, { "outcome": "refused" }], "said": "It is already here." }], "albums": 1 })); assert_eq!(here, json!({ "said": "It is already here.", "landing": "s2" })); let none = ask("music.landed", json!({ "answers": [{ "songs": [{ "outcome": "here", "id": "s2" }], "said": "a" }, { "songs": [{ "outcome": "worse", "id": "s9" }], "said": "b" }], "albums": 2 })); assert_eq!(none, json!({ "said": "b", "landing": "s9" }), "nothing added: the last capture's own words"); }