auditmysite 0.9.27

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
//! Browser installer - explicit Chrome for Testing download
//!
//! Downloads to ~/.auditmysite/browsers/ — only when explicitly requested
//! via `auditmysite browser install`. No auto-download.

use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;

use tracing::{info, warn};

use super::types::InstallTarget;
use crate::error::{AuditError, Result};

/// Chrome for Testing API endpoint
const CFT_API_URL: &str =
    "https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json";

/// Fallback version if API is unavailable
const FALLBACK_VERSION: &str = "131.0.6778.108";

/// Browser installer
pub struct BrowserInstaller;

impl BrowserInstaller {
    /// Install a browser to ~/.auditmysite/browsers/
    pub async fn install(
        target: InstallTarget,
        version: Option<&str>,
        force: bool,
    ) -> Result<PathBuf> {
        let base_dir = Self::browsers_dir()?;
        let (target_name, channel) = match target {
            InstallTarget::ChromeForTesting => ("chrome-for-testing", "chrome"),
            InstallTarget::HeadlessShell => ("headless-shell", "chrome-headless-shell"),
        };

        let target_dir = base_dir.join(target_name);

        // Check if already installed
        if target_dir.exists() && !force {
            let existing_version = fs::read_to_string(target_dir.join("version.txt"))
                .unwrap_or_default()
                .trim()
                .to_string();

            if !existing_version.is_empty() {
                println!("Already installed: {} v{}", target_name, existing_version);
                println!("Use --force to reinstall.");

                let binary_path = Self::binary_path(&target_dir, target);
                return Ok(binary_path);
            }
        }

        // Resolve version
        let resolved_version = if let Some(v) = version {
            v.to_string()
        } else {
            Self::fetch_latest_version().await.unwrap_or_else(|e| {
                warn!("Could not fetch latest version from API: {}", e);
                info!("Using fallback version: {}", FALLBACK_VERSION);
                FALLBACK_VERSION.to_string()
            })
        };

        println!("Installing {} v{}...", target_name, resolved_version);

        // Build download URL
        let download_url = Self::build_download_url(&resolved_version, channel);
        let archive_name = format!("{}.zip", target_name);

        // Prepare directory
        if target_dir.exists() {
            fs::remove_dir_all(&target_dir).map_err(|e| AuditError::BrowserLaunchFailed {
                reason: format!("Failed to clean existing install: {}", e),
            })?;
        }
        fs::create_dir_all(&target_dir).map_err(|e| AuditError::BrowserLaunchFailed {
            reason: format!("Failed to create install directory: {}", e),
        })?;

        let archive_path = target_dir.join(&archive_name);

        // Download
        println!("Downloading from: {}", download_url);
        Self::download_file(&download_url, &archive_path).await?;

        // Extract
        println!("Extracting...");
        Self::extract_archive(&archive_path, &target_dir)?;

        // Clean up archive
        fs::remove_file(&archive_path).ok();

        // Write version file
        fs::write(target_dir.join("version.txt"), &resolved_version).ok();

        // Make executable
        let binary_path = Self::binary_path(&target_dir, target);
        Self::set_executable(&binary_path)?;

        // macOS: remove quarantine attribute
        #[cfg(target_os = "macos")]
        Self::remove_quarantine(&target_dir);

        println!(
            "Installed {} v{} at {}",
            target_name,
            resolved_version,
            binary_path.display()
        );

        Ok(binary_path)
    }

    /// Remove a managed browser installation
    pub fn remove(target: InstallTarget) -> Result<()> {
        let base_dir = Self::browsers_dir()?;
        let target_name = match target {
            InstallTarget::ChromeForTesting => "chrome-for-testing",
            InstallTarget::HeadlessShell => "headless-shell",
        };
        let target_dir = base_dir.join(target_name);

        if target_dir.exists() {
            fs::remove_dir_all(&target_dir).map_err(|e| AuditError::BrowserLaunchFailed {
                reason: format!("Failed to remove {}: {}", target_name, e),
            })?;
            println!("Removed {}", target_name);
        } else {
            println!("{} is not installed", target_name);
        }

        Ok(())
    }

    /// Remove all managed browsers including legacy
    pub fn remove_all() -> Result<()> {
        Self::remove(InstallTarget::ChromeForTesting)?;
        Self::remove(InstallTarget::HeadlessShell)?;

        // Also clean up legacy location
        if let Ok(legacy_dir) = Self::legacy_chromium_dir() {
            if legacy_dir.exists() {
                fs::remove_dir_all(&legacy_dir).ok();
                println!("Removed legacy chromium install");
            }
        }

        Ok(())
    }

    /// Fetch latest stable version from Chrome for Testing API
    async fn fetch_latest_version() -> Result<String> {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(10))
            .build()
            .map_err(|e| AuditError::BrowserLaunchFailed {
                reason: format!("HTTP client error: {}", e),
            })?;

        let resp: serde_json::Value = client
            .get(CFT_API_URL)
            .send()
            .await
            .map_err(|e| AuditError::BrowserLaunchFailed {
                reason: format!("Failed to fetch versions: {}", e),
            })?
            .json()
            .await
            .map_err(|e| AuditError::BrowserLaunchFailed {
                reason: format!("Failed to parse versions: {}", e),
            })?;

        resp["channels"]["Stable"]["version"]
            .as_str()
            .map(|s| s.to_string())
            .ok_or_else(|| AuditError::BrowserLaunchFailed {
                reason: "Could not find stable version in API response".to_string(),
            })
    }

    fn build_download_url(version: &str, channel: &str) -> String {
        let platform = Self::platform_string();
        format!(
            "https://storage.googleapis.com/chrome-for-testing-public/{}/{}/{}-{}.zip",
            version, platform, channel, platform
        )
    }

    fn platform_string() -> &'static str {
        if cfg!(target_os = "macos") {
            if cfg!(target_arch = "aarch64") {
                "mac-arm64"
            } else {
                "mac-x64"
            }
        } else if cfg!(target_os = "linux") {
            "linux64"
        } else {
            "win64"
        }
    }

    fn binary_path(target_dir: &Path, target: InstallTarget) -> PathBuf {
        let platform = Self::platform_string();

        match target {
            InstallTarget::ChromeForTesting => {
                let inner_dir = format!("chrome-{}", platform);
                if cfg!(target_os = "macos") {
                    target_dir
                        .join(&inner_dir)
                        .join("Google Chrome for Testing.app")
                        .join("Contents")
                        .join("MacOS")
                        .join("Google Chrome for Testing")
                } else if cfg!(target_os = "windows") {
                    target_dir.join(&inner_dir).join("chrome.exe")
                } else {
                    target_dir.join(&inner_dir).join("chrome")
                }
            }
            InstallTarget::HeadlessShell => {
                let inner_dir = format!("chrome-headless-shell-{}", platform);
                if cfg!(target_os = "windows") {
                    target_dir
                        .join(&inner_dir)
                        .join("chrome-headless-shell.exe")
                } else {
                    target_dir.join(&inner_dir).join("chrome-headless-shell")
                }
            }
        }
    }

    fn browsers_dir() -> Result<PathBuf> {
        let dir = dirs::home_dir()
            .ok_or_else(|| AuditError::ConfigError("Could not find home directory".to_string()))?
            .join(".auditmysite")
            .join("browsers");
        Ok(dir)
    }

    fn legacy_chromium_dir() -> Result<PathBuf> {
        let dir = dirs::home_dir()
            .ok_or_else(|| AuditError::ConfigError("Could not find home directory".to_string()))?
            .join(".auditmysite")
            .join("chromium");
        Ok(dir)
    }

    async fn download_file(url: &str, dest: &Path) -> Result<()> {
        use futures::StreamExt;

        let max_retries = 2;
        let mut last_error = None;
        let mut response = None;

        for attempt in 0..=max_retries {
            if attempt > 0 {
                let delay = Duration::from_secs(2u64.pow(attempt as u32));
                warn!(
                    "Retrying download (attempt {}/{}, backoff {:?})",
                    attempt + 1,
                    max_retries + 1,
                    delay
                );
                tokio::time::sleep(delay).await;
            }

            match reqwest::get(url).await {
                Ok(resp) if resp.status().is_success() => {
                    response = Some(resp);
                    break;
                }
                Ok(resp) => {
                    last_error = Some(format!("HTTP {}", resp.status()));
                }
                Err(e) => {
                    last_error = Some(format!("Download failed: {}", e));
                }
            }
        }

        let response = response.ok_or_else(|| AuditError::BrowserLaunchFailed {
            reason: last_error.unwrap_or_else(|| "Download failed after retries".to_string()),
        })?;

        let total_size = response.content_length().unwrap_or(0);
        let mut downloaded = 0u64;

        let mut file = fs::File::create(dest).map_err(|e| AuditError::BrowserLaunchFailed {
            reason: format!("Failed to create file: {}", e),
        })?;

        let mut stream = response.bytes_stream();

        while let Some(chunk_result) = stream.next().await {
            let chunk = chunk_result.map_err(|e| AuditError::BrowserLaunchFailed {
                reason: format!("Download chunk failed: {}", e),
            })?;

            file.write_all(&chunk)
                .map_err(|e| AuditError::BrowserLaunchFailed {
                    reason: format!("Write failed: {}", e),
                })?;

            downloaded += chunk.len() as u64;

            if total_size > 0 {
                let percent = (downloaded * 100).checked_div(total_size).unwrap_or(0);
                if downloaded % (total_size / 10).max(1) < chunk.len() as u64 {
                    println!(
                        "  {}% ({}/{} MB)",
                        percent,
                        downloaded / 1_000_000,
                        total_size / 1_000_000
                    );
                }
            }
        }

        Ok(())
    }

    fn extract_archive(archive_path: &Path, dest_dir: &Path) -> Result<()> {
        let file = fs::File::open(archive_path).map_err(|e| AuditError::BrowserLaunchFailed {
            reason: format!("Failed to open archive: {}", e),
        })?;

        let mut archive =
            zip::ZipArchive::new(file).map_err(|e| AuditError::BrowserLaunchFailed {
                reason: format!("Failed to read zip: {}", e),
            })?;

        let total_files = archive.len();

        for i in 0..total_files {
            let mut file = archive
                .by_index(i)
                .map_err(|e| AuditError::BrowserLaunchFailed {
                    reason: format!("Failed to read file from archive: {}", e),
                })?;

            let outpath = match file.enclosed_name() {
                Some(path) => dest_dir.join(path),
                None => continue,
            };

            if file.name().ends_with('/') {
                fs::create_dir_all(&outpath).ok();
            } else {
                if let Some(p) = outpath.parent() {
                    fs::create_dir_all(p).ok();
                }
                let mut outfile =
                    fs::File::create(&outpath).map_err(|e| AuditError::BrowserLaunchFailed {
                        reason: format!("Failed to create extracted file: {}", e),
                    })?;
                std::io::copy(&mut file, &mut outfile).map_err(|e| {
                    AuditError::BrowserLaunchFailed {
                        reason: format!("Failed to extract file: {}", e),
                    }
                })?;
            }
        }

        Ok(())
    }

    fn set_executable(path: &Path) -> Result<()> {
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            if path.exists() {
                let mut perms = fs::metadata(path)
                    .map_err(|e| AuditError::BrowserLaunchFailed {
                        reason: format!("Failed to get permissions: {}", e),
                    })?
                    .permissions();
                perms.set_mode(0o755);
                fs::set_permissions(path, perms).map_err(|e| AuditError::BrowserLaunchFailed {
                    reason: format!("Failed to set permissions: {}", e),
                })?;
            }
        }
        Ok(())
    }

    #[cfg(target_os = "macos")]
    fn remove_quarantine(dir: &Path) {
        use std::process::Command;
        // Remove macOS quarantine attribute recursively
        let _ = Command::new("xattr")
            .args(["-rd", "com.apple.quarantine"])
            .arg(dir)
            .output();
    }
}

// ── Legacy compatibility ──────────────────────────────────────

/// Legacy: Chromium installer (redirects to new installer)
pub struct ChromiumInstaller;

impl ChromiumInstaller {
    /// Legacy: Ensure Chromium is available
    /// Now uses the new BrowserInstaller but maintains the old API
    pub async fn ensure_chromium() -> Result<PathBuf> {
        // Check new location first
        let browsers_dir = dirs::home_dir()
            .ok_or_else(|| AuditError::ConfigError("Could not find home directory".to_string()))?
            .join(".auditmysite")
            .join("browsers")
            .join("chrome-for-testing");

        if browsers_dir.exists() {
            let binary =
                BrowserInstaller::binary_path(&browsers_dir, InstallTarget::ChromeForTesting);
            if binary.exists() {
                return Ok(binary);
            }
        }

        // Check legacy location
        let legacy_path = Self::local_chromium_path()?;
        if legacy_path.exists() {
            return Ok(legacy_path);
        }

        // Install to new location
        BrowserInstaller::install(InstallTarget::ChromeForTesting, None, false).await
    }

    fn local_chromium_path() -> Result<PathBuf> {
        let cache_dir = dirs::home_dir()
            .ok_or_else(|| AuditError::ConfigError("Could not find home directory".to_string()))?
            .join(".auditmysite")
            .join("chromium");

        Ok(if cfg!(target_os = "macos") {
            cache_dir
                .join("chrome-mac")
                .join("Chromium.app")
                .join("Contents")
                .join("MacOS")
                .join("Chromium")
        } else if cfg!(target_os = "linux") {
            cache_dir.join("chrome-linux").join("chrome")
        } else {
            cache_dir.join("chrome-win").join("chrome.exe")
        })
    }
}