auditmysite 1.0.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
//! Audit mode runners.
//!
//! Implements the three top-level audit modes (single URL, batch, comparison)
//! plus the interactive sitemap-suggestion flow. Extracted from main.rs.

use std::io::{self, IsTerminal};
use std::sync::Arc;

use colored::Colorize;
use dialoguer::Select;
use indicatif::{ProgressBar, ProgressStyle};
use tracing::info;

use auditmysite::audit::normalize;
use auditmysite::audit::{
    analyze_crawl_links, analyze_sitemap_diagnostics, cache_matches_signature,
    compute_batch_verdict, compute_verdict, crawl_site, hydrate_cached_report, load_artifacts,
    parse_sitemap, read_url_file, run_concurrent_batch, run_single_audit, to_audit_report,
    BatchConfig, CrawlResult, PipelineConfig, Verdict,
};
use auditmysite::browser::{BrowserManager, BrowserOptions};
use auditmysite::cli::{Args, OutputFormat, RequestMode};
use auditmysite::error::{AuditError, Result};
use auditmysite::util::truncate_url;

use crate::plan::{print_batch_audit_plan, print_single_audit_plan};
use crate::report_writers::{
    output_batch_as_single_reports, output_batch_report, output_screen_reader_sidecar,
    output_single_report,
};
use crate::sitemap_suggest::{
    check_url_reachable, discover_populated_sitemap, looks_like_base_url,
};

const BOT_USER_AGENT: &str = concat!(
    "auditmysite/",
    env!("CARGO_PKG_VERSION"),
    " (+https://github.com/casoon/auditmysite)"
);

pub async fn run_single_mode(
    args: &Args,
    config: &Option<auditmysite::cli::Config>,
) -> Result<Verdict> {
    let url = args
        .url
        .as_ref()
        .ok_or_else(|| AuditError::ConfigError("URL required".to_string()))?;

    if let Some(batch_verdict) = maybe_offer_sitemap_scan(args, url, config).await? {
        return Ok(batch_verdict);
    }

    print_single_audit_plan(args, url);

    // Quick reachability check before spinning up a browser
    check_url_reachable(url, args.quiet).await?;

    info!("Starting audit for: {}", url);

    if args.reuse_cache && !args.force_refresh {
        let expected_signature = PipelineConfig::from(args).audit_signature();
        match load_artifacts(url)? {
            Some(cached) if cache_matches_signature(&cached.meta, &expected_signature) => {
                if !args.quiet {
                    println!(
                        "{} {}",
                        "Cache hit:".green().bold(),
                        "using cached audit artifacts".dimmed()
                    );
                }

                let verdict_cfg = config
                    .as_ref()
                    .map(|c| c.effective_verdict_config())
                    .unwrap_or_default();
                // The verdict always derives from the stored NormalizedReport,
                // so it is identical regardless of --format (#404).
                let verdict_result = compute_verdict(&cached.audit, &verdict_cfg);

                // Prefer the full cached report so every module section renders
                // faithfully; fall back to the lossy reconstruction only for
                // legacy entries written before report.json existed (#404).
                let mut report = cached
                    .report
                    .clone()
                    .unwrap_or_else(|| to_audit_report(&cached, &args.lang));
                // screen_reader_audit is #[serde(skip)] and therefore absent from
                // the persisted report — rebuild it from the cached AXTree so the
                // cached report renders the same sections as a fresh run (#404).
                hydrate_cached_report(&mut report, &cached.snapshot, &args.lang);

                match args.effective_format() {
                    OutputFormat::Json => {
                        // Build the JSON from the hydrated full report (not the
                        // normalized-only `cached.audit`) so `detail.modules`
                        // carries the same module data the PDF renders (#404) —
                        // a cache hit must not drop the module blob.
                        output_single_report(&report, args, Some(&verdict_result))?;
                        output_screen_reader_sidecar(&report, args)?;
                    }
                    OutputFormat::Table
                    | OutputFormat::Pdf
                    | OutputFormat::Ai
                    | OutputFormat::Summary => {
                        output_single_report(&report, args, Some(&verdict_result))?;
                    }
                }
                print_verdict(&verdict_result, args.quiet);
                return Ok(verdict_result.verdict);
            }
            Some(_) if !args.quiet => {
                println!(
                    "{} {}",
                    "Cache skipped:".yellow().bold(),
                    "cached artifacts were produced with a different audit configuration — \
                     running a fresh audit"
                        .dimmed()
                );
            }
            _ => {}
        }
    }

    let browser_options = BrowserOptions {
        chrome_path: args.chrome_path.clone(),
        headless: true,
        disable_gpu: true,
        no_sandbox: args.no_sandbox,
        disable_images: args.disable_images,
        window_size: (1920, 1080),
        timeout_secs: args.effective_timeout(),
        verbose: args.verbose,
        user_agent_override: (args.request_mode == RequestMode::Bot)
            .then(|| BOT_USER_AGENT.to_string()),
    };

    if !args.quiet {
        println!("{}", "Starting browser...".dimmed());
    }
    let browser = BrowserManager::with_options(browser_options).await?;

    if !args.quiet {
        println!(
            "{} Chrome {} ({})",
            "Found:".green().bold(),
            browser.chrome_version().unwrap_or("unknown version"),
            browser.chrome_path().display()
        );
        println!("{} {}", "Auditing:".cyan().bold(), url);
    }

    let pipeline_config = PipelineConfig::from(args);
    let audit_result = run_single_audit(url, &browser, &pipeline_config).await;
    let close_result = browser.close().await;
    let mut report = audit_result?;
    close_result?;

    // Evaluate performance budgets from config
    if let Some(ref cfg) = *config {
        if !cfg.budgets.is_empty() {
            report.experience.budget_violations =
                auditmysite::audit::evaluate_budgets(&report, &cfg.budgets);
            if !report.experience.budget_violations.is_empty() && !args.quiet {
                use auditmysite::audit::BudgetSeverity;
                let errors = report
                    .experience
                    .budget_violations
                    .iter()
                    .filter(|v| v.severity == BudgetSeverity::Error)
                    .count();
                let warnings = report
                    .experience
                    .budget_violations
                    .iter()
                    .filter(|v| v.severity == BudgetSeverity::Warning)
                    .count();
                println!(
                    "{} {}{}: {} Error{}, {} Warning{}",
                    "Budget:".yellow().bold(),
                    report.experience.budget_violations.len(),
                    if report.experience.budget_violations.len() == 1 {
                        " violation"
                    } else {
                        " violations"
                    },
                    errors,
                    if errors == 1 { "" } else { "s" },
                    warnings,
                    if warnings == 1 { "" } else { "s" },
                );
            }
        }
    }

    let normalized = normalize(&report).normalized;
    let verdict_cfg = config
        .as_ref()
        .map(|c| c.effective_verdict_config())
        .unwrap_or_default();
    let verdict_result = compute_verdict(&normalized, &verdict_cfg);
    output_single_report(&report, args, Some(&verdict_result))?;
    print_verdict(&verdict_result, args.quiet);
    Ok(verdict_result.verdict)
}

async fn maybe_offer_sitemap_scan(
    args: &Args,
    url: &str,
    config: &Option<auditmysite::cli::Config>,
) -> Result<Option<Verdict>> {
    if args.no_sitemap_suggest {
        return Ok(None);
    }
    if !looks_like_base_url(url) {
        return Ok(None);
    }

    if !args.quiet {
        print!("{} ", "Checking for sitemap...".dimmed());
        let _ = std::io::Write::flush(&mut std::io::stdout());
    }

    let Some((sitemap_url, url_count)) = discover_populated_sitemap(url).await? else {
        if !args.quiet {
            println!();
        }
        return Ok(None);
    };

    if !args.quiet {
        println!();
    }

    if args.prefer_sitemap {
        let batch_args = suggested_sitemap_batch_args(args, sitemap_url);
        return run_batch_mode(&batch_args, config).await.map(Some);
    }

    if args.quiet {
        return Ok(None);
    }

    // dialoguer opens /dev/tty directly — only stdin needs to be a terminal
    if !io::stdin().is_terminal() {
        println!();
        println!("{}", "Sitemap found".cyan().bold());
        println!(
            "  {} {} ({} URLs)",
            "Source:".dimmed(),
            sitemap_url,
            url_count
        );
        println!(
            "  {}",
            "Non-interactive run: only the specified single URL will be audited. Use --prefer-sitemap or --sitemap for a full scan."
                .dimmed()
        );
        println!();
        return Ok(None);
    }

    println!();
    println!("{}", "Sitemap found".cyan().bold());
    println!(
        "  {} {} ({} URLs)",
        "Source:".dimmed(),
        sitemap_url,
        url_count
    );
    println!(
        "  {}",
        "For a base URL, a full sitemap scan is often more useful than just the homepage.".dimmed()
    );
    println!();

    let sample_label =
        "Sample scan (20 URLs) — average across pages, good for template issues".to_string();
    let full_label = format!("Scan sitemap (all {} URLs)", url_count);
    let items = vec![
        "Check single URL (homepage)",
        sample_label.as_str(),
        full_label.as_str(),
    ];
    let selection = Select::new()
        .with_prompt("How would you like to proceed?")
        .items(&items)
        .default(0)
        .interact()
        .map_err(|e| AuditError::ConfigError(e.to_string()))?;

    if selection == 1 {
        let mut batch_args = suggested_sitemap_batch_args(args, sitemap_url);
        batch_args.max_pages = 20;
        println!();
        return run_batch_mode(&batch_args, config).await.map(Some);
    }

    if selection == 2 {
        let batch_args = suggested_sitemap_batch_args(args, sitemap_url);
        println!();
        return run_batch_mode(&batch_args, config).await.map(Some);
    }

    println!();
    Ok(None)
}

pub fn suggested_sitemap_batch_args(args: &Args, sitemap_url: String) -> Args {
    let mut batch_args = args.clone();
    batch_args.url = None;
    batch_args.sitemap = Some(sitemap_url);

    // A user who started a single-URL audit without an explicit format expects a
    // generated report file even when switching into sitemap mode interactively.
    if batch_args.format.is_none() {
        batch_args.format = Some(OutputFormat::Pdf);
    }

    batch_args
}

pub async fn run_batch_mode(
    args: &Args,
    config: &Option<auditmysite::cli::Config>,
) -> Result<Verdict> {
    let mut crawl_result: Option<CrawlResult> = None;

    let url_source: &str;
    let urls = if let Some(ref sitemap_url) = args.sitemap {
        url_source = "sitemap";
        if !args.quiet {
            println!("{} {}", "Fetching sitemap:".cyan().bold(), sitemap_url);
        }
        parse_sitemap(sitemap_url).await?
    } else if args.crawl {
        url_source = "crawl";
        let seed_url = args
            .url
            .as_deref()
            .ok_or_else(|| AuditError::ConfigError("No crawl seed URL specified".to_string()))?;
        if !args.quiet {
            println!("{} {}", "Crawling site:".cyan().bold(), seed_url);
        }
        let crawl = crawl_site(seed_url, args.max_pages, args.crawl_depth).await?;
        if !args.quiet {
            println!(
                "{} {} pages discovered at depth <= {}",
                "Discovered:".cyan().bold(),
                crawl.pages.len(),
                args.crawl_depth
            );
        }
        let urls = crawl.urls();
        crawl_result = Some(crawl);
        urls
    } else if let Some(ref url_file) = args.url_file {
        url_source = "url_file";
        if !args.quiet {
            println!(
                "{} {}",
                "Reading URL file:".cyan().bold(),
                url_file.display()
            );
        }
        read_url_file(url_file.to_str().unwrap_or(""))?
    } else {
        return Err(AuditError::ConfigError(
            "No batch source specified".to_string(),
        ));
    };

    if urls.is_empty() {
        if !args.quiet {
            println!("{} No URLs found to audit.", "Warning:".yellow().bold());
        }
        return Ok(Verdict::Warn);
    }

    let total_discovered = urls.len();
    let total_urls = if args.max_pages > 0 {
        args.max_pages.min(total_discovered)
    } else {
        total_discovered
    };

    let sample = auditmysite::audit::SampleMetadata {
        source: url_source.to_string(),
        total_discovered,
        audited: total_urls,
        sample_limit: (args.max_pages > 0).then_some(args.max_pages),
        selection: if total_urls < total_discovered {
            "first_n".to_string()
        } else {
            "all".to_string()
        },
        is_sample: total_urls < total_discovered,
    };

    let audited_urls: Vec<String> = urls.iter().take(total_urls).cloned().collect();

    if !args.quiet {
        if sample.is_sample {
            println!(
                "{} auditing {} of {} discovered URLs ({} order, first {})",
                "Sample:".yellow().bold(),
                total_urls,
                total_discovered,
                url_source,
                total_urls
            );
        }
        println!(
            "{} {} URLs with {} parallel workers\n",
            "Auditing:".cyan().bold(),
            total_urls,
            args.effective_concurrency()
        );
        print_batch_audit_plan(args, total_urls);
    }

    let batch_config = BatchConfig::from(args);

    let progress_bar = if !args.quiet {
        let pb = ProgressBar::new(total_urls as u64);
        pb.set_style(
            ProgressStyle::default_bar()
                .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta}) {msg}")
                .expect("Invalid template")
                .progress_chars("#>-"),
        );
        Some(pb)
    } else {
        None
    };

    #[allow(clippy::type_complexity)]
    let progress: Option<Arc<dyn Fn(usize, usize, &str, Option<&str>) + Send + Sync>> =
        if let Some(ref pb) = progress_bar {
            let pb_clone = pb.clone();
            Some(Arc::new(move |current, _total, url, error| {
                pb_clone.set_position(current as u64);
                if let Some(err) = error {
                    pb_clone.println(format!("  ✗ {url}\n    {err}"));
                } else {
                    pb_clone.set_message(truncate_url(url, 50));
                }
            }))
        } else {
            None
        };

    let mut batch_report = run_concurrent_batch(urls, &batch_config, progress).await?;
    batch_report = batch_report.with_sample(sample);

    if url_source == "sitemap" {
        let diagnostics = analyze_sitemap_diagnostics(&audited_urls, &batch_report.reports).await;
        if !args.quiet {
            println!(
                "{} {} URLs checked, {} sitemap issues",
                "Sitemap check:".cyan().bold(),
                diagnostics.checked_urls,
                diagnostics.http_issues.len()
                    + diagnostics.orphan_sitemap_urls.len()
                    + diagnostics.linked_not_in_sitemap.len()
            );
        }
        batch_report = batch_report.with_sitemap_diagnostics(diagnostics);
    }

    if let Some(ref crawl) = crawl_result {
        let diagnostics = analyze_crawl_links(crawl).await;
        if !args.quiet {
            println!(
                "{} {} internal links checked, {} broken",
                "Link check:".cyan().bold(),
                diagnostics.checked_internal_links,
                diagnostics.broken_internal_links.len()
            );
        }
        batch_report = batch_report.with_crawl_diagnostics(diagnostics);
    }

    if let Some(pb) = progress_bar {
        pb.finish_with_message("Complete");
    }

    if !args.quiet {
        println!(
            "\n{} {}/{} passed, {} violations found in {}ms",
            "Results:".green().bold(),
            batch_report.summary.passed,
            batch_report.summary.total_urls,
            batch_report.summary.total_violations,
            batch_report.total_duration_ms
        );
    }

    let verdict_cfg = config
        .as_ref()
        .map(|c| c.effective_verdict_config())
        .unwrap_or_default();
    let verdict_result = compute_batch_verdict(&batch_report.summary, &verdict_cfg);

    if args.per_page_reports {
        output_batch_as_single_reports(&batch_report, args)?;
        print_verdict(&verdict_result, args.quiet);
        return Ok(verdict_result.verdict);
    }

    output_batch_report(&batch_report, args, Some(&verdict_result))?;
    print_verdict(&verdict_result, args.quiet);
    Ok(verdict_result.verdict)
}

fn print_verdict(vr: &auditmysite::VerdictResult, quiet: bool) {
    if quiet {
        return;
    }
    let label = match vr.verdict {
        auditmysite::Verdict::Pass => "PASS".green().bold(),
        auditmysite::Verdict::Warn => "WARN".yellow().bold(),
        auditmysite::Verdict::Fail => "FAIL".red().bold(),
    };
    if vr.reasons.is_empty() {
        println!("\n{}", label);
    } else {
        println!("\n{} — {}", label, vr.reasons.join(", ").dimmed());
    }
}