use singtrainer::song_analysis::{Analysis, load_analysis}; #[test] fn reads_optional_outputs_from_one_folder() { let dir = std::env::temp_dir().join(format!("song-analysis-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); std::fs::write(dir.join("meta.json"), r#"{"title":"Example","bpm":120.0,"key":null,"duration":3.0,"schema_version":1,"errors":{}}"#).unwrap(); std::fs::write( dir.join("beats.json"), r#"{"beats":[0.0,0.5],"downbeats":[0.0]}"#, ) .unwrap(); let value: Analysis = load_analysis(&dir).unwrap(); assert_eq!(value.meta.title, "Example"); assert_eq!(value.beats.unwrap().downbeats, vec![0.0]); assert!(value.pitch.is_none()); std::fs::remove_dir_all(dir).unwrap(); } #[test] fn rejects_unknown_schema_version() { let dir = std::env::temp_dir().join(format!("song-analysis-version-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); std::fs::write(dir.join("meta.json"), r#"{"title":"Example","bpm":null,"key":null,"duration":3.0,"schema_version":3,"errors":{}}"#).unwrap(); assert!(load_analysis(&dir).is_err()); std::fs::remove_dir_all(dir).unwrap(); } #[test] fn reads_v2_metadata_and_lyric_provenance() { let dir = std::env::temp_dir().join(format!("song-analysis-v2-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); std::fs::write(dir.join("meta.json"), r#"{"title":"Example","bpm":120.0,"bpm_source":"automatic","source_offset_s":0.0,"key":null,"duration":3.0,"schema_version":2,"errors":{}}"#).unwrap(); std::fs::write(dir.join("lyrics.json"), r#"{"verified":true,"lines":[{"start":0.1,"end":0.3,"source_line":2,"section":"verse","alignment_conf":0.8,"words":[{"w":"Hello","start":0.1,"end":0.3,"conf":0.8}]}]}"#).unwrap(); let result = load_analysis(&dir).unwrap(); assert_eq!(result.meta.source_offset_s, Some(0.0)); assert_eq!(result.lyrics.unwrap().lines[0].source_line, Some(2)); std::fs::remove_dir_all(dir).unwrap(); } #[test] fn rejects_v2_metadata_without_timeline_origin() { let dir = std::env::temp_dir().join(format!("song-analysis-v2-missing-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); std::fs::write(dir.join("meta.json"), r#"{"title":"Example","bpm":120.0,"bpm_source":"automatic","key":null,"duration":3.0,"schema_version":2,"errors":{}}"#).unwrap(); assert!(load_analysis(&dir).is_err()); std::fs::remove_dir_all(dir).unwrap(); }