auditmysite 1.5.1

Resource-efficient WCAG 2.1 AA (+ select 2.2 criteria) 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
//! Customer-facing Search Experience score.
//!
//! This is a presentation-level composite: it does not collect new data, but
//! combines existing SEO, UX, AI visibility, content visibility and mobile
//! signals so the front report does not overstate classic technical SEO.

use crate::assessment::AssessmentLevel;
use crate::audit::normalized::AuditContext;
use crate::i18n::I18n;
use crate::output::report_model::{SearchExperienceComponent, SearchExperiencePresentation};
use crate::seo::profile::PageType;

const W_TECHNICAL_SEO: u32 = 35;
const W_CONTENT: u32 = 25;
const W_TRUST: u32 = 15;
const W_AI: u32 = 10;
const W_STRUCTURE: u32 = 10;
const W_MOBILE: u32 = 5;

pub fn build_search_experience(
    normalized: &AuditContext<'_>,
    i18n: &I18n,
) -> Option<SearchExperiencePresentation> {
    let seo = normalized.raw_seo?;
    let en = i18n.locale() == "en";

    let mut components = Vec::new();

    components.push(SearchExperienceComponent {
        label: if en {
            "Technical SEO".into()
        } else {
            "Technisches SEO".into()
        },
        score: seo.score,
        weight_pct: W_TECHNICAL_SEO,
        explanation: if en {
            "Title, description, canonical, sitemap, structured data and crawlable links."
        } else {
            "Title, Description, Canonical, Sitemap, strukturierte Daten und crawlbare Links."
        }
        .into(),
    });

    if let Some(ux) = normalized.raw_ux {
        let has_issue_for = |kind: crate::ux::UxDimensionKind| {
            ux.issues.iter().any(|issue| issue.kind.dimension() == kind)
        };
        components.push(SearchExperienceComponent {
            label: if en {
                "Content clarity".into()
            } else {
                "Inhaltsverständlichkeit".into()
            },
            score: ux.content_clarity.score,
            weight_pct: W_CONTENT,
            // Re-derive in the run locale; the stored summary is canonical
            // English and would otherwise leak into the German report (#406).
            explanation: crate::ux::ux_dimension_summary(
                ux.content_clarity.kind,
                ux.content_clarity.score,
                en,
                has_issue_for(ux.content_clarity.kind),
            ),
        });
        components.push(SearchExperienceComponent {
            label: if en {
                "Trust signals".into()
            } else {
                "Vertrauenssignale".into()
            },
            score: trust_score(normalized, ux.trust_signals.score),
            weight_pct: W_TRUST,
            explanation: crate::ux::ux_dimension_summary(
                ux.trust_signals.kind,
                ux.trust_signals.score,
                en,
                has_issue_for(ux.trust_signals.kind),
            ),
        });
        components.push(SearchExperienceComponent {
            label: if en {
                "Structure and semantics".into()
            } else {
                "Struktur und Semantik".into()
            },
            score: structure_score(normalized, ux.visual_hierarchy.score),
            weight_pct: W_STRUCTURE,
            explanation: crate::ux::ux_dimension_summary(
                ux.visual_hierarchy.kind,
                ux.visual_hierarchy.score,
                en,
                has_issue_for(ux.visual_hierarchy.kind),
            ),
        });
    } else if let Some(profile) = seo.content_profile.as_ref() {
        components.push(SearchExperienceComponent {
            label: if en {
                "Content clarity".into()
            } else {
                "Inhaltsverständlichkeit".into()
            },
            score: profile.page_classification.content_depth_score,
            weight_pct: W_CONTENT,
            explanation: if en {
                "Estimated from page type and content-depth signals."
            } else {
                "Aus Seitentyp und Content-Tiefe abgeleitet."
            }
            .into(),
        });
        components.push(SearchExperienceComponent {
            label: if en {
                "Structure and semantics".into()
            } else {
                "Struktur und Semantik".into()
            },
            score: profile.signal_strength.overall_pct,
            weight_pct: W_STRUCTURE,
            explanation: if en {
                "Estimated from headings, structured data and page profile signals."
            } else {
                "Aus Überschriften, strukturierten Daten und Seitenprofil abgeleitet."
            }
            .into(),
        });
    }

    if let Some(ai) = normalized.raw_ai_visibility {
        let ai_score = weighted_average(&[
            (ai.readability.dimension.score, 40),
            (ai.citation.dimension.score, 30),
            (ai.chunks.dimension.score, 30),
        ]);
        components.push(SearchExperienceComponent {
            label: if en {
                "AI readability".into()
            } else {
                "KI-Lesbarkeit".into()
            },
            score: ai_score,
            weight_pct: W_AI,
            explanation: if en {
                "Readability, chunk quality and citation likelihood for AI-assisted systems."
            } else {
                "Lesbarkeit, Abschnittsqualität und Zitierbarkeit für KI-gestützte Systeme."
            }
            .into(),
        });
    }

    if let Some(mobile) = normalized.raw_mobile {
        components.push(SearchExperienceComponent {
            label: if en {
                "Mobile readability".into()
            } else {
                "Mobile Lesbarkeit".into()
            },
            score: mobile.score,
            weight_pct: W_MOBILE,
            explanation: if en {
                "Mobile usability, readable text and tappable controls."
            } else {
                "Mobile Nutzbarkeit, lesbarer Text und gut antippbare Bedienelemente."
            }
            .into(),
        });
    }

    let score = composite_score(&components);
    let mut warnings = build_warnings(normalized, score, en);
    warnings.truncate(5);

    Some(SearchExperiencePresentation {
        score,
        label: if en {
            "Search Experience".into()
        } else {
            "Sichtbarkeit & Nutzerverständnis".into()
        },
        interpretation: interpretation(score, seo.score, &components, en),
        components,
        warnings,
    })
}

fn composite_score(components: &[SearchExperienceComponent]) -> u32 {
    let total_weight: u32 = components.iter().map(|c| c.weight_pct).sum();
    if total_weight == 0 {
        return 0;
    }
    let weighted: u32 = components.iter().map(|c| c.score * c.weight_pct).sum();
    ((weighted as f64 / total_weight as f64).round() as u32).min(100)
}

fn weighted_average(items: &[(u32, u32)]) -> u32 {
    let total_weight: u32 = items.iter().map(|(_, w)| *w).sum();
    if total_weight == 0 {
        return 0;
    }
    let sum: u32 = items.iter().map(|(score, weight)| score * weight).sum();
    ((sum as f64 / total_weight as f64).round() as u32).min(100)
}

fn trust_score(normalized: &AuditContext<'_>, ux_score: u32) -> u32 {
    let local_business_penalty = normalized
        .raw_content_visibility
        .map(|cv| {
            cv.local_business
                .iter()
                .filter(|s| {
                    matches!(
                        s.level,
                        AssessmentLevel::Warning | AssessmentLevel::Violation
                    )
                })
                .count() as u32
                * 8
        })
        .unwrap_or(0);
    ux_score.saturating_sub(local_business_penalty.min(24))
}

fn structure_score(normalized: &AuditContext<'_>, ux_score: u32) -> u32 {
    let seo_structure = normalized
        .raw_seo
        .and_then(|seo| seo.content_profile.as_ref())
        .map(|profile| {
            let heading = category_score(profile, "Überschriften")
                .or_else(|| category_score(profile, "Headings"))
                .unwrap_or(profile.signal_strength.overall_pct);
            let schema = category_score(profile, "Strukturierte Daten")
                .or_else(|| category_score(profile, "Structured data"))
                .unwrap_or(profile.signal_strength.overall_pct);
            weighted_average(&[(heading, 55), (schema, 45)])
        });
    match seo_structure {
        Some(score) => weighted_average(&[(ux_score, 55), (score, 45)]),
        None => ux_score,
    }
}

fn category_score(profile: &crate::seo::profile::SeoContentProfile, name: &str) -> Option<u32> {
    profile
        .signal_strength
        .categories
        .iter()
        .find(|c| c.name == name)
        .map(|c| c.pct())
}

fn build_warnings(normalized: &AuditContext<'_>, score: u32, en: bool) -> Vec<String> {
    let mut warnings = Vec::new();
    if let Some(seo) = normalized.raw_seo {
        if seo.score >= 75 && score + 10 < seo.score {
            warnings.push(if en {
                format!(
                    "Technical SEO is {} / 100, but content, trust or AI signals reduce the customer-facing visibility score.",
                    seo.score
                )
            } else {
                format!(
                    "Technisches SEO liegt bei {} / 100, aber Content-, Trust- oder KI-Signale senken die kundennahe Sichtbarkeit.",
                    seo.score
                )
            });
        }
        if seo.technical.word_count < 900
            && seo
                .image_efficiency
                .as_ref()
                .is_some_and(|ie| ie.total_images >= 30)
        {
            warnings.push(if en {
                format!(
                    "{} words with {} images: important messages may be image-led rather than readable HTML.",
                    seo.technical.word_count,
                    seo.image_efficiency.as_ref().map(|ie| ie.total_images).unwrap_or(0)
                )
            } else {
                format!(
                    "{} Wörter bei {} Bildern: wichtige Aussagen können eher im Bild als im lesbaren HTML liegen.",
                    seo.technical.word_count,
                    seo.image_efficiency.as_ref().map(|ie| ie.total_images).unwrap_or(0)
                )
            });
        }
        if seo
            .content_profile
            .as_ref()
            .is_some_and(|p| p.page_classification.primary_type == PageType::MediaHeavy)
        {
            warnings.push(if en {
                "The page profile is media-heavy; image text should be backed by readable HTML copy."
                    .into()
            } else {
                "Das Seitenprofil ist medienlastig; Bildtext sollte durch lesbaren HTML-Text abgesichert werden."
                    .into()
            });
        }
    }

    if let Some(ux) = normalized.raw_ux {
        if ux.content_clarity.score < 70 {
            warnings.push(format!(
                "{}: {} / 100 — {}",
                if en {
                    "Content clarity"
                } else {
                    "Inhaltsverständlichkeit"
                },
                ux.content_clarity.score,
                // Re-derive in the run locale; the stored summary is canonical
                // English and would otherwise leak into the German PDF (#406).
                crate::ux::ux_dimension_summary(
                    ux.content_clarity.kind,
                    ux.content_clarity.score,
                    en,
                    false,
                )
            ));
        }
        if ux.trust_signals.score < 70 {
            warnings.push(format!(
                "{}: {} / 100 — {}",
                if en {
                    "Trust signals"
                } else {
                    "Vertrauenssignale"
                },
                ux.trust_signals.score,
                crate::ux::ux_dimension_summary(
                    ux.trust_signals.kind,
                    ux.trust_signals.score,
                    en,
                    false,
                )
            ));
        }
    }

    if let Some(cv) = normalized.raw_content_visibility {
        if cv.local_business.iter().any(|s| s.level.is_problem()) {
            warnings.push(if en {
                "LocalBusiness or local trust data is incomplete or not machine-readable."
                    .into()
            } else {
                "LocalBusiness- oder lokale Vertrauensdaten sind unvollstaendig oder nicht maschinenlesbar."
                    .into()
            });
        }
    }

    warnings
}

fn interpretation(
    score: u32,
    technical_seo: u32,
    components: &[SearchExperienceComponent],
    en: bool,
) -> String {
    let weak: Vec<&SearchExperienceComponent> =
        components.iter().filter(|c| c.score < 70).collect();
    if technical_seo >= 75 && !weak.is_empty() && score + 10 < technical_seo {
        let names = weak
            .iter()
            .map(|c| c.label.as_str())
            .collect::<Vec<_>>()
            .join(", ");
        if en {
            format!(
                "Technically findable, but not equally strong for users and AI systems. Weak parts: {names}."
            )
        } else {
            format!(
                "Technisch auffindbar, aber für Nutzer und KI-Systeme nicht gleich stark verständlich. Schwächere Bestandteile: {names}."
            )
        }
    } else if score >= 80 {
        if en {
            "Strong search experience: technical visibility, content clarity and trust signals align."
        } else {
            "Starke Search Experience: technische Auffindbarkeit, Inhaltsverständlichkeit und Vertrauen passen zusammen."
        }
        .into()
    } else if score >= 60 {
        if en {
            "Solid base with visible gaps in content, trust or machine readability."
        } else {
            "Solide Basis mit sichtbaren Lücken bei Inhalt, Vertrauen oder maschineller Lesbarkeit."
        }
        .into()
    } else {
        if en {
            "Limited search experience: the page is not sufficiently understandable, trustworthy or extractable."
        } else {
            "Eingeschränkte Search Experience: Die Seite ist nicht ausreichend verständlich, vertrauensstark oder extrahierbar."
        }
        .into()
    }
}

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

    #[test]
    fn composite_normalizes_missing_weights() {
        let components = vec![
            SearchExperienceComponent {
                label: "SEO".into(),
                score: 80,
                weight_pct: 35,
                explanation: String::new(),
            },
            SearchExperienceComponent {
                label: "Content".into(),
                score: 40,
                weight_pct: 25,
                explanation: String::new(),
            },
        ];

        assert_eq!(composite_score(&components), 63);
    }

    #[test]
    fn interpretation_mentions_gap_to_technical_seo() {
        let components = vec![
            SearchExperienceComponent {
                label: "Technical SEO".into(),
                score: 82,
                weight_pct: 35,
                explanation: String::new(),
            },
            SearchExperienceComponent {
                label: "Trust".into(),
                score: 35,
                weight_pct: 15,
                explanation: String::new(),
            },
        ];

        let text = interpretation(68, 82, &components, false);
        assert!(text.contains("Technisch auffindbar"));
        assert!(text.contains("Trust"));
    }
}