summaryrefslogtreecommitdiff
path: root/morphology-img/tests/file_gen.rs
blob: 167a9d8a5d75684c053c588c87873f126a2e3590 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
use morphology_img::file_gen::{generate_filename, create_temp_dir, write_support_files, generate_ly_file, generate_ly_files};
use morphology::{Sequence, Value};
use std::fs;
use std::path::PathBuf;

// Tests for generate_filename

#[test]
fn test_generate_filename_single_digit_padding() {
    // With 10 sequences (1-9, plus 0), we need 2-digit padding
    let filename = generate_filename(0, 10);
    assert_eq!(filename.to_str().unwrap(), "sequence-00.ly");

    let filename = generate_filename(5, 10);
    assert_eq!(filename.to_str().unwrap(), "sequence-05.ly");

    let filename = generate_filename(9, 10);
    assert_eq!(filename.to_str().unwrap(), "sequence-09.ly");
}

#[test]
fn test_generate_filename_double_digit_padding() {
    // With 100 sequences, we need 3-digit padding
    let filename = generate_filename(0, 100);
    assert_eq!(filename.to_str().unwrap(), "sequence-000.ly");

    let filename = generate_filename(42, 100);
    assert_eq!(filename.to_str().unwrap(), "sequence-042.ly");

    let filename = generate_filename(99, 100);
    assert_eq!(filename.to_str().unwrap(), "sequence-099.ly");
}

#[test]
fn test_generate_filename_triple_digit_padding() {
    // With 1000 sequences, we need 4-digit padding
    let filename = generate_filename(0, 1000);
    assert_eq!(filename.to_str().unwrap(), "sequence-0000.ly");

    let filename = generate_filename(42, 1000);
    assert_eq!(filename.to_str().unwrap(), "sequence-0042.ly");

    let filename = generate_filename(123, 1000);
    assert_eq!(filename.to_str().unwrap(), "sequence-0123.ly");

    let filename = generate_filename(999, 1000);
    assert_eq!(filename.to_str().unwrap(), "sequence-0999.ly");
}

#[test]
fn test_generate_filename_single_sequence() {
    // With just 1 sequence, we only need 1-digit padding
    let filename = generate_filename(0, 1);
    assert_eq!(filename.to_str().unwrap(), "sequence-0.ly");
}

#[test]
fn test_generate_filename_ten_sequences_boundary() {
    // At exactly 10, we need 2 digits
    let filename = generate_filename(0, 10);
    assert_eq!(filename.to_str().unwrap(), "sequence-00.ly");
}

#[test]
fn test_generate_filename_lexicographic_ordering() {
    // Verify that filenames sort correctly
    let names: Vec<PathBuf> = (0..15).map(|i| generate_filename(i, 15)).collect();

    let mut sorted_names = names.clone();
    sorted_names.sort();

    // If padding is correct, the sorted order should be the same as generated order
    assert_eq!(names, sorted_names);
}

#[test]
fn test_generate_filename_has_ly_extension() {
    let filename = generate_filename(5, 10);
    assert!(filename.to_str().unwrap().ends_with(".ly"));
}

#[test]
fn test_generate_filename_different_counts_same_index() {
    // Same index but different total counts should have different padding
    let name_10 = generate_filename(5, 10);
    let name_100 = generate_filename(5, 100);
    let name_1000 = generate_filename(5, 1000);

    assert_eq!(name_10.to_str().unwrap(), "sequence-05.ly");
    assert_eq!(name_100.to_str().unwrap(), "sequence-005.ly");
    assert_eq!(name_1000.to_str().unwrap(), "sequence-0005.ly");
}

// Tests for create_temp_dir

#[test]
fn test_create_temp_dir_success() {
    // Test that we can create a temp directory
    let temp_dir = create_temp_dir();
    assert!(temp_dir.is_ok(), "should successfully create temp directory");

    let dir = temp_dir.unwrap();
    let path = dir.path();

    // Verify the directory exists
    assert!(path.exists(), "temp directory should exist");
    assert!(path.is_dir(), "temp path should be a directory");

    // TempDir will be dropped here and automatically cleaned up
}

#[test]
fn test_create_temp_dir_is_empty() {
    // Test that the newly created temp directory is empty
    let temp_dir = create_temp_dir().unwrap();
    let entries = fs::read_dir(temp_dir.path()).unwrap();

    assert_eq!(entries.count(), 0, "new temp directory should be empty");
}

#[test]
fn test_create_temp_dir_automatic_cleanup() {
    let path;
    {
        let temp_dir = create_temp_dir().unwrap();
        path = temp_dir.path().to_path_buf();
        assert!(path.exists(), "temp directory should exist while TempDir is in scope");
    } // TempDir dropped here

    // After TempDir is dropped, directory should be cleaned up
    assert!(!path.exists(), "temp directory should be cleaned up after drop");
}

#[test]
fn test_create_temp_dir_multiple_independent() {
    // Test that we can create multiple independent temp directories
    let dir1 = create_temp_dir().unwrap();
    let dir2 = create_temp_dir().unwrap();

    assert_ne!(dir1.path(), dir2.path(), "temp directories should be different");
    assert!(dir1.path().exists());
    assert!(dir2.path().exists());
}

// Tests for write_support_files

#[test]
fn test_write_support_files_success() {
    let temp_dir = create_temp_dir().unwrap();

    // Write support files
    let result = write_support_files(temp_dir.path());
    assert!(result.is_ok(), "should successfully write support files");

    // Verify all three files exist
    let format_path = temp_dir.path().join("format.ily");
    let entry_path = temp_dir.path().join("entry.ily");
    let elements_path = temp_dir.path().join("elements.ily");

    assert!(format_path.exists(), "format.ily should exist");
    assert!(entry_path.exists(), "entry.ily should exist");
    assert!(elements_path.exists(), "elements.ily should exist");
}

#[test]
fn test_write_support_files_content_correctness() {
    let temp_dir = create_temp_dir().unwrap();

    write_support_files(temp_dir.path()).unwrap();

    // Read back the files and verify they have the correct content
    let format_content = fs::read_to_string(temp_dir.path().join("format.ily")).unwrap();
    let entry_content = fs::read_to_string(temp_dir.path().join("entry.ily")).unwrap();
    let elements_content = fs::read_to_string(temp_dir.path().join("elements.ily")).unwrap();

    // Verify content is non-empty
    assert!(!format_content.is_empty(), "format.ily should have content");
    assert!(!entry_content.is_empty(), "entry.ily should have content");
    assert!(!elements_content.is_empty(), "elements.ily should have content");

    // Verify content matches the embedded templates
    use morphology_img::template::{get_format_ily, get_entry_ily, get_elements_ily};

    assert_eq!(format_content, get_format_ily(), "format.ily content should match template");
    assert_eq!(entry_content, get_entry_ily(), "entry.ily content should match template");
    assert_eq!(elements_content, get_elements_ily(), "elements.ily content should match template");
}

#[test]
fn test_write_support_files_are_readable() {
    let temp_dir = create_temp_dir().unwrap();
    write_support_files(temp_dir.path()).unwrap();

    // Verify files are readable
    let format_path = temp_dir.path().join("format.ily");
    let entry_path = temp_dir.path().join("entry.ily");
    let elements_path = temp_dir.path().join("elements.ily");

    assert!(fs::read_to_string(&format_path).is_ok(), "format.ily should be readable");
    assert!(fs::read_to_string(&entry_path).is_ok(), "entry.ily should be readable");
    assert!(fs::read_to_string(&elements_path).is_ok(), "elements.ily should be readable");
}

#[test]
fn test_write_support_files_count() {
    let temp_dir = create_temp_dir().unwrap();
    write_support_files(temp_dir.path()).unwrap();

    // Count files in directory
    let entries: Vec<_> = fs::read_dir(temp_dir.path())
        .unwrap()
        .filter_map(|e| e.ok())
        .collect();

    assert_eq!(entries.len(), 3, "should have exactly 3 support files");
}

#[test]
fn test_write_support_files_overwrite() {
    let temp_dir = create_temp_dir().unwrap();

    // Write support files first time
    write_support_files(temp_dir.path()).unwrap();

    // Write again (should overwrite)
    let result = write_support_files(temp_dir.path());
    assert!(result.is_ok(), "should be able to overwrite existing files");

    // Verify files still exist and have correct content
    let format_content = fs::read_to_string(temp_dir.path().join("format.ily")).unwrap();
    assert!(!format_content.is_empty());
}

// Integration test

#[test]
fn test_complete_file_generation_workflow() {
    // Test the complete workflow of creating temp dir and writing support files
    let temp_dir = create_temp_dir().unwrap();
    let temp_path = temp_dir.path();

    // Directory should be empty initially
    assert_eq!(fs::read_dir(temp_path).unwrap().count(), 0);

    // Write support files
    write_support_files(temp_path).unwrap();

    // Directory should now have 3 files
    assert_eq!(fs::read_dir(temp_path).unwrap().count(), 3);

    // Generate some filenames
    let filenames: Vec<_> = (0..5).map(|i| generate_filename(i, 5)).collect();

    // Verify filenames are correctly padded
    assert_eq!(filenames[0].to_str().unwrap(), "sequence-0.ly");
    assert_eq!(filenames[4].to_str().unwrap(), "sequence-4.ly");

    // Simulate writing .ly files
    for filename in &filenames {
        let path = temp_path.join(filename);
        fs::write(&path, "test content").unwrap();
    }

    // Now we should have 3 .ily files + 5 .ly files = 8 files
    assert_eq!(fs::read_dir(temp_path).unwrap().count(), 8);
}

// Tests for generate_ly_file

#[test]
fn test_generate_ly_file_simple_sequence() {
    // Create a simple test sequence
    let seq = Sequence::new(vec![Value::origin()]);

    let temp_dir = create_temp_dir().unwrap();
    let output_path = temp_dir.path().join("test.ly");

    // Generate .ly file
    let result = generate_ly_file(&seq, &output_path);
    assert!(result.is_ok(), "should successfully generate .ly file");

    // Verify file exists
    assert!(output_path.exists(), "generated file should exist");

    // Verify file is readable
    let content = fs::read_to_string(&output_path).unwrap();
    assert!(!content.is_empty(), "file should not be empty");
}

#[test]
fn test_generate_ly_file_content_structure() {
    // Create the example sequence from the preliminary overview
    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();
    let output_path = temp_dir.path().join("test.ly");

    generate_ly_file(&seq, &output_path).unwrap();

    // Read the file and verify its content
    let content = fs::read_to_string(&output_path).unwrap();

    // Verify it contains the template structure
    assert!(content.contains(r#"\version "2.24.0""#), "should have version declaration");
    assert!(content.contains(r#"\score"#), "should have score block");
    assert!(content.contains(r#"\entry"#), "should have entry block");
    assert!(content.contains(r#"\include "format.ily""#), "should include format.ily");
    assert!(content.contains(r#"\include "entry.ily""#), "should include entry.ily");
    assert!(content.contains(r#"\include "elements.ily""#), "should include elements.ily");

    // Verify it contains the expected markup
    assert!(content.contains("f'4 f4 e4 b4"), "should contain the generated markup");

    // Verify the placeholder is gone
    assert!(!content.contains("%%%"), "should not contain placeholder");
}

#[test]
fn test_generate_ly_file_is_valid_utf8() {
    let seq = Sequence::new(vec![Value::new(1, 0)]);

    let temp_dir = create_temp_dir().unwrap();
    let output_path = temp_dir.path().join("test.ly");

    generate_ly_file(&seq, &output_path).unwrap();

    // Verify file is valid UTF-8
    let result = fs::read_to_string(&output_path);
    assert!(result.is_ok(), "file should be valid UTF-8");
}

#[test]
fn test_generate_ly_file_overwrite() {
    let seq1 = Sequence::new(vec![Value::origin()]);
    let seq2 = Sequence::new(vec![Value::new(1, 0)]);

    let temp_dir = create_temp_dir().unwrap();
    let output_path = temp_dir.path().join("test.ly");

    // Generate first file
    generate_ly_file(&seq1, &output_path).unwrap();
    let content1 = fs::read_to_string(&output_path).unwrap();

    // Generate second file (overwrite)
    generate_ly_file(&seq2, &output_path).unwrap();
    let content2 = fs::read_to_string(&output_path).unwrap();

    // Contents should be different
    assert_ne!(content1, content2, "overwrite should change file content");
}

// Tests for generate_ly_files (batch)

#[test]
fn test_generate_ly_files_multiple_sequences() {
    // Create multiple test 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();

    // Generate .ly files
    let result = generate_ly_files(&sequences, temp_dir.path());
    assert!(result.is_ok(), "should successfully generate all files");

    let paths = result.unwrap();

    // Verify we got the correct number of paths
    assert_eq!(paths.len(), 3, "should return 3 paths");

    // Verify all files exist
    for path in &paths {
        assert!(path.exists(), "each generated file should exist");
    }
}

#[test]
fn test_generate_ly_files_correct_filenames() {
    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();
    let paths = generate_ly_files(&sequences, temp_dir.path()).unwrap();

    // Verify filenames are correctly generated
    assert!(paths[0].ends_with("sequence-0.ly"));
    assert!(paths[1].ends_with("sequence-1.ly"));
    assert!(paths[2].ends_with("sequence-2.ly"));
}

#[test]
fn test_generate_ly_files_each_has_unique_content() {
    let sequences = vec![
        Sequence::new(vec![Value::origin()]),
        Sequence::new(vec![Value::new(1, 0)]),
    ];

    let temp_dir = create_temp_dir().unwrap();
    let paths = generate_ly_files(&sequences, temp_dir.path()).unwrap();

    // Read content of both files
    let content1 = fs::read_to_string(&paths[0]).unwrap();
    let content2 = fs::read_to_string(&paths[1]).unwrap();

    // Contents should be different
    assert_ne!(content1, content2, "each file should have unique content based on its sequence");
}

#[test]
fn test_generate_ly_files_empty_slice() {
    let sequences: Vec<Sequence> = vec![];

    let temp_dir = create_temp_dir().unwrap();
    let result = generate_ly_files(&sequences, temp_dir.path());

    assert!(result.is_ok(), "should handle empty slice");
    assert_eq!(result.unwrap().len(), 0, "should return empty vector");
}

#[test]
fn test_generate_ly_files_single_sequence() {
    let sequences = vec![Sequence::new(vec![Value::origin()])];

    let temp_dir = create_temp_dir().unwrap();
    let paths = generate_ly_files(&sequences, temp_dir.path()).unwrap();

    assert_eq!(paths.len(), 1);
    assert!(paths[0].exists());
    assert!(paths[0].ends_with("sequence-0.ly"));
}

#[test]
fn test_generate_ly_files_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();
    let paths = generate_ly_files(&sequences, temp_dir.path()).unwrap();

    // Read the markup from each file
    for (i, path) in paths.iter().enumerate() {
        let content = fs::read_to_string(path).unwrap();

        // Verify the content corresponds to the right sequence
        // Sequence with Value::new(outer, 0), max_abs_inner = 0, multiplier = 3
        // pitch = outer * 3 + 0
        // Then integer_to_pitch_name: 15→c''', 9→d'', 3→e'
        // (15+6=21, 21%7=0→"c", 21/7=3→''') (9+6=15, 15%7=1→"d", 15/7=2→'') (3+6=9, 9%7=2→"e", 9/7=1→')
        match i {
            0 => assert!(content.contains("c'''4"), "first file should have sequence 0 content"),
            1 => assert!(content.contains("d''4"), "second file should have sequence 1 content"),
            2 => assert!(content.contains("e'4"), "third file should have sequence 2 content"),
            _ => panic!("unexpected index"),
        }
    }
}

// Integration test: Complete pipeline

#[test]
fn test_complete_ly_generation_pipeline() {
    // Test the complete workflow from sequence to .ly file

    // 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),
        ]),
    ];

    // Create temp directory
    let temp_dir = create_temp_dir().unwrap();

    // Write support files
    write_support_files(temp_dir.path()).unwrap();

    // Generate .ly files
    let ly_paths = generate_ly_files(&sequences, temp_dir.path()).unwrap();

    // Verify we have 2 .ly files
    assert_eq!(ly_paths.len(), 2);

    // Verify directory now has 5 files total (3 .ily + 2 .ly)
    assert_eq!(fs::read_dir(temp_dir.path()).unwrap().count(), 5);

    // Verify each .ly file has correct structure
    for path in &ly_paths {
        let content = fs::read_to_string(path).unwrap();

        // All files should have the template structure
        assert!(content.contains(r#"\version"#));
        assert!(content.contains(r#"\score"#));
        assert!(content.contains(r#"\entry"#));

        // All files should reference the .ily files
        assert!(content.contains(r#"\include "format.ily""#));
        assert!(content.contains(r#"\include "entry.ily""#));
        assert!(content.contains(r#"\include "elements.ily""#));

        // Should not contain placeholder
        assert!(!content.contains("%%%"));
    }

    // Verify specific content for first file
    // Sequence: [(1,0), (-1,1), (-1,0), (0,0)]
    // max_abs_inner = 1, multiplier = 4
    // Pitches: 1*4+0=4, -1*4+1=-3, -1*4+0=-4, 0*4+0=0 → f'4 f4 e4 b4
    let content0 = fs::read_to_string(&ly_paths[0]).unwrap();
    assert!(content0.contains("f'4 f4 e4 b4"), "first file should have correct markup");

    // Verify specific content for second file
    // Sequence: [(0,0), (1,0)]
    // max_abs_inner = 0, multiplier = 3
    // Pitches: 0*3+0=0→b4, 1*3+0=3→e'4
    let content1 = fs::read_to_string(&ly_paths[1]).unwrap();
    assert!(content1.contains("b4 e'4"), "second file should have correct markup");
}