use morphology_img::{render_sequences, RenderConfig, ProgressCallback}; use morphology::{Sequence, Value}; use tempfile::TempDir; use std::fs; use std::sync::{Arc, Mutex}; /// Test the complete pipeline with basic configuration #[test] #[ignore] fn test_render_sequences_basic() { // Create test sequences let sequences = vec![ Sequence::new(vec![Value::origin()]), Sequence::new(vec![Value::new(1, 0)]), ]; // Use temp directory for output let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()); // Render sequences let svg_paths = render_sequences(&sequences, &config).unwrap(); // Verify correct number of SVGs assert_eq!(svg_paths.len(), 2, "should generate 2 SVG files"); // Verify all SVGs exist for svg_path in &svg_paths { assert!(svg_path.exists(), "SVG should exist: {}", svg_path.display()); // Verify SVG has content let content = fs::read_to_string(svg_path).unwrap(); assert!(content.contains(" 1000, "should have substantial SVG content"); } /// Test with empty sequence list #[test] #[ignore] fn test_render_sequences_empty() { let sequences: Vec = vec![]; let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()); let svg_paths = render_sequences(&sequences, &config).unwrap(); assert_eq!(svg_paths.len(), 0, "should return empty vector for empty input"); } /// Test with single sequence #[test] #[ignore] fn test_render_sequences_single() { let sequences = vec![Sequence::new(vec![Value::origin()])]; let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()); let svg_paths = render_sequences(&sequences, &config).unwrap(); assert_eq!(svg_paths.len(), 1); assert!(svg_paths[0].exists()); } /// Test with many sequences #[test] #[ignore] fn test_render_sequences_many() { // Create 10 sequences let sequences: Vec = (0..10) .map(|i| Sequence::new(vec![Value::new(i, 0)])) .collect(); let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()); let svg_paths = render_sequences(&sequences, &config).unwrap(); assert_eq!(svg_paths.len(), 10); // Verify all exist and have content for svg_path in &svg_paths { assert!(svg_path.exists()); let content = fs::read_to_string(svg_path).unwrap(); assert!(content.contains(" 1000, "SVG should have substantial content"); assert!(content.contains("")); } } /// Test that function returns error if LilyPond is not available #[test] fn test_render_sequences_requires_lilypond() { let sequences = vec![Sequence::new(vec![Value::origin()])]; let output_dir = TempDir::new().unwrap(); // Use a non-existent binary let config = RenderConfig::new(output_dir.path()) .lilypond_binary("/this/does/not/exist/lilypond"); let result = render_sequences(&sequences, &config); // Should fail with LilyPondNotFound or execution error assert!(result.is_err(), "should fail when LilyPond is not available"); } /// Integration test: Complete realistic workflow #[test] #[ignore] fn test_render_sequences_realistic_workflow() { // Create a realistic set of sequences let sequences: Vec = vec![ Sequence::new(vec![Value::origin()]), Sequence::new(vec![Value::new(1, 0), Value::new(-1, 0)]), Sequence::new(vec![Value::new(2, 0), Value::origin(), Value::new(-2, 0)]), Sequence::new(vec![ Value::new(1, 0), Value::new(-1, 1), Value::new(-1, 0), Value::new(0, 0), ]), ]; let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()); // Render all sequences let svg_paths = render_sequences(&sequences, &config).unwrap(); // Verify results assert_eq!(svg_paths.len(), 4, "should generate 4 SVGs"); // Verify all files exist and are valid for (i, svg_path) in svg_paths.iter().enumerate() { assert!(svg_path.exists(), "SVG {} should exist", i); // Verify in correct directory assert!(svg_path.starts_with(output_dir.path())); // Verify content is valid SVG let content = fs::read_to_string(svg_path).unwrap(); assert!(content.contains(""), "SVG {} should have closing tag", i); assert!(content.contains("xmlns"), "SVG {} should have XML namespace", i); // Verify substantial content assert!(content.len() > 500, "SVG {} should have substantial content", i); } // Verify correct filenames assert_eq!(svg_paths[0].file_name().unwrap().to_str().unwrap(), "sequence-0.svg"); assert_eq!(svg_paths[3].file_name().unwrap().to_str().unwrap(), "sequence-3.svg"); } // Tests for progress callback /// Test that progress callback is called correctly #[test] #[ignore] fn test_progress_callback_basic() { let sequences = vec![ Sequence::new(vec![Value::origin()]), Sequence::new(vec![Value::new(1, 0)]), Sequence::new(vec![Value::new(2, 0)]), ]; let progress_calls = Arc::new(Mutex::new(Vec::new())); let progress_calls_clone = progress_calls.clone(); let callback: ProgressCallback = Arc::new(move |current, total| { progress_calls_clone.lock().unwrap().push((current, total)); }); let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()) .progress_callback(callback); let svg_paths = render_sequences(&sequences, &config).unwrap(); // Verify all SVGs were created assert_eq!(svg_paths.len(), 3); // Verify progress callback was called correctly let calls = progress_calls.lock().unwrap(); assert_eq!(calls.len(), 3, "callback should be called once per sequence"); // Verify calls have correct values assert_eq!(calls[0], (1, 3), "first call should be (1, 3)"); assert_eq!(calls[1], (2, 3), "second call should be (2, 3)"); assert_eq!(calls[2], (3, 3), "third call should be (3, 3)"); } /// Test progress callback with single sequence #[test] #[ignore] fn test_progress_callback_single() { let sequences = vec![Sequence::new(vec![Value::origin()])]; let progress_calls = Arc::new(Mutex::new(Vec::new())); let progress_calls_clone = progress_calls.clone(); let callback: ProgressCallback = Arc::new(move |current, total| { progress_calls_clone.lock().unwrap().push((current, total)); }); let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()) .progress_callback(callback); let _svg_paths = render_sequences(&sequences, &config).unwrap(); let calls = progress_calls.lock().unwrap(); assert_eq!(calls.len(), 1); assert_eq!(calls[0], (1, 1)); } /// Test progress callback with many sequences #[test] #[ignore] fn test_progress_callback_many() { let sequences: Vec = (0..10) .map(|i| Sequence::new(vec![Value::new(i, 0)])) .collect(); let progress_calls = Arc::new(Mutex::new(Vec::new())); let progress_calls_clone = progress_calls.clone(); let callback: ProgressCallback = Arc::new(move |current, total| { progress_calls_clone.lock().unwrap().push((current, total)); }); let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()) .progress_callback(callback); let svg_paths = render_sequences(&sequences, &config).unwrap(); assert_eq!(svg_paths.len(), 10); let calls = progress_calls.lock().unwrap(); assert_eq!(calls.len(), 10); // Verify each call has correct values for (i, &(current, total)) in calls.iter().enumerate() { assert_eq!(current, i + 1); assert_eq!(total, 10); } } /// Test that rendering works without progress callback #[test] #[ignore] fn test_no_progress_callback() { let sequences = vec![ Sequence::new(vec![Value::origin()]), Sequence::new(vec![Value::new(1, 0)]), ]; let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()); // No progress callback set let svg_paths = render_sequences(&sequences, &config).unwrap(); // Should work normally without callback assert_eq!(svg_paths.len(), 2); for svg_path in &svg_paths { assert!(svg_path.exists()); } } /// Test progress callback with empty sequence list #[test] #[ignore] fn test_progress_callback_empty() { let sequences: Vec = vec![]; let progress_calls = Arc::new(Mutex::new(Vec::new())); let progress_calls_clone = progress_calls.clone(); let callback: ProgressCallback = Arc::new(move |current, total| { progress_calls_clone.lock().unwrap().push((current, total)); }); let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()) .progress_callback(callback); let svg_paths = render_sequences(&sequences, &config).unwrap(); assert_eq!(svg_paths.len(), 0); // Callback should not be called for empty list let calls = progress_calls.lock().unwrap(); assert_eq!(calls.len(), 0, "callback should not be called for empty input"); } /// Test that progress callback can track actual progress #[test] #[ignore] fn test_progress_callback_tracking() { let sequences: Vec = (0..5) .map(|i| Sequence::new(vec![Value::new(i, 0)])) .collect(); let last_progress = Arc::new(Mutex::new((0, 0))); let last_progress_clone = last_progress.clone(); let callback: ProgressCallback = Arc::new(move |current, total| { let mut progress = last_progress_clone.lock().unwrap(); *progress = (current, total); }); let output_dir = TempDir::new().unwrap(); let config = RenderConfig::new(output_dir.path()) .progress_callback(callback); let svg_paths = render_sequences(&sequences, &config).unwrap(); assert_eq!(svg_paths.len(), 5); // Last progress should be 5/5 let final_progress = last_progress.lock().unwrap(); assert_eq!(*final_progress, (5, 5), "final progress should be 5/5"); }