auditmysite 0.21.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
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
//! Journey Analysis — user-flow evaluation from AXTree data
//!
//! Evaluates how well a page supports a typical user journey:
//! Entry → Orientation → Navigation → Interaction → Conversion.

use serde::{Deserialize, Serialize};
use tracing::info;

use crate::accessibility::AXTree;
use crate::journey::page_intent::{detect_page_intent, PageIntent};
use crate::journey::scoring::{journey_dimension_score, weighted_average_with_intent};
use crate::ux::saturating_penalty;

// ── Generic / CTA keyword lists (shared with UX but scoped here) ───

const GENERIC_LINK_LABELS: &[&str] = &[
    // English
    "more",
    "click here",
    "read more",
    "learn more",
    "details",
    "info",
    "link",
    "here",
    "view",
    "see more",
    "see all",
    // German
    "mehr",
    "hier",
    "klicken",
    "weiter",
    "hier klicken",
    "mehr erfahren",
    "weiterlesen",
    "alle anzeigen",
    "ansehen",
    "jetzt lesen",
    "öffnen",
    // French
    "ici",
    "cliquez ici",
    "en savoir plus",
    "lire la suite",
    "lire plus",
    "voir plus",
    "voir tout",
    "télécharger",
    // Spanish
    "aquí",
    "haz clic aquí",
    "leer más",
    "saber más",
    "ver más",
    "ver todo",
    // Italian
    "qui",
    "clicca qui",
    "leggi di più",
    "scopri di più",
    "vedi di più",
    "vedi tutto",
    // Portuguese
    "aqui",
    "clique aqui",
    "leia mais",
    "saiba mais",
    "ver mais",
    // Dutch
    "hier",
    "klik hier",
    "meer lezen",
    "lees meer",
    "bekijk meer",
    "volgende",
    // Swedish
    "här",
    "klicka här",
    "läs mer",
    // Norwegian
    "her",
    "klikk her",
    "les mer",
    // Polish
    "tutaj",
    "kliknij tutaj",
    "czytaj więcej",
    "więcej",
    // Turkish
    "devamını oku",
    "daha fazla",
];

const CTA_KEYWORDS: &[&str] = &[
    // English
    "buy",
    "order",
    "contact",
    "start",
    "register",
    "sign up",
    "book",
    "free",
    "trial",
    "get started",
    "subscribe",
    "cart",
    "checkout",
    "download",
    "demo",
    "newsletter",
    // German
    "kaufen",
    "bestellen",
    "kontakt",
    "anfrage",
    "starten",
    "registrieren",
    "anmelden",
    "buchen",
    "jetzt",
    "kostenlos",
    "testen",
    "termin",
    "beratung",
    "angebot",
    "warenkorb",
    "kasse",
    "herunterladen",
    // French
    "acheter",
    "commander",
    "contacter",
    "commencer",
    "s'inscrire",
    "réserver",
    "gratuit",
    "essai",
    "télécharger",
    "abonner",
    "panier",
    // Spanish
    "comprar",
    "pedir",
    "contactar",
    "empezar",
    "registrarse",
    "reservar",
    "gratis",
    "prueba",
    "descargar",
    "suscribirse",
    "carrito",
    // Italian
    "acquistare",
    "ordinare",
    "contattare",
    "iniziare",
    "registrarsi",
    "prenotare",
    "gratuito",
    "prova",
    "scaricare",
    "iscriversi",
    "carrello",
    // Portuguese
    "comprar",
    "pedir",
    "contactar",
    "começar",
    "registrar",
    "reservar",
    "grátis",
    "baixar",
    "assinar",
    "carrinho",
    // Dutch
    "kopen",
    "bestellen",
    "contact",
    "starten",
    "registreren",
    "boeken",
    "gratis",
    "proberen",
    "downloaden",
    "abonneren",
    "winkelwagen",
    // Swedish
    "köpa",
    "beställa",
    "kontakta",
    "starta",
    "registrera",
    "boka",
    "gratis",
    "ladda ner",
    "prenumerera",
    // Polish
    "kupić",
    "zamówić",
    "kontakt",
    "zarejestrować",
    "zarezerwować",
    "bezpłatny",
    "pobierz",
    "subskrybować",
    // Turkish
    "satın al",
    "sipariş ver",
    "iletişim",
    "başla",
    "kayıt ol",
    "rezervasyon",
    "ücretsiz",
    "indir",
    "abone ol",
];

// ── Public types ────────────────────────────────────────────────────

/// Complete journey analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JourneyAnalysis {
    /// Overall journey score (0–100)
    pub score: u32,
    /// Grade (A–F)
    pub grade: String,
    /// Detected page intent
    pub page_intent: PageIntent,
    /// Per-dimension results
    pub entry_clarity: JourneyDimension,
    pub orientation: JourneyDimension,
    pub navigation: JourneyDimension,
    pub interaction: JourneyDimension,
    pub conversion: JourneyDimension,
    /// Friction points found along the journey
    pub friction_points: Vec<FrictionPoint>,
}

/// A scored journey dimension
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JourneyDimension {
    pub name: String,
    pub score: u32,
    pub weight: f64,
    pub summary: String,
}

/// A point of friction in the user journey
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FrictionPoint {
    pub step: String,
    pub severity: String,
    pub problem: String,
    pub impact: String,
    pub recommendation: String,
}

// ── Analysis entry point ────────────────────────────────────────────

/// Analyze user journey quality from the Accessibility Tree.
/// Runs purely on already-extracted AXTree data — no CDP calls needed.
pub fn analyze_journey(tree: &AXTree) -> JourneyAnalysis {
    info!("Analyzing user journey...");

    let page_intent = detect_page_intent(tree);
    let (w_entry, w_orient, w_nav, w_interact, w_convert) = page_intent.weights();

    let mut friction_points = Vec::new();

    let entry_clarity = analyze_entry_clarity(tree, &mut friction_points);
    let orientation = analyze_orientation(tree, &mut friction_points);
    let navigation = analyze_navigation(tree, &mut friction_points);
    let interaction = analyze_interaction(tree, &mut friction_points);
    let conversion = analyze_conversion(tree, &mut friction_points);

    let score = weighted_average_with_intent(&[
        (entry_clarity.score, w_entry),
        (orientation.score, w_orient),
        (navigation.score, w_nav),
        (interaction.score, w_interact),
        (conversion.score, w_convert),
    ]);

    let grade = match score {
        90..=100 => "A",
        80..=89 => "B",
        70..=79 => "C",
        60..=69 => "D",
        _ => "F",
    }
    .to_string();

    info!(
        "Journey analysis: score={}, intent={:?}, friction_points={}",
        score,
        page_intent,
        friction_points.len()
    );

    // Update dimensions with intent-adjusted weights for display
    let mut entry_clarity = entry_clarity;
    entry_clarity.weight = w_entry;
    let mut orientation = orientation;
    orientation.weight = w_orient;
    let mut navigation = navigation;
    navigation.weight = w_nav;
    let mut interaction = interaction;
    interaction.weight = w_interact;
    let mut conversion = conversion;
    conversion.weight = w_convert;

    JourneyAnalysis {
        score,
        grade,
        page_intent,
        entry_clarity,
        orientation,
        navigation,
        interaction,
        conversion,
        friction_points,
    }
}

// ── Dimension analyzers ─────────────────────────────────────────────

/// Entry Clarity: Is the purpose of this page immediately clear?
fn analyze_entry_clarity(tree: &AXTree, friction: &mut Vec<FrictionPoint>) -> JourneyDimension {
    let headings = tree.headings();
    let mut penalties = Vec::new();

    // H1 presence and clarity
    let h1s: Vec<_> = headings
        .iter()
        .filter(|h| h.heading_level() == Some(1))
        .collect();

    if h1s.is_empty() {
        penalties.push(45.0);
        friction.push(FrictionPoint {
            step: "Entry".into(),
            severity: "high".into(),
            problem: "Keine H1-Überschrift — Seitenzweck nicht sofort erkennbar".into(),
            impact: "Nutzer verstehen nicht, worum es auf dieser Seite geht".into(),
            recommendation: "Eine aussagekräftige H1-Überschrift setzen".into(),
        });
    } else if h1s.len() > 1 {
        penalties.push(15.0);
        friction.push(FrictionPoint {
            step: "Entry".into(),
            severity: "medium".into(),
            problem: format!("{} H1-Überschriften — unklarer Seitenfokus", h1s.len()),
            impact: "Nutzer wissen nicht, was der Hauptinhalt ist".into(),
            recommendation: "Genau eine H1-Überschrift pro Seite verwenden".into(),
        });
    } else {
        // Check H1 quality: very short or generic?
        if let Some(name) = h1s[0].name.as_deref() {
            if name.len() < 5 {
                penalties.push(20.0);
            }
        }
    }

    // Page title (check for title-like node in tree)
    let has_title = tree
        .iter()
        .any(|n| n.role.as_deref() == Some("RootWebArea") && n.name.is_some());
    if !has_title {
        penalties.push(20.0);
        friction.push(FrictionPoint {
            step: "Entry".into(),
            severity: "medium".into(),
            problem: "Kein Seitentitel erkannt".into(),
            impact: "Tab-Titel ist leer — Nutzer verlieren den Kontext beim Tabwechsel".into(),
            recommendation: "Aussagekräftigen <title> setzen".into(),
        });
    }

    // Early content: check if there's substantial text in the first portion of the tree
    let early_text_len: usize = tree
        .iter()
        .take(50)
        .filter(|n| matches!(n.role.as_deref(), Some("StaticText" | "heading")))
        .filter_map(|n| n.name.as_ref())
        .map(|n| n.len())
        .sum();

    if early_text_len < 30 {
        penalties.push(20.0);
        friction.push(FrictionPoint {
            step: "Entry".into(),
            severity: "medium".into(),
            problem: "Wenig sichtbarer Text im oberen Seitenbereich".into(),
            impact: "Nutzer erhalten keine sofortige Orientierung".into(),
            recommendation: "Relevanten Einleitungstext above-the-fold platzieren".into(),
        });
    }

    let score = journey_dimension_score(&penalties, 100.0);
    let summary = if score >= 85 {
        "Seitenzweck ist sofort klar erkennbar".into()
    } else if score >= 60 {
        "Einstieg grundsätzlich verständlich, aber verbesserungswürdig".into()
    } else {
        "Seitenzweck ist nicht auf den ersten Blick erkennbar".into()
    };

    JourneyDimension {
        name: "Entry Clarity".into(),
        score,
        weight: 0.20,
        summary,
    }
}

/// Orientation: Can the user tell where they are and where they can go?
fn analyze_orientation(tree: &AXTree, friction: &mut Vec<FrictionPoint>) -> JourneyDimension {
    let mut penalties = Vec::new();

    // Navigation landmark
    let has_nav = tree.iter().any(|n| n.role.as_deref() == Some("navigation"));
    if !has_nav {
        penalties.push(40.0);
        friction.push(FrictionPoint {
            step: "Orientation".into(),
            severity: "high".into(),
            problem: "Kein Navigationsbereich (<nav>) erkannt".into(),
            impact: "Nutzer können sich nicht orientieren und finden keine Hauptnavigation".into(),
            recommendation: "Hauptnavigation in ein <nav>-Element einschließen".into(),
        });
    }

    // Breadcrumbs or secondary nav hint
    let has_breadcrumb = tree.iter().any(|n| {
        let name = n.name.as_deref().unwrap_or("").to_lowercase();
        name.contains("breadcrumb")
            || n.role.as_deref() == Some("navigation") && name.contains("breadcrumb")
    });
    // Not penalized heavily, but bonus signals
    if !has_breadcrumb {
        penalties.push(10.0);
    }

    // Main landmark
    let has_main = tree.iter().any(|n| n.role.as_deref() == Some("main"));
    if !has_main {
        penalties.push(20.0);
        friction.push(FrictionPoint {
            step: "Orientation".into(),
            severity: "medium".into(),
            problem: "Kein Hauptinhaltsbereich (<main>) erkannt".into(),
            impact: "Screenreader-Nutzer können den Hauptinhalt nicht direkt anspringen".into(),
            recommendation: "Hauptinhalt in ein <main>-Element einschließen".into(),
        });
    }

    // Footer / complementary landmark
    let has_footer = tree
        .iter()
        .any(|n| n.role.as_deref() == Some("contentinfo"));
    if !has_footer {
        penalties.push(10.0);
    }

    // Heading structure: are there sub-sections?
    let heading_count = tree.headings().len();
    if heading_count < 2 {
        penalties.push(15.0);
        friction.push(FrictionPoint {
            step: "Orientation".into(),
            severity: "low".into(),
            problem: "Kaum Zwischenüberschriften zur Orientierung".into(),
            impact: "Nutzer können Inhalte nicht scannen".into(),
            recommendation: "Inhalte mit H2/H3-Überschriften gliedern".into(),
        });
    }

    let score = journey_dimension_score(&penalties, 100.0);
    let summary = if score >= 85 {
        "Gute Orientierung durch Navigation, Landmarks und Struktur".into()
    } else if score >= 60 {
        "Grundlegende Orientierung vorhanden, Struktur ausbaufähig".into()
    } else {
        "Mangelnde Orientierung — Navigation oder Landmarks fehlen".into()
    };

    JourneyDimension {
        name: "Orientation".into(),
        score,
        weight: 0.20,
        summary,
    }
}

/// Navigation: Are links clear, non-redundant, and well-structured?
fn analyze_navigation(tree: &AXTree, friction: &mut Vec<FrictionPoint>) -> JourneyDimension {
    let links = tree.links();
    let mut penalties = Vec::new();

    // Generic link labels
    let generic_count = links
        .iter()
        .filter(|l| {
            let name = l.name.as_deref().unwrap_or("").to_lowercase();
            let trimmed = name.trim();
            GENERIC_LINK_LABELS
                .iter()
                .any(|g| trimmed == *g || trimmed.starts_with(g))
        })
        .count();

    if generic_count > 0 {
        let p = saturating_penalty(generic_count as f64, 30.0, 8.0);
        penalties.push(p);
        if generic_count >= 3 {
            friction.push(FrictionPoint {
                step: "Navigation".into(),
                severity: "medium".into(),
                problem: format!(
                    "{} Links mit generischen Texten (\"mehr\", \"hier\")",
                    generic_count
                ),
                impact: "Nutzer können nicht unterscheiden, wohin Links führen".into(),
                recommendation: "Linktexte beschreibend formulieren, die das Ziel benennen".into(),
            });
        }
    }

    // Empty / unnamed links
    let empty_links = links
        .iter()
        .filter(|l| l.name.as_deref().is_none_or(|n| n.trim().is_empty()))
        .count();

    if empty_links > 0 {
        let p = saturating_penalty(empty_links as f64, 35.0, 5.0);
        penalties.push(p);
        if empty_links >= 2 {
            friction.push(FrictionPoint {
                step: "Navigation".into(),
                severity: "high".into(),
                problem: format!("{} Links ohne erkennbaren Text", empty_links),
                impact: "Screenreader-Nutzer erfahren nicht, was der Link tut".into(),
                recommendation: "Alle Links mit beschreibendem Text oder aria-label versehen"
                    .into(),
            });
        }
    }

    // Duplicate link texts pointing potentially different places
    let mut link_names: Vec<String> = links
        .iter()
        .filter_map(|l| l.name.as_deref())
        .filter(|n| !n.trim().is_empty())
        .map(|n| n.to_lowercase().trim().to_string())
        .collect();
    link_names.sort();
    let total_links = link_names.len();
    link_names.dedup();
    let duplicate_count = total_links.saturating_sub(link_names.len());

    if duplicate_count > 5 {
        let p = saturating_penalty((duplicate_count - 5) as f64, 20.0, 10.0);
        penalties.push(p);
        if duplicate_count > 10 {
            friction.push(FrictionPoint {
                step: "Navigation".into(),
                severity: "low".into(),
                problem: format!("{} doppelte Linktexte auf der Seite", duplicate_count),
                impact: "Gleiche Beschriftung für unterschiedliche Ziele verwirrt Nutzer".into(),
                recommendation:
                    "Linktexte eindeutig formulieren oder mit aria-label differenzieren".into(),
            });
        }
    }

    // Link density: too many links for page size
    if links.len() > 60 {
        let excess = (links.len() - 60) as f64;
        let p = saturating_penalty(excess, 15.0, 60.0);
        penalties.push(p);
    }

    let score = journey_dimension_score(&penalties, 100.0);
    let summary = if score >= 85 {
        "Links sind verständlich, eindeutig und gut strukturiert".into()
    } else if score >= 60 {
        "Navigation nutzbar, aber einige Links sind unklar oder redundant".into()
    } else {
        "Navigationsprobleme: unklare, leere oder redundante Links".into()
    };

    JourneyDimension {
        name: "Navigation".into(),
        score,
        weight: 0.25,
        summary,
    }
}

/// Interaction: Can users interact with controls effectively?
fn analyze_interaction(tree: &AXTree, friction: &mut Vec<FrictionPoint>) -> JourneyDimension {
    let buttons = tree.nodes_with_role("button");
    let form_controls = tree.form_controls();
    let mut penalties = Vec::new();

    // Buttons without accessible names
    let unnamed_buttons = buttons
        .iter()
        .filter(|b| b.name.as_deref().is_none_or(|n| n.trim().is_empty()))
        .count();

    if unnamed_buttons > 0 {
        let p = saturating_penalty(unnamed_buttons as f64, 40.0, 3.0);
        penalties.push(p);
        friction.push(FrictionPoint {
            step: "Interaction".into(),
            severity: "high".into(),
            problem: format!("{} Buttons ohne erkennbare Beschriftung", unnamed_buttons),
            impact: "Nutzer wissen nicht, was ein Button auslöst".into(),
            recommendation: "Alle Buttons mit beschreibendem Text oder aria-label versehen".into(),
        });
    }

    // Form controls without labels
    let unlabeled_forms = form_controls
        .iter()
        .filter(|fc| fc.name.as_deref().is_none_or(|n| n.trim().is_empty()))
        .count();

    if unlabeled_forms > 0 {
        let p = saturating_penalty(unlabeled_forms as f64, 35.0, 3.0);
        penalties.push(p);
        if unlabeled_forms >= 2 {
            friction.push(FrictionPoint {
                step: "Interaction".into(),
                severity: "high".into(),
                problem: format!("{} Formularfelder ohne Label", unlabeled_forms),
                impact: "Nutzer wissen nicht, welche Eingabe erwartet wird".into(),
                recommendation:
                    "Jedes Formularfeld mit sichtbarem <label> oder aria-label verbinden".into(),
            });
        }
    }

    // Generic button labels
    let generic_buttons = buttons
        .iter()
        .filter(|b| {
            let name = b.name.as_deref().unwrap_or("").to_lowercase();
            let trimmed = name.trim();
            matches!(
                trimmed,
                "ok" | "submit"
                    | "go"
                    | "next"
                    | "click"
                    | "button"
                    | "send"
                    // German
                    | "senden"
                    | "absenden"
                    | "los"
                    | "weiter"
                    | "schicken"
                    // French
                    | "envoyer"
                    | "suivant"
                    | "continuer"
                    | "valider"
                    // Spanish
                    | "enviar"
                    | "siguiente"
                    | "continuar"
                    | "aceptar"
                    // Italian
                    | "invia"
                    | "successivo"
                    | "continua"
                    | "conferma"
                    // Portuguese
                    | "próximo"
                    | "confirmar"
                    // Dutch
                    | "verzenden"
                    | "volgende"
                    | "doorgaan"
                    | "bevestigen"
                    // Swedish
                    | "skicka"
                    | "nästa"
                    | "fortsätt"
                    // Polish
                    | "wyślij"
                    | "następny"
                    | "kontynuuj"
                    // Turkish
                    | "gönder"
                    | "ileri"
                    | "devam"
            )
        })
        .count();

    if generic_buttons >= 2 {
        let p = saturating_penalty(generic_buttons as f64, 20.0, 5.0);
        penalties.push(p);
        friction.push(FrictionPoint {
            step: "Interaction".into(),
            severity: "low".into(),
            problem: format!(
                "{} Buttons mit generischen Labels (\"OK\", \"Submit\")",
                generic_buttons
            ),
            impact: "Kontext der Aktion ist nicht klar".into(),
            recommendation:
                "Buttons mit handlungsbeschreibenden Texten benennen (z. B. \"Nachricht senden\")"
                    .into(),
        });
    }

    let score = journey_dimension_score(&penalties, 100.0);
    let summary = if score >= 85 {
        "Interaktive Elemente sind klar beschriftet und bedienbar".into()
    } else if score >= 60 {
        "Grundlegende Interaktion möglich, aber Beschriftungen teilweise unklar".into()
    } else {
        "Erhebliche Interaktionsprobleme: unbeschriftete Buttons oder Formularfelder".into()
    };

    JourneyDimension {
        name: "Interaction".into(),
        score,
        weight: 0.20,
        summary,
    }
}

/// Conversion: Can the user reach and complete the page's primary goal?
fn analyze_conversion(tree: &AXTree, friction: &mut Vec<FrictionPoint>) -> JourneyDimension {
    let buttons = tree.nodes_with_role("button");
    let links = tree.links();
    let mut penalties = Vec::new();

    // CTA presence
    let cta_found = buttons.iter().chain(links.iter()).any(|n| {
        let name = n.name.as_deref().unwrap_or("").to_lowercase();
        CTA_KEYWORDS.iter().any(|kw| name.contains(kw))
    });

    if !cta_found {
        penalties.push(40.0);
        friction.push(FrictionPoint {
            step: "Conversion".into(),
            severity: "high".into(),
            problem: "Kein erkennbarer Call-to-Action auf der Seite".into(),
            impact: "Nutzer haben keine klare Handlungsaufforderung".into(),
            recommendation: "Einen primären CTA definieren und prominent platzieren".into(),
        });
    }

    // Are there interactive blockers before CTA? (modals, cookie banners hinted by roles)
    let has_dialog = tree
        .iter()
        .any(|n| matches!(n.role.as_deref(), Some("dialog" | "alertdialog")));
    if has_dialog {
        penalties.push(15.0);
        friction.push(FrictionPoint {
            step: "Conversion".into(),
            severity: "medium".into(),
            problem: "Dialog/Overlay erkannt, der den Nutzerpfad unterbrechen kann".into(),
            impact: "Cookie-Banner oder Modals können den CTA verdecken".into(),
            recommendation:
                "Sicherstellen, dass Overlays einfach schließbar sind und den CTA nicht blockieren"
                    .into(),
        });
    }

    // Form complexity: too many fields can reduce conversion
    let form_controls = tree.form_controls();
    if form_controls.len() > 10 {
        let excess = (form_controls.len() - 10) as f64;
        let p = saturating_penalty(excess, 20.0, 10.0);
        penalties.push(p);
        if form_controls.len() > 15 {
            friction.push(FrictionPoint {
                step: "Conversion".into(),
                severity: "medium".into(),
                problem: format!("{} Formularfelder — hohe Eingabehürde", form_controls.len()),
                impact: "Komplexe Formulare reduzieren die Abschlussrate".into(),
                recommendation:
                    "Formular auf wesentliche Felder reduzieren oder in Schritte aufteilen".into(),
            });
        }
    }

    // CTA competition: multiple equally strong CTAs dilute focus
    let cta_count: usize = buttons
        .iter()
        .chain(links.iter())
        .filter(|n| {
            let name = n.name.as_deref().unwrap_or("").to_lowercase();
            CTA_KEYWORDS.iter().any(|kw| name.contains(kw))
        })
        .count();

    if cta_count > 5 {
        let p = saturating_penalty((cta_count - 5) as f64, 15.0, 5.0);
        penalties.push(p);
    }

    let score = journey_dimension_score(&penalties, 100.0);
    let summary = if score >= 85 {
        "Klarer Conversion-Pfad mit erreichbarem CTA".into()
    } else if score >= 60 {
        "Conversion-Pfad vorhanden, aber durch Hindernisse beeinträchtigt".into()
    } else {
        "Kein klarer Conversion-Pfad erkennbar".into()
    };

    JourneyDimension {
        name: "Conversion".into(),
        score,
        weight: 0.15,
        summary,
    }
}