From a20f9a327c4d9e5ac8f42db4e2e9fb29770c3d37 Mon Sep 17 00:00:00 2001 From: jacksonfu14 Date: Mon, 17 Nov 2025 06:56:09 +0100 Subject: feature: generate and process ly files --- morphology-img/tests/render.rs | 548 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 548 insertions(+) create mode 100644 morphology-img/tests/render.rs (limited to 'morphology-img/tests/render.rs') diff --git a/morphology-img/tests/render.rs b/morphology-img/tests/render.rs new file mode 100644 index 0000000..dc2c54a --- /dev/null +++ b/morphology-img/tests/render.rs @@ -0,0 +1,548 @@ +use morphology_img::render::{check_lilypond_available, render_ly_to_svg, render_ly_files_to_svg}; +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; + +/// This test requires LilyPond to be installed on the system. +/// Run with: cargo test -- --ignored +#[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); + + // Verify the version string is not empty + assert!(!version.is_empty(), "version string should not be empty"); + + // Verify it contains "LilyPond" or starts with "GNU" + assert!( + version.contains("LilyPond") || version.starts_with("GNU"), + "version string should contain 'LilyPond' or start with 'GNU', got: {}", + version + ); +} + +/// Test that verifies the error type when LilyPond is not found. +/// This test will pass if LilyPond is NOT installed, or will be skipped if it is. +#[test] +fn test_lilypond_not_found_error_type() { + // Try to check for LilyPond + let result = check_lilypond_available(); + + // If it fails, verify it's the right error type + if let Err(e) = result { + match e { + RenderError::LilyPondNotFound(msg) => { + // Verify the error message is helpful + 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 { + // If LilyPond is available, we can't test the error case, so skip + println!("LilyPond is installed, skipping error test"); + } +} + +/// Test that we handle the case where the command exists but fails +#[test] +fn test_command_failure_handling() { + use std::process::Command; + + // Use a command that exists but will fail with --version + // (most systems have 'false' which always returns non-zero) + let output = Command::new("false") + .output(); + + if output.is_ok() { + let out = output.unwrap(); + // If false command exists, verify we can detect failure status + assert!(!out.status.success(), "false command should have non-success status"); + } +} + +// Tests for render_ly_to_svg (single file SVG generation) + +/// Test rendering a single .ly file to SVG +#[test] +#[ignore] +fn test_render_single_svg() { + // Create a simple sequence + let seq = Sequence::new(vec![Value::origin()]); + + // Generate .ly file + 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(); + + // Render to SVG + let output_dir = TempDir::new().unwrap(); + let svg_path = render_ly_to_svg( + &ly_path, + output_dir.path(), + Path::new("lilypond"), + ).unwrap(); + + // Verify SVG exists + assert!(svg_path.exists(), "SVG file should exist"); + + // Verify SVG has content + let svg_content = std::fs::read_to_string(&svg_path).unwrap(); + assert!(svg_content.contains(" 1000, "SVG should have substantial content"); +} + +/// Test that output directory is created if it doesn't exist +#[test] +#[ignore] +fn test_creates_output_directory() { + 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(); + + // Use a nested directory that doesn't exist yet + let output_base = TempDir::new().unwrap(); + let output_dir = output_base.path().join("nested").join("output"); + + assert!(!output_dir.exists(), "output directory should not exist yet"); + + let svg_path = render_ly_to_svg( + &ly_path, + &output_dir, + Path::new("lilypond"), + ).unwrap(); + + // Verify directory was created + assert!(output_dir.exists(), "output directory should be created"); + assert!(svg_path.exists(), "SVG should be in created directory"); +} + +/// Test rendering multiple different sequences +#[test] +#[ignore] +fn test_render_multiple_different_sequences() { + 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 output_dir = TempDir::new().unwrap(); + + for (i, seq) in sequences.iter().enumerate() { + let ly_path = temp_dir.path().join(format!("seq-{}.ly", i)); + generate_ly_file(seq, &ly_path).unwrap(); + + let svg_path = render_ly_to_svg( + &ly_path, + output_dir.path(), + Path::new("lilypond"), + ).unwrap(); + + assert!(svg_path.exists()); + assert_eq!(svg_path.file_name().unwrap().to_str().unwrap(), format!("seq-{}.svg", i)); + } + + // Verify all 3 SVGs were created + let entries: Vec<_> = std::fs::read_dir(output_dir.path()) + .unwrap() + .filter_map(|e| e.ok()) + .collect(); + assert_eq!(entries.len(), 3, "should have 3 SVG files"); +} + +/// Test that SVG content is valid and contains expected elements +#[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_path = render_ly_to_svg( + &ly_path, + output_dir.path(), + Path::new("lilypond"), + ).unwrap(); + + let svg_content = std::fs::read_to_string(&svg_path).unwrap(); + + // Verify SVG structure + assert!(svg_content.contains(""), "should contain closing tag"); + assert!(svg_content.contains("xmlns"), "should contain XML namespace"); + + // LilyPond SVGs typically contain these elements + assert!(svg_content.contains(" group elements"); +} + +/// Test rendering with relative path for lilypond binary +#[test] +#[ignore] +fn test_render_with_relative_lilypond_path() { + 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(); + + // Use just "lilypond" as the binary name (will be found in PATH) + let svg_path = render_ly_to_svg( + &ly_path, + output_dir.path(), + Path::new("lilypond"), + ).unwrap(); + + assert!(svg_path.exists()); +} + +/// Test that the function returns an error if LilyPond fails +#[test] +#[ignore] +fn test_render_error_on_invalid_ly_file() { + use std::fs; + + let temp_dir = create_temp_dir().unwrap(); + + // Create an invalid .ly file (missing required structure) + 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_ly_to_svg( + &ly_path, + output_dir.path(), + Path::new("lilypond"), + ); + + // Should return an error + assert!(result.is_err(), "should fail on invalid .ly file"); + + match result { + Err(RenderError::LilyPondExecution(msg)) => { + assert!(!msg.is_empty(), "error message should not be empty"); + } + _ => panic!("expected LilyPondExecution error"), + } +} + +// Tests for render_ly_files_to_svg (batch SVG generation) + +/// Test rendering multiple .ly files to SVGs in batch +#[test] +#[ignore] +fn test_render_multiple_svgs_batch() { + // Create multiple sequences + 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(); + + // Generate .ly files + let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap(); + + // Render all to SVGs + let output_dir = TempDir::new().unwrap(); + let svg_paths = render_ly_files_to_svg( + &ly_files, + output_dir.path(), + Path::new("lilypond"), + ).unwrap(); + + // Verify all SVGs were created + assert_eq!(svg_paths.len(), 3, "should create 3 SVG files"); + + for svg_path in &svg_paths { + assert!(svg_path.exists(), "each SVG should exist"); + + // Verify SVG has content + let content = std::fs::read_to_string(svg_path).unwrap(); + assert!(content.contains(" = vec![]; + + let output_dir = TempDir::new().unwrap(); + let svg_paths = render_ly_files_to_svg( + &ly_files, + output_dir.path(), + Path::new("lilypond"), + ).unwrap(); + + assert_eq!(svg_paths.len(), 0, "should return empty vector for empty input"); +} + +/// Test batch rendering with single file +#[test] +#[ignore] +fn test_batch_render_single_file() { + 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("single.ly"); + generate_ly_file(&seq, &ly_path).unwrap(); + + let ly_files = vec![ly_path]; + + let output_dir = TempDir::new().unwrap(); + let svg_paths = render_ly_files_to_svg( + &ly_files, + output_dir.path(), + Path::new("lilypond"), + ).unwrap(); + + assert_eq!(svg_paths.len(), 1); + assert!(svg_paths[0].exists()); + assert_eq!(svg_paths[0].file_name().unwrap().to_str().unwrap(), "single.svg"); +} + +/// Test that batch rendering stops on first error +#[test] +#[ignore] +fn test_batch_render_stops_on_error() { + use std::fs; + + let temp_dir = create_temp_dir().unwrap(); + + // Create one valid and one invalid .ly file + let seq = Sequence::new(vec![Value::origin()]); + write_support_files(temp_dir.path()).unwrap(); + + let valid_ly = temp_dir.path().join("valid.ly"); + generate_ly_file(&seq, &valid_ly).unwrap(); + + let invalid_ly = temp_dir.path().join("invalid.ly"); + fs::write(&invalid_ly, "invalid lilypond syntax!!!").unwrap(); + + let ly_files = vec![valid_ly, invalid_ly]; + + let output_dir = TempDir::new().unwrap(); + let result = render_ly_files_to_svg( + &ly_files, + output_dir.path(), + Path::new("lilypond"), + ); + + // Should fail due to invalid file + assert!(result.is_err(), "should fail on invalid file"); + + match result { + Err(RenderError::LilyPondExecution(_)) => { + // Expected error type + } + _ => panic!("expected LilyPondExecution error"), + } +} + +/// Test batch rendering with large number of files +#[test] +#[ignore] +fn test_batch_render_many_files() { + // Create 10 sequences + let sequences: Vec = (0..10) + .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_ly_files_to_svg( + &ly_files, + output_dir.path(), + Path::new("lilypond"), + ).unwrap(); + + // Verify all 10 SVGs were created + assert_eq!(svg_paths.len(), 10, "should create 10 SVG files"); + + for (i, svg_path) in svg_paths.iter().enumerate() { + assert!(svg_path.exists()); + // With 10 files, we need 2-digit padding: 00, 01, 02, ..., 09 + assert_eq!( + svg_path.file_name().unwrap().to_str().unwrap(), + format!("sequence-{:02}.svg", i) + ); + } + + // Verify all files have content + for svg_path in &svg_paths { + let content = std::fs::read_to_string(svg_path).unwrap(); + assert!(content.contains(" 100, "SVG should have substantial content"); + } +} + +/// Integration test: Complete pipeline from sequences to SVGs +#[test] +#[ignore] +fn test_complete_pipeline_batch_rendering() { + // Create test sequences + 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), + ]), + ]; + + // Create temp directory and write support files + let temp_dir = create_temp_dir().unwrap(); + write_support_files(temp_dir.path()).unwrap(); + + // Generate .ly files + let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap(); + assert_eq!(ly_files.len(), 3); + + // Render all to SVGs + let output_dir = TempDir::new().unwrap(); + let svg_paths = render_ly_files_to_svg( + &ly_files, + output_dir.path(), + Path::new("lilypond"), + ).unwrap(); + + // Verify complete pipeline + assert_eq!(svg_paths.len(), 3); + + // Verify each SVG + for (i, svg_path) in svg_paths.iter().enumerate() { + // File exists + assert!(svg_path.exists(), "SVG {} should exist", i); + + // Correct filename + assert_eq!( + svg_path.file_name().unwrap().to_str().unwrap(), + format!("sequence-{}.svg", i) + ); + + // Has valid SVG content + let content = std::fs::read_to_string(svg_path).unwrap(); + assert!(content.contains(""), "SVG {} should have closing tag", i); + } + + // Verify all files are in output directory + 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"); +} -- cgit v1.2.3