complior-cli 0.10.0

AI Act Compliance Scanner & Fixer — CLI
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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
#[cfg(test)]
mod tests {
    use crate::headless::format::{
        FormatOptions, format_human, format_json, format_sarif, sarif_level,
    };
    use crate::types::{
        CategoryScore, CheckResultType, ExternalToolResult, Finding, FindingExplanation,
        FrameworkScoreResult, ScanResult, ScoreBreakdown, Severity, Zone,
    };

    fn default_opts() -> FormatOptions {
        FormatOptions {
            framework_scores: None,
            quiet: false,
            prev_score: None,
        }
    }

    fn mock_scan_result() -> ScanResult {
        ScanResult {
            score: ScoreBreakdown {
                total_score: 72.0,
                zone: Zone::Yellow,
                category_scores: vec![CategoryScore {
                    category: "transparency".into(),
                    weight: 0.3,
                    score: 80.0,
                    obligation_count: 5,
                    passed_count: 4,
                }],
                critical_cap_applied: false,
                total_checks: 25,
                passed_checks: 18,
                failed_checks: 5,
                skipped_checks: 2,
                confidence_summary: None,
            },
            findings: vec![
                Finding {
                    check_id: "fria".into(),
                    r#type: CheckResultType::Fail,
                    message: "Missing FRIA document".into(),
                    severity: Severity::High,
                    obligation_id: Some("OBL-015".into()),
                    article_reference: Some("Art. 27".into()),
                    fix: Some("Create docs/FRIA.md".into()),
                    file: None,
                    line: None,
                    code_context: None,
                    fix_diff: None,
                    priority: None,
                    confidence: None,
                    confidence_level: None,
                    evidence: None,
                    explanation: None,
                    agent_id: None,
                    doc_quality: None,
                    l5_analyzed: None,
                },
                Finding {
                    check_id: "l4-bare-llm".into(),
                    r#type: CheckResultType::Fail,
                    message: "Bare LLM API call detected".into(),
                    severity: Severity::Medium,
                    obligation_id: None,
                    article_reference: Some("Art. 50(1)".into()),
                    fix: Some("Wrap with complior(client)".into()),
                    file: Some("src/chat/anthropic.ts".into()),
                    line: Some(8),
                    code_context: None,
                    fix_diff: None,
                    priority: None,
                    confidence: None,
                    confidence_level: None,
                    evidence: None,
                    explanation: None,
                    agent_id: None,
                    doc_quality: None,
                    l5_analyzed: None,
                },
            ],
            project_path: "/tmp/test-project".into(),
            scanned_at: "2026-02-19T12:00:00Z".into(),
            duration: 1234,
            files_scanned: 42,
            files_excluded: None,
            deep_analysis: None,
            l5_cost: None,
            regulation_version: None,
            tier: None,
            external_tool_results: None,
            agent_summaries: None,
            filter_context: None,
            top_actions: None,
        }
    }

    fn make_finding(
        check_id: &str,
        typ: CheckResultType,
        message: &str,
        severity: Severity,
    ) -> Finding {
        Finding {
            check_id: check_id.into(),
            r#type: typ,
            message: message.into(),
            severity,
            obligation_id: None,
            article_reference: None,
            fix: None,
            file: None,
            line: None,
            code_context: None,
            fix_diff: None,
            priority: None,
            confidence: None,
            confidence_level: None,
            evidence: None,
            explanation: None,
            agent_id: None,
            doc_quality: None,
            l5_analyzed: None,
        }
    }

    // ── JSON / SARIF (unchanged) ────────────────────────────────

    #[test]
    fn format_json_output() {
        let result = mock_scan_result();
        let json = format_json(&result);
        let parsed: serde_json::Value = serde_json::from_str(&json).expect("valid JSON");
        assert_eq!(parsed["score"]["totalScore"], 72.0);
        assert_eq!(parsed["findings"].as_array().unwrap().len(), 2);
    }

    #[test]
    fn format_sarif_output() {
        let result = mock_scan_result();
        let sarif = format_sarif(&result);
        let parsed: serde_json::Value = serde_json::from_str(&sarif).expect("valid SARIF JSON");
        assert_eq!(parsed["version"], "2.1.0");
        let runs = parsed["runs"].as_array().unwrap();
        assert_eq!(runs.len(), 1);
        let results = runs[0]["results"].as_array().unwrap();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0]["ruleId"], "fria");
        assert_eq!(results[0]["level"], "error"); // High = error
        assert_eq!(results[1]["level"], "warning"); // Medium = warning
    }

    #[test]
    fn sarif_level_mapping() {
        assert_eq!(sarif_level(&Severity::Critical), "error");
        assert_eq!(sarif_level(&Severity::High), "error");
        assert_eq!(sarif_level(&Severity::Medium), "warning");
        assert_eq!(sarif_level(&Severity::Low), "note");
        assert_eq!(sarif_level(&Severity::Info), "note");
    }

    // ── Data extraction helpers (unchanged) ─────────────────────

    #[test]
    fn project_name_via_header() {
        // project_name is tested indirectly through format_human header
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        assert!(text.contains("test-project"));
    }

    // ── New format_human tests ──────────────────────────────────

    #[test]
    fn format_human_header() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Header with version
        assert!(text.contains("Complior v"));
        assert!(text.contains("EU AI Act Compliance Scanner"));
        // Scan info
        assert!(text.contains("test-project"));
        assert!(text.contains("42 collected"));
        assert!(text.contains("L1"));
        assert!(text.contains("L4"));
    }

    #[test]
    fn format_human_score_block() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Compliance score
        assert!(text.contains("COMPLIANCE SCORE"));
        assert!(text.contains("72 / 100"));
        // Security score N/A hint when no framework_scores
        assert!(text.contains("SECURITY SCORE"));
        assert!(text.contains("N/A"));
    }

    #[test]
    fn format_human_dual_score() {
        let result = mock_scan_result();
        let opts = FormatOptions {
            framework_scores: Some(vec![
                FrameworkScoreResult {
                    framework_id: "eu-ai-act".into(),
                    framework_name: "EU AI Act 2024/1689".into(),
                    score: 72.0,
                    grade: "C".into(),
                    grade_type: "letter".into(),
                    gaps: 5,
                    total_checks: 25,
                    passed_checks: 18,
                    deadline: None,
                    categories: vec![],
                },
                FrameworkScoreResult {
                    framework_id: "owasp-llm-top10".into(),
                    framework_name: "OWASP LLM Top 10".into(),
                    score: 85.0,
                    grade: "B".into(),
                    grade_type: "letter".into(),
                    gaps: 2,
                    total_checks: 10,
                    passed_checks: 8,
                    deadline: None,
                    categories: vec![],
                },
            ]),
            quiet: false,
            prev_score: None,
        };
        let text = format_human(&result, &opts);
        assert!(text.contains("COMPLIANCE SCORE"));
        assert!(text.contains("SECURITY SCORE"));
        assert!(text.contains("85 / 100"));
        // Framework breakdown
        assert!(text.contains("Framework Breakdown"));
        assert!(text.contains("EU AI Act 2024/1689"));
        assert!(text.contains("OWASP LLM Top 10"));
    }

    #[test]
    fn format_human_findings_section() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Findings header
        assert!(text.contains("FINDINGS"));
        assert!(text.contains("2 total"));
        assert!(text.contains("1 high"));
        assert!(text.contains("1 medium"));
        // Finding details
        assert!(text.contains("HIGH"));
        assert!(text.contains("MEDIUM"));
        // Layer subgroup headers
        assert!(text.contains("L1"));
        assert!(text.contains("File Presence"));
        assert!(text.contains("L4"));
        assert!(text.contains("Code Patterns"));
        // Article references
        assert!(text.contains("Art. 27"));
        assert!(text.contains("Art. 50(1)"));
        // Labels
        assert!(text.contains("Fundamental Rights Assessment"));
        assert!(text.contains("Bare LLM API Call"));
        // Messages
        assert!(text.contains("Missing FRIA document"));
        assert!(text.contains("Bare LLM API call detected"));
        // Fix suggestions
        assert!(text.contains("Create docs/FRIA.md"));
        assert!(text.contains("Wrap with complior(client)"));
        // File location
        assert!(text.contains("src/chat/anthropic.ts:8"));
    }

    #[test]
    fn format_human_no_findings() {
        let mut result = mock_scan_result();
        result.findings.clear();
        result.score.failed_checks = 0;
        result.score.total_score = 85.0;
        let text = format_human(&result, &default_opts());
        assert!(text.contains("No compliance issues found"));
        assert!(text.contains("on track"));
    }

    #[test]
    fn format_human_layer_results() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        assert!(text.contains("Layer Results"));
        assert!(text.contains("File Presence"));
        assert!(text.contains("FAIL")); // L1 has high severity → FAIL
        assert!(text.contains("Code Patterns"));
        assert!(text.contains("WARN")); // L4 has only medium → WARN
    }

    #[test]
    fn format_human_quick_actions() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        assert!(text.contains("QUICK ACTIONS"));
        assert!(text.contains("Auto-fix available"));
        assert!(text.contains("complior fix"));
        assert!(text.contains("complior scan --deep"));
        assert!(text.contains("complior tui"));
        assert!(text.contains("Next"));
    }

    #[test]
    fn format_human_deep_scan_hint_absent_when_deep() {
        let mut result = mock_scan_result();
        result.tier = Some(2);
        let text = format_human(&result, &default_opts());
        // Should NOT suggest --deep when already Tier 2
        assert!(!text.contains("complior scan --deep"));
        // Deep mode label in header
        assert!(text.contains("Deep Mode"));
    }

    #[test]
    fn format_human_critical_cap_warning() {
        let mut result = mock_scan_result();
        result.score.critical_cap_applied = true;
        result.score.total_score = 40.0; // Cap only shows when score <= 50
        let text = format_human(&result, &default_opts());
        assert!(text.contains("Score capped"));
        assert!(text.contains("critical violations"));
    }

    #[test]
    fn format_human_critical_cap_hidden_when_score_high() {
        let mut result = mock_scan_result();
        result.score.critical_cap_applied = true;
        result.score.total_score = 78.0; // Flag set but score high — cap not limiting
        let text = format_human(&result, &default_opts());
        assert!(
            !text.contains("Score capped"),
            "cap message should be hidden when score > 50"
        );
    }

    #[test]
    fn format_human_severity_counts() {
        let mut result = mock_scan_result();
        result.findings = vec![
            make_finding(
                "fria",
                CheckResultType::Fail,
                "Missing FRIA",
                Severity::Critical,
            ),
            make_finding(
                "risk-management",
                CheckResultType::Fail,
                "Missing risk mgmt",
                Severity::High,
            ),
            make_finding(
                "risk-management",
                CheckResultType::Fail,
                "Missing risk mgmt 2",
                Severity::High,
            ),
            make_finding(
                "l4-bare-llm",
                CheckResultType::Fail,
                "Bare LLM",
                Severity::Medium,
            ),
        ];
        let text = format_human(&result, &default_opts());
        assert!(text.contains("4 total"));
        assert!(text.contains("1 critical"));
        assert!(text.contains("2 high"));
        assert!(text.contains("1 medium"));
    }

    #[test]
    fn format_human_finding_limits_medium() {
        let mut result = mock_scan_result();
        result.findings.clear();
        // 8 medium findings — should only show 5
        for i in 0..8 {
            let mut f = make_finding(
                "l4-bare-llm",
                CheckResultType::Fail,
                &format!("Bare LLM instance {i}"),
                Severity::Medium,
            );
            f.fix = Some(format!("Fix {i}"));
            f.file = Some(format!("src/file{i}.ts"));
            f.line = Some(i + 1);
            result.findings.push(f);
        }
        let text = format_human(&result, &default_opts());
        // Header shows all 8
        assert!(text.contains("8 total"));
        assert!(text.contains("8 medium"));
        // But only 5 are displayed, rest hidden
        assert!(text.contains("3 medium not shown"));
    }

    #[test]
    fn format_human_low_findings_hidden() {
        let mut result = mock_scan_result();
        result.findings = vec![
            make_finding(
                "fria",
                CheckResultType::Fail,
                "Missing FRIA",
                Severity::High,
            ),
            make_finding(
                "l4-bare-llm",
                CheckResultType::Fail,
                "Bare LLM",
                Severity::Low,
            ),
        ];
        let text = format_human(&result, &default_opts());
        // Low findings are not displayed
        assert!(text.contains("1 low not shown"));
        // High finding is shown
        assert!(text.contains("HIGH"));
        // LOW severity label should NOT appear in findings section
        // (it appears in the "not shown" note only)
    }

    #[test]
    fn format_human_all_critical_high_shown() {
        let mut result = mock_scan_result();
        result.findings.clear();
        // 10 critical findings — all should be shown
        for i in 0..10 {
            result.findings.push(make_finding(
                &format!("check-{i}"),
                CheckResultType::Fail,
                &format!("Critical issue {i}"),
                Severity::Critical,
            ));
        }
        let text = format_human(&result, &default_opts());
        assert!(text.contains("10 total"));
        assert!(text.contains("10 critical"));
        // All 10 findings should be shown (all critical messages present)
        assert!(text.contains("Critical issue 9"));
        // No "not shown" note
        assert!(!text.contains("not shown"));
    }

    #[test]
    fn format_human_layer_subgrouping() {
        let mut result = mock_scan_result();
        result.findings = vec![
            make_finding(
                "fria",
                CheckResultType::Fail,
                "L1 issue A",
                Severity::Critical,
            ),
            make_finding(
                "documentation",
                CheckResultType::Fail,
                "L1 issue B",
                Severity::High,
            ),
            make_finding(
                "l4-bare-llm",
                CheckResultType::Fail,
                "L4 issue",
                Severity::Medium,
            ),
            make_finding("l2-fria", CheckResultType::Fail, "L2 issue", Severity::High),
        ];
        let text = format_human(&result, &default_opts());
        // Layer subgroup headers should appear
        assert!(text.contains("L1"));
        assert!(text.contains("File Presence"));
        assert!(text.contains("L2"));
        assert!(text.contains("Document Structure"));
        assert!(text.contains("L4"));
        assert!(text.contains("Code Patterns"));
        // L1 group should come before L2, L2 before L4
        let l1_pos = text.find("File Presence").unwrap();
        let l2_pos = text.find("Document Structure").unwrap();
        let l4_pos = text.find("Code Patterns").unwrap();
        assert!(l1_pos < l2_pos);
        assert!(l2_pos < l4_pos);
    }

    #[test]
    fn format_human_clean_fix_message() {
        let mut result = mock_scan_result();
        result.findings = vec![Finding {
            check_id: "l4-bare-llm".into(),
            r#type: CheckResultType::Fail,
            message: "Bare LLM call".into(),
            severity: Severity::High,
            obligation_id: None,
            article_reference: None,
            fix: Some("Fix complior.injection: Validate user input before LLM call".into()),
            file: None,
            line: None,
            code_context: None,
            fix_diff: None,
            priority: None,
            confidence: None,
            confidence_level: None,
            evidence: None,
            explanation: None,
            agent_id: None,
            doc_quality: None,
            l5_analyzed: None,
        }];
        let text = format_human(&result, &default_opts());
        // Engine prefix should be stripped
        assert!(!text.contains("Fix complior.injection:"));
        // Clean message should remain
        assert!(text.contains("Validate user input before LLM call"));
    }

    #[test]
    fn format_human_layer_group_headers() {
        let mut result = mock_scan_result();
        result.findings = vec![
            make_finding("fria", CheckResultType::Fail, "L1 finding", Severity::High),
            make_finding(
                "l2-fria",
                CheckResultType::Fail,
                "L2 finding",
                Severity::High,
            ),
            make_finding(
                "l3-missing-bias-testing",
                CheckResultType::Fail,
                "L3 finding",
                Severity::High,
            ),
            make_finding(
                "l4-bare-llm",
                CheckResultType::Fail,
                "L4 finding",
                Severity::High,
            ),
            make_finding(
                "l4-nhi-api-key",
                CheckResultType::Fail,
                "NHI finding",
                Severity::High,
            ),
            make_finding(
                "cross-doc-code-mismatch",
                CheckResultType::Fail,
                "Cross finding",
                Severity::High,
            ),
        ];
        let text = format_human(&result, &default_opts());
        // Layer group headers with labels
        assert!(text.contains("L1") && text.contains("File Presence"));
        assert!(text.contains("L2") && text.contains("Document Structure"));
        assert!(text.contains("L3") && text.contains("Dependencies"));
        assert!(text.contains("L4") && text.contains("Code Patterns"));
        assert!(text.contains("NHI") && text.contains("Secrets"));
        assert!(text.contains("CROSS") && text.contains("Cross-Layer"));
    }

    #[test]
    fn format_human_deep_mode_grouping() {
        let mut result = mock_scan_result();
        result.tier = Some(2);
        result.external_tool_results = Some(vec![ExternalToolResult {
            tool: "semgrep".into(),
            version: "1.0.0".into(),
            findings: vec![],
            duration: 1000,
            exit_code: 0,
            error: None,
        }]);
        result.findings = vec![
            make_finding(
                "fria",
                CheckResultType::Fail,
                "Base scan finding",
                Severity::High,
            ),
            make_finding(
                "ext-semgrep-unsafe-deser",
                CheckResultType::Fail,
                "Deep finding",
                Severity::High,
            ),
        ];
        let text = format_human(&result, &default_opts());
        // Deep mode grouping (uppercase)
        assert!(text.contains("NEW IN --DEEP"));
        assert!(text.contains("FROM BASE SCAN"));
        // Deep findings grouped under L4+ layer header
        assert!(text.contains("Ext. Code Analysis"));
    }

    #[test]
    fn format_human_deep_mode_layers() {
        let mut result = mock_scan_result();
        result.tier = Some(2);
        result.findings = vec![
            make_finding("fria", CheckResultType::Fail, "Base", Severity::High),
            make_finding(
                "ext-semgrep-unsafe-deser",
                CheckResultType::Fail,
                "Semgrep finding",
                Severity::High,
            ),
            make_finding(
                "ext-bandit-B301",
                CheckResultType::Fail,
                "Bandit finding",
                Severity::High,
            ),
        ];
        let text = format_human(&result, &default_opts());
        // Header shows tool names for deep layers
        assert!(text.contains("L4+ Semgrep"));
        assert!(text.contains("L4+ Bandit"));
        assert!(text.contains("L3+ ModelScan"));
        assert!(text.contains("NHI+ detect-secrets"));
        // Layer Results section uses descriptive labels
        assert!(text.contains("Ext. Code Analysis"));
        // Scan info shows deep mode
        assert!(text.contains("Deep Mode"));
    }

    #[test]
    fn format_human_pass_findings_layer_status() {
        let mut result = mock_scan_result();
        result.findings = vec![make_finding(
            "l4-disclosure",
            CheckResultType::Pass,
            "Disclosure found",
            Severity::Info,
        )];
        result.score.failed_checks = 0;
        result.score.total_score = 100.0;
        let text = format_human(&result, &default_opts());
        // Layer Results should show PASS for L4
        assert!(text.contains("Layer Results"));
        assert!(text.contains("PASS"));
        assert!(text.contains("Code Patterns"));
    }

    #[test]
    fn format_human_next_hint_critical() {
        let mut result = mock_scan_result();
        result.findings = vec![
            make_finding(
                "fria",
                CheckResultType::Fail,
                "Critical",
                Severity::Critical,
            ),
            make_finding(
                "risk-management",
                CheckResultType::Fail,
                "Also critical",
                Severity::Critical,
            ),
        ];
        let text = format_human(&result, &default_opts());
        assert!(text.contains("fix 2 critical issues"));
    }

    #[test]
    fn format_human_next_hint_high() {
        let mut result = mock_scan_result();
        result.findings = vec![make_finding(
            "fria",
            CheckResultType::Fail,
            "High",
            Severity::High,
        )];
        let text = format_human(&result, &default_opts());
        assert!(text.contains("fix 1 high-severity issue"));
    }

    #[test]
    fn format_human_next_hint_compliant() {
        let mut result = mock_scan_result();
        result.findings.clear();
        result.score.total_score = 90.0;
        result.score.failed_checks = 0;
        let text = format_human(&result, &default_opts());
        assert!(text.contains("on track for EU AI Act compliance"));
    }

    #[test]
    fn format_human_framework_breakdown_bar_chart() {
        let result = mock_scan_result();
        let opts = FormatOptions {
            framework_scores: Some(vec![FrameworkScoreResult {
                framework_id: "eu-ai-act".into(),
                framework_name: "EU AI Act 2024/1689".into(),
                score: 60.0,
                grade: "D".into(),
                grade_type: "letter".into(),
                gaps: 10,
                total_checks: 25,
                passed_checks: 15,
                deadline: None,
                categories: vec![],
            }]),
            quiet: false,
            prev_score: None,
        };
        let text = format_human(&result, &opts);
        assert!(text.contains("Framework Breakdown"));
        assert!(text.contains("EU AI Act 2024/1689"));
        assert!(text.contains("60 / 100"));
        // Bar chart characters (uses ASCII fallback in test/CI environment)
        use crate::headless::format::colors::{bar_empty, bar_filled};
        assert!(text.contains(bar_filled()));
        assert!(text.contains(bar_empty()));
    }

    #[test]
    fn format_human_no_framework_breakdown_without_scores() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // No framework breakdown when no framework_scores provided
        assert!(!text.contains("Framework Breakdown"));
    }

    #[test]
    fn format_human_generate_docs_action() {
        let mut result = mock_scan_result();
        result.findings = vec![
            // L1 finding (missing doc) triggers "Generate docs" action
            make_finding(
                "fria",
                CheckResultType::Fail,
                "Missing FRIA",
                Severity::High,
            ),
        ];
        let text = format_human(&result, &default_opts());
        assert!(text.contains("Generate docs"));
        assert!(text.contains("complior docs generate --missing"));
    }

    #[test]
    fn format_human_separator_width() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Dynamic separator uses h_line() repeated to display_width()
        use crate::headless::format::colors::h_line;
        use crate::headless::format::layers::display_width;
        let sep = h_line().repeat(display_width());
        assert!(text.contains(&sep));
    }

    #[test]
    fn format_human_finding_with_article() {
        let mut result = mock_scan_result();
        result.findings = vec![Finding {
            check_id: "fria".into(),
            r#type: CheckResultType::Fail,
            message: "Missing FRIA document".into(),
            severity: Severity::High,
            obligation_id: None,
            article_reference: Some("Art. 27".into()),
            fix: Some("Create docs/FRIA.md".into()),
            file: None,
            line: None,
            code_context: None,
            fix_diff: None,
            priority: None,
            confidence: None,
            confidence_level: None,
            evidence: None,
            explanation: None,
            agent_id: None,
            doc_quality: None,
            l5_analyzed: None,
        }];
        let text = format_human(&result, &default_opts());
        // Article and label combined on same line
        assert!(text.contains("Art. 27"));
        assert!(text.contains("Fundamental Rights Assessment"));
    }

    #[test]
    fn format_human_finding_article_from_explanation() {
        let mut result = mock_scan_result();
        result.findings = vec![Finding {
            check_id: "fria".into(),
            r#type: CheckResultType::Fail,
            message: "Missing FRIA".into(),
            severity: Severity::High,
            obligation_id: None,
            article_reference: None,
            fix: None,
            file: None,
            line: None,
            code_context: None,
            fix_diff: None,
            priority: None,
            confidence: None,
            confidence_level: None,
            evidence: None,
            explanation: Some(FindingExplanation {
                article: "Art. 27".into(),
                penalty: String::new(),
                deadline: String::new(),
                business_impact: String::new(),
            }),
            agent_id: None,
            doc_quality: None,
            l5_analyzed: None,
        }];
        let text = format_human(&result, &default_opts());
        // Article from explanation fallback
        assert!(text.contains("Art. 27"));
    }

    #[test]
    fn format_human_ext_finding_label() {
        let mut result = mock_scan_result();
        result.tier = Some(2);
        result.findings = vec![make_finding(
            "ext-bandit-B301",
            CheckResultType::Fail,
            "Pickle issue",
            Severity::High,
        )];
        let text = format_human(&result, &default_opts());
        // ext_check_label should provide nice label
        assert!(text.contains("Unsafe Pickle Usage"));
    }

    // ── New tests for CLI output spec compliance ──────────────────

    #[test]
    fn format_human_finding_ids_hidden() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Finding IDs are hidden from human output (kept in JSON/SARIF)
        assert!(!text.contains("F-001"));
        assert!(!text.contains("F-002"));
    }

    #[test]
    fn format_human_layer_tag_in_finding() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Layer tags should appear in finding lines
        assert!(text.contains("[L1]") || text.contains("[L4]"));
    }

    #[test]
    fn format_human_grade_in_score() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Grade letter should appear near score (72 = C)
        assert!(text.contains("C"));
    }

    #[test]
    fn format_human_elapsed_time() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Elapsed time should be displayed (1234ms = 1.2s)
        assert!(text.contains("Elapsed"));
        assert!(text.contains("1.2s"));
    }

    #[test]
    fn format_human_quiet_mode() {
        let mut result = mock_scan_result();
        result.findings = vec![
            make_finding(
                "l1-crit",
                CheckResultType::Fail,
                "Critical issue",
                Severity::Critical,
            ),
            make_finding(
                "l4-med",
                CheckResultType::Fail,
                "Medium issue",
                Severity::Medium,
            ),
        ];
        let opts = FormatOptions {
            framework_scores: None,
            quiet: true,
            prev_score: None,
        };
        let text = format_human(&result, &opts);
        // Quiet mode: shows critical findings
        assert!(text.contains("CRITICAL FINDINGS"));
        assert!(text.contains("Critical issue"));
        // Quiet mode: does NOT show quick actions or framework breakdown
        assert!(!text.contains("QUICK ACTIONS"));
        assert!(!text.contains("Framework Breakdown"));
    }

    #[test]
    fn format_human_security_na_hint() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Security N/A when no OWASP/MITRE framework data
        assert!(text.contains("SECURITY SCORE"));
        assert!(text.contains("N/A"));
        assert!(text.contains("complior eval --security"));
    }

    #[test]
    fn format_human_docs_command() {
        let result = mock_scan_result();
        let text = format_human(&result, &default_opts());
        // Docs command hint for findings with article reference
        assert!(
            text.contains("complior docs --article 27")
                || text.contains("complior docs --article 50")
        );
    }

    #[test]
    fn format_human_prev_score_delta() {
        let result = mock_scan_result();
        let opts = FormatOptions {
            framework_scores: None,
            quiet: false,
            prev_score: Some(85.0),
        };
        let text = format_human(&result, &opts);
        // Previous score delta displayed
        assert!(text.contains("was 85"));
    }

    #[test]
    fn format_human_severity_icons_distinct() {
        // Critical and High should have different icons
        use crate::headless::format::colors::severity_icon;
        let crit = severity_icon(&Severity::Critical);
        let high = severity_icon(&Severity::High);
        assert_ne!(crit, high, "Critical and High should have different icons");
    }

    #[test]
    fn format_json_has_grade() {
        let result = mock_scan_result();
        let json_text = format_json(&result);
        let v: serde_json::Value = serde_json::from_str(&json_text).unwrap();
        let grade = v.get("grade").expect("grade object should exist");
        assert_eq!(grade.get("compliance").unwrap().as_str().unwrap(), "C");
    }

    #[test]
    fn format_json_has_finding_ids() {
        let result = mock_scan_result();
        let json_text = format_json(&result);
        let v: serde_json::Value = serde_json::from_str(&json_text).unwrap();
        let findings = v.get("findings").unwrap().as_array().unwrap();
        // All fail findings should have IDs
        let ids: Vec<&str> = findings
            .iter()
            .filter_map(|f| f.get("id").and_then(|v| v.as_str()))
            .collect();
        assert!(!ids.is_empty());
        assert!(ids.contains(&"F-001"));
    }

    #[test]
    fn format_json_has_obligation_ids() {
        let result = mock_scan_result();
        let json_text = format_json(&result);
        let v: serde_json::Value = serde_json::from_str(&json_text).unwrap();
        let findings = v.get("findings").unwrap().as_array().unwrap();
        // Finding with obligationId should also have obligationIds array
        let has_ids = findings
            .iter()
            .any(|f| f.get("obligationIds").and_then(|v| v.as_array()).is_some());
        assert!(
            has_ids,
            "At least one finding should have obligationIds array"
        );
    }

    #[test]
    fn resolve_grade_boundaries() {
        use crate::headless::format::colors::resolve_grade;
        assert_eq!(resolve_grade(95.0), "A");
        assert_eq!(resolve_grade(90.0), "A");
        assert_eq!(resolve_grade(89.9), "B");
        assert_eq!(resolve_grade(75.0), "B");
        assert_eq!(resolve_grade(74.9), "C");
        assert_eq!(resolve_grade(60.0), "C");
        assert_eq!(resolve_grade(59.9), "D");
        assert_eq!(resolve_grade(40.0), "D");
        assert_eq!(resolve_grade(39.9), "F");
        assert_eq!(resolve_grade(0.0), "F");
    }

    #[test]
    fn cli_parse_quiet_flag() {
        use crate::cli::Cli;
        use clap::Parser;
        let cli = Cli::parse_from(["complior", "scan", "--quiet"]);
        match cli.command {
            Some(crate::cli::Command::Scan { quiet, .. }) => assert!(quiet),
            _ => panic!("Expected Scan command"),
        }
    }

    #[test]
    fn cli_parse_quiet_short() {
        use crate::cli::Cli;
        use clap::Parser;
        let cli = Cli::parse_from(["complior", "scan", "-q"]);
        match cli.command {
            Some(crate::cli::Command::Scan { quiet, .. }) => assert!(quiet),
            _ => panic!("Expected Scan command"),
        }
    }

    #[test]
    fn cli_parse_no_color_flag() {
        use crate::cli::Cli;
        use clap::Parser;
        let cli = Cli::parse_from(["complior", "--no-color", "scan"]);
        assert!(cli.no_color);
    }

    #[test]
    fn format_human_large_project_warning() {
        let mut result = mock_scan_result();
        result.files_scanned = 600;
        let text = format_human(&result, &default_opts());
        assert!(text.contains("Large project"));
        assert!(text.contains(".compliorignore"));
    }

    #[test]
    fn format_human_files_excluded() {
        let mut result = mock_scan_result();
        result.files_excluded = Some(15);
        let text = format_human(&result, &default_opts());
        assert!(text.contains("15 excluded"));
    }

    /// V1-M17: Quiet mode must produce ≤5 non-empty lines (score block only).
    /// No header, no scan info, no layer list — just score for CI consumption.
    #[test]
    fn format_human_quiet_compact() {
        let result = mock_scan_result();
        let opts = FormatOptions {
            framework_scores: None,
            quiet: true,
            prev_score: None,
        };
        let text = format_human(&result, &opts);
        let lines: Vec<&str> = text.lines().filter(|l| !l.trim().is_empty()).collect();
        // Quiet mode: ≤5 non-empty lines (score block only, no header/info)
        assert!(
            lines.len() <= 5,
            "Quiet mode should produce ≤5 non-empty lines, got {}: {lines:?}",
            lines.len(),
        );
        // Must NOT contain header or scan info
        assert!(
            !text.contains("Complior v"),
            "Quiet mode should not show header"
        );
        assert!(
            !text.contains("Scanning"),
            "Quiet mode should not show scan info"
        );
        assert!(
            !text.contains("Elapsed"),
            "Quiet mode should not show elapsed time"
        );
        assert!(
            !text.contains("Layers"),
            "Quiet mode should not show layer list"
        );
        // Must still contain the score
        assert!(
            text.contains("COMPLIANCE SCORE"),
            "Quiet mode must show score"
        );
    }

    #[test]
    fn sort_findings_full_order() {
        use crate::headless::format::layers::sort_findings_full;
        let f1 = make_finding("l4-bare", CheckResultType::Fail, "m1", Severity::Medium);
        let f2 = make_finding("l1-risk", CheckResultType::Fail, "m2", Severity::Critical);
        let f3 = make_finding("l2-fria", CheckResultType::Fail, "m3", Severity::High);
        let mut refs: Vec<&Finding> = vec![&f1, &f2, &f3];
        sort_findings_full(&mut refs);
        // Critical first, then High, then Medium
        assert_eq!(refs[0].severity, Severity::Critical);
        assert_eq!(refs[1].severity, Severity::High);
        assert_eq!(refs[2].severity, Severity::Medium);
    }
}