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
// 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.

//! Comprehensive tests for batch processing functionality

use assert_cmd::cargo_bin;
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::{tempdir, TempDir};

/// Test helper to create a HEDL command
fn hedl_cmd() -> Command {
    Command::new(cargo_bin!("hedl"))
}

/// Create a temporary directory with multiple test HEDL files
fn create_test_files(count: usize) -> (TempDir, Vec<String>) {
    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.to_str().unwrap().to_string());
    }

    (dir, paths)
}

/// Create test files with some invalid ones
fn create_mixed_test_files() -> (TempDir, Vec<String>, Vec<String>) {
    let dir = tempdir().expect("Failed to create temp dir");
    let mut valid_paths = Vec::new();
    let mut invalid_paths = Vec::new();

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

    // Create invalid files
    for i in 0..2 {
        let path = dir.path().join(format!("invalid{i}.hedl"));
        let content = format!(
            r"%VERSION: 1.0
---
invalid syntax here {i}
"
        );
        fs::write(&path, content).expect("Failed to write invalid file");
        invalid_paths.push(path.to_str().unwrap().to_string());
    }

    (dir, valid_paths, invalid_paths)
}

// ============================================================================
// Batch Validate Tests
// ============================================================================

#[test]
fn test_batch_validate_success() {
    let (_dir, paths) = create_test_files(5);

    hedl_cmd()
        .arg("batch-validate")
        .args(&paths)
        .assert()
        .success()
        .stdout(predicate::str::contains("Batch Operation:"))
        .stdout(predicate::str::contains("validate"));
}

#[test]
fn test_batch_validate_with_strict_mode() {
    let (_dir, paths) = create_test_files(3);

    hedl_cmd()
        .arg("batch-validate")
        .args(&paths)
        .arg("--strict")
        .assert()
        .success();
}

#[test]
fn test_batch_validate_with_parallel_flag() {
    let (_dir, paths) = create_test_files(10);

    hedl_cmd()
        .arg("batch-validate")
        .args(&paths)
        .arg("--parallel")
        .assert()
        .success()
        .stdout(predicate::str::contains("Total files: 10"));
}

#[test]
fn test_batch_validate_with_verbose() {
    let (_dir, paths) = create_test_files(3);

    hedl_cmd()
        .arg("batch-validate")
        .args(&paths)
        .arg("--verbose")
        .assert()
        .success();
}

#[test]
fn test_batch_validate_mixed_results() {
    let (_dir, valid, invalid) = create_mixed_test_files();
    let mut all_paths = valid.clone();
    all_paths.extend(invalid);

    hedl_cmd()
        .arg("batch-validate")
        .args(&all_paths)
        .assert()
        .failure()
        .stderr(predicate::str::contains("Validation failures:"));
}

#[test]
fn test_batch_validate_empty_file_list() {
    hedl_cmd().arg("batch-validate").assert().success(); // No files = nothing to validate = success
}

#[test]
fn test_batch_validate_nonexistent_files() {
    hedl_cmd()
        .arg("batch-validate")
        .arg("/nonexistent/file1.hedl")
        .arg("/nonexistent/file2.hedl")
        .assert()
        .failure()
        .stderr(predicate::str::contains("failed"));
}

// ============================================================================
// Batch Format Tests
// ============================================================================

#[test]
fn test_batch_format_stdout() {
    let (_dir, paths) = create_test_files(3);

    hedl_cmd()
        .arg("batch-format")
        .args(&paths)
        .assert()
        .success()
        .stdout(predicate::str::contains("Batch Operation:"))
        .stdout(predicate::str::contains("format"));
}

#[test]
fn test_batch_format_to_output_dir() {
    let (_dir, paths) = create_test_files(5);
    let output_dir = tempdir().expect("Failed to create output dir");

    hedl_cmd()
        .arg("batch-format")
        .args(&paths)
        .arg("--output-dir")
        .arg(output_dir.path())
        .assert()
        .success();

    // Verify output files were created
    for i in 0..5 {
        let output_file = output_dir.path().join(format!("test{i}.hedl"));
        assert!(
            output_file.exists(),
            "Output file {} should exist",
            output_file.display()
        );

        let content = fs::read_to_string(&output_file).expect("Failed to read output file");
        assert!(content.contains("%VERSION: 1.0"));
        assert!(content.contains(&format!("id: {i}")));
    }
}

#[test]
fn test_batch_format_check_mode() {
    let dir = tempdir().expect("Failed to create temp dir");

    // Create a canonical file
    let canonical_path = dir.path().join("canonical.hedl");
    let canonical_content = r"%VERSION: 1.0
---
a: 1
b: 2
";
    fs::write(&canonical_path, canonical_content).expect("Failed to write canonical file");

    // Create a non-canonical file
    let non_canonical_path = dir.path().join("non_canonical.hedl");
    let non_canonical_content = r"%VERSION: 1.0
---
a:1
b:2
";
    fs::write(&non_canonical_path, non_canonical_content)
        .expect("Failed to write non-canonical file");

    // Check should fail because one file is not canonical
    hedl_cmd()
        .arg("batch-format")
        .arg(canonical_path.to_str().unwrap())
        .arg(non_canonical_path.to_str().unwrap())
        .arg("--check")
        .assert()
        .failure();
}

#[test]
fn test_batch_format_with_ditto() {
    let dir = tempdir().expect("Failed to create temp dir");
    let path = dir.path().join("test.hedl");

    let content = r"%VERSION: 1.0
%STRUCT: T: [id,v]
---
d:@T
 | x,1
 | y,1
 | z,1
";
    fs::write(&path, content).expect("Failed to write test file");

    hedl_cmd()
        .arg("batch-format")
        .arg(path.to_str().unwrap())
        .arg("--ditto")
        .assert()
        .success();
}

#[test]
fn test_batch_format_with_counts() {
    let dir = tempdir().expect("Failed to create temp dir");
    let path = dir.path().join("test.hedl");

    let content = "%V:2.0
%NULL:~
%QUOTE:\"
%S:Team:[id,name]
---
teams:@Team
 |t1,Warriors
 |t2,Lakers
";
    fs::write(&path, content).expect("Failed to write test file");

    let output_dir = tempdir().expect("Failed to create output dir");

    hedl_cmd()
        .arg("batch-format")
        .arg(path.to_str().unwrap())
        .arg("--with-counts")
        .arg("--output-dir")
        .arg(output_dir.path())
        .assert()
        .success();

    let output_file = output_dir.path().join("test.hedl");
    let formatted = fs::read_to_string(&output_file).expect("Failed to read output");
    // v2.0 uses %C:Type.total=N directives instead of Type(N) syntax
    assert!(formatted.contains("%C:") && formatted.contains(".total=2"));
}

#[test]
fn test_batch_format_parallel() {
    let (_dir, paths) = create_test_files(20);

    hedl_cmd()
        .arg("batch-format")
        .args(&paths)
        .arg("--parallel")
        .assert()
        .success()
        .stdout(predicate::str::contains("Total files: 20"));
}

#[test]
fn test_batch_format_verbose() {
    let (_dir, paths) = create_test_files(3);

    hedl_cmd()
        .arg("batch-format")
        .args(&paths)
        .arg("--verbose")
        .assert()
        .success();
}

// ============================================================================
// Batch Lint Tests
// ============================================================================

#[test]
fn test_batch_lint_success() {
    let (_dir, paths) = create_test_files(5);

    hedl_cmd()
        .arg("batch-lint")
        .args(&paths)
        .assert()
        .success()
        .stdout(predicate::str::contains("Batch Operation:"))
        .stdout(predicate::str::contains("lint"));
}

#[test]
fn test_batch_lint_parallel() {
    let (_dir, paths) = create_test_files(10);

    hedl_cmd()
        .arg("batch-lint")
        .args(&paths)
        .arg("--parallel")
        .assert()
        .success();
}

#[test]
fn test_batch_lint_verbose() {
    let (_dir, paths) = create_test_files(3);

    hedl_cmd()
        .arg("batch-lint")
        .args(&paths)
        .arg("--verbose")
        .assert()
        .success();
}

#[test]
fn test_batch_lint_warn_error() {
    let (_dir, paths) = create_test_files(3);

    // This test behavior depends on what lint warnings are found
    // Just verify the command runs
    let _result = hedl_cmd()
        .arg("batch-lint")
        .args(&paths)
        .arg("--warn-error")
        .assert();
}

// ============================================================================
// Performance and Edge Case Tests
// ============================================================================

#[test]
fn test_batch_large_number_of_files() {
    let (_dir, paths) = create_test_files(50);

    hedl_cmd()
        .arg("batch-validate")
        .args(&paths)
        .arg("--parallel")
        .assert()
        .success()
        .stdout(predicate::str::contains("Total files: 50"));
}

#[test]
fn test_batch_single_file() {
    let (_dir, paths) = create_test_files(1);

    hedl_cmd()
        .arg("batch-validate")
        .args(&paths)
        .assert()
        .success()
        .stdout(predicate::str::contains("Total files: 1"));
}

#[test]
fn test_batch_mixed_success_failure_counts() {
    let (_dir, valid, invalid) = create_mixed_test_files();
    let mut all_paths = valid.clone();
    all_paths.extend(invalid.clone());

    hedl_cmd()
        .arg("batch-validate")
        .args(&all_paths)
        .assert()
        .failure()
        .stdout(predicate::str::contains("Succeeded: 3"))
        .stdout(predicate::str::contains("Failed: 2"));
}

#[test]
fn test_batch_throughput_reporting() {
    let (_dir, paths) = create_test_files(10);

    hedl_cmd()
        .arg("batch-validate")
        .args(&paths)
        .assert()
        .success()
        .stdout(predicate::str::contains("files/s"));
}

// ============================================================================
// Error Handling Tests
// ============================================================================

#[test]
fn test_batch_continues_on_error() {
    let (_dir, valid, invalid) = create_mixed_test_files();
    let mut all_paths = valid.clone();
    all_paths.extend(invalid);

    let output = hedl_cmd()
        .arg("batch-validate")
        .args(&all_paths)
        .assert()
        .failure();

    // Verify it processed all files (didn't stop at first error)
    let stderr = String::from_utf8_lossy(&output.get_output().stderr);
    assert!(
        stderr.contains("3 of 5 files failed") || stderr.contains("2 of 5 files failed"),
        "Should report total failures, not stop at first error"
    );
}

#[test]
fn test_batch_format_invalid_output_dir() {
    let (_dir, paths) = create_test_files(2);

    hedl_cmd()
        .arg("batch-format")
        .args(&paths)
        .arg("--output-dir")
        .arg("/invalid/nonexistent/directory/that/cannot/be/created")
        .assert()
        .failure();
}

// ============================================================================
// Integration Tests
// ============================================================================

#[test]
fn test_batch_validate_then_format() {
    let (_dir, paths) = create_test_files(5);

    // First validate
    hedl_cmd()
        .arg("batch-validate")
        .args(&paths)
        .assert()
        .success();

    // Then format
    let output_dir = tempdir().expect("Failed to create output dir");
    hedl_cmd()
        .arg("batch-format")
        .args(&paths)
        .arg("--output-dir")
        .arg(output_dir.path())
        .assert()
        .success();

    // Verify formatted files exist
    for i in 0..5 {
        let formatted_file = output_dir.path().join(format!("test{i}.hedl"));
        assert!(formatted_file.exists());
    }
}

#[test]
fn test_batch_format_then_lint() {
    let (_dir, paths) = create_test_files(3);
    let output_dir = tempdir().expect("Failed to create output dir");

    // Format files
    hedl_cmd()
        .arg("batch-format")
        .args(&paths)
        .arg("--output-dir")
        .arg(output_dir.path())
        .assert()
        .success();

    // Collect formatted file paths
    let formatted_paths: Vec<String> = (0..3)
        .map(|i| {
            output_dir
                .path()
                .join(format!("test{i}.hedl"))
                .to_str()
                .unwrap()
                .to_string()
        })
        .collect();

    // Lint formatted files
    hedl_cmd()
        .arg("batch-lint")
        .args(&formatted_paths)
        .assert()
        .success();
}

#[test]
fn test_batch_all_operations_sequence() {
    let (_dir, paths) = create_test_files(5);
    let output_dir = tempdir().expect("Failed to create output dir");

    // Validate
    hedl_cmd()
        .arg("batch-validate")
        .args(&paths)
        .assert()
        .success();

    // Format
    hedl_cmd()
        .arg("batch-format")
        .args(&paths)
        .arg("--output-dir")
        .arg(output_dir.path())
        .assert()
        .success();

    // Collect formatted paths
    let formatted_paths: Vec<String> = (0..5)
        .map(|i| {
            output_dir
                .path()
                .join(format!("test{i}.hedl"))
                .to_str()
                .unwrap()
                .to_string()
        })
        .collect();

    // Lint
    hedl_cmd()
        .arg("batch-lint")
        .args(&formatted_paths)
        .assert()
        .success();
}