auditmysite 1.7.2

Resource-efficient WCAG 2.2 AA 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
//! Content weight analyzer
//!
//! Analyzes page resources by type and provides optimization recommendations.

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

use crate::error::{AuditError, Result};

/// Content weight analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContentWeight {
    /// Total page size in bytes
    pub total_bytes: u64,
    /// Total transfer size (compressed) in bytes
    pub transfer_bytes: u64,
    /// Breakdown by resource type
    pub breakdown: ResourceBreakdown,
    /// Number of requests
    pub request_count: u32,
    /// Estimated carbon footprint for one page view based on transfer bytes.
    #[serde(default)]
    pub carbon: CarbonEstimate,
    /// Optimization recommendations
    pub recommendations: Vec<String>,
}

/// Sustainable Web Design-style transfer-byte carbon estimate.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CarbonEstimate {
    /// Estimated grams CO₂e for one page view.
    pub grams_co2e_per_view: f64,
    /// Human-readable efficiency rating.
    pub rating: String,
    /// Model description used for the estimate.
    pub model: String,
}

impl CarbonEstimate {
    /// Formats `grams_co2e_per_view` for display. Truncating a genuinely small
    /// but nonzero footprint to two decimals reads as "0.00 g" — i.e. "no
    /// footprint" — rather than "very small footprint" (same failure class as
    /// the root-cause share-percentage truncation, see
    /// `output/pdf/single_report.rs`). Near-zero values get a "< 0.01"
    /// qualifier instead; all other values keep the normal two-decimal format.
    pub fn format_grams(&self) -> String {
        if self.grams_co2e_per_view > 0.0 && self.grams_co2e_per_view < 0.005 {
            "< 0.01".to_string()
        } else {
            format!("{:.2}", self.grams_co2e_per_view)
        }
    }
}

/// Resource breakdown by type
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResourceBreakdown {
    /// HTML documents
    pub html: ResourceStats,
    /// CSS stylesheets
    pub css: ResourceStats,
    /// JavaScript files
    pub javascript: ResourceStats,
    /// Images (all formats)
    pub images: ResourceStats,
    /// Fonts
    pub fonts: ResourceStats,
    /// Media (video/audio)
    pub media: ResourceStats,
    /// Other resources
    pub other: ResourceStats,
}

/// Statistics for a resource type
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResourceStats {
    /// Number of resources
    pub count: u32,
    /// Total size in bytes
    pub bytes: u64,
    /// Transfer size (compressed) in bytes
    pub transfer_bytes: u64,
    /// Largest resource size
    pub largest_bytes: u64,
    /// Largest resource URL
    pub largest_url: Option<String>,
}

impl ContentWeight {
    /// Get compression ratio (transfer/total)
    pub fn compression_ratio(&self) -> f64 {
        if self.total_bytes == 0 {
            1.0
        } else {
            self.transfer_bytes as f64 / self.total_bytes as f64
        }
    }

    /// Check if page is considered "heavy"
    pub fn is_heavy(&self) -> bool {
        // > 3MB total is considered heavy
        self.total_bytes > 3_000_000
    }

    /// Get formatted total size
    pub fn formatted_total(&self) -> String {
        format_bytes(self.total_bytes)
    }

    /// Get formatted transfer size
    pub fn formatted_transfer(&self) -> String {
        format_bytes(self.transfer_bytes)
    }
}

/// Format bytes as human-readable string
pub(super) fn format_bytes(bytes: u64) -> String {
    if bytes >= 1_000_000 {
        format!("{:.1} MB", bytes as f64 / 1_000_000.0)
    } else if bytes >= 1_000 {
        format!("{:.1} KB", bytes as f64 / 1_000.0)
    } else {
        format!("{} B", bytes)
    }
}

/// Analyze content weight of a page
///
/// # Arguments
/// * `page` - The chromiumoxide Page to analyze
///
/// # Returns
/// * `Ok(ContentWeight)` - The analysis results
/// * `Err(AuditError)` - If analysis fails
pub async fn analyze_content_weight(page: &Page) -> Result<ContentWeight> {
    info!("Analyzing content weight...");

    // Get resource timing entries via JavaScript.
    // Include the navigation entry (main HTML document) which is not in 'resource' entries.
    let js_code = r#"
    (() => {
        const resources = performance.getEntriesByType('resource');
        const nav = performance.getEntriesByType('navigation')[0];
        const navEntry = nav ? [{
            name: nav.name,
            type: 'navigation',
            transferSize: nav.transferSize || 0,
            decodedSize: nav.decodedBodySize || 0,
            duration: nav.duration
        }] : [];
        return JSON.stringify([...navEntry, ...resources.map(r => ({
            name: r.name,
            type: r.initiatorType,
            transferSize: r.transferSize || 0,
            decodedSize: r.decodedBodySize || 0,
            duration: r.duration
        }))]);
    })()
    "#;

    let js_result = page
        .evaluate(js_code)
        .await
        .map_err(|e| AuditError::CdpError(format!("Resource analysis failed: {}", e)))?;

    let json_str = js_result.value().and_then(|v| v.as_str()).unwrap_or("[]");

    let resources: Vec<ResourceEntry> = serde_json::from_str(json_str).unwrap_or_default();

    let mut breakdown = ResourceBreakdown::default();
    let mut total_bytes: u64 = 0;
    let mut transfer_bytes: u64 = 0;
    let mut request_count: u32 = 0;

    for resource in &resources {
        request_count += 1;
        // decodedBodySize is 0 for cached resources and some font/image types;
        // fall back to transferSize so the breakdown doesn't silently show zero.
        let effective_size = if resource.decoded_size > 0 {
            resource.decoded_size
        } else {
            resource.transfer_size
        };
        total_bytes += effective_size;
        transfer_bytes += resource.transfer_size;

        let stats = match categorize_resource(&resource.name, &resource.resource_type) {
            ResourceCategory::Html => &mut breakdown.html,
            ResourceCategory::Css => &mut breakdown.css,
            ResourceCategory::JavaScript => &mut breakdown.javascript,
            ResourceCategory::Image => &mut breakdown.images,
            ResourceCategory::Font => &mut breakdown.fonts,
            ResourceCategory::Media => &mut breakdown.media,
            ResourceCategory::Other => &mut breakdown.other,
        };

        stats.count += 1;
        stats.bytes += effective_size;
        stats.transfer_bytes += resource.transfer_size;

        if effective_size > stats.largest_bytes {
            stats.largest_bytes = effective_size;
            stats.largest_url = Some(truncate_url(&resource.name));
        }
    }

    // Generate recommendations
    let carbon = estimate_carbon(transfer_bytes);
    let recommendations =
        generate_recommendations(&breakdown, total_bytes, transfer_bytes, &carbon);

    info!(
        "Content weight: {} total, {} transfer, {} requests",
        format_bytes(total_bytes),
        format_bytes(transfer_bytes),
        request_count
    );

    Ok(ContentWeight {
        total_bytes,
        transfer_bytes,
        breakdown,
        request_count,
        carbon,
        recommendations,
    })
}

#[derive(Debug, Deserialize)]
struct ResourceEntry {
    name: String,
    #[serde(rename = "type")]
    resource_type: String,
    #[serde(rename = "transferSize")]
    transfer_size: u64,
    #[serde(rename = "decodedSize")]
    decoded_size: u64,
}

enum ResourceCategory {
    Html,
    Css,
    JavaScript,
    Image,
    Font,
    Media,
    Other,
}

fn categorize_resource(url: &str, initiator_type: &str) -> ResourceCategory {
    let url_lower = url.to_lowercase();

    // Font files loaded via CSS @font-face carry initiatorType="css" in
    // PerformanceResourceTiming. Check font extensions before the CSS branch
    // so they are counted in the correct category.
    if url_lower.contains(".woff")
        || url_lower.contains(".ttf")
        || url_lower.contains(".otf")
        || url_lower.contains(".eot")
    {
        return ResourceCategory::Font;
    }

    if initiator_type == "navigation" {
        ResourceCategory::Html
    } else if url_lower.ends_with(".css") || initiator_type == "css" {
        ResourceCategory::Css
    } else if url_lower.ends_with(".js") || initiator_type == "script" {
        ResourceCategory::JavaScript
    } else if url_lower.ends_with(".html") || url_lower.ends_with(".htm") {
        ResourceCategory::Html
    } else if url_lower.ends_with(".png")
        || url_lower.ends_with(".jpg")
        || url_lower.ends_with(".jpeg")
        || url_lower.ends_with(".gif")
        || url_lower.ends_with(".webp")
        || url_lower.ends_with(".svg")
        || url_lower.ends_with(".ico")
        || initiator_type == "img"
    {
        ResourceCategory::Image
    } else if url_lower.ends_with(".mp4")
        || url_lower.ends_with(".webm")
        || url_lower.ends_with(".mp3")
        || url_lower.ends_with(".wav")
    {
        ResourceCategory::Media
    } else {
        ResourceCategory::Other
    }
}

fn truncate_url(url: &str) -> String {
    crate::util::truncate_url(url, 80)
}

fn generate_recommendations(
    breakdown: &ResourceBreakdown,
    total_bytes: u64,
    transfer_bytes: u64,
    carbon: &CarbonEstimate,
) -> Vec<String> {
    let mut recommendations = Vec::new();

    // Check total size
    if total_bytes > 5_000_000 {
        recommendations.push(format!(
            "Page size ({}) exceeds 5MB. Consider lazy loading and code splitting.",
            format_bytes(total_bytes)
        ));
    } else if total_bytes > 3_000_000 {
        recommendations.push(format!(
            "Page size ({}) is heavy. Consider optimizing resources.",
            format_bytes(total_bytes)
        ));
    }

    // Check compression
    if total_bytes > 0 {
        let ratio = transfer_bytes as f64 / total_bytes as f64;
        if ratio > 0.8 {
            recommendations.push("Enable gzip/brotli compression for text resources.".to_string());
        }
    }

    // Check JavaScript
    if breakdown.javascript.bytes > 1_000_000 {
        recommendations.push(format!(
            "JavaScript ({}) is large. Consider code splitting and tree shaking.",
            format_bytes(breakdown.javascript.bytes)
        ));
    }

    // Check images
    if breakdown.images.bytes > 2_000_000 {
        recommendations.push(format!(
            "Images ({}) are heavy. Use WebP format and responsive images.",
            format_bytes(breakdown.images.bytes)
        ));
    }

    // Check CSS
    if breakdown.css.bytes > 500_000 {
        recommendations.push(format!(
            "CSS ({}) is large. Remove unused styles and consider critical CSS.",
            format_bytes(breakdown.css.bytes)
        ));
    }

    // Check fonts
    if breakdown.fonts.count > 3 {
        recommendations.push(format!(
            "Using {} font files. Consider reducing font variations.",
            breakdown.fonts.count
        ));
    }

    // Check request count
    if breakdown.javascript.count + breakdown.css.count > 20 {
        recommendations
            .push("Many JS/CSS files. Consider bundling to reduce HTTP requests.".to_string());
    }

    if carbon.grams_co2e_per_view > 1.0 {
        recommendations.push(format!(
            "Estimated carbon footprint is {} g CO2e per view ({}). Reduce transfer bytes to improve sustainability.",
            carbon.format_grams(), carbon.rating
        ));
    }

    recommendations
}

pub(super) fn estimate_carbon(transfer_bytes: u64) -> CarbonEstimate {
    // Conservative Sustainable Web Design-style transfer estimate:
    // 0.81 kWh / GB transfer × 442 gCO2e / kWh = 358.02 gCO2e / GB.
    const GRAMS_PER_GB: f64 = 0.81 * 442.0;
    let gb = transfer_bytes as f64 / 1_000_000_000.0;
    let grams = gb * GRAMS_PER_GB;
    CarbonEstimate {
        grams_co2e_per_view: grams,
        rating: carbon_rating(grams).to_string(),
        model: "transfer_bytes * 0.81 kWh/GB * 442 gCO2e/kWh".to_string(),
    }
}

fn carbon_rating(grams: f64) -> &'static str {
    if grams <= 0.095 {
        "A+"
    } else if grams <= 0.186 {
        "A"
    } else if grams <= 0.341 {
        "B"
    } else if grams <= 0.493 {
        "C"
    } else if grams <= 0.656 {
        "D"
    } else if grams <= 0.846 {
        "E"
    } else {
        "F"
    }
}

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

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(500), "500 B");
        assert_eq!(format_bytes(1500), "1.5 KB");
        assert_eq!(format_bytes(1_500_000), "1.5 MB");
    }

    #[test]
    fn test_categorize_resource() {
        assert!(matches!(
            categorize_resource("style.css", "css"),
            ResourceCategory::Css
        ));
        assert!(matches!(
            categorize_resource("app.js", "script"),
            ResourceCategory::JavaScript
        ));
        assert!(matches!(
            categorize_resource("photo.jpg", "img"),
            ResourceCategory::Image
        ));
    }

    #[test]
    fn test_content_weight_compression_ratio() {
        let weight = ContentWeight {
            total_bytes: 1000,
            transfer_bytes: 300,
            breakdown: ResourceBreakdown::default(),
            request_count: 5,
            carbon: crate::performance::CarbonEstimate::default(),
            recommendations: vec![],
        };

        assert!((weight.compression_ratio() - 0.3).abs() < 0.001);
    }

    #[test]
    fn test_estimate_carbon_from_transfer_bytes() {
        let estimate = estimate_carbon(1_000_000_000);

        assert!((estimate.grams_co2e_per_view - 358.02).abs() < 0.01);
        assert_eq!(estimate.rating, "F");
    }

    #[test]
    fn test_carbon_rating_for_light_page() {
        let estimate = estimate_carbon(100_000);

        assert_eq!(estimate.rating, "A+");
        assert!(estimate.grams_co2e_per_view < 0.1);
    }

    #[test]
    fn test_format_grams_discloses_near_zero_instead_of_truncating() {
        let near_zero = CarbonEstimate {
            grams_co2e_per_view: 0.003,
            rating: "A+".to_string(),
            model: String::new(),
        };
        assert_eq!(near_zero.format_grams(), "< 0.01");

        let normal = CarbonEstimate {
            grams_co2e_per_view: 0.42,
            rating: "A".to_string(),
            model: String::new(),
        };
        assert_eq!(normal.format_grams(), "0.42");

        let zero = CarbonEstimate {
            grams_co2e_per_view: 0.0,
            rating: "A+".to_string(),
            model: String::new(),
        };
        assert_eq!(zero.format_grams(), "0.00");
    }

    #[test]
    fn test_is_heavy() {
        let light = ContentWeight {
            total_bytes: 1_000_000,
            transfer_bytes: 500_000,
            breakdown: ResourceBreakdown::default(),
            request_count: 10,
            carbon: crate::performance::CarbonEstimate::default(),
            recommendations: vec![],
        };
        assert!(!light.is_heavy());

        let heavy = ContentWeight {
            total_bytes: 5_000_000,
            transfer_bytes: 3_000_000,
            breakdown: ResourceBreakdown::default(),
            request_count: 50,
            carbon: crate::performance::CarbonEstimate::default(),
            recommendations: vec![],
        };
        assert!(heavy.is_heavy());
    }
}