hedl-cli 2.0.0

HEDL command-line interface
Documentation
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
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Unit tests for batch processing module
//!
//! These tests verify the internal behavior of the batch processing engine,
//! including configuration, progress tracking, error handling, and performance
//! characteristics.

use hedl_cli::batch::{
    BatchConfig, BatchExecutor, BatchOperation, FileResult, FormatOperation, LintOperation,
    ValidationOperation,
};
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::{tempdir, TempDir};

// ============================================================================
// Test Helpers
// ============================================================================

/// Create test files with valid HEDL content
fn create_valid_test_files(count: usize) -> (TempDir, Vec<PathBuf>) {
    let dir = tempdir().expect("Failed to create temp dir");
    let mut paths = Vec::new();

    for i in 0..count {
        let path = dir.path().join(format!("test{i}.hedl"));
        let content = format!(
            r"%VERSION: 1.0
---
id: {}
name: Test {}
value: {}
",
            i,
            i,
            i * 10
        );
        fs::write(&path, content).expect("Failed to write test file");
        paths.push(path);
    }

    (dir, paths)
}

/// Create test files with matrix lists for format testing
fn create_matrix_test_files(count: usize) -> (TempDir, Vec<PathBuf>) {
    let dir = tempdir().expect("Failed to create temp dir");
    let mut paths = Vec::new();

    for i in 0..count {
        let path = dir.path().join(format!("matrix{i}.hedl"));
        let base = i * 100;
        let content = format!(
            "%V:2.0
%NULL:~
%QUOTE:\"
%S:Item:[id,value]
---
items:@Item
 |item_{},100
 |item_{},200
 |item_{},300
",
            base,
            base + 1,
            base + 2
        );
        fs::write(&path, content).expect("Failed to write test file");
        paths.push(path);
    }

    (dir, paths)
}

/// Create test files with some invalid content
fn create_mixed_validity_files() -> (TempDir, Vec<PathBuf>) {
    let dir = tempdir().expect("Failed to create temp dir");
    let mut paths = Vec::new();

    // Valid files
    for i in 0..3 {
        let path = dir.path().join(format!("valid{i}.hedl"));
        let content = format!(
            r"%VERSION: 1.0
---
id: {i}
"
        );
        fs::write(&path, content).expect("Failed to write valid file");
        paths.push(path);
    }

    // Invalid files
    for i in 0..2 {
        let path = dir.path().join(format!("invalid{i}.hedl"));
        let content = "invalid syntax here";
        fs::write(&path, content).expect("Failed to write invalid file");
        paths.push(path);
    }

    (dir, paths)
}

/// Create files with varying sizes
fn create_varying_size_files() -> (TempDir, Vec<PathBuf>) {
    let dir = tempdir().expect("Failed to create temp dir");
    let mut paths = Vec::new();

    // Small file (< 1KB)
    let small_path = dir.path().join("small.hedl");
    fs::write(&small_path, "%VERSION: 1.0\n---\nid: 1\n").expect("Failed to write small file");
    paths.push(small_path);

    // Medium file (~ 5KB)
    let medium_path = dir.path().join("medium.hedl");
    let mut medium_content =
        String::from("%VERSION: 1.0\n%STRUCT: Item (100): [id,value]\n---\nitems:@Item\n");
    for i in 0..100 {
        medium_content.push_str(&format!(" |item_{i},value_{i}\n"));
    }
    fs::write(&medium_path, medium_content).expect("Failed to write medium file");
    paths.push(medium_path);

    // Large file (~ 50KB)
    let large_path = dir.path().join("large.hedl");
    let mut large_content = String::from(
        "%VERSION: 1.0\n%STRUCT: Item (1000): [id,name,description,value]\n---\nitems:@Item\n",
    );
    for i in 0..1000 {
        large_content.push_str(&format!(
            " |item_{i},name_{i},This is a longer description for item number {i},value_{i}\n"
        ));
    }
    fs::write(&large_path, large_content).expect("Failed to write large file");
    paths.push(large_path);

    (dir, paths)
}

// ============================================================================
// BatchConfig Tests
// ============================================================================

#[test]
fn test_batch_config_default() {
    let config = BatchConfig::default();
    assert_eq!(config.parallel_threshold, 10);
    assert_eq!(config.max_threads, None);
    assert_eq!(config.progress_interval, 1);
    assert!(!config.verbose);
}

#[test]
fn test_batch_config_custom() {
    let config = BatchConfig {
        parallel_threshold: 5,
        max_threads: Some(4),
        progress_interval: 10,
        verbose: true,
        max_files: Some(5000),
    };
    assert_eq!(config.parallel_threshold, 5);
    assert_eq!(config.max_threads, Some(4));
    assert_eq!(config.progress_interval, 10);
    assert!(config.verbose);
    assert_eq!(config.max_files, Some(5000));
}

#[test]
fn test_batch_config_clone() {
    let config1 = BatchConfig {
        parallel_threshold: 20,
        max_threads: Some(8),
        progress_interval: 5,
        verbose: true,
        max_files: Some(10_000),
    };
    let config2 = config1.clone();
    assert_eq!(config1.parallel_threshold, config2.parallel_threshold);
    assert_eq!(config1.max_threads, config2.max_threads);
    assert_eq!(config1.max_files, config2.max_files);
}

// ============================================================================
// FileResult Tests
// ============================================================================

#[test]
fn test_file_result_success() {
    let path = PathBuf::from("test.hedl");
    let result = FileResult::success(path.clone(), 42);
    assert!(result.is_success());
    assert!(!result.is_failure());
    assert_eq!(result.path, path);
    assert_eq!(result.result.unwrap(), 42);
}

#[test]
fn test_file_result_failure() {
    let path = PathBuf::from("test.hedl");
    let result: FileResult<()> = FileResult::failure(
        path.clone(),
        hedl_cli::error::CliError::parse("test error".to_string()),
    );
    assert!(!result.is_success());
    assert!(result.is_failure());
    assert_eq!(result.path, path);
    assert!(result.result.is_err());
}

// ============================================================================
// BatchExecutor Tests - Validation
// ============================================================================

#[test]
fn test_validation_operation_success() {
    let (_dir, files) = create_valid_test_files(5);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 5);
    assert_eq!(results.success_count(), 5);
    assert_eq!(results.failure_count(), 0);
    assert!(results.all_succeeded());
    assert!(!results.has_failures());
}

#[test]
fn test_validation_operation_with_failures() {
    let (_dir, files) = create_mixed_validity_files();
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 5);
    assert_eq!(results.success_count(), 3);
    assert_eq!(results.failure_count(), 2);
    assert!(!results.all_succeeded());
    assert!(results.has_failures());

    // Check that failures are collected
    let failures: Vec<_> = results.failures().collect();
    assert_eq!(failures.len(), 2);
}

#[test]
fn test_validation_operation_strict_mode() {
    let (_dir, files) = create_valid_test_files(3);
    let processor = BatchExecutor::new(BatchConfig::default());

    // Non-strict should succeed
    let operation_normal = ValidationOperation { strict: false };
    let results_normal = processor
        .process(&files, operation_normal, false)
        .expect("Processing should succeed");
    assert!(results_normal.all_succeeded());

    // Strict mode should also succeed for valid files
    let operation_strict = ValidationOperation { strict: true };
    let results_strict = processor
        .process(&files, operation_strict, false)
        .expect("Processing should succeed");
    assert!(results_strict.all_succeeded());
}

#[test]
fn test_validation_operation_nonexistent_file() {
    let files = vec![PathBuf::from("/nonexistent/file.hedl")];
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 1);
    assert_eq!(results.failure_count(), 1);
    assert!(results.has_failures());
}

// ============================================================================
// BatchExecutor Tests - Format
// ============================================================================

#[test]
fn test_format_operation_success() {
    let (_dir, files) = create_valid_test_files(5);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = FormatOperation {
        check: false,
        ditto: false,
        with_counts: false,
    };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 5);
    assert_eq!(results.success_count(), 5);
    assert!(results.all_succeeded());

    // Verify that formatted output is returned
    for result in results.successes() {
        let formatted = result.result.as_ref().unwrap();
        assert!(formatted.contains("%VERSION: 1.0"));
    }
}

#[test]
fn test_format_operation_with_counts() {
    let (_dir, files) = create_matrix_test_files(3);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = FormatOperation {
        check: false,
        ditto: false,
        with_counts: true,
    };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    // Check that processing completed
    assert_eq!(results.total_files(), 3);

    // Files should succeed - if not, there's a parsing issue
    if !results.all_succeeded() {
        for result in results.failures() {
            eprintln!(
                "File {} failed: {:?}",
                result.path.display(),
                result.result.as_ref().err()
            );
        }
    }

    assert!(
        results.success_count() > 0,
        "At least some files should succeed"
    );

    // Verify that count hints are added to successful files
    for result in results.successes() {
        let formatted = result.result.as_ref().unwrap();
        // v2.0 uses %C:Type.total=N directives instead of Type(N) syntax
        assert!(
            formatted.contains("%C:") && formatted.contains(".total=3"),
            "Expected count directive %C:Type.total=3 in formatted output for file {:?}",
            result.path
        );
    }
}

#[test]
fn test_format_operation_with_ditto() {
    let (_dir, files) = create_valid_test_files(3);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = FormatOperation {
        check: false,
        ditto: true,
        with_counts: false,
    };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.success_count(), 3);
    assert!(results.all_succeeded());
}

#[test]
fn test_format_operation_check_mode() {
    let (_dir, files) = create_valid_test_files(3);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = FormatOperation {
        check: true,
        ditto: false,
        with_counts: false,
    };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    // Files may or may not be canonical, so we just check that processing completed
    assert_eq!(results.total_files(), 3);
}

#[test]
fn test_format_operation_invalid_file() {
    let (_dir, files) = create_mixed_validity_files();
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = FormatOperation {
        check: false,
        ditto: false,
        with_counts: false,
    };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.success_count(), 3);
    assert_eq!(results.failure_count(), 2);
}

// ============================================================================
// BatchExecutor Tests - Lint
// ============================================================================

#[test]
fn test_lint_operation_success() {
    let (_dir, files) = create_valid_test_files(5);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = LintOperation { warn_error: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 5);
    assert!(results.success_count() <= 5); // May have lint warnings

    // Verify that diagnostics are returned
    for result in results.successes() {
        let diagnostics = result.result.as_ref().unwrap();
        // Diagnostics is a Vec<String>
        assert!(diagnostics.is_empty() || !diagnostics.is_empty());
    }
}

#[test]
fn test_lint_operation_warn_error() {
    let (_dir, files) = create_valid_test_files(3);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = LintOperation { warn_error: true };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 3);
    // With warn_error, any warnings become failures
}

#[test]
fn test_lint_operation_invalid_file() {
    let (_dir, files) = create_mixed_validity_files();
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = LintOperation { warn_error: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    // Invalid files should fail to parse
    assert!(results.failure_count() >= 2);
}

// ============================================================================
// BatchExecutor Tests - Parallelization
// ============================================================================

#[test]
fn test_serial_processing_small_batch() {
    let (_dir, files) = create_valid_test_files(5);
    let processor = BatchExecutor::new(BatchConfig {
        parallel_threshold: 100, // Force serial
        ..Default::default()
    });
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 5);
    assert_eq!(results.success_count(), 5);
}

#[test]
fn test_parallel_processing_large_batch() {
    let (_dir, files) = create_valid_test_files(50);
    let processor = BatchExecutor::new(BatchConfig {
        parallel_threshold: 10, // Force parallel
        ..Default::default()
    });
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 50);
    assert_eq!(results.success_count(), 50);
}

#[test]
fn test_parallel_threshold_boundary() {
    let (_dir, files) = create_valid_test_files(10);

    // Just below threshold - should be serial
    let processor_serial = BatchExecutor::new(BatchConfig {
        parallel_threshold: 11,
        ..Default::default()
    });
    let operation_serial = ValidationOperation { strict: false };
    let results_serial = processor_serial
        .process(&files, operation_serial, false)
        .expect("Processing should succeed");
    assert_eq!(results_serial.success_count(), 10);

    // At threshold - should be parallel
    let processor_parallel = BatchExecutor::new(BatchConfig {
        parallel_threshold: 10,
        ..Default::default()
    });
    let operation_parallel = ValidationOperation { strict: false };
    let results_parallel = processor_parallel
        .process(&files, operation_parallel, false)
        .expect("Processing should succeed");
    assert_eq!(results_parallel.success_count(), 10);
}

#[test]
fn test_max_threads_configuration() {
    let (_dir, files) = create_valid_test_files(20);

    // Test with limited threads
    let processor = BatchExecutor::new(BatchConfig {
        parallel_threshold: 10,
        max_threads: Some(2),
        ..Default::default()
    });
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 20);
    assert_eq!(results.success_count(), 20);
}

// ============================================================================
// BatchExecutor Tests - Performance Characteristics
// ============================================================================

#[test]
fn test_varying_file_sizes() {
    let (_dir, files) = create_varying_size_files();
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 3);
    assert_eq!(results.success_count(), 3, "All files should succeed");
}

#[test]
fn test_throughput_calculation() {
    let (_dir, files) = create_valid_test_files(10);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    let throughput = results.throughput();
    // Throughput may be 0 for very fast operations (elapsed_ms = 0)
    assert!(
        throughput >= 0.0,
        "Throughput should be non-negative, got {throughput}"
    );
    if throughput > 0.0 {
        assert!(
            throughput < 1_000_000.0,
            "Throughput should be reasonable (files/s)"
        );
    }
}

#[test]
fn test_processing_time_recorded() {
    let (_dir, files) = create_valid_test_files(5);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    // Elapsed time is recorded (u128 is always >= 0)
    // Just verify it exists
    let _ = results.elapsed_ms;
}

// ============================================================================
// BatchExecutor Tests - Progress Reporting
// ============================================================================

#[test]
fn test_progress_disabled() {
    let (_dir, files) = create_valid_test_files(10);
    let processor = BatchExecutor::new(BatchConfig {
        progress_interval: 0, // Disable progress
        ..Default::default()
    });
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.success_count(), 10);
}

#[test]
fn test_progress_with_verbose() {
    let (_dir, files) = create_valid_test_files(5);
    let processor = BatchExecutor::new(BatchConfig {
        verbose: true,
        ..Default::default()
    });
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, true)
        .expect("Processing should succeed");

    assert_eq!(results.success_count(), 5);
}

#[test]
fn test_progress_interval() {
    let (_dir, files) = create_valid_test_files(20);
    let processor = BatchExecutor::new(BatchConfig {
        progress_interval: 5, // Report every 5 files
        ..Default::default()
    });
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, true)
        .expect("Processing should succeed");

    assert_eq!(results.success_count(), 20);
}

// ============================================================================
// BatchResults Tests
// ============================================================================

#[test]
fn test_batch_results_statistics() {
    let (_dir, files) = create_mixed_validity_files();
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 5);
    assert_eq!(results.success_count(), 3);
    assert_eq!(results.failure_count(), 2);
    assert!(!results.all_succeeded());
    assert!(results.has_failures());
}

#[test]
fn test_batch_results_iterators() {
    let (_dir, files) = create_mixed_validity_files();
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    // Test successes iterator
    let successes: Vec<_> = results.successes().collect();
    assert_eq!(successes.len(), 3);

    // Test failures iterator
    let failures: Vec<_> = results.failures().collect();
    assert_eq!(failures.len(), 2);
}

#[test]
fn test_batch_results_all_success() {
    let (_dir, files) = create_valid_test_files(5);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert!(results.all_succeeded());
    assert!(!results.has_failures());
    assert_eq!(results.failures().count(), 0);
}

// ============================================================================
// Edge Cases and Boundary Conditions
// ============================================================================

#[test]
fn test_empty_file_list() {
    let files: Vec<PathBuf> = vec![];
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 0);
    assert_eq!(results.success_count(), 0);
    assert_eq!(results.failure_count(), 0);
}

#[test]
fn test_single_file() {
    let (_dir, files) = create_valid_test_files(1);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 1);
    assert_eq!(results.success_count(), 1);
}

#[test]
fn test_very_large_batch() {
    let (_dir, files) = create_valid_test_files(200);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 200);
    assert_eq!(results.success_count(), 200);
}

#[test]
fn test_all_failures() {
    let dir = tempdir().expect("Failed to create temp dir");
    let mut files = Vec::new();

    // Create only invalid files
    for i in 0..5 {
        let path = dir.path().join(format!("invalid{i}.hedl"));
        fs::write(&path, "invalid syntax").expect("Failed to write file");
        files.push(path);
    }

    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = ValidationOperation { strict: false };

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 5);
    assert_eq!(results.failure_count(), 5);
    assert!(results.has_failures());
    assert!(!results.all_succeeded());
}

// ============================================================================
// Custom Operation Tests
// ============================================================================

/// Custom operation that counts lines in files
#[derive(Clone)]
struct LineCountOperation;

impl BatchOperation for LineCountOperation {
    type Output = usize;

    fn process_file(&self, path: &Path) -> Result<Self::Output, hedl_cli::error::CliError> {
        let content = std::fs::read_to_string(path)
            .map_err(|e| hedl_cli::error::CliError::io_error(path, e))?;
        Ok(content.lines().count())
    }

    fn name(&self) -> &'static str {
        "line-count"
    }
}

#[test]
fn test_custom_operation() {
    let (_dir, files) = create_valid_test_files(5);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = LineCountOperation;

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 5);
    assert_eq!(results.success_count(), 5);

    // Each file should have at least 4 lines (VERSION, ---, and content)
    for result in results.successes() {
        let line_count = result.result.as_ref().unwrap();
        assert!(*line_count >= 4);
    }
}

/// Custom operation that always fails
#[derive(Clone)]
struct AlwaysFailOperation;

impl BatchOperation for AlwaysFailOperation {
    type Output = ();

    fn process_file(&self, path: &Path) -> Result<Self::Output, hedl_cli::error::CliError> {
        Err(hedl_cli::error::CliError::parse(format!(
            "Intentional failure for {}",
            path.display()
        )))
    }

    fn name(&self) -> &'static str {
        "always-fail"
    }
}

#[test]
fn test_custom_operation_all_failures() {
    let (_dir, files) = create_valid_test_files(3);
    let processor = BatchExecutor::new(BatchConfig::default());
    let operation = AlwaysFailOperation;

    let results = processor
        .process(&files, operation, false)
        .expect("Processing should succeed");

    assert_eq!(results.total_files(), 3);
    assert_eq!(results.failure_count(), 3);
    assert!(!results.all_succeeded());
}