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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
|
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("<svg"), "should contain SVG markup");
assert!(!content.is_empty(), "SVG should not be empty");
}
// Verify SVGs are in output directory
for svg_path in &svg_paths {
assert!(svg_path.starts_with(output_dir.path()));
}
}
/// Test with the example sequence from preliminary overview
#[test]
#[ignore]
fn test_render_sequences_example() {
let sequences = vec![
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());
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert_eq!(svg_paths.len(), 1);
assert!(svg_paths[0].exists());
// Verify substantial content
let content = fs::read_to_string(&svg_paths[0]).unwrap();
assert!(content.len() > 1000, "should have substantial SVG content");
}
/// Test with empty sequence list
#[test]
#[ignore]
fn test_render_sequences_empty() {
let sequences: Vec<Sequence> = 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<Sequence> = (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("<svg"));
}
}
/// Test that temporary files are cleaned up by default
#[test]
#[ignore]
fn test_render_sequences_temp_cleanup() {
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();
// Temporary files should not be visible after function returns
// (they're cleaned up automatically)
// We can't directly test this without implementation details,
// but we can verify the function completed successfully
}
/// Test with custom LilyPond binary path
#[test]
#[ignore]
fn test_render_sequences_custom_binary() {
let sequences = vec![Sequence::new(vec![Value::origin()])];
let output_dir = TempDir::new().unwrap();
let config = RenderConfig::new(output_dir.path())
.lilypond_binary("lilypond"); // Use system lilypond
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert_eq!(svg_paths.len(), 1);
assert!(svg_paths[0].exists());
}
/// Test that output directory is created if it doesn't exist
#[test]
#[ignore]
fn test_render_sequences_creates_output_dir() {
let sequences = vec![Sequence::new(vec![Value::origin()])];
let temp_base = TempDir::new().unwrap();
let output_dir = temp_base.path().join("nested").join("output");
assert!(!output_dir.exists(), "output directory should not exist yet");
let config = RenderConfig::new(&output_dir);
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert!(output_dir.exists(), "output directory should be created");
assert_eq!(svg_paths.len(), 1);
assert!(svg_paths[0].exists());
}
/// Test SVG filenames match expected pattern
#[test]
#[ignore]
fn test_render_sequences_filenames() {
let sequences = vec![
Sequence::new(vec![Value::origin()]),
Sequence::new(vec![Value::new(1, 0)]),
Sequence::new(vec![Value::new(2, 0)]),
];
let output_dir = TempDir::new().unwrap();
let config = RenderConfig::new(output_dir.path());
let svg_paths = render_sequences(&sequences, &config).unwrap();
// With 3 sequences, we should have sequence-0.svg, sequence-1.svg, sequence-2.svg
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 that sequences are rendered in order
#[test]
#[ignore]
fn test_render_sequences_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 output_dir = TempDir::new().unwrap();
let config = RenderConfig::new(output_dir.path());
let svg_paths = render_sequences(&sequences, &config).unwrap();
// Verify order is preserved
assert_eq!(svg_paths.len(), 3);
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 with different sequence lengths
#[test]
#[ignore]
fn test_render_sequences_different_lengths() {
let sequences = vec![
Sequence::new(vec![Value::origin()]), // length 1
Sequence::new(vec![Value::origin(), Value::new(1, 0)]), // length 2
Sequence::new(vec![Value::origin(), Value::new(1, 0), Value::new(2, 0)]), // length 3
];
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(), 3);
// All should be rendered successfully
for svg_path in &svg_paths {
assert!(svg_path.exists());
let content = fs::read_to_string(svg_path).unwrap();
assert!(content.contains("<svg"));
}
}
/// Test that each SVG has unique content
#[test]
#[ignore]
fn test_render_sequences_unique_content() {
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());
let svg_paths = render_sequences(&sequences, &config).unwrap();
let content1 = fs::read_to_string(&svg_paths[0]).unwrap();
let content2 = fs::read_to_string(&svg_paths[1]).unwrap();
// Contents should be different
assert_ne!(content1, content2, "each SVG should have unique content");
}
/// Test complete pipeline with complex sequences
#[test]
#[ignore]
fn test_render_sequences_complex() {
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::new(2, 0),
Value::new(-2, 0),
]),
Sequence::new(vec![
Value::origin(),
Value::new(1, 0),
Value::new(2, 0),
Value::new(3, 0),
]),
];
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(), 3);
// Verify all have substantial content
for svg_path in &svg_paths {
let content = fs::read_to_string(svg_path).unwrap();
assert!(content.len() > 1000, "SVG should have substantial content");
assert!(content.contains("<svg"));
assert!(content.contains("</svg>"));
}
}
/// 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<Sequence> = 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"), "SVG {} should have svg tag", i);
assert!(content.contains("</svg>"), "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<Sequence> = (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<Sequence> = 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<Sequence> = (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");
}
// ============================================================================
// Batch Chunking Tests
// ============================================================================
#[test]
#[ignore]
fn test_batch_chunking_basic() {
// Create 25 sequences
let sequences: Vec<Sequence> = (0..25)
.map(|i| {
Sequence::new(vec![Value::new(i, 0)])
})
.collect();
let output_dir = TempDir::new().unwrap();
let config = RenderConfig::new(output_dir.path())
.max_batch_size(10);
let svg_paths = render_sequences(&sequences, &config).unwrap();
// All 25 SVGs should be created despite batching
assert_eq!(svg_paths.len(), 25);
// Verify all SVGs exist
for path in &svg_paths {
assert!(path.exists(), "SVG file should exist: {}", path.display());
}
}
#[test]
#[ignore]
fn test_batch_size_exact_match() {
// Create exactly 10 sequences with batch size 10
let sequences: Vec<Sequence> = (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())
.max_batch_size(10);
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert_eq!(svg_paths.len(), 10);
for path in &svg_paths {
assert!(path.exists());
}
}
#[test]
#[ignore]
fn test_batch_size_larger_than_total() {
// Batch size larger than total sequences
let sequences: Vec<Sequence> = (0..5)
.map(|i| {
Sequence::new(vec![Value::new(i, 0)])
})
.collect();
let output_dir = TempDir::new().unwrap();
let config = RenderConfig::new(output_dir.path())
.max_batch_size(100);
let svg_paths = render_sequences(&sequences, &config).unwrap();
// Should still render all 5 sequences in one batch
assert_eq!(svg_paths.len(), 5);
for path in &svg_paths {
assert!(path.exists());
}
}
#[test]
#[ignore]
fn test_no_batch_limit() {
// No batch limit set (None) - should render all at once
let sequences: Vec<Sequence> = (0..15)
.map(|i| {
Sequence::new(vec![Value::new(i, 0)])
})
.collect();
let output_dir = TempDir::new().unwrap();
let config = RenderConfig::new(output_dir.path());
// Note: not setting max_batch_size, should default to None
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert_eq!(svg_paths.len(), 15);
for path in &svg_paths {
assert!(path.exists());
}
}
#[test]
#[ignore]
fn test_batch_chunking_preserves_order() {
// Test that chunking preserves the order of sequences
let sequences: Vec<Sequence> = (0..12)
.map(|i| {
Sequence::new(vec![Value::new(i, 0), Value::new(i, 1)])
})
.collect();
let output_dir = TempDir::new().unwrap();
let config = RenderConfig::new(output_dir.path())
.max_batch_size(5);
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert_eq!(svg_paths.len(), 12);
// Verify files are in order (sequence-00.svg through sequence-11.svg)
for (i, path) in svg_paths.iter().enumerate() {
let expected_filename = format!("sequence-{:02}.svg", i);
assert_eq!(
path.file_name().unwrap().to_str().unwrap(),
expected_filename,
"SVG files should be in order"
);
}
}
#[test]
#[ignore]
fn test_batch_chunking_with_progress() {
// Test that progress callback works correctly with batching
let sequences: Vec<Sequence> = (0..8)
.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())
.max_batch_size(3)
.progress_callback(callback);
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert_eq!(svg_paths.len(), 8);
// Progress should track within each batch
let calls = progress_calls.lock().unwrap();
assert!(!calls.is_empty(), "Progress callback should be called");
// Note: Progress is per-batch, so we should see:
// Batch 1 (3 items): (1,3), (2,3), (3,3)
// Batch 2 (3 items): (1,3), (2,3), (3,3)
// Batch 3 (2 items): (1,2), (2,2)
assert_eq!(calls.len(), 8, "Should have 8 progress calls total");
}
#[test]
#[ignore]
fn test_batch_size_one() {
// Edge case: batch size of 1 (process one at a time)
let sequences: Vec<Sequence> = (0..3)
.map(|i| {
Sequence::new(vec![Value::new(i, 0)])
})
.collect();
let output_dir = TempDir::new().unwrap();
let config = RenderConfig::new(output_dir.path())
.max_batch_size(1);
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert_eq!(svg_paths.len(), 3);
for path in &svg_paths {
assert!(path.exists());
}
}
// ============================================================================
// Parallel Processing Tests
// ============================================================================
#[test]
#[ignore]
fn test_parallel_rendering_basic() {
// Test that parallel rendering works correctly
let sequences: Vec<Sequence> = (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())
.parallel(true);
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert_eq!(svg_paths.len(), 10);
for path in &svg_paths {
assert!(path.exists(), "SVG file should exist: {}", path.display());
}
}
#[test]
#[ignore]
fn test_parallel_vs_sequential_same_results() {
// Verify that parallel and sequential produce the same results
let sequences: Vec<Sequence> = (0..5)
.map(|i| {
Sequence::new(vec![Value::new(i, 0), Value::new(i, 1)])
})
.collect();
// Sequential rendering
let output_dir_seq = TempDir::new().unwrap();
let config_seq = RenderConfig::new(output_dir_seq.path())
.parallel(false);
let svg_paths_seq = render_sequences(&sequences, &config_seq).unwrap();
// Parallel rendering
let output_dir_par = TempDir::new().unwrap();
let config_par = RenderConfig::new(output_dir_par.path())
.parallel(true);
let svg_paths_par = render_sequences(&sequences, &config_par).unwrap();
// Both should produce same number of files
assert_eq!(svg_paths_seq.len(), svg_paths_par.len());
// Both should have files with same names
for (seq_path, par_path) in svg_paths_seq.iter().zip(svg_paths_par.iter()) {
assert_eq!(
seq_path.file_name(),
par_path.file_name(),
"Sequential and parallel should produce same filenames"
);
assert!(seq_path.exists());
assert!(par_path.exists());
}
}
#[test]
#[ignore]
fn test_parallel_with_progress_callback() {
// Test that progress callbacks work with parallel rendering
let sequences: Vec<Sequence> = (0..8)
.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())
.parallel(true)
.progress_callback(callback);
let svg_paths = render_sequences(&sequences, &config).unwrap();
assert_eq!(svg_paths.len(), 8);
// Progress callback should have been called 8 times
let calls = progress_calls.lock().unwrap();
assert_eq!(calls.len(), 8, "Progress callback should be called for each file");
// All calls should have total=8
for (_current, total) in calls.iter() {
assert_eq!(*total, 8, "Total should always be 8");
}
}
#[test]
#[ignore]
fn test_parallel_default_is_false() {
let config = RenderConfig::default();
assert!(!config.parallel, "Parallel should default to false");
let config2 = RenderConfig::new("output");
assert!(!config2.parallel, "Parallel should default to false");
}
#[test]
#[ignore]
fn test_parallel_with_batch_chunking() {
// Test parallel rendering combined with batch chunking
let sequences: Vec<Sequence> = (0..15)
.map(|i| {
Sequence::new(vec![Value::new(i, 0)])
})
.collect();
let output_dir = TempDir::new().unwrap();
let config = RenderConfig::new(output_dir.path())
.parallel(true)
.max_batch_size(5);
let svg_paths = render_sequences(&sequences, &config).unwrap();
// All 15 SVGs should be created
assert_eq!(svg_paths.len(), 15);
// Verify files are in order (sequence-00.svg through sequence-14.svg)
for (i, path) in svg_paths.iter().enumerate() {
let expected_filename = format!("sequence-{:02}.svg", i);
assert_eq!(
path.file_name().unwrap().to_str().unwrap(),
expected_filename,
"Files should be in correct order"
);
assert!(path.exists());
}
}
#[test]
#[ignore]
fn test_benchmark_parallel_vs_sequential() {
use std::time::Instant;
// Create many sequences for meaningful benchmark
let sequences: Vec<Sequence> = (0..20)
.map(|i| {
Sequence::new(vec![Value::new(i, 0)])
})
.collect();
// Sequential rendering
let output_dir_seq = TempDir::new().unwrap();
let config_seq = RenderConfig::new(output_dir_seq.path())
.parallel(false);
let start = Instant::now();
let svg_paths_seq = render_sequences(&sequences, &config_seq).unwrap();
let sequential_time = start.elapsed();
assert_eq!(svg_paths_seq.len(), 20);
// Parallel rendering
let output_dir_par = TempDir::new().unwrap();
let config_par = RenderConfig::new(output_dir_par.path())
.parallel(true);
let start = Instant::now();
let svg_paths_par = render_sequences(&sequences, &config_par).unwrap();
let parallel_time = start.elapsed();
assert_eq!(svg_paths_par.len(), 20);
// Print timing results
println!("\n=== Parallel vs Sequential Benchmark (20 sequences) ===");
println!("Sequential time: {:?}", sequential_time);
println!("Parallel time: {:?}", parallel_time);
if parallel_time < sequential_time {
let speedup = sequential_time.as_secs_f64() / parallel_time.as_secs_f64();
println!("Speedup: {:.2}x faster", speedup);
} else {
println!("Note: Parallel was slower (overhead or system constraints)");
}
// No strict assertion on speed - depends on system
// Just verify both completed successfully
}
|