etdl-cli 0.2.0

ETDL CLI: compile and validate .etdl documents with IEC 61025 fault tree and IEC 62502 event tree analysis, generating Rust for microservices
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
#[cfg(test)]
mod tests {
    use etdl_parser::ast::EtlDocument;
    use std::collections::BTreeMap;
    use std::path::PathBuf;

    fn fixture_path(filename: &str) -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("tests")
            .join("fixtures")
            .join(filename)
    }

    #[test]
    fn test_parse_full_worked_example() {
        let path = fixture_path("order-fulfillment.etdl");
        let doc = etdl_parser::parse_document_from_file(&path).unwrap();

        assert_eq!(doc.etdl, "1.0.0");
        assert_eq!(doc.info.title, "Order Fulfillment Event Tree");
        assert_eq!(doc.info.domain, "FulfillmentContext");

        assert_eq!(doc.asyncapi_imports.len(), 2);
        assert!(doc.asyncapi_imports.contains_key("orders_api"));
        assert!(doc.asyncapi_imports.contains_key("payment_api"));

        assert_eq!(doc.event_trees.len(), 1);
        let tree = &doc.event_trees["OrderFulfillment"];
        assert_eq!(tree.initiating_event.id, "OrderPlacedTrigger");
        assert_eq!(tree.initiating_event.message.alias, "orders_api");
        assert_eq!(
            tree.initiating_event.message.pointer,
            "#/components/messages/OrderPlaced"
        );

        assert_eq!(tree.nodes.len(), 5);

        let barrier = match &tree.nodes["InventoryCheckBarrier"] {
            etdl_parser::ast::Node::Barrier(b) => b,
            _ => panic!("expected barrier"),
        };
        assert_eq!(barrier.branches.len(), 2);
        assert_eq!(barrier.branches[0].outcome, "SUCCESS");
        assert!(matches!(
            &barrier.branches[0].condition,
            etdl_parser::ast::Condition::Comparison(_)
        ));
        assert!(matches!(
            &barrier.branches[1].condition,
            etdl_parser::ast::Condition::Default
        ));

        let op = match &tree.nodes["ProcessPaymentOperation"] {
            etdl_parser::ast::Node::Operation(op) => op,
            _ => panic!("expected operation"),
        };
        assert_eq!(op.handler, "stripe_charge_handler");
        assert!(op.retry_policy.is_some());
        let retry = op.retry_policy.as_ref().unwrap();
        assert_eq!(retry.max_attempts, 3);
        assert_eq!(op.timeout_ms, Some(5000));
        assert!(op.on_failure.is_some());
        assert!(op.on_failure_probability_source.is_some());

        let fault_trees = doc.fault_trees.as_ref().unwrap();
        assert_eq!(fault_trees.len(), 1);
        let ft = &fault_trees["PaymentGatewayFailure"];
        assert_eq!(ft.top_event.id, "PaymentCaptureFailed");

        let gates = ft.gates.as_ref().unwrap();
        assert_eq!(gates.len(), 1);
        let gate = &gates["GatewayUnavailableOrRejected"];
        assert!(matches!(gate.gate_type, etdl_parser::ast::GateType::Or));
        assert_eq!(gate.inputs.len(), 2);

        assert_eq!(ft.basic_events.len(), 2);
        let be1 = &ft.basic_events["GatewayUnreachable"];
        assert_eq!(be1.probability, Some(0.008));
        let be2 = &ft.basic_events["ChargeRejected"];
        assert_eq!(be2.failure_rate, Some(0.00021));
        assert_eq!(be2.mission_time, Some(24.0));
    }

    #[test]
    fn test_asyncapi_loading() {
        let base_dir = fixture_path("");
        let doc_path = fixture_path("order-fulfillment.etdl");
        let doc = etdl_parser::parse_document_from_file(&doc_path).unwrap();

        let mut registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        for (alias, location) in &doc.asyncapi_imports {
            registry.load(alias, location, &base_dir).unwrap();
        }

        let message_ref = &doc.event_trees["OrderFulfillment"].initiating_event.message;
        let resolved = registry.resolve(message_ref).unwrap();
        assert!(resolved.get("payload").is_some());
        let payload = &resolved["payload"];
        assert_eq!(payload["type"], "object");
    }

    #[test]
    fn test_compile_worked_example() {
        let base_dir = fixture_path("");
        let doc_path = fixture_path("order-fulfillment.etdl");
        let doc = etdl_parser::parse_document_from_file(&doc_path).unwrap();

        let registry = etdl_parser::load_asyncapi_imports(&doc, &base_dir).unwrap();

        let compiler = etdl_compiler::Compiler::new();
        let result = compiler.compile(&doc, &registry);

        assert!(
            result.rust_output.is_some(),
            "Compilation should produce Rust code. Diagnostics: {:?}",
            result.diagnostics
        );

        let rust_code = result.rust_output.unwrap();
        println!("=== GENERATED RUST CODE ===\n{}\n=== END ===", rust_code);

        assert!(rust_code.contains("AUTOGENERATED BY ETDL COMPILER"));
        assert!(rust_code.contains("handle_order_placed_trigger"));
        assert!(rust_code.contains("BranchMonitor::new"));
        assert!(rust_code.contains("stripe_charge_handler"));
        assert!(rust_code.contains("record_branch"));
        assert!(
            rust_code.contains("InventoryCheckBarrier"),
            "Generated code should reference InventoryCheckBarrier"
        );

        let errors: Vec<_> = result.diagnostics.iter().filter(|d| d.is_error()).collect();
        assert!(
            errors.is_empty(),
            "Compilation should have no errors, got: {:?}",
            errors
        );
    }

    #[test]
    fn test_ecel_parsing() {
        use etdl_parser::ecel::*;

        let cond = parse_condition("message.payload.items[*].qty > 0").unwrap();
        match cond {
            Condition::Comparison(cmp) => {
                assert_eq!(cmp.op, Comparator::Gt);
                match &cmp.left {
                    Operand::Path(path) => {
                        assert_eq!(path.segments.len(), 5);
                    }
                    _ => panic!("expected path"),
                }
                match &cmp.right {
                    Operand::Literal(Literal::Number(n)) => assert_eq!(*n, 0.0),
                    _ => panic!("expected number literal"),
                }
            }
            _ => panic!("expected comparison"),
        }
    }

    #[test]
    fn test_fault_tree_probability() {
        let _base_dir = fixture_path("");
        let doc_path = fixture_path("order-fulfillment.etdl");
        let doc = etdl_parser::parse_document_from_file(&doc_path).unwrap();

        let mut diagnostics = Vec::new();
        let probs = etdl_compiler::fault_tree::resolve_fault_trees(&doc, &mut diagnostics);

        assert!(
            diagnostics.iter().all(|d| !d.is_error()),
            "Fault tree resolution should have no errors: {:?}",
            diagnostics
        );

        let payment_failure_prob = probs.get("PaymentGatewayFailure").unwrap();
        let charge_rejected_prob = 1.0 - (-0.00021_f64 * 24.0).exp();
        let expected = 1.0 - (1.0 - 0.008) * (1.0 - charge_rejected_prob);

        assert!(
            (payment_failure_prob - expected).abs() < 0.00001,
            "Expected ~{:.6}, got {:.6}",
            expected,
            payment_failure_prob
        );
    }

    #[test]
    fn test_validation_detects_errors() {
        let yaml = r#"
etdl: "1.0.0"
info:
  title: "Bad Tree"
  version: "1.0.0"
  domain: "Test"
asyncapi_imports: {}
eventTrees:
  Bad:
    initiatingEvent:
      id: Test
      message: "bad#/foo"
      next: MissingNode
    nodes: {}
"#;
        let doc: EtlDocument = serde_yaml::from_str(yaml).unwrap();
        let mut diagnostics = Vec::new();

        let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        etdl_compiler::validate::validate_document(&doc, &registry, &mut diagnostics);

        let errors: Vec<_> = diagnostics.iter().filter(|d| d.is_error()).collect();
        assert!(!errors.is_empty(), "Should detect errors in bad document");
        assert!(
            errors
                .iter()
                .any(|d| d.code == "E-103" || d.code == "V-101"),
            "Should have reference or structural errors, got: {:?}",
            errors
        );
    }

    #[test]
    fn test_advanced_fault_tree_features() {
        use etdl_parser::ast::{BasicEventType, GateType};

        let path = fixture_path("advanced-fault-tree.etdl");
        let doc = etdl_parser::parse_document_from_file(&path).unwrap();

        let ft = &doc.fault_trees.as_ref().unwrap()["AdvancedTree"];

        // New gate types parse.
        let gates = ft.gates.as_ref().unwrap();
        assert_eq!(gates["InhibitedFailure"].gate_type, GateType::Inhibit);
        assert_eq!(
            gates["InhibitedFailure"].inhibit_condition.as_deref(),
            Some("Control loop armed")
        );
        assert_eq!(gates["PriorityFailure"].gate_type, GateType::PriorityAnd);

        // eventType parses.
        assert_eq!(
            ft.basic_events["ExternalCause"].event_type,
            Some(BasicEventType::House)
        );
        assert_eq!(
            ft.basic_events["UnderAnalyzed"].event_type,
            Some(BasicEventType::Undeveloped)
        );
        assert_eq!(ft.basic_events["EventA"].event_type, None);

        // transfers parse.
        let transfers = ft.transfers.as_ref().unwrap();
        assert_eq!(
            transfers["SubAnalysis"].target,
            "#/faultTrees/OtherTree/topEvent"
        );
        assert_eq!(
            transfers["SubAnalysis"].label.as_deref(),
            Some("See sub-analysis")
        );

        // Probability resolution: INHIBIT = 0.1*0.5 = 0.05,
        // PRIORITY_AND = (0.2*0.3)/2! = 0.03, OR over them and the leaves.
        let mut diags = Vec::new();
        let probs = etdl_compiler::fault_tree::resolve_fault_trees(&doc, &mut diags);
        let advanced = probs["AdvancedTree"];

        let inhibit = 0.1 * 0.5;
        let priority = (0.2 * 0.3) / 2.0;
        let expected = 1.0 - (1.0 - inhibit) * (1.0 - priority) * (1.0 - 0.01) * (1.0 - 0.02);
        assert!(
            (advanced - expected).abs() < 0.00001,
            "Expected ~{:.6}, got {:.6}",
            expected,
            advanced
        );

        let errors: Vec<_> = diags.iter().filter(|d| d.is_error()).collect();
        assert!(
            errors.is_empty(),
            "advanced fixture should have no errors: {:?}",
            errors
        );
    }

    #[test]
    fn test_inhibit_gate_requires_condition() {
        use etdl_parser::ast::GateType;

        let yaml = r#"
etdl: "1.0.0"
info:
  title: "Inhibit Missing Condition"
  version: "1.0.0"
  domain: "Test"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent:
      id: Trig
      message: "api#/m"
      next: C
    nodes:
      C:
        type: consequence
        operation: terminate
faultTrees:
  F:
    topEvent:
      id: Top
      description: "top"
      rootCause: G
    gates:
      G:
        type: INHIBIT
        inputs: [A, B]
    basicEvents:
      A:
        description: "a"
        probability: 0.1
      B:
        description: "b"
        probability: 0.2
"#;
        let doc: EtlDocument = serde_yaml::from_str(yaml).unwrap();
        let mut diags = Vec::new();
        let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        etdl_compiler::validate::validate_document(&doc, &registry, &mut diags);

        assert!(
            diags.iter().any(|d| d.code == "V-505"),
            "INHIBIT without inhibitCondition should produce V-505, got {:?}",
            diags
        );

        // Sanity: gate type parsed as Inhibit.
        let ft = &doc.fault_trees.as_ref().unwrap()["F"];
        assert_eq!(ft.gates.as_ref().unwrap()["G"].gate_type, GateType::Inhibit);
    }

    #[test]
    fn test_validation_diagnostics_carry_span_keys() {
        use etdl_parser::spanned::{build_span_index, SpanKey};

        let yaml = r#"
etdl: "1.0.0"
info:
  title: "Bad"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent:
      id: I
      message: "a#/m"
      next: MissingNode
    nodes:
      Present:
        type: consequence
        operation: terminate
"#;
        let doc: EtlDocument = serde_yaml::from_str(yaml).unwrap();
        let mut diags = Vec::new();
        let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        etdl_compiler::validate::validate_document(&doc, &registry, &mut diags);

        let v101 = diags
            .iter()
            .find(|d| d.code == "V-101")
            .expect("V-101 present");
        let key = v101.key.as_ref().expect("diagnostic carries a span key");
        assert_eq!(
            key,
            &SpanKey::InitiatingEvent {
                tree: "T".to_string(),
                field: "next",
            }
        );

        // Resolving the key against the span index yields a real position.
        let index = build_span_index(yaml).unwrap();
        let el = index.resolve(key).expect("key resolves");
        let span = el.key_span.unwrap_or(el.span);
        // "      next: MissingNode" is 0-based line 12 (empty leading line).
        assert_eq!(span.line, 12);
        assert!(span.column > 0);
    }

    #[test]
    fn test_duplicate_node_ids_warn() {
        let yaml = r#"
etdl: "1.0.0"
info:
  title: "Dup"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent:
      id: I
      message: "a#/m"
      next: N
    nodes:
      N:
        type: consequence
        operation: terminate
      N:
        type: consequence
        operation: terminate
"#;
        let dups = etdl_parser::spanned::detect_duplicate_ids(yaml).unwrap();
        assert_eq!(dups.len(), 1);
        assert_eq!(dups[0].id, "N");
        assert_eq!(dups[0].kind, "node");
        // Duplicate key is on the second occurrence, 0-based line 17.
        assert_eq!(dups[0].span.line, 17);
    }

    #[test]
    fn test_language_version_major_gate() {
        let yaml = r#"
etdl: "2.0.0"
info:
  title: "Future"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent:
      id: I
      message: "a#/m"
      next: N
    nodes:
      N:
        type: consequence
        operation: terminate
"#;
        let doc: EtlDocument = serde_yaml::from_str(yaml).unwrap();
        let mut diags = Vec::new();
        let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        etdl_compiler::validate::validate_document(&doc, &registry, &mut diags);
        assert!(
            diags.iter().any(|d| d.code == "E-100"),
            "future major version must be rejected with E-100, got {:?}",
            diags
        );
    }

    #[test]
    fn test_handler_identifier_validation() {
        let yaml = r#"
etdl: "1.0.0"
info:
  title: "Handler"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent:
      id: I
      message: "a#/m"
      next: O
    nodes:
      O:
        type: operation
        action: execute
        handler: "not a valid id!"
        next: C
      C:
        type: consequence
        operation: terminate
"#;
        let doc: EtlDocument = serde_yaml::from_str(yaml).unwrap();
        let mut diags = Vec::new();
        let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        etdl_compiler::validate::validate_document(&doc, &registry, &mut diags);
        assert!(
            diags.iter().any(|d| d.code == "V-301"),
            "invalid handler should produce V-301, got {:?}",
            diags
        );
    }

    #[test]
    fn test_non_terminating_path_is_v104() {
        // An operation chain that ends in an operation with no consequence
        // anywhere reachable must be rejected with V-104.
        let yaml = r#"
etdl: "1.0.0"
info:
  title: "NoTerminal"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent:
      id: I
      message: "a#/m"
      next: O1
    nodes:
      O1:
        type: operation
        action: execute
        handler: "h1"
        next: O2
      O2:
        type: operation
        action: execute
        handler: "h2"
        next: O2
"#;
        let doc: EtlDocument = serde_yaml::from_str(yaml).unwrap();
        let mut diags = Vec::new();
        let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        etdl_compiler::validate::validate_document(&doc, &registry, &mut diags);
        assert!(
            diags.iter().any(|d| d.code == "V-104"),
            "non-terminating path should produce V-104, got {:?}",
            diags
        );
    }

    #[test]
    fn test_branch_probability_range_and_sum() {
        let yaml = r#"
etdl: "1.0.0"
info:
  title: "BranchSum"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent:
      id: I
      message: "a#/m"
      next: B
    nodes:
      B:
        type: barrier
        branches:
          - outcome: SUCCESS
            condition: "message.payload.ok == true"
            probability: 0.9
            next: C
          - outcome: FAILURE
            condition: default
            probability: 0.2
            next: C
      C:
        type: consequence
        operation: terminate
"#;
        let doc: EtlDocument = serde_yaml::from_str(yaml).unwrap();
        let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        // The E-103 (unresolvable alias) blocks later stages in the Compiler
        // pipeline; call validate_document + validate_probability_sums directly
        // to verify the sum rule in isolation.
        let mut diags = Vec::new();
        etdl_compiler::validate::validate_document(&doc, &registry, &mut diags);
        let resolved =
            etdl_compiler::validate::resolve_probability_links(&doc, &BTreeMap::new(), &mut diags);
        etdl_compiler::validate::validate_probability_sums(&doc, &resolved, &mut diags);
        assert!(
            diags.iter().any(|d| d.code == "V-203"),
            "branch probabilities 0.9+0.2 must fail V-203, got {:?}",
            diags
        );
    }

    #[test]
    fn test_consequence_revisit_not_cycle() {
        // Two branches pointing at the same consequence must NOT be a V-102 cycle.
        let yaml = r#"
etdl: "1.0.0"
info:
  title: "Revisit"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent:
      id: I
      message: "a#/m"
      next: B
    nodes:
      B:
        type: barrier
        branches:
          - outcome: SUCCESS
            condition: default
            probability: 0.5
            next: C
          - outcome: FAILURE
            condition: default
            probability: 0.5
            next: C
      C:
        type: consequence
        operation: terminate
"#;
        let doc: EtlDocument = serde_yaml::from_str(yaml).unwrap();
        let mut diags = Vec::new();
        let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        etdl_compiler::validate::validate_document(&doc, &registry, &mut diags);
        assert!(
            !diags.iter().any(|d| d.code == "V-102"),
            "revisiting a consequence must not be flagged as a cycle, got {:?}",
            diags
        );
    }

    #[test]
    fn test_transfer_target_must_exist() {
        let yaml = r##"
etdl: "1.0.0"
info:
  title: "Transfer"
  version: "1.0.0"
  domain: "D"
asyncapi_imports: {}
eventTrees:
  T:
    initiatingEvent:
      id: I
      message: "a#/m"
      next: C
    nodes:
      C:
        type: consequence
        operation: terminate
faultTrees:
  F:
    topEvent:
      id: Top
      description: "top"
      rootCause: E
    basicEvents:
      E:
        description: "e"
        probability: 0.01
    transfers:
      Gone:
        target: "#/faultTrees/DoesNotExist/topEvent"
        label: "see other"
"##;
        let doc: EtlDocument = serde_yaml::from_str(yaml).unwrap();
        let mut diags = Vec::new();
        let registry = etdl_parser::asyncapi::AsyncApiRegistry::new();
        etdl_compiler::validate::validate_document(&doc, &registry, &mut diags);
        assert!(
            diags.iter().any(|d| d.code == "V-506"),
            "transfer to a missing fault tree should produce V-506, got {:?}",
            diags
        );
    }

    // --- CLI subprocess behavior ---

    fn run_cli(args: &[&str]) -> (std::process::Output, String) {
        let bin = env!("CARGO_BIN_EXE_etdl");
        let out = std::process::Command::new(bin)
            .args(args)
            .output()
            .expect("cli runs");
        let stdout = String::from_utf8_lossy(&out.stdout).to_string();
        (out, stdout)
    }

    #[test]
    fn cli_validate_exit_zero_on_valid() {
        let (out, _) = run_cli(&[
            "validate",
            fixture_path("order-fulfillment.etdl").to_str().unwrap(),
        ]);
        assert_eq!(out.status.code(), Some(0));
    }

    #[test]
    fn cli_validate_exit_one_on_invalid() {
        let dir = std::env::temp_dir();
        let bad = dir.join("etdl_cli_bad_validate.etdl");
        std::fs::write(&bad, "etdl: \"2.0.0\"\n").unwrap();
        let (out, _) = run_cli(&["validate", bad.to_str().unwrap()]);
        assert_eq!(out.status.code(), Some(1));
        let _ = std::fs::remove_file(&bad);
    }

    #[test]
    fn cli_validate_json_output() {
        let (out, stdout) = run_cli(&[
            "validate",
            "--json",
            fixture_path("order-fulfillment.etdl").to_str().unwrap(),
        ]);
        assert_eq!(out.status.code(), Some(0));
        let v: serde_json::Value = serde_json::from_str(&stdout).expect("valid JSON");
        assert_eq!(v["results"][0]["valid"], serde_json::json!(true));
    }

    #[test]
    fn cli_analyze_json_output() {
        let (out, stdout) = run_cli(&[
            "analyze",
            "--json",
            fixture_path("order-fulfillment.etdl").to_str().unwrap(),
        ]);
        assert_eq!(out.status.code(), Some(0));
        let v: serde_json::Value = serde_json::from_str(&stdout).expect("valid JSON");
        assert_eq!(v["eventTrees"], serde_json::json!(1));
        assert_eq!(v["faultTrees"], serde_json::json!(1));
    }

    #[test]
    fn cli_version() {
        let (out, stdout) = run_cli(&["--version"]);
        assert_eq!(out.status.code(), Some(0));
        assert!(stdout.contains("etdl "));
    }
}