auditmysite 1.7.2

Resource-efficient WCAG 2.2 AA Accessibility Checker in Rust
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
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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
use super::*;
use crate::audit::normalized::normalize;
use crate::cli::WcagLevel;
use crate::wcag::WcagResults;

fn first_page(report: &UnifiedReport) -> &PageEntry {
    &report.pages[0]
}

#[test]
fn test_single_envelope_shape() {
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        500,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);

    assert_eq!(unified.schema_version, "2.0");
    assert_eq!(unified.report_type, "single");
    assert_eq!(unified.pages.len(), 1);
    assert_eq!(unified.summary.url_count, 1);

    let output = unified.to_json(true).unwrap();
    assert!(output.contains("\"schema_version\": \"2.0\""));
    assert!(output.contains("\"report_type\": \"single\""));
    assert!(output.contains("\"metric_context\""));
    assert!(output.contains("\"higher_is_better\": true"));
    assert!(output.contains("example.com"));
    assert!(output.contains("\"accessibility_score\": 100"));
}

#[test]
fn test_management_risks_rationale_has_no_debug_format_leak() {
    // Regression (all 5 same-day live single-reports, 2026-08-31): perf/mobile
    // are Option<u32> and were formatted via `{:?}` in build_management_risks,
    // producing "performance Some(85), mobile Some(85)" literally in the JSON
    // rationale text instead of clean numbers/a "not measured" fallback.
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        500,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);

    let conversion_risk = unified
        .summary
        .management_risks
        .iter()
        .find(|r| r.dimension == "Conversion / usability")
        .expect("Conversion / usability risk must be present");
    assert!(
        !conversion_risk.rationale.contains("Some(") && !conversion_risk.rationale.contains("None"),
        "rationale leaks Rust Option debug format: {}",
        conversion_risk.rationale
    );
    assert!(
        conversion_risk.rationale.contains("not measured"),
        "expected a clean fallback for unmeasured performance/mobile scores: {}",
        conversion_risk.rationale
    );
}

#[test]
fn test_management_risks_reflect_blocking_issues_not_just_legal_flags() {
    // Regression (satower-mosterei.de, 2026-08-31): overall risk_level
    // "medium", certificate "EINGESCHRÄNKT", verdict "fail", 4 blocking
    // interaction issues (buttons without an accessible name, WCAG 4.1.2,
    // "medium" severity so legal_flags/critical/high all stayed 0) -- yet
    // every management_risks dimension previously read "low" regardless,
    // directly contradicting the report's own risk gate. "Legal / BFSG-EAA"
    // and "Conversion / usability" must escalate once blocking_issues > 0
    // even when legal_flags/critical/high are all zero.
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        500,
    );
    let normalized = normalize(&report);
    let mut with_blockers = normalized.normalized.clone();
    with_blockers.risk.blocking_issues = 4;
    assert_eq!(with_blockers.severity_counts.critical, 0);
    assert_eq!(with_blockers.severity_counts.high, 0);
    assert_eq!(with_blockers.risk.legal_flags, 0);

    let risks = helpers::build_management_risks(std::slice::from_ref(&with_blockers));

    let legal = risks
        .iter()
        .find(|r| r.dimension == "Legal / BFSG-EAA")
        .expect("Legal / BFSG-EAA risk must be present");
    assert_ne!(legal.level, "low", "rationale: {}", legal.rationale);
    assert!(legal.rationale.contains("4 blocking"));

    let usability = risks
        .iter()
        .find(|r| r.dimension == "Conversion / usability")
        .expect("Conversion / usability risk must be present");
    assert_ne!(usability.level, "low", "rationale: {}", usability.rationale);
}

#[test]
fn test_single_summary_fields_present() {
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        500,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);

    assert_eq!(
        unified.summary.accessibility_score,
        normalized.normalized.score
    );
    assert_eq!(
        unified.summary.overall_score,
        normalized.normalized.overall_score
    );
    assert_eq!(unified.summary.violation_count, 0);
    assert_eq!(unified.summary.finding_count, 0);
    assert_eq!(unified.summary.finding_occurrence_count, 0);
    assert_eq!(unified.summary.occurrence_counts_scope, "wcag_only");
    assert_eq!(unified.summary.passed_url_count, 1);
    assert_eq!(unified.summary.failed_url_count, 0);
}

/// #651: the batch summary says how many URLs the scores cover out of how
/// many were attempted; single reports carry no such field.
#[test]
fn test_batch_summary_states_attempted_url_count() {
    let batch = BatchReport::from_reports(
        vec![AuditReport::new(
            "https://example.com/a".to_string(),
            WcagLevel::AA,
            WcagResults::new(),
            100,
        )],
        vec![crate::audit::BatchError {
            url: "https://example.com/b".to_string(),
            error: "Browser pool timeout: no page available after 615 seconds".to_string(),
            blocked_reason: None,
        }],
        200,
    );
    let json: serde_json::Value =
        serde_json::from_str(&UnifiedReport::batch(&batch).to_json(false).unwrap()).unwrap();
    assert_eq!(json["summary"]["url_count"], 1);
    assert_eq!(json["summary"]["attempted_url_count"], 2);
    assert_eq!(json["errors"][0]["url"], "https://example.com/b");

    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        500,
    );
    let normalized = normalize(&report);
    let single: serde_json::Value = serde_json::from_str(
        &UnifiedReport::single(&normalized, &report)
            .to_json(false)
            .unwrap(),
    )
    .unwrap();
    assert!(single["summary"].get("attempted_url_count").is_none());
}

#[test]
fn test_assessments_failures_en_status_and_artifact_manifest_are_serialized() {
    use crate::taxonomy::Severity;
    use crate::wcag::Violation;

    let mut results = WcagResults::new();
    results.add_warning(Violation::new(
        "1.3.1",
        "Structure warning",
        WcagLevel::A,
        Severity::Medium,
        "Verify the visual grouping.",
        "n1",
    ));
    results.add_not_testable(Violation::new(
        "1.1.1",
        "Manual alternative review",
        WcagLevel::A,
        Severity::Medium,
        "Verify the text alternative in context.",
        "n2",
    ));
    results.add_positive(Violation::new(
        "2.4.1",
        "Skip link present",
        WcagLevel::A,
        Severity::Low,
        "A skip link was detected.",
        "n3",
    ));
    results.rule_outcomes.push(
        crate::wcag::RuleRun::not_run("image_alt", crate::wcag::NotRun::Errored)
            .with_wcag(["1.1.1"])
            .in_viewport("mobile")
            .with_reason("dom_evaluation_failed"),
    );

    let mut report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        results,
        500,
    );
    report.accessibility.execution.quality = crate::audit::AuditQuality {
        status: crate::audit::AuditQualityStatus::Insufficient,
        qualified_results: true,
        failed_rule_checks: 1,
        partial_or_failed_modules: 0,
        reasons: vec!["rule_checks_failed".to_string()],
    };
    report.screen_reader_audit = Some(crate::screen_reader::build_sr_audit_report(
        &report.url,
        report.timestamp,
        &crate::AXTree::new(),
        "en",
        None,
    ));

    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let page = first_page(&unified);

    assert_eq!(page.accessibility_assessments.len(), 3);
    assert!(page
        .accessibility_assessments
        .iter()
        .any(|assessment| assessment.kind == "manual_review"));
    assert_eq!(unified.artifacts.len(), 1);
    assert_eq!(unified.artifacts[0].kind, "screen_reader_audit");

    let clause = page
        .detail
        .as_ref()
        .unwrap()
        .en301549_annex
        .clauses
        .iter()
        .find(|clause| clause.wcag == "1.1.1")
        .unwrap();
    assert_eq!(clause.status, En301549ClauseStatusKind::NotEvaluated);

    let output = unified.to_json(false).unwrap();
    assert!(!output.contains("/Users/"));
    assert!(!output.contains("stacktrace"));
}

#[test]
fn test_single_taxonomy_fields() {
    use crate::taxonomy::Severity;
    use crate::wcag::Violation;

    let mut results = WcagResults::new();
    results.add_violation(Violation::new(
        "1.1.1",
        "Non-text Content",
        WcagLevel::A,
        Severity::High,
        "Missing alt",
        "n1",
    ));

    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        results,
        500,
    );
    let normalized = normalize(&report);
    let output = format_json_normalized(&normalized, &report, true).unwrap();

    assert!(output.contains("\"dimension\""));
    assert!(output.contains("\"subcategory\""));
    assert!(output.contains("\"issue_class\""));
    assert!(output.contains("\"aggregation_key\""));
    assert!(output.contains("\"user_impact\""));
    assert!(output.contains("\"principle_coverage\""));
}

#[test]
fn test_single_score_matches_normalized() {
    use crate::taxonomy::Severity;
    use crate::wcag::Violation;

    let mut results = WcagResults::new();
    results.add_violation(Violation::new(
        "1.1.1",
        "Alt",
        WcagLevel::A,
        Severity::High,
        "Missing",
        "n1",
    ));

    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        results,
        500,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let page = first_page(&unified);

    assert_eq!(page.accessibility_score, normalized.normalized.score);
    assert_eq!(page.grade, normalized.normalized.grade);
    assert_eq!(page.certificate, normalized.normalized.certificate);
}

#[test]
fn test_single_violations_match_severity_counts() {
    use crate::taxonomy::Severity;
    use crate::wcag::Violation;

    let mut results = WcagResults::new();
    for node in ["n1", "n2", "n3"] {
        results.add_violation(Violation::new(
            "1.1.1",
            "Alt",
            WcagLevel::A,
            Severity::High,
            "Missing alt",
            node,
        ));
    }
    results.add_violation(Violation::new(
        "1.4.3",
        "Contrast",
        WcagLevel::AA,
        Severity::High,
        "Low contrast",
        "n4",
    ));

    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        results,
        500,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let page = first_page(&unified);

    assert_eq!(
        page.violation_count,
        page.findings
            .iter()
            .filter(|finding| finding.category == "wcag")
            .map(|f| f.occurrence_count)
            .sum::<usize>()
    );
    for finding in &page.findings {
        assert!(!finding.occurrences.is_empty());
    }
}

#[test]
fn test_batch_envelope_shape() {
    use crate::audit::BatchReport;

    let reports = vec![
        AuditReport::new(
            "https://example.com/a".to_string(),
            WcagLevel::AA,
            WcagResults::new(),
            100,
        ),
        AuditReport::new(
            "https://example.com/b".to_string(),
            WcagLevel::AA,
            WcagResults::new(),
            100,
        ),
    ];
    let batch = BatchReport::from_reports(reports, vec![], 200);
    let unified = UnifiedReport::batch(&batch);

    assert_eq!(unified.report_type, "batch");
    assert_eq!(unified.pages.len(), 2);
    assert!(unified.audit_scope.is_some());
    assert!(unified.execution_environment.is_some());
    let site_analysis = unified
        .site_analysis
        .as_ref()
        .expect("batch site analysis present");
    for key in [
        "module_averages",
        "consistency",
        "page_types",
        "content_quality",
        "content_topics",
        "duplicates",
        "performance",
        "structured_data",
        "interactive_summary",
        "accessibility_assessments",
    ] {
        assert!(
            site_analysis.get(key).is_some(),
            "missing site_analysis.{key}"
        );
    }
    let consistency = &site_analysis["consistency"];
    for key in [
        "navigation",
        "headings",
        "canonical",
        "orphan_pages",
        "schema_graph",
        "help",
        "wcag_cross_page",
    ] {
        assert!(consistency.get(key).is_some(), "missing consistency.{key}");
    }
    // Batch pages carry a compact detail with fix_guidance only (#256).
    // No new data is collected — it is derived from the findings already
    // normalized for each page; with no violations fix_guidance is empty.
    assert!(unified.pages.iter().all(|p| p.detail.is_some()));
    for page in &unified.pages {
        let detail = page.detail.as_ref().expect("batch page detail present");
        assert!(detail.fix_guidance.is_empty());
    }

    let output = unified.to_json(true).unwrap();
    assert!(output.contains("\"report_type\": \"batch\""));
    assert!(output.contains("\"schema_version\": \"2.0\""));
    // No sample metadata attached → the block is omitted.
    assert!(!output.contains("\"sample\""));
}

#[test]
fn test_batch_envelope_includes_sample_metadata() {
    use crate::audit::{BatchReport, SampleMetadata};

    let reports = vec![AuditReport::new(
        "https://example.com/a".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        100,
    )];
    let batch = BatchReport::from_reports(reports, vec![], 200).with_sample(SampleMetadata {
        source: "sitemap".to_string(),
        total_discovered: 487,
        audited: 20,
        sample_limit: Some(20),
        selection: "first_n".to_string(),
        is_sample: true,
    });

    let json: serde_json::Value =
        serde_json::from_str(&UnifiedReport::batch(&batch).to_json(false).unwrap()).unwrap();
    let sample = &json["sample"];
    assert_eq!(sample["source"], "sitemap");
    assert_eq!(sample["total_discovered"], 487);
    assert_eq!(sample["audited"], 20);
    assert_eq!(sample["sample_limit"], 20);
    assert_eq!(sample["selection"], "first_n");
    assert_eq!(sample["is_sample"], true);
}

#[test]
fn test_worst_risk_all_low() {
    use crate::audit::compute_worst_risk;
    use crate::audit::normalized::RiskLevel;
    use crate::wcag::WcagResults;
    // No critical/high/medium pages — result must be Low
    let reports: Vec<crate::audit::normalized::NormalizedReport> = (0..3)
        .map(|_| {
            crate::audit::normalized::normalize(&AuditReport::new(
                "https://example.com".to_string(),
                WcagLevel::AA,
                WcagResults::new(),
                100,
            ))
            .normalized
        })
        .collect();
    let result = compute_worst_risk(&reports);
    assert_eq!(
        result,
        RiskLevel::Low,
        "all-low batch must report Low risk, got {:?}",
        result
    );
}

#[test]
fn test_modules_under_page_detail() {
    use crate::output::module::active_modules;
    use crate::performance::{PerformanceGrade, PerformanceScore, WebVitals};

    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        500,
    )
    .with_performance(crate::audit::PerformanceResults {
        vitals: WebVitals::default(),
        score: PerformanceScore {
            overall: 80,
            grade: PerformanceGrade::Gold,
            lcp_score: Some(20),
            fcp_score: Some(20),
            cls_score: Some(20),
            interactivity_score: Some(20),
            si_score: Some(20),
            metrics_available: 5,
            size_penalty: None,
            js_penalty: None,
            request_penalty: None,
            dom_penalty: None,
            is_capped: None,
        },
        render_blocking: None,
        content_weight: None,
        third_party: None,
        critical_chain: None,
        minification: None,
        animations: None,
        coverage: None,
        measurement_warnings: vec![],
    })
    .with_seo(crate::seo::SeoAnalysis::default())
    .with_security(crate::security::SecurityAnalysis {
        score: 90,
        grade: "A".to_string(),
        headers: Default::default(),
        ssl: Default::default(),
        issues: vec![],
        recommendations: vec![],
        protection: Default::default(),
        sourcemap_leaks: Default::default(),
    })
    .with_ux(crate::ux::analyze_ux(&crate::AXTree::new()))
    .with_journey(crate::journey::analyze_journey(&crate::AXTree::new()));

    let active_keys: Vec<&'static str> = active_modules(&report)
        .into_iter()
        .map(|(key, _)| key)
        .collect();

    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let json_str = unified.to_json(true).unwrap();
    let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();

    let modules = &json_value["pages"][0]["detail"]["modules"];
    for key in &active_keys {
        assert!(
            modules.get(key).is_some(),
            "Module '{}' is active but missing from pages[0].detail.modules",
            key
        );
    }
}

/// Builds an AuditReport with all modules registered in `active_modules()`.
fn all_active_modules_report() -> AuditReport {
    use crate::audit::PerformanceResults;
    use crate::dark_mode::DarkModeAnalysis;
    use crate::mobile::{
        ContentSizing, FontSizeAnalysis, MobileFriendliness, TouchTargetAnalysis, ViewportAnalysis,
    };
    use crate::performance::{PerformanceGrade, PerformanceScore, WebVitals};

    let mut report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        0,
    )
    .with_performance(PerformanceResults {
        vitals: WebVitals::default(),
        score: PerformanceScore {
            overall: 80,
            grade: PerformanceGrade::Gold,
            lcp_score: None,
            fcp_score: None,
            cls_score: None,
            interactivity_score: None,
            si_score: None,
            metrics_available: 0,
            size_penalty: None,
            js_penalty: None,
            request_penalty: None,
            dom_penalty: None,
            is_capped: None,
        },
        render_blocking: None,
        content_weight: None,
        third_party: None,
        critical_chain: None,
        minification: None,
        animations: None,
        coverage: None,
        measurement_warnings: vec![],
    })
    .with_seo(crate::seo::SeoAnalysis::default())
    .with_security(crate::security::SecurityAnalysis {
        score: 80,
        grade: "B".into(),
        headers: Default::default(),
        ssl: Default::default(),
        issues: vec![],
        recommendations: vec![],
        protection: Default::default(),
        sourcemap_leaks: Default::default(),
    })
    .with_html_conform(crate::html_conform::HtmlConformAnalysis {
        score: 90,
        checked: true,
        error_count: 0,
        warning_count: 1,
        info_count: 0,
        distinct_defect_count: 1,
        findings: vec![],
        raw_html: None,
    })
    .with_mobile(MobileFriendliness {
        score: 75,
        viewport: ViewportAnalysis::default(),
        touch_targets: TouchTargetAnalysis::default(),
        font_sizes: FontSizeAnalysis::default(),
        content_sizing: ContentSizing::default(),
        issues: vec![],
    })
    .with_ux(crate::ux::analyze_ux(&crate::AXTree::new()))
    .with_journey(crate::journey::analyze_journey(&crate::AXTree::new()))
    .with_dark_mode(DarkModeAnalysis {
        supported: false,
        class_based_dark_mode: false,
        score: 50,
        detection_methods: vec![],
        color_scheme_css: false,
        meta_color_scheme: None,
        meta_theme_color_dark: false,
        css_custom_properties: 0,
        dark_contrast_violations: 0,
        light_only_violations: 0,
        dark_only_violations: 0,
        contrast_violations: vec![],
        print: Default::default(),
        forced_colors: Default::default(),
        vision_deficiency: Default::default(),
        issues: vec![],
    })
    .with_best_practices(crate::best_practices::BestPracticesAnalysis {
        console_errors: crate::best_practices::ConsoleErrorsAnalysis {
            errors: vec![],
            warnings: vec![],
            error_count: 0,
            warning_count: 0,
        },
        vulnerable_libraries: crate::best_practices::VulnerableLibrariesAnalysis {
            detected: vec![],
            vulnerable: vec![],
            has_vulnerabilities: false,
            duplicate_libraries: vec![],
        },
        score: 100,
    })
    .with_tech_stack(crate::tech_stack::TechStackAnalysis {
        detected: vec![],
        findings: vec![],
        score: 100,
        grade: "A".into(),
    })
    .with_patterns(crate::patterns::PatternAnalysis {
        recognized: vec![],
        violations: vec![],
        journey_candidates: vec![],
        help_mechanisms: vec![],
    })
    .with_design_quality(crate::design_quality::DesignQualityAnalysis {
        findings: vec![crate::design_quality::DesignQualityFinding {
            rule_id: "design.line_length".to_string(),
            level: crate::design_quality::FindingLevel::Advisory,
            confidence: crate::design_quality::Confidence::Medium,
            measurement_type: "heuristic".to_string(),
            selector: "p.body-text".to_string(),
            evidence: "~110 characters per line across 3 lines.".to_string(),
            message: "This body text wraps at a line length that may reduce readability."
                .to_string(),
        }],
        rules_run: vec!["design.line_length".to_string()],
    });
    let sq = crate::source_quality::analyze_source_quality(&report);
    let av = crate::ai_visibility::analyze_ai_visibility(&report);
    report.discoverability.source_quality = Some(sq);
    report.discoverability.ai_visibility = Some(av);
    // content_visibility is set separately per test — its JSON emission
    // is conditional on signal_count > 0.
    report
}

#[test]
fn test_json_all_active_modules_non_null() {
    use crate::output::module::active_modules;

    let report = all_active_modules_report();
    let active_keys: Vec<&'static str> = active_modules(&report)
        .into_iter()
        .map(|(key, _)| key)
        // content_visibility is intentionally skipped: the JSON emitter suppresses it
        // when signal_count == 0 (an empty fixture produces no signals). This is
        // expected behavior, not a bug — tested via the PDF ViewModel path instead.
        .filter(|k| *k != "content_visibility")
        .collect();

    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let json_str = unified.to_json(true).unwrap();
    let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    let modules = &json_value["pages"][0]["detail"]["modules"];

    for key in &active_keys {
        let value = modules.get(key);
        assert!(
            value.is_some(),
            "Module '{}' missing from pages[0].detail.modules",
            key
        );
        assert!(
            !value.unwrap().is_null(),
            "Module '{}' is null in pages[0].detail.modules",
            key
        );
    }
}

#[test]
fn test_search_experience_serialized_in_single_detail_modules() {
    let report = all_active_modules_report();
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let json_str = unified.to_json(true).unwrap();
    let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    let search_experience = &json_value["pages"][0]["detail"]["modules"]["search_experience"];

    assert!(
        search_experience.is_object(),
        "search_experience must be serialized in single report detail modules"
    );
    assert_eq!(search_experience["measurement_type"], "composite");
    assert!(search_experience["score"].as_u64().is_some());
    assert!(
        search_experience["components"]
            .as_array()
            .is_some_and(|components| !components.is_empty()),
        "search_experience must include its component inputs"
    );
}

/// #581: the subcategory breakdown the PDF shows must also be in the canonical
/// single-report JSON (canonical English names, 0-100 scores), while batch
/// page details stay compact and omit it.
#[test]
fn test_subcategory_scores_serialized_in_single_detail_only() {
    let report = all_active_modules_report();
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let json_value: serde_json::Value =
        serde_json::from_str(&unified.to_json(true).unwrap()).unwrap();
    let detail = &json_value["pages"][0]["detail"];

    let accessibility = detail["accessibility_subcategory_scores"]
        .as_array()
        .expect("accessibility_subcategory_scores must be serialized in single detail");
    assert_eq!(
        accessibility.len(),
        7,
        "all 7 subcategories are always present"
    );
    let security = detail["security_category_scores"]
        .as_array()
        .expect("security_category_scores must be serialized when Security ran");
    assert!(!security.is_empty());
    for entry in accessibility.iter().chain(security) {
        let name = entry["name"].as_str().expect("entry name");
        assert!(
            name.is_ascii(),
            "canonical English name expected, got {name:?}"
        );
        assert!(entry["score"].as_u64().is_some_and(|s| s <= 100));
        assert!(entry.get("subcategory_kind").is_none());
        assert!(entry.get("category_kind").is_none());
    }

    let batch = crate::audit::BatchReport::from_reports(vec![report], vec![], 0);
    let batch_json: serde_json::Value =
        serde_json::from_str(&UnifiedReport::batch(&batch).to_json(true).unwrap()).unwrap();
    let batch_detail = &batch_json["pages"][0]["detail"];
    assert!(batch_detail
        .get("accessibility_subcategory_scores")
        .is_none());
    assert!(batch_detail.get("security_category_scores").is_none());
}

#[test]
fn test_dual_viewport_summary_serialized_in_single_detail_modules() {
    let mut report = all_active_modules_report();
    report.dual_viewport = Some(crate::audit::DualViewportResults {
        desktop: crate::audit::ViewportAuditData {
            wcag_results: WcagResults::new(),
            accessibility_score: 92.0,
            performance: None,
            seo: None,
            mobile: None,
            ux: None,
            journey: None,
            screenshot: None,
            module_runs: vec![],
        },
        mobile: crate::audit::ViewportAuditData {
            wcag_results: WcagResults::new(),
            accessibility_score: 71.0,
            performance: None,
            seo: None,
            mobile: None,
            ux: None,
            journey: None,
            screenshot: None,
            module_runs: vec![],
        },
    });

    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let json_str = unified.to_json(true).unwrap();
    let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    let dual = &json_value["pages"][0]["detail"]["modules"]["dual_viewport"];

    assert!(dual.is_object(), "dual_viewport summary must be serialized");
    assert_eq!(dual["desktop"]["accessibility_score"], 92);
    assert_eq!(dual["mobile"]["accessibility_score"], 71);
    assert_eq!(dual["desktop"]["wcag"]["violations"], 0);
}

#[test]
fn test_score_breakdown_present_for_viewport_weighted() {
    use crate::audit::{ViewportScoreSet, ViewportScores};

    let mut report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        100,
    );
    report.viewport_scores = Some(ViewportScores {
        desktop: ViewportScoreSet {
            accessibility: 100,
            performance: None,
            overall: 100,
        },
        mobile: ViewportScoreSet {
            accessibility: 100,
            performance: None,
            overall: 100,
        },
        weighted_overall: 100,
    });
    let normalized = normalize(&report);
    assert_eq!(
        normalized.normalized.score_calculation_method,
        "viewport_weighted"
    );
    assert!(
        normalized.normalized.score_breakdown.is_some(),
        "NormalizedReport must have score_breakdown for viewport_weighted"
    );
    let unified = UnifiedReport::single(&normalized, &report);
    let json_str = unified.to_json(true).unwrap();
    let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    assert!(
        json_value["pages"][0].get("score_breakdown").is_some()
            && !json_value["pages"][0]["score_breakdown"].is_null(),
        "score_breakdown must be present and non-null for viewport_weighted pages"
    );
    assert_eq!(
        json_value["pages"][0]["score_breakdown"]["viewport_blended_accessibility"],
        json_value["pages"][0]["accessibility_score"]
    );
}

#[test]
fn test_batch_page_detail_omitted_when_none() {
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        100,
    );
    let normalized = normalize(&report);
    let page = super::build_page(&normalized.normalized, None, None);
    let json_str = serde_json::to_string(&page).unwrap();
    let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();
    assert!(
        !json_value.as_object().unwrap().contains_key("detail"),
        "batch page must not emit \"detail\" key when detail is None, got: {}",
        json_str
    );
}

#[test]
fn test_en301549_annex_present_on_single_detail() {
    use crate::taxonomy::Severity;
    use crate::wcag::Violation;

    let mut results = WcagResults::new();
    results.add_violation(Violation::new(
        "1.1.1",
        "Non-text Content",
        WcagLevel::A,
        Severity::High,
        "Missing alt",
        "n1",
    ));
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        results,
        500,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let page = first_page(&unified);
    let detail = page.detail.as_ref().expect("single report has detail");
    let annex = &detail.en301549_annex;

    assert_eq!(annex.clauses.len(), 50);
    assert_eq!(
        annex.standard_version,
        crate::wcag::en301549::EN301549_VERSION
    );
    assert_eq!(
        annex.mapping_version,
        crate::wcag::en301549::EN301549_MAPPING_VERSION
    );
    assert!(!annex.disclaimer.is_empty());

    let clause_111 = annex
        .clauses
        .iter()
        .find(|c| c.wcag == "1.1.1")
        .expect("1.1.1 present");
    assert!(matches!(
        clause_111.status,
        crate::output::json::En301549ClauseStatusKind::ViolationsFound
    ));
    assert_eq!(clause_111.findings.len(), 1);

    // Summary-level batch roll-up must stay empty on single reports — the
    // full per-page annex lives under detail instead (see doc comment on
    // `UnifiedSummary::en301549_rollup`).
    assert!(unified.summary.en301549_rollup.is_empty());
}

#[test]
fn test_en301549_batch_rollup_present_and_page_detail_carries_full_annex() {
    let reports: Vec<AuditReport> = (0..3)
        .map(|i| {
            AuditReport::new(
                format!("https://example.com/{i}"),
                WcagLevel::AA,
                WcagResults::new(),
                100,
            )
        })
        .collect();
    let batch = crate::audit::BatchReport::from_reports(reports, vec![], 0);
    let unified = UnifiedReport::batch(&batch);

    assert_eq!(unified.summary.en301549_rollup.len(), 50);
    for rollup in &unified.summary.en301549_rollup {
        assert_eq!(rollup.affected_pages, 0, "no violations were seeded");
    }

    for page in &unified.pages {
        let detail = page.detail.as_ref().expect("batch page detail present");
        assert_eq!(detail.en301549_annex.clauses.len(), 50);
    }
}

/// English-locale guard (#406): the EN 301 549 annex (disclaimer + titles)
/// must contain no German umlauts/ß anywhere in the single-report JSON.
#[test]
fn test_en301549_annex_en_guard_no_umlauts() {
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        500,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let page = first_page(&unified);
    let annex_json = serde_json::to_string(&page.detail.as_ref().unwrap().en301549_annex).unwrap();
    let has_umlaut = |s: &str| s.chars().any(|c| "äöüÄÖÜß".contains(c));
    assert!(
        !has_umlaut(&annex_json),
        "en301549_annex must be canonical English, found umlaut/ß in: {annex_json}"
    );
}

#[test]
fn test_bik_guide_present_on_single_detail() {
    use crate::taxonomy::Severity;
    use crate::wcag::Violation;

    let mut results = WcagResults::new();
    results.add_violation(Violation::new(
        "1.1.1",
        "Non-text Content",
        WcagLevel::A,
        Severity::High,
        "Missing alt",
        "n1",
    ));
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        results,
        500,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let page = first_page(&unified);
    let detail = page.detail.as_ref().expect("single report has detail");
    let guide = &detail.bik_guide;

    assert_eq!(guide.guide, crate::wcag::bik_guide::BIK_GUIDE_SOURCE);
    assert_eq!(guide.chapters.len(), 6);

    let images = guide
        .chapters
        .iter()
        .find(|c| c.id == "images")
        .expect("images chapter present");
    assert_eq!(
        images.status,
        crate::output::json::BikChapterStatusKind::FindingsPresent
    );
    assert_eq!(images.findings.len(), 1);

    // A chapter with no active detector at all (Pdf) must still render its
    // documented "not checked" state, never omitted from the six chapters.
    let pdf = guide
        .chapters
        .iter()
        .find(|c| c.id == "pdf")
        .expect("pdf chapter present");
    assert_eq!(
        pdf.status,
        crate::output::json::BikChapterStatusKind::NotChecked
    );
    assert!(pdf.findings.is_empty());
}

/// English-locale guard (#406): the BIK guide mapping's per-chapter data
/// (titles, ids, findings) must contain no German umlauts/ß in the
/// single-report JSON. Scoped to `chapters`, not the whole annex — `guide`
/// intentionally carries the source guide's own German proper name ("BIK für
/// Alle"), an untranslated citation, not generated prose.
#[test]
fn test_bik_guide_en_guard_no_umlauts() {
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        500,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let page = first_page(&unified);
    let chapters_json =
        serde_json::to_string(&page.detail.as_ref().unwrap().bik_guide.chapters).unwrap();
    let has_umlaut = |s: &str| s.chars().any(|c| "äöüÄÖÜß".contains(c));
    assert!(
        !has_umlaut(&chapters_json),
        "bik_guide.chapters must be canonical English, found umlaut/ß in: {chapters_json}"
    );
}

#[test]
fn test_collection_errors_absent_when_empty() {
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        100,
    );
    let normalized = normalize(&report);
    let unified = UnifiedReport::single(&normalized, &report);
    let json_str = unified.to_json(false).unwrap();
    let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();

    assert!(
        !json_value
            .as_object()
            .unwrap()
            .contains_key("collection_errors"),
        "collection_errors must be absent from JSON when there are no errors"
    );
    let detail = &json_value["pages"][0]["detail"];
    assert!(
        !detail
            .as_object()
            .unwrap()
            .contains_key("collection_errors"),
        "detail.collection_errors must be absent from JSON when there are no errors"
    );
}

#[test]
fn test_collection_errors_serialized_when_present() {
    let mut unified = UnifiedReport {
        schema_version: "2.0",
        report_type: "batch",
        tool_version: env!("CARGO_PKG_VERSION"),
        build_id: env!("AUDITMYSITE_BUILD_SHA"),
        metadata: ReportMetadata {
            tool: "test".to_string(),
            timestamp: chrono::DateTime::<chrono::Utc>::UNIX_EPOCH,
            wcag_level: "AA".to_string(),
            execution_time_ms: 0,
        },
        audit_scope: None,
        execution_environment: None,
        audit_quality: Default::default(),
        metric_context: metric_context("batch"),
        artifacts: Vec::new(),
        summary: UnifiedSummary {
            url_count: 0,
            accessibility_score: 0,
            overall_score: 0,
            score: 0,
            grade: "F".to_string(),
            certificate: "None".to_string(),
            risk_level: crate::audit::normalized::RiskLevel::Low,
            violation_count: 0,
            finding_count: 0,
            finding_occurrence_count: 0,
            severity_counts: crate::audit::normalized::SeverityCounts {
                critical: 0,
                high: 0,
                medium: 0,
                low: 0,
                total: 0,
            },
            severity_counts_scope: "wcag_only".to_string(),
            occurrence_counts: crate::audit::normalized::SeverityCounts::default(),
            occurrence_counts_scope: "wcag_only".to_string(),
            passed_url_count: 0,
            failed_url_count: 0,
            attempted_url_count: None,
            exclusions: None,
            violated_rule_count: 0,
            top_recurring_rules: vec![],
            template_clusters: vec![],
            en301549_rollup: vec![],
            performance_score: None,
            seo_score: None,
            security_score: None,
            mobile_score: None,
            ux_score: None,
            journey_score: None,
            performance_throttled_avg_score: None,
            lh_mobile_score: None,
            wcag_coverage: build_wcag_coverage_for_level("AA"),
            overall_score_weight_basis: 100,
            overall_score_weight_basis_uniform: None,
            accessibility_score_breakdown: vec![],
            management_risks: vec![],
            top_actions: vec![],
            duplicate_content: vec![],
            missing_tag_prevalence: vec![],
            canonical_issues: vec![],
            hreflang_issues: vec![],
            sitemap_http_issues: vec![],
            orphan_sitemap_urls: vec![],
            linked_not_in_sitemap: vec![],
            robots_conflicts: vec![],
            minification_inconsistencies: vec![],
            redirect_chain_issues: vec![],
            crawl_depth_diagnostics: None,
            commerce: None,
        },
        sample: None,
        pages: vec![],
        url_matrix: vec![],
        internal_comparison: None,
        crawl_diagnostics: None,
        sitemap_diagnostics: None,
        site_analysis: None,
        errors: vec![],
        collection_errors: vec![ReportError {
            module: "crawl_diagnostics",
            error_type: "serialization_failed",
            reason: "NaN value in field".to_string(),
        }],
        verdict: Verdict::Pass,
        verdict_reasons: Vec::new(),
    };
    let json_str = unified.to_json(false).unwrap();
    let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();

    let errs = &json_value["collection_errors"];
    assert!(errs.is_array(), "collection_errors must be an array");
    assert_eq!(errs.as_array().unwrap().len(), 1);
    assert_eq!(errs[0]["module"], "crawl_diagnostics");
    assert_eq!(errs[0]["error_type"], "serialization_failed");
    assert!(errs[0]["reason"].as_str().unwrap().contains("NaN"));

    // Verify detail-level collection_errors work the same way
    let report = AuditReport::new(
        "https://example.com".to_string(),
        WcagLevel::AA,
        WcagResults::new(),
        100,
    );
    let normalized = normalize(&report);
    let mut page_unified = UnifiedReport::single(&normalized, &report);
    if let Some(detail) = page_unified.pages[0].detail.as_mut() {
        detail.collection_errors.push(ReportError {
            module: "tech_stack",
            error_type: "serialization_failed",
            reason: "custom serializer error".to_string(),
        });
    }
    let json_str2 = page_unified.to_json(false).unwrap();
    let json_value2: serde_json::Value = serde_json::from_str(&json_str2).unwrap();
    let detail_errs = &json_value2["pages"][0]["detail"]["collection_errors"];
    assert!(detail_errs.is_array());
    assert_eq!(detail_errs[0]["module"], "tech_stack");
    // suppress unused warning from the first mut binding
    let _ = &mut unified;
}