summaryrefslogtreecommitdiff
path: root/morphology-img/tests/render.rs
blob: 3e86f973bfcf67e8449890a4d2db18f03a86fe3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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("<svg"));
    }
}

/// Test batch rendering preserves order
#[test]
#[ignore]
fn test_render_batch_preserves_order() {
    let sequences = vec![
        Sequence::new(vec![Value::new(5, 0)]),
        Sequence::new(vec![Value::new(3, 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[0].file_name().unwrap().to_str().unwrap(), "sequence-0.svg");
    assert_eq!(svg_paths[1].file_name().unwrap().to_str().unwrap(), "sequence-1.svg");
    assert_eq!(svg_paths[2].file_name().unwrap().to_str().unwrap(), "sequence-2.svg");
}

/// Test batch rendering with empty list
#[test]
fn test_render_batch_empty() {
    let ly_files: Vec<std::path::PathBuf> = 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<Sequence> = (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("<svg"));
    }
}

/// Test rendering with example sequence from development plan
#[test]
#[ignore]
fn test_render_example_sequence() {
    let seq = Sequence::new(vec![
        Value::new(1, 0),
        Value::new(-1, 1),
        Value::new(-1, 0),
        Value::new(0, 0),
    ]);

    let temp_dir = create_temp_dir().unwrap();
    write_support_files(temp_dir.path()).unwrap();

    let ly_path = temp_dir.path().join("example.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();

    assert_eq!(svg_paths.len(), 1);
    assert!(svg_paths[0].exists());

    let svg_content = std::fs::read_to_string(&svg_paths[0]).unwrap();
    assert!(svg_content.contains("<svg"));
    assert!(svg_content.len() > 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("<svg"), "should contain <svg tag");
    assert!(svg_content.contains("</svg>"), "should contain closing </svg> tag");
    assert!(svg_content.contains("xmlns"), "should contain XML namespace");
    assert!(svg_content.contains("<g"), "should contain <g> 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"), "SVG {} should have svg tag", i);
        assert!(content.contains("</svg>"), "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");
}