auditmysite 0.26.0

Resource-efficient WCAG 2.1 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
//! Unified assessment type system (issue #51).
//!
//! Provides common types for assessment-level results, evidence confidence,
//! and content signals that all analysis modules can share. This is the
//! foundation for Content Visibility Analysis (#54) and Evidence Model (#52).
//!
//! # Design
//!
//! Every finding from every module maps to a [`ContentSignal`]:
//! - WCAG violations → `Violation` or `Warning` level, `Accessibility` area
//! - SEO signal checks → `Pass` / `Warning`, `Seo` area
//! - Pattern detections → `Positive`, `Pattern` area
//! - Untestable criteria → `NotTestable`
//!
//! [`EvidenceConfidence`] expresses how reliable the automated detection is —
//! not whether the content claim is true.

use serde::{Deserialize, Serialize};

use crate::patterns::{PatternConfidence, RecognizedPattern};
use crate::seo::profile::SignalCheck;
use crate::wcag::types::FindingKind;

// ─── Content Area ────────────────────────────────────────────────────────────

/// Top-level domain a signal belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContentArea {
    Accessibility,
    Seo,
    SourceQuality,
    AiVisibility,
    Performance,
    Security,
    Mobile,
    Pattern,
    Content,
}

impl ContentArea {
    pub fn label(&self) -> &'static str {
        match self {
            Self::Accessibility => "Accessibility",
            Self::Seo => "SEO",
            Self::SourceQuality => "Source Quality",
            Self::AiVisibility => "AI Visibility",
            Self::Performance => "Performance",
            Self::Security => "Security",
            Self::Mobile => "Mobile",
            Self::Pattern => "Pattern",
            Self::Content => "Content",
        }
    }
}

// ─── Assessment Level ─────────────────────────────────────────────────────────

/// Assessment outcome of a single check — generalized across all modules.
///
/// This is the cross-module counterpart of [`crate::wcag::types::FindingKind`].
/// `Pass` is the only variant added here that `FindingKind` does not have.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AssessmentLevel {
    /// Check passed cleanly.
    Pass,
    /// Concrete problem detected automatically.
    Violation,
    /// Heuristic suspicion; automated tool cannot confirm without more context.
    Warning,
    /// Good pattern actively detected (positive evidence).
    Positive,
    /// Criterion exists but cannot be evaluated automatically.
    NotTestable,
}

impl AssessmentLevel {
    pub fn label(&self) -> &'static str {
        match self {
            Self::Pass => "Pass",
            Self::Violation => "Violation",
            Self::Warning => "Warning",
            Self::Positive => "Positive",
            Self::NotTestable => "Not Testable",
        }
    }

    pub fn is_problem(&self) -> bool {
        matches!(self, Self::Violation | Self::Warning)
    }
}

impl From<FindingKind> for AssessmentLevel {
    fn from(kind: FindingKind) -> Self {
        match kind {
            FindingKind::Violation => Self::Violation,
            FindingKind::Warning => Self::Warning,
            FindingKind::Positive => Self::Positive,
            FindingKind::NotTestable => Self::NotTestable,
        }
    }
}

// ─── Evidence Confidence ─────────────────────────────────────────────────────

/// How reliable the automated signal is — not whether the content claim is true.
///
/// - `High`: direct DOM/AXTree attribute or known structural fact
/// - `Medium`: heuristic with good precision in practice
/// - `Low`: weak proxy, presence-only, or highly context-dependent
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceConfidence {
    Low,
    Medium,
    High,
}

impl EvidenceConfidence {
    pub fn label(&self) -> &'static str {
        match self {
            Self::High => "High",
            Self::Medium => "Medium",
            Self::Low => "Low",
        }
    }
}

impl From<PatternConfidence> for EvidenceConfidence {
    fn from(pc: PatternConfidence) -> Self {
        match pc {
            PatternConfidence::Strong => Self::High,
            PatternConfidence::Partial => Self::Medium,
        }
    }
}

// ─── Evidence Source ─────────────────────────────────────────────────────────

/// Where the automated signal was detected.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceSource {
    /// HTML `<meta>` tag
    Meta,
    /// JSON-LD structured data
    JsonLd,
    /// Visible text content on the page
    VisibleText,
    /// `<a>` href or link text
    Link,
    /// HTTP response header
    HttpHeader,
    /// Browser Accessibility Tree (AXTree) node
    AxTree,
    /// HTML DOM attribute (e.g. `alt`, `title`, `aria-*`)
    DomAttribute,
    /// Computed CSS property
    CssProperty,
    /// Derived or aggregated value
    Computed,
}

// ─── Content Evidence ─────────────────────────────────────────────────────────

/// Machine-readable provenance for a single signal.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContentEvidence {
    /// Where the value was found.
    pub source: EvidenceSource,
    /// Dotted path to the field, e.g. `"LocalBusiness.geo.latitude"`.
    pub field_path: Option<String>,
    /// Short value excerpt for display in reports.
    pub value_excerpt: Option<String>,
    /// How reliable this evidence is.
    pub confidence: EvidenceConfidence,
}

impl ContentEvidence {
    pub fn new(source: EvidenceSource, confidence: EvidenceConfidence) -> Self {
        Self {
            source,
            field_path: None,
            value_excerpt: None,
            confidence,
        }
    }

    pub fn with_field(mut self, path: impl Into<String>) -> Self {
        self.field_path = Some(path.into());
        self
    }

    pub fn with_value(mut self, excerpt: impl Into<String>) -> Self {
        self.value_excerpt = Some(excerpt.into());
        self
    }
}

// ─── Content Signal ───────────────────────────────────────────────────────────

/// A single, self-contained assessment result from any module.
///
/// This is the common output unit across all analysis dimensions. Use the
/// builder methods to construct signals. Evidence is optional but recommended
/// wherever it can be derived automatically.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContentSignal {
    /// Which analysis domain produced this signal.
    pub area: ContentArea,
    /// Outcome of the check.
    pub level: AssessmentLevel,
    /// How reliably the automated tool detected this signal.
    pub confidence: EvidenceConfidence,
    /// Short human-readable label (suitable for table/list display).
    pub title: String,
    /// Full description or finding detail.
    pub detail: String,
    /// Machine-readable provenance (may be empty for derived signals).
    pub evidence: Vec<ContentEvidence>,
}

impl ContentSignal {
    pub fn new(
        area: ContentArea,
        level: AssessmentLevel,
        confidence: EvidenceConfidence,
        title: impl Into<String>,
        detail: impl Into<String>,
    ) -> Self {
        Self {
            area,
            level,
            confidence,
            title: title.into(),
            detail: detail.into(),
            evidence: vec![],
        }
    }

    pub fn with_evidence(mut self, ev: ContentEvidence) -> Self {
        self.evidence.push(ev);
        self
    }

    pub fn with_evidence_list(mut self, list: Vec<ContentEvidence>) -> Self {
        self.evidence = list;
        self
    }

    // ── Convenience constructors ─────────────────────────────────────────

    pub fn pass(
        area: ContentArea,
        confidence: EvidenceConfidence,
        title: impl Into<String>,
        detail: impl Into<String>,
    ) -> Self {
        Self::new(area, AssessmentLevel::Pass, confidence, title, detail)
    }

    pub fn violation(
        area: ContentArea,
        confidence: EvidenceConfidence,
        title: impl Into<String>,
        detail: impl Into<String>,
    ) -> Self {
        Self::new(area, AssessmentLevel::Violation, confidence, title, detail)
    }

    pub fn warning(
        area: ContentArea,
        confidence: EvidenceConfidence,
        title: impl Into<String>,
        detail: impl Into<String>,
    ) -> Self {
        Self::new(area, AssessmentLevel::Warning, confidence, title, detail)
    }

    pub fn positive(
        area: ContentArea,
        confidence: EvidenceConfidence,
        title: impl Into<String>,
        detail: impl Into<String>,
    ) -> Self {
        Self::new(area, AssessmentLevel::Positive, confidence, title, detail)
    }
}

// ─── From conversions ────────────────────────────────────────────────────────

impl From<&crate::wcag::Violation> for ContentSignal {
    fn from(v: &crate::wcag::Violation) -> Self {
        let level = AssessmentLevel::from(v.kind);
        let confidence = match v.kind {
            FindingKind::Violation => EvidenceConfidence::High,
            FindingKind::Warning => EvidenceConfidence::Medium,
            _ => EvidenceConfidence::Medium,
        };
        let mut sig = Self::new(
            ContentArea::Accessibility,
            level,
            confidence,
            format!("WCAG {} – {}", v.rule, v.rule_name),
            v.message.clone(),
        );
        // Map ViolationEvidence items from the violation itself.
        for ev in &v.evidence {
            let source = match ev.source.as_str() {
                "ax_tree" => EvidenceSource::AxTree,
                "dom_attribute" => EvidenceSource::DomAttribute,
                "meta" => EvidenceSource::Meta,
                "css_property" => EvidenceSource::CssProperty,
                "http_header" => EvidenceSource::HttpHeader,
                _ => EvidenceSource::Computed,
            };
            let mut ce = ContentEvidence::new(source, confidence);
            if let Some(f) = &ev.field {
                ce = ce.with_field(f);
            }
            if let Some(val) = &ev.value {
                ce = ce.with_value(val);
            }
            sig.evidence.push(ce);
        }
        // Fallback: if no structured evidence, use the selector.
        if sig.evidence.is_empty() {
            if let Some(sel) = &v.selector {
                sig.evidence.push(
                    ContentEvidence::new(EvidenceSource::AxTree, EvidenceConfidence::High)
                        .with_value(sel),
                );
            }
        }
        sig
    }
}

/// Convert a SEO signal check into a `ContentSignal`.
/// `category` is the parent `SignalCategory::name`, used to build the title.
pub fn signal_from_check(check: &SignalCheck, category: &str) -> ContentSignal {
    let level = if check.passed {
        AssessmentLevel::Pass
    } else {
        AssessmentLevel::Warning
    };
    let detail = check.detail.clone().unwrap_or_else(|| check.label.clone());
    let mut sig = ContentSignal::new(
        ContentArea::Seo,
        level,
        EvidenceConfidence::Medium,
        format!("{} – {}", category, check.label),
        detail,
    );
    sig.evidence.push(ContentEvidence::new(
        EvidenceSource::Computed,
        EvidenceConfidence::Medium,
    ));
    sig
}

impl From<&RecognizedPattern> for ContentSignal {
    fn from(p: &RecognizedPattern) -> Self {
        let confidence = EvidenceConfidence::from(p.confidence);
        ContentSignal::positive(
            ContentArea::Pattern,
            confidence,
            p.pattern.clone(),
            p.message.clone(),
        )
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn assessment_level_from_finding_kind() {
        assert_eq!(
            AssessmentLevel::from(FindingKind::Violation),
            AssessmentLevel::Violation
        );
        assert_eq!(
            AssessmentLevel::from(FindingKind::Warning),
            AssessmentLevel::Warning
        );
        assert_eq!(
            AssessmentLevel::from(FindingKind::Positive),
            AssessmentLevel::Positive
        );
        assert_eq!(
            AssessmentLevel::from(FindingKind::NotTestable),
            AssessmentLevel::NotTestable
        );
    }

    #[test]
    fn assessment_level_is_problem() {
        assert!(AssessmentLevel::Violation.is_problem());
        assert!(AssessmentLevel::Warning.is_problem());
        assert!(!AssessmentLevel::Pass.is_problem());
        assert!(!AssessmentLevel::Positive.is_problem());
        assert!(!AssessmentLevel::NotTestable.is_problem());
    }

    #[test]
    fn evidence_confidence_from_pattern_confidence() {
        assert_eq!(
            EvidenceConfidence::from(PatternConfidence::Strong),
            EvidenceConfidence::High
        );
        assert_eq!(
            EvidenceConfidence::from(PatternConfidence::Partial),
            EvidenceConfidence::Medium
        );
    }

    #[test]
    fn evidence_confidence_ordering() {
        assert!(EvidenceConfidence::High > EvidenceConfidence::Medium);
        assert!(EvidenceConfidence::Medium > EvidenceConfidence::Low);
    }

    #[test]
    fn content_signal_violation_from_wcag_violation() {
        use crate::cli::WcagLevel;
        use crate::wcag::types::Violation;

        let v = Violation::new(
            "1.1.1",
            "Non-text Content",
            WcagLevel::A,
            crate::wcag::Severity::High,
            "Image missing alt",
            "img#logo",
        )
        .with_selector("img#logo");
        let sig = ContentSignal::from(&v);
        assert_eq!(sig.area, ContentArea::Accessibility);
        assert_eq!(sig.level, AssessmentLevel::Violation);
        assert!(!sig.evidence.is_empty());
    }

    #[test]
    fn content_signal_from_recognized_pattern() {
        let p = RecognizedPattern {
            pattern: "MainNavigation".to_string(),
            message: "nav[aria-label] with 5 links found".to_string(),
            confidence: PatternConfidence::Strong,
        };
        let sig = ContentSignal::from(&p);
        assert_eq!(sig.area, ContentArea::Pattern);
        assert_eq!(sig.level, AssessmentLevel::Positive);
        assert_eq!(sig.confidence, EvidenceConfidence::High);
    }

    #[test]
    fn content_signal_from_signal_check_pass() {
        let check = SignalCheck {
            label: "Title vorhanden".to_string(),
            passed: true,
            detail: Some("42 Zeichen".to_string()),
        };
        let sig = signal_from_check(&check, "Meta-Tags");
        assert_eq!(sig.area, ContentArea::Seo);
        assert_eq!(sig.level, AssessmentLevel::Pass);
        assert!(sig.title.contains("Meta-Tags"));
    }

    #[test]
    fn content_signal_from_signal_check_fail() {
        let check = SignalCheck {
            label: "Description vorhanden".to_string(),
            passed: false,
            detail: None,
        };
        let sig = signal_from_check(&check, "Meta-Tags");
        assert_eq!(sig.level, AssessmentLevel::Warning);
    }

    #[test]
    fn content_signal_builder_with_evidence() {
        let sig = ContentSignal::violation(
            ContentArea::Seo,
            EvidenceConfidence::High,
            "Missing canonical",
            "No canonical URL found",
        )
        .with_evidence(
            ContentEvidence::new(EvidenceSource::Meta, EvidenceConfidence::High)
                .with_field("canonical")
                .with_value("(absent)"),
        );
        assert_eq!(sig.evidence.len(), 1);
        assert_eq!(sig.evidence[0].field_path.as_deref(), Some("canonical"));
    }

    #[test]
    fn content_signal_from_violation_maps_violation_evidence() {
        use crate::cli::WcagLevel;
        use crate::wcag::types::{Violation, ViolationEvidence};

        let v = Violation::new(
            "4.1.2",
            "Name, Role, Value",
            WcagLevel::A,
            crate::wcag::Severity::High,
            "Missing role",
            "btn-1",
        )
        .with_evidence_item(ViolationEvidence::ax_tree("button.nav-toggle"))
        .with_evidence_item(ViolationEvidence::dom_attribute("aria-label", None));

        let sig = ContentSignal::from(&v);
        assert_eq!(sig.evidence.len(), 2);
        assert!(matches!(sig.evidence[0].source, EvidenceSource::AxTree));
        assert!(matches!(
            sig.evidence[1].source,
            EvidenceSource::DomAttribute
        ));
        assert_eq!(
            sig.evidence[0].value_excerpt.as_deref(),
            Some("button.nav-toggle")
        );
    }

    #[test]
    fn content_signal_from_violation_uses_selector_fallback_when_no_evidence() {
        use crate::cli::WcagLevel;
        use crate::wcag::types::Violation;

        let v = Violation::new(
            "1.1.1",
            "Non-text Content",
            WcagLevel::A,
            crate::wcag::Severity::High,
            "No alt",
            "img-1",
        )
        .with_selector("img.hero");

        let sig = ContentSignal::from(&v);
        assert_eq!(sig.evidence.len(), 1);
        assert!(matches!(sig.evidence[0].source, EvidenceSource::AxTree));
        assert_eq!(sig.evidence[0].value_excerpt.as_deref(), Some("img.hero"));
    }
}