use morphology_img::render::{check_lilypond_available, render_all_ly_files_batch}; use morphology_img::error::RenderError; use morphology_img::file_gen::{create_temp_dir, generate_ly_file, write_support_files, generate_ly_files}; use morphology::{Sequence, Value}; use std::path::Path; use tempfile::TempDir; /// Test that LilyPond is available (requires LilyPond installation) #[test] #[ignore] fn test_lilypond_available() { let result = check_lilypond_available(); assert!(result.is_ok(), "LilyPond should be available for this test"); let version = result.unwrap(); println!("LilyPond version: {}", version); assert!(!version.is_empty(), "version string should not be empty"); assert!( version.contains("LilyPond") || version.starts_with("GNU"), "version string should contain 'LilyPond' or start with 'GNU', got: {}", version ); } /// Test error type when LilyPond is not found #[test] fn test_lilypond_not_found_error_type() { let result = check_lilypond_available(); if let Err(e) = result { match e { RenderError::LilyPondNotFound(msg) => { assert!(!msg.is_empty(), "error message should not be empty"); assert!( msg.contains("LilyPond") || msg.contains("PATH"), "error message should mention LilyPond or PATH" ); } _ => panic!("Expected RenderError::LilyPondNotFound, got: {:?}", e), } } else { println!("LilyPond is installed, skipping error test"); } } /// Test rendering multiple .ly files to SVGs in a single batch #[test] #[ignore] fn test_render_batch_basic() { let sequences = vec![ Sequence::new(vec![Value::origin()]), Sequence::new(vec![Value::new(1, 0)]), Sequence::new(vec![Value::new(-1, 0)]), ]; let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap(); let output_dir = TempDir::new().unwrap(); let svg_paths = render_all_ly_files_batch( &ly_files, output_dir.path(), Path::new("lilypond"), ).unwrap(); assert_eq!(svg_paths.len(), 3); for svg_path in &svg_paths { assert!(svg_path.exists()); let content = std::fs::read_to_string(svg_path).unwrap(); assert!(content.contains(" = vec![]; let output_dir = TempDir::new().unwrap(); let svg_paths = render_all_ly_files_batch( &ly_files, output_dir.path(), Path::new("lilypond"), ).unwrap(); assert_eq!(svg_paths.len(), 0); } /// Test batch rendering creates output directory #[test] #[ignore] fn test_render_batch_creates_output_dir() { let seq = Sequence::new(vec![Value::origin()]); let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); let ly_path = temp_dir.path().join("test.ly"); generate_ly_file(&seq, &ly_path).unwrap(); let output_base = TempDir::new().unwrap(); let output_dir = output_base.path().join("nested").join("output"); assert!(!output_dir.exists()); let svg_paths = render_all_ly_files_batch( &[ly_path], &output_dir, Path::new("lilypond"), ).unwrap(); assert!(output_dir.exists()); assert_eq!(svg_paths.len(), 1); } /// Test with invalid .ly file #[test] #[ignore] fn test_render_batch_invalid_ly_file() { use std::fs; let temp_dir = create_temp_dir().unwrap(); let ly_path = temp_dir.path().join("invalid.ly"); fs::write(&ly_path, "this is not valid lilypond syntax!!!").unwrap(); let output_dir = TempDir::new().unwrap(); let result = render_all_ly_files_batch( &[ly_path], output_dir.path(), Path::new("lilypond"), ); assert!(result.is_err()); match result { Err(RenderError::LilyPondExecution(msg)) => { assert!(!msg.is_empty()); } _ => panic!("expected LilyPondExecution error"), } } /// Test batch rendering with many files #[test] #[ignore] fn test_render_batch_many_files() { let sequences: Vec = (0..20) .map(|i| Sequence::new(vec![Value::new(i, 0)])) .collect(); let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap(); let output_dir = TempDir::new().unwrap(); let svg_paths = render_all_ly_files_batch( &ly_files, output_dir.path(), Path::new("lilypond"), ).unwrap(); assert_eq!(svg_paths.len(), 20); for svg_path in &svg_paths { assert!(svg_path.exists()); let content = std::fs::read_to_string(svg_path).unwrap(); assert!(content.contains(" 1000, "SVG should have substantial content"); } /// Test that SVG content is valid #[test] #[ignore] fn test_svg_content_validity() { let seq = Sequence::new(vec![Value::origin()]); let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); let ly_path = temp_dir.path().join("test.ly"); generate_ly_file(&seq, &ly_path).unwrap(); let output_dir = TempDir::new().unwrap(); let svg_paths = render_all_ly_files_batch( &[ly_path], output_dir.path(), Path::new("lilypond"), ).unwrap(); let svg_content = std::fs::read_to_string(&svg_paths[0]).unwrap(); assert!(svg_content.contains(""), "should contain closing tag"); assert!(svg_content.contains("xmlns"), "should contain XML namespace"); assert!(svg_content.contains(" group elements"); } /// Integration test: Complete pipeline from sequences to SVGs #[test] #[ignore] fn test_complete_pipeline_batch_rendering() { let sequences = vec![ Sequence::new(vec![ Value::new(1, 0), Value::new(-1, 1), Value::new(-1, 0), Value::new(0, 0), ]), Sequence::new(vec![ Value::origin(), Value::new(1, 0), ]), Sequence::new(vec![ Value::new(2, 0), ]), ]; let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap(); assert_eq!(ly_files.len(), 3); let output_dir = TempDir::new().unwrap(); let svg_paths = render_all_ly_files_batch( &ly_files, output_dir.path(), Path::new("lilypond"), ).unwrap(); assert_eq!(svg_paths.len(), 3); for (i, svg_path) in svg_paths.iter().enumerate() { assert!(svg_path.exists(), "SVG {} should exist", i); assert_eq!( svg_path.file_name().unwrap().to_str().unwrap(), format!("sequence-{}.svg", i) ); let content = std::fs::read_to_string(svg_path).unwrap(); assert!(content.contains(""), "SVG {} should have closing tag", i); } let entries: Vec<_> = std::fs::read_dir(output_dir.path()) .unwrap() .filter_map(|e| e.ok()) .collect(); assert_eq!(entries.len(), 3, "output directory should have exactly 3 files"); }