icookforms 0.1.0

The World's Reference Cookie Audit Software - Complete Security & Compliance Analysis
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
//! Google Consent Detection Module
//!
//! This module detects and analyzes Google Consent Mode V2 and Additional Consent
//! signals from various sources:
//! - HTTP cookies
//! - URL parameters
//! - HTTP requests
//!
//! # Overview
//!
//! Google Consent signals can appear in multiple places:
//!
//! ## Cookies
//! - `addtl_consent`: AC String (e.g., "2~1.35.41~dv.9.21")
//! - `CookieConsent`, `OptanonConsent`: CMP consent cookies
//! - `_ga`, `_gid`, `_gat`: Google Analytics cookies
//! - `_gcl_au`, `_gcl_aw`: Google Ads cookies
//!
//! ## URL Parameters
//! - `gcd`: Consent Mode V2 parameter (e.g., "11l1m1n1p5")
//! - `gcs`: Legacy Consent Mode V1 parameter (e.g., "G111")
//! - `addtl_consent`: AC String in URL
//!
//! # Examples
//!
//! ```
//! # use icookforms::compliance::google_consent::detect_google_consent_cookies;
//! # use icookforms::types::Cookie;
//! let cookies = vec![
//!     Cookie::new("addtl_consent".to_string(), "2~1.35.41~dv.9.21".to_string()),
//!     Cookie::new("_ga".to_string(), "GA1.2.123456.789".to_string()),
//! ];
//!
//! let analysis = detect_google_consent_cookies(&cookies);
//! assert!(analysis.ac_string.is_some());
//! assert!(analysis.has_analytics_cookies);
//! ```

use super::{parse_ac_string, parse_gcd_parameter, ACString, ConsentModeV2};
use crate::types::Cookie;
use std::collections::HashMap;
use url::Url;

/// Google Consent Analysis Results
///
/// Contains comprehensive analysis of Google consent signals found in cookies,
/// URLs, and HTTP requests.
#[derive(Debug, Clone, Default)]
#[allow(clippy::struct_excessive_bools)]
pub struct GoogleConsentAnalysis {
    /// Parsed Additional Consent String (if found)
    pub ac_string: Option<ACString>,

    /// Parsed Consent Mode V2 data (if found)
    pub consent_mode_v2: Option<ConsentModeV2>,

    /// Legacy Consent Mode V1 string (if found)
    pub gcs_value: Option<String>,

    /// Consent cookie found (various CMPs)
    pub consent_cookie: Option<Cookie>,

    /// Google Analytics cookies detected
    pub has_analytics_cookies: bool,

    /// Google Ads cookies detected
    pub has_ads_cookies: bool,

    /// `DoubleClick` cookies detected
    pub has_doubleclick_cookies: bool,

    /// `YouTube` cookies detected
    pub has_youtube_cookies: bool,

    /// All Google-related cookies found
    pub google_cookies: Vec<Cookie>,

    /// Source of consent data (cookie, url, both)
    pub consent_source: ConsentSource,
}

/// Google Consent Data extracted from URLs or requests
///
/// Contains raw consent data before full analysis.
#[derive(Debug, Clone, Default)]
pub struct GoogleConsentData {
    /// Legacy gcs parameter value
    pub gcs: Option<String>,

    /// Parsed Consent Mode V2 from gcd parameter
    pub consent_mode_v2: Option<ConsentModeV2>,

    /// Parsed AC String from URL parameter
    pub ac_string: Option<ACString>,
}

/// Source of consent information
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConsentSource {
    /// No consent information found
    None,

    /// Consent found in cookies only
    Cookie,

    /// Consent found in URL parameters only
    Url,

    /// Consent found in both cookies and URL
    Both,
}

impl Default for ConsentSource {
    fn default() -> Self {
        Self::None
    }
}

impl GoogleConsentAnalysis {
    /// Creates a new empty analysis
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Checks if any consent information was found
    #[must_use]
    pub fn has_consent_data(&self) -> bool {
        self.ac_string.is_some() || self.consent_mode_v2.is_some() || self.consent_cookie.is_some()
    }

    /// Checks if any Google cookies were detected
    #[must_use]
    pub fn has_google_cookies(&self) -> bool {
        self.has_analytics_cookies
            || self.has_ads_cookies
            || self.has_doubleclick_cookies
            || self.has_youtube_cookies
    }

    /// Checks if the configuration appears compliant
    ///
    /// Returns true if:
    /// - No Google cookies are present, OR
    /// - Google cookies are present AND consent data exists
    #[must_use]
    pub fn appears_compliant(&self) -> bool {
        !self.has_google_cookies() || self.has_consent_data()
    }

    /// Gets a summary of detected Google products
    #[must_use]
    pub fn detected_products(&self) -> Vec<String> {
        let mut products = Vec::new();

        if self.has_analytics_cookies {
            products.push("Google Analytics".to_string());
        }
        if self.has_ads_cookies {
            products.push("Google Ads".to_string());
        }
        if self.has_doubleclick_cookies {
            products.push("DoubleClick".to_string());
        }
        if self.has_youtube_cookies {
            products.push("YouTube".to_string());
        }

        products
    }
}

/// Detects Google consent signals in a collection of cookies
///
/// Analyzes cookies to detect:
/// - Additional Consent (AC) Strings
/// - Consent Mode cookies from various CMPs
/// - Google Analytics cookies
/// - Google Ads cookies
/// - `DoubleClick` cookies
/// - `YouTube` cookies
///
/// # Arguments
/// * `cookies` - Slice of cookies to analyze
///
/// # Returns
/// `GoogleConsentAnalysis` with detected signals
///
/// # Examples
/// ```
/// # use icookforms::compliance::google_consent::detect_google_consent_cookies;
/// # use icookforms::types::Cookie;
/// let cookies = vec![
///     Cookie::new("addtl_consent".to_string(), "2~1.35.41~dv.9.21".to_string()),
///     Cookie::new("_ga".to_string(), "GA1.2.123456.789".to_string()),
/// ];
///
/// let analysis = detect_google_consent_cookies(&cookies);
/// assert!(analysis.has_consent_data());
/// assert!(analysis.has_analytics_cookies);
/// ```
#[must_use]
pub fn detect_google_consent_cookies(cookies: &[Cookie]) -> GoogleConsentAnalysis {
    let mut analysis = GoogleConsentAnalysis::new();
    let mut google_cookies = Vec::new();

    for cookie in cookies {
        let name_lower = cookie.name.to_lowercase();

        match name_lower.as_str() {
            // Additional Consent cookie
            "addtl_consent" | "addtlconsent" => {
                if let Ok(ac_string) = parse_ac_string(&cookie.value) {
                    analysis.ac_string = Some(ac_string);
                    analysis.consent_source = match analysis.consent_source {
                        ConsentSource::None => ConsentSource::Cookie,
                        ConsentSource::Url => ConsentSource::Both,
                        _ => analysis.consent_source,
                    };
                }
                google_cookies.push(cookie.clone());
            }

            // Common CMP consent cookies (including "consent" which can also be a Google-wide cookie)
            "cookieconsent" | "optanonconsent" | "cookie_consent" | "euconsent-v2"
            | "cookielaw" | "consent" => {
                analysis.consent_cookie = Some(cookie.clone());
                analysis.consent_source = match analysis.consent_source {
                    ConsentSource::None => ConsentSource::Cookie,
                    ConsentSource::Url => ConsentSource::Both,
                    _ => analysis.consent_source,
                };

                // If it's the "consent" cookie and it's from Google domain, also track it
                if name_lower == "consent" {
                    let is_google_consent = cookie
                        .domain
                        .as_ref()
                        .is_some_and(|d| d.contains(".google."));

                    if is_google_consent {
                        google_cookies.push(cookie.clone());
                    }
                }
            }

            // Google Analytics cookies
            "_ga" | "_gid" | "_gat" | "_gat_gtag_" | "__utma" | "__utmb" | "__utmc" | "__utmz"
            | "__utmt" | "_gac_" => {
                analysis.has_analytics_cookies = true;
                google_cookies.push(cookie.clone());
            }

            // Google Ads cookies
            "_gcl_au" | "_gcl_aw" | "_gcl_dc" | "_gcl_gb" | "_gcl_gf" | "_gcl_ha" => {
                analysis.has_ads_cookies = true;
                google_cookies.push(cookie.clone());
            }

            // DoubleClick cookies
            "test_cookie" | "ide" | "__gads" | "__gac" => {
                let is_doubleclick = cookie
                    .domain
                    .as_ref()
                    .is_some_and(|d| d.contains("doubleclick.net") || d.contains(".google."));

                if is_doubleclick {
                    analysis.has_doubleclick_cookies = true;
                    google_cookies.push(cookie.clone());
                }
            }

            // YouTube cookies
            "visitor_info1_live" | "ysc" | "pref" | "yt-remote-" => {
                let is_youtube = cookie
                    .domain
                    .as_ref()
                    .is_some_and(|d| d.contains("youtube.com") || d.contains("googlevideo.com"));

                if is_youtube {
                    analysis.has_youtube_cookies = true;
                    google_cookies.push(cookie.clone());
                }
            }

            _ => {
                // Check if it's a Google cookie by domain
                if let Some(ref domain) = cookie.domain {
                    if is_google_domain(domain) {
                        google_cookies.push(cookie.clone());
                    }
                }
            }
        }
    }

    analysis.google_cookies = google_cookies;
    analysis
}

/// Detects Google consent signals in a URL
///
/// Parses URL query parameters to extract:
/// - `gcd`: Consent Mode V2 parameter
/// - `gcs`: Legacy Consent Mode V1 parameter
/// - `addtl_consent`: AC String parameter
///
/// # Arguments
/// * `url` - URL string to analyze
///
/// # Returns
/// * `Some(GoogleConsentData)` if consent signals are found
/// * `None` if no consent signals or URL is invalid
///
/// # Examples
/// ```
/// # use icookforms::compliance::google_consent::detect_google_consent_in_url;
/// let url = "https://example.com?gcd=11l1m1n1p5&addtl_consent=2~1.35";
/// let data = detect_google_consent_in_url(url).unwrap();
/// assert!(data.consent_mode_v2.is_some());
/// assert!(data.ac_string.is_some());
/// ```
#[must_use]
pub fn detect_google_consent_in_url(url: &str) -> Option<GoogleConsentData> {
    let parsed = Url::parse(url).ok()?;
    let params: HashMap<_, _> = parsed.query_pairs().collect();

    let mut data = GoogleConsentData::default();
    let mut found_any = false;

    // Detect gcs parameter (v1)
    if let Some(gcs) = params.get("gcs") {
        data.gcs = Some(gcs.to_string());
        found_any = true;
    }

    // Detect gcd parameter (v2)
    if let Some(gcd) = params.get("gcd") {
        if let Ok(consent) = parse_gcd_parameter(gcd) {
            data.consent_mode_v2 = Some(consent);
            found_any = true;
        }
    }

    // Detect addtl_consent parameter
    if let Some(ac) = params.get("addtl_consent") {
        if let Ok(ac_string) = parse_ac_string(ac) {
            data.ac_string = Some(ac_string);
            found_any = true;
        }
    }

    if found_any {
        Some(data)
    } else {
        None
    }
}

/// Detects Google consent signals in an HTTP request
///
/// Combines detection from both cookies and URL parameters.
///
/// # Arguments
/// * `url` - Request URL
/// * `cookies` - Request cookies
///
/// # Returns
/// `GoogleConsentAnalysis` combining all detected signals
///
/// # Examples
/// ```
/// # use icookforms::compliance::google_consent::detect_in_http_request;
/// # use icookforms::types::Cookie;
/// let url = "https://example.com?gcd=11l1m1n1p5";
/// let cookies = vec![
///     Cookie::new("_ga".to_string(), "GA1.2.123456.789".to_string()),
/// ];
///
/// let analysis = detect_in_http_request(url, &cookies);
/// assert!(analysis.consent_mode_v2.is_some());
/// assert!(analysis.has_analytics_cookies);
/// ```
#[must_use]
pub fn detect_in_http_request(url: &str, cookies: &[Cookie]) -> GoogleConsentAnalysis {
    let mut analysis = detect_google_consent_cookies(cookies);

    if let Some(url_data) = detect_google_consent_in_url(url) {
        // Merge URL data with cookie analysis
        if url_data.consent_mode_v2.is_some() {
            analysis.consent_mode_v2 = url_data.consent_mode_v2;
            analysis.consent_source = match analysis.consent_source {
                ConsentSource::None => ConsentSource::Url,
                ConsentSource::Cookie => ConsentSource::Both,
                _ => analysis.consent_source,
            };
        }

        if url_data.gcs.is_some() {
            analysis.gcs_value = url_data.gcs;
            analysis.consent_source = match analysis.consent_source {
                ConsentSource::None => ConsentSource::Url,
                ConsentSource::Cookie => ConsentSource::Both,
                _ => analysis.consent_source,
            };
        }

        if url_data.ac_string.is_some() && analysis.ac_string.is_none() {
            analysis.ac_string = url_data.ac_string;
            analysis.consent_source = match analysis.consent_source {
                ConsentSource::None => ConsentSource::Url,
                ConsentSource::Cookie => ConsentSource::Both,
                _ => analysis.consent_source,
            };
        }
    }

    analysis
}

/// Checks if a domain is a Google domain
///
/// # Arguments
/// * `domain` - Domain string to check
///
/// # Returns
/// `true` if the domain belongs to Google, `false` otherwise
fn is_google_domain(domain: &str) -> bool {
    let google_domains = [
        ".google.com",
        ".google.",
        ".googleapis.com",
        ".googleusercontent.com",
        ".googlesyndication.com",
        ".googletagmanager.com",
        ".googleadservices.com",
        ".google-analytics.com",
        ".doubleclick.net",
        ".youtube.com",
        ".ytimg.com",
        ".googlevideo.com",
        ".ggpht.com",
        ".gstatic.com",
    ];

    google_domains.iter().any(|&g| domain.contains(g))
}

/// Extracts Google Tag Manager container IDs from a URL
///
/// # Arguments
/// * `url` - URL to analyze
///
/// # Returns
/// Vector of GTM container IDs found (e.g., `["GTM-XXXXXX"]`)
#[must_use]
pub fn extract_gtm_containers(url: &str) -> Vec<String> {
    let mut containers = Vec::new();

    if let Ok(parsed) = Url::parse(url) {
        // Check if it's a GTM URL
        if parsed.host_str() == Some("www.googletagmanager.com") {
            if let Some(path) = parsed.path_segments() {
                for segment in path {
                    if segment.starts_with("GTM-") {
                        containers.push(segment.to_string());
                    }
                }
            }
        }

        // Check URL parameters for GTM ID
        let params: HashMap<_, _> = parsed.query_pairs().collect();
        if let Some(id) = params.get("id") {
            if id.starts_with("GTM-") {
                containers.push(id.to_string());
            }
        }
    }

    containers
}

/// Extracts Google Analytics measurement IDs from a URL
///
/// # Arguments
/// * `url` - URL to analyze
///
/// # Returns
/// Vector of GA measurement IDs found (e.g., `["G-XXXXXXXXXX", "UA-XXXXXXXX-X"]`)
#[must_use]
pub fn extract_ga_ids(url: &str) -> Vec<String> {
    let mut ids = Vec::new();

    if let Ok(parsed) = Url::parse(url) {
        let params: HashMap<_, _> = parsed.query_pairs().collect();

        // Check for GA4 measurement ID (G-XXXXXXXXXX)
        if let Some(id) = params.get("id") {
            if id.starts_with("G-") {
                ids.push(id.to_string());
            }
        }

        // Check for Universal Analytics ID (UA-XXXXXXXX-X)
        if let Some(id) = params.get("tid") {
            if id.starts_with("UA-") {
                ids.push(id.to_string());
            }
        }
    }

    ids
}

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

    // ========================================================================
    // Cookie Detection Tests
    // ========================================================================

    #[test]
    fn test_detect_ac_string_cookie() {
        let cookies = vec![Cookie::new(
            "addtl_consent".to_string(),
            "2~1.35.41~dv.9.21".to_string(),
        )];

        let analysis = detect_google_consent_cookies(&cookies);
        assert!(analysis.ac_string.is_some());
        assert_eq!(analysis.consent_source, ConsentSource::Cookie);
    }

    #[test]
    fn test_detect_analytics_cookies() {
        let cookies = vec![
            Cookie::new("_ga".to_string(), "GA1.2.123456.789".to_string()),
            Cookie::new("_gid".to_string(), "GA1.2.987654.321".to_string()),
        ];

        let analysis = detect_google_consent_cookies(&cookies);
        assert!(analysis.has_analytics_cookies);
        assert_eq!(analysis.google_cookies.len(), 2);
    }

    #[test]
    fn test_detect_ads_cookies() {
        let cookies = vec![
            Cookie::new("_gcl_au".to_string(), "1.1234567890.1234567890".to_string()),
            Cookie::new("_gcl_aw".to_string(), "GCL.1234567890.abcdef".to_string()),
        ];

        let analysis = detect_google_consent_cookies(&cookies);
        assert!(analysis.has_ads_cookies);
        assert_eq!(analysis.google_cookies.len(), 2);
    }

    #[test]
    fn test_detect_consent_cookie() {
        let cookies = vec![Cookie::new(
            "CookieConsent".to_string(),
            "accepted".to_string(),
        )];

        let analysis = detect_google_consent_cookies(&cookies);
        assert!(analysis.consent_cookie.is_some());
    }

    #[test]
    fn test_no_google_cookies() {
        let cookies = vec![
            Cookie::new("session".to_string(), "abc123".to_string()),
            Cookie::new("csrf".to_string(), "xyz789".to_string()),
        ];

        let analysis = detect_google_consent_cookies(&cookies);
        assert!(!analysis.has_google_cookies());
        assert!(analysis.google_cookies.is_empty());
    }

    // ========================================================================
    // URL Detection Tests
    // ========================================================================

    #[test]
    fn test_detect_gcd_in_url() {
        let url = "https://example.com?gcd=11l1m1n1p5";
        let data = detect_google_consent_in_url(url).unwrap();
        assert!(data.consent_mode_v2.is_some());
    }

    #[test]
    fn test_detect_gcs_in_url() {
        let url = "https://example.com?gcs=G111";
        let data = detect_google_consent_in_url(url).unwrap();
        assert!(data.gcs.is_some());
        assert_eq!(data.gcs.unwrap(), "G111");
    }

    #[test]
    fn test_detect_ac_in_url() {
        let url = "https://example.com?addtl_consent=2~1.35.41~dv.9.21";
        let data = detect_google_consent_in_url(url).unwrap();
        assert!(data.ac_string.is_some());
    }

    #[test]
    fn test_detect_multiple_params_in_url() {
        let url = "https://example.com?gcd=11l1m1n1p5&addtl_consent=2~1.35&other=value";
        let data = detect_google_consent_in_url(url).unwrap();
        assert!(data.consent_mode_v2.is_some());
        assert!(data.ac_string.is_some());
    }

    #[test]
    fn test_no_consent_in_url() {
        let url = "https://example.com?page=1&sort=asc";
        let data = detect_google_consent_in_url(url);
        assert!(data.is_none());
    }

    // ========================================================================
    // HTTP Request Detection Tests
    // ========================================================================

    #[test]
    fn test_detect_in_request_cookies_only() {
        let url = "https://example.com";
        let cookies = vec![Cookie::new(
            "_ga".to_string(),
            "GA1.2.123456.789".to_string(),
        )];

        let analysis = detect_in_http_request(url, &cookies);
        assert!(analysis.has_analytics_cookies);
        assert_eq!(analysis.consent_source, ConsentSource::None);
    }

    #[test]
    fn test_detect_in_request_url_only() {
        let url = "https://example.com?gcd=11l1m1n1p5";
        let cookies: Vec<Cookie> = vec![];

        let analysis = detect_in_http_request(url, &cookies);
        assert!(analysis.consent_mode_v2.is_some());
        assert_eq!(analysis.consent_source, ConsentSource::Url);
    }

    #[test]
    fn test_detect_in_request_both_sources() {
        let url = "https://example.com?gcd=11l1m1n1p5";
        let cookies = vec![
            Cookie::new("addtl_consent".to_string(), "2~1.35".to_string()),
            Cookie::new("_ga".to_string(), "GA1.2.123456.789".to_string()),
        ];

        let analysis = detect_in_http_request(url, &cookies);
        assert!(analysis.consent_mode_v2.is_some());
        assert!(analysis.ac_string.is_some());
        assert!(analysis.has_analytics_cookies);
        assert_eq!(analysis.consent_source, ConsentSource::Both);
    }

    // ========================================================================
    // GoogleConsentAnalysis Methods Tests
    // ========================================================================

    #[test]
    fn test_has_consent_data() {
        let mut analysis = GoogleConsentAnalysis::new();
        assert!(!analysis.has_consent_data());

        analysis.ac_string = Some(ACString::new());
        assert!(analysis.has_consent_data());
    }

    #[test]
    fn test_appears_compliant() {
        // No Google cookies = compliant
        let mut analysis = GoogleConsentAnalysis::new();
        assert!(analysis.appears_compliant());

        // Google cookies + no consent = non-compliant
        analysis.has_analytics_cookies = true;
        assert!(!analysis.appears_compliant());

        // Google cookies + consent = compliant
        analysis.ac_string = Some(ACString::new());
        assert!(analysis.appears_compliant());
    }

    #[test]
    fn test_detected_products() {
        let mut analysis = GoogleConsentAnalysis::new();
        assert!(analysis.detected_products().is_empty());

        analysis.has_analytics_cookies = true;
        analysis.has_ads_cookies = true;

        let products = analysis.detected_products();
        assert_eq!(products.len(), 2);
        assert!(products.contains(&"Google Analytics".to_string()));
        assert!(products.contains(&"Google Ads".to_string()));
    }

    // ========================================================================
    // Utility Function Tests
    // ========================================================================

    #[test]
    fn test_is_google_domain() {
        assert!(is_google_domain(".google.com"));
        assert!(is_google_domain("www.google.com"));
        assert!(is_google_domain(".googleapis.com"));
        assert!(is_google_domain(".doubleclick.net"));
        assert!(is_google_domain(".youtube.com"));
        assert!(!is_google_domain("example.com"));
        assert!(!is_google_domain("nogoogle.net"));
    }

    #[test]
    fn test_extract_gtm_containers() {
        let url = "https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXX";
        let containers = extract_gtm_containers(url);
        assert_eq!(containers.len(), 1);
        assert_eq!(containers[0], "GTM-XXXXXX");
    }

    #[test]
    fn test_extract_ga_ids() {
        // GA4
        let url = "https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX";
        let ids = extract_ga_ids(url);
        assert!(ids.contains(&"G-XXXXXXXXXX".to_string()));

        // Universal Analytics
        let url2 = "https://www.google-analytics.com/collect?tid=UA-12345678-1";
        let ids2 = extract_ga_ids(url2);
        assert!(ids2.contains(&"UA-12345678-1".to_string()));
    }
}