halldyll-parser 0.1.0

HTML/CSS parsing and content extraction for halldyll scraper
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
//! Content fingerprinting and change detection for halldyll-parser
//!
//! This module handles:
//! - Content hashing for change detection
//! - Structural fingerprinting
//! - AMP page detection
//! - Content comparison
//! - Cache control hints

use scraper::{Html, Selector};
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use crate::selector::SELECTORS;
use crate::types::ParserResult;

// ============================================================================
// TYPES
// ============================================================================

/// Content fingerprint for change detection
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct ContentFingerprint {
    /// Hash of the full HTML content
    pub full_hash: u64,
    /// Hash of just the main content (excluding nav, footer, etc.)
    pub content_hash: u64,
    /// Hash of the text content only (no tags)
    pub text_hash: u64,
    /// Hash of the document structure (tag hierarchy)
    pub structure_hash: u64,
    /// Number of elements in the document
    pub element_count: usize,
    /// Number of text nodes
    pub text_node_count: usize,
    /// Content length in bytes
    pub content_length: usize,
    /// Main content length
    pub main_content_length: usize,
}

impl ContentFingerprint {
    /// Check if content has changed compared to another fingerprint
    pub fn has_changed(&self, other: &ContentFingerprint) -> bool {
        self.content_hash != other.content_hash
    }

    /// Check if only minor changes occurred (same structure, different content)
    pub fn has_minor_changes(&self, other: &ContentFingerprint) -> bool {
        self.structure_hash == other.structure_hash && 
        self.content_hash != other.content_hash
    }

    /// Check if structure changed (major change)
    pub fn has_structural_changes(&self, other: &ContentFingerprint) -> bool {
        self.structure_hash != other.structure_hash
    }

    /// Get similarity percentage (0.0 to 1.0)
    pub fn similarity(&self, other: &ContentFingerprint) -> f64 {
        let mut matches = 0.0;
        let total = 4.0;

        if self.content_hash == other.content_hash { matches += 1.0; }
        if self.text_hash == other.text_hash { matches += 1.0; }
        if self.structure_hash == other.structure_hash { matches += 1.0; }
        
        // Element count similarity
        let count_diff = (self.element_count as i64 - other.element_count as i64).abs();
        let max_count = self.element_count.max(other.element_count) as f64;
        if max_count > 0.0 {
            matches += 1.0 - (count_diff as f64 / max_count);
        } else {
            matches += 1.0;
        }

        matches / total
    }
}

/// AMP (Accelerated Mobile Pages) information
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AmpInfo {
    /// Whether this page is an AMP page
    pub is_amp: bool,
    /// Whether this is an AMP HTML page (⚡ or amp attribute)
    pub is_amp_html: bool,
    /// URL to the AMP version of this page (if not AMP)
    pub amp_url: Option<String>,
    /// URL to the canonical (non-AMP) version (if this is AMP)
    pub canonical_url: Option<String>,
    /// AMP version detected
    pub amp_version: Option<String>,
    /// Whether AMP runtime is included
    pub has_amp_runtime: bool,
    /// AMP components used
    pub components: Vec<String>,
}

impl AmpInfo {
    pub fn new() -> Self {
        Self::default()
    }

    /// Check if page has AMP version available
    pub fn has_amp_version(&self) -> bool {
        self.amp_url.is_some()
    }
}

/// Cache hints extracted from the page
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct CacheHints {
    /// ETag value if found
    pub etag: Option<String>,
    /// Last-Modified header value
    pub last_modified: Option<String>,
    /// Cache-Control directives
    pub cache_control: Option<String>,
    /// Whether page indicates it shouldn't be cached
    pub no_cache: bool,
    /// Max-age value in seconds
    pub max_age: Option<u32>,
}

// ============================================================================
// FINGERPRINTING FUNCTIONS
// ============================================================================

/// Generate content fingerprint from HTML
pub fn generate_fingerprint(html: &str) -> ParserResult<ContentFingerprint> {
    let document = Html::parse_document(html);
    
    // Main content hash (excluding boilerplate)
    let main_content = extract_main_content(&document);
    
    // Text-only hash
    let text_content = extract_text_only(&document);
    
    // Structure hash
    let structure = extract_structure(&document);
    
    let fingerprint = ContentFingerprint {
        full_hash: hash_string(html),
        content_length: html.len(),
        content_hash: hash_string(&main_content),
        main_content_length: main_content.len(),
        text_hash: hash_string(&text_content),
        structure_hash: hash_string(&structure),
        element_count: count_elements(&document),
        text_node_count: count_text_nodes(&document),
    };
    
    Ok(fingerprint)
}

/// Generate fingerprint from parsed document
pub fn fingerprint_document(document: &Html) -> ContentFingerprint {
    let html = document.html();
    generate_fingerprint(&html).unwrap_or_default()
}

/// Hash a string using DefaultHasher
fn hash_string(s: &str) -> u64 {
    let mut hasher = DefaultHasher::new();
    s.hash(&mut hasher);
    hasher.finish()
}

/// Extract main content area (excluding nav, header, footer, sidebar)
fn extract_main_content(document: &Html) -> String {
    // Try to find main content area
    let main_selectors = [
        "main",
        "article",
        "[role='main']",
        ".content",
        "#content",
        ".post-content",
        ".article-content",
        ".entry-content",
    ];

    for selector_str in main_selectors {
        if let Ok(sel) = Selector::parse(selector_str) {
            let content: String = document.select(&sel)
                .map(|el| el.html())
                .collect();
            
            if !content.is_empty() {
                return content;
            }
        }
    }

    // Fallback to body without boilerplate
    if let Some(body) = document.select(&SELECTORS.body).next() {
        let mut content = body.html();
        
        // Remove common boilerplate elements
        let boilerplate = ["<nav", "<header", "<footer", "<aside", "<script", "<style"];
        for bp in boilerplate {
            if let Some(start) = content.find(bp) {
                if let Some(end) = content[start..].find('>') {
                    // Find closing tag - simplified removal
                    let tag_end = start + end + 1;
                    content = format!("{}{}", &content[..start], &content[tag_end..]);
                }
            }
        }
        
        return content;
    }

    document.html()
}

/// Extract text content only (no HTML tags)
fn extract_text_only(document: &Html) -> String {
    document.root_element()
        .text()
        .collect::<String>()
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

/// Extract document structure (tag hierarchy)
fn extract_structure(document: &Html) -> String {
    let mut structure = String::new();
    extract_structure_recursive(document.root_element(), &mut structure, 0);
    structure
}

/// Recursively extract structure
fn extract_structure_recursive(
    element: scraper::ElementRef,
    structure: &mut String,
    depth: usize,
) {
    // Add tag name with depth indicator
    structure.push_str(&format!("{}:{}", depth, element.value().name()));
    
    // Add significant attributes
    for attr in ["id", "class", "role"] {
        if let Some(val) = element.value().attr(attr) {
            // Only include first class and id for fingerprinting
            let short_val: String = val.split_whitespace().take(1).collect();
            if !short_val.is_empty() {
                structure.push_str(&format!("[{}={}]", attr, short_val));
            }
        }
    }
    
    structure.push(';');
    
    // Recurse into children (limit depth for performance)
    if depth < 10 {
        for child in element.children() {
            if let Some(el) = scraper::ElementRef::wrap(child) {
                // Skip script and style
                let name = el.value().name();
                if name != "script" && name != "style" && name != "noscript" {
                    extract_structure_recursive(el, structure, depth + 1);
                }
            }
        }
    }
}

/// Count total elements in document
fn count_elements(document: &Html) -> usize {
    if let Ok(sel) = Selector::parse("*") {
        document.select(&sel).count()
    } else {
        0
    }
}

/// Count text nodes in document
fn count_text_nodes(document: &Html) -> usize {
    document.root_element()
        .text()
        .filter(|t| !t.trim().is_empty())
        .count()
}

// ============================================================================
// AMP DETECTION
// ============================================================================

/// Extract AMP information from document
pub fn extract_amp_info(document: &Html, base_url: Option<&url::Url>) -> ParserResult<AmpInfo> {
    let mut info = AmpInfo::new();

    // Check if this is an AMP page
    info.is_amp_html = detect_is_amp_page(document);
    info.is_amp = info.is_amp_html;

    // Get AMP URL (if this is not an AMP page)
    if !info.is_amp {
        info.amp_url = extract_amp_link(document, base_url);
        if info.amp_url.is_some() {
            info.is_amp = true; // Has AMP version
        }
    }

    // Get canonical URL (if this is an AMP page)
    if info.is_amp_html {
        info.canonical_url = extract_canonical_link(document, base_url);
    }

    // Check for AMP runtime
    info.has_amp_runtime = detect_amp_runtime(document);

    // Extract AMP components
    info.components = extract_amp_components(document);

    // Try to detect AMP version
    info.amp_version = detect_amp_version(document);

    Ok(info)
}

/// Check if document is an AMP HTML page
fn detect_is_amp_page(document: &Html) -> bool {
    // Check for ⚡ or amp attribute on html element
    if let Some(html) = document.select(&SELECTORS.html).next() {
        // Check for amp or ⚡ attribute
        if html.value().attr("amp").is_some() || html.value().attr("").is_some() {
            return true;
        }
        
        // Check class
        if html.value().classes().any(|c| c == "amp" || c == "") {
            return true;
        }
    }

    // Check for AMP boilerplate in head
    let html_str = document.html();
    html_str.contains("amp-boilerplate") || 
    html_str.contains("cdn.ampproject.org")
}

/// Extract link to AMP version
fn extract_amp_link(document: &Html, base_url: Option<&url::Url>) -> Option<String> {
    if let Ok(sel) = Selector::parse("link[rel='amphtml']") {
        if let Some(el) = document.select(&sel).next() {
            if let Some(href) = el.value().attr("href") {
                return resolve_url(href, base_url);
            }
        }
    }
    None
}

/// Extract canonical link
fn extract_canonical_link(document: &Html, base_url: Option<&url::Url>) -> Option<String> {
    if let Ok(sel) = Selector::parse("link[rel='canonical']") {
        if let Some(el) = document.select(&sel).next() {
            if let Some(href) = el.value().attr("href") {
                return resolve_url(href, base_url);
            }
        }
    }
    None
}

/// Check for AMP runtime script
fn detect_amp_runtime(document: &Html) -> bool {
    if let Ok(sel) = Selector::parse("script[src*='cdn.ampproject.org']") {
        return document.select(&sel).next().is_some();
    }
    false
}

/// Extract AMP component names
fn extract_amp_components(document: &Html) -> Vec<String> {
    let mut components = Vec::new();

    // Find custom element scripts
    if let Ok(sel) = Selector::parse("script[custom-element]") {
        for el in document.select(&sel) {
            if let Some(name) = el.value().attr("custom-element") {
                if !components.contains(&name.to_string()) {
                    components.push(name.to_string());
                }
            }
        }
    }

    // Also check for amp-* tags in the document
    let html = document.html().to_lowercase();
    let amp_tags = [
        "amp-img", "amp-video", "amp-audio", "amp-carousel",
        "amp-accordion", "amp-sidebar", "amp-lightbox",
        "amp-analytics", "amp-ad", "amp-social-share",
        "amp-form", "amp-list", "amp-bind", "amp-state",
    ];

    for tag in amp_tags {
        if html.contains(&format!("<{}", tag)) {
            let tag_str = tag.to_string();
            if !components.contains(&tag_str) {
                components.push(tag_str);
            }
        }
    }

    components
}

/// Detect AMP version from runtime URL
fn detect_amp_version(document: &Html) -> Option<String> {
    if let Ok(sel) = Selector::parse("script[src*='cdn.ampproject.org']") {
        if let Some(el) = document.select(&sel).next() {
            if let Some(src) = el.value().attr("src") {
                // Try to extract version from URL like /v0.js or /v0/amp-component-0.1.js
                if src.contains("/v0") {
                    return Some("v0".to_string());
                }
                // Could parse more specific versions here
            }
        }
    }
    None
}

/// Resolve URL helper
fn resolve_url(href: &str, base_url: Option<&url::Url>) -> Option<String> {
    if href.starts_with("http://") || href.starts_with("https://") {
        return Some(href.to_string());
    }

    if let Some(base) = base_url {
        return base.join(href).ok().map(|u| u.to_string());
    }

    None
}

// ============================================================================
// CACHE HINTS EXTRACTION
// ============================================================================

/// Extract cache hints from meta tags
pub fn extract_cache_hints(document: &Html) -> CacheHints {
    let mut hints = CacheHints::default();

    // Check for no-cache meta tag
    if let Ok(sel) = Selector::parse("meta[http-equiv='Cache-Control']") {
        if let Some(el) = document.select(&sel).next() {
            if let Some(content) = el.value().attr("content") {
                hints.cache_control = Some(content.to_string());
                hints.no_cache = content.to_lowercase().contains("no-cache") ||
                                 content.to_lowercase().contains("no-store");
                
                // Try to extract max-age
                if let Some(pos) = content.to_lowercase().find("max-age=") {
                    let start = pos + 8;
                    let num: String = content[start..]
                        .chars()
                        .take_while(|c| c.is_ascii_digit())
                        .collect();
                    hints.max_age = num.parse().ok();
                }
            }
        }
    }

    // Check for Pragma: no-cache
    if let Ok(sel) = Selector::parse("meta[http-equiv='Pragma']") {
        if let Some(el) = document.select(&sel).next() {
            if let Some(content) = el.value().attr("content") {
                if content.to_lowercase().contains("no-cache") {
                    hints.no_cache = true;
                }
            }
        }
    }

    hints
}

// ============================================================================
// CONVENIENCE FUNCTIONS
// ============================================================================

/// Check if content has changed between two HTML strings
pub fn has_content_changed(old_html: &str, new_html: &str) -> bool {
    let old_fp = generate_fingerprint(old_html).unwrap_or_default();
    let new_fp = generate_fingerprint(new_html).unwrap_or_default();
    old_fp.has_changed(&new_fp)
}

/// Get content similarity between two HTML strings (0.0 to 1.0)
pub fn content_similarity(html1: &str, html2: &str) -> f64 {
    let fp1 = generate_fingerprint(html1).unwrap_or_default();
    let fp2 = generate_fingerprint(html2).unwrap_or_default();
    fp1.similarity(&fp2)
}

/// Check if page is an AMP page
pub fn is_amp_page(document: &Html) -> bool {
    detect_is_amp_page(document)
}

/// Get AMP URL if available
pub fn get_amp_url(document: &Html) -> Option<String> {
    extract_amp_link(document, None)
}

/// Quick hash of HTML content
pub fn quick_hash(html: &str) -> u64 {
    hash_string(html)
}

// ============================================================================
// TESTS
// ============================================================================

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

    fn parse_html(html: &str) -> Html {
        Html::parse_document(html)
    }

    #[test]
    fn test_generate_fingerprint() {
        let html = "<html><body><p>Hello world</p></body></html>";
        let fp = generate_fingerprint(html).unwrap();

        assert!(fp.full_hash != 0);
        assert!(fp.content_hash != 0);
        assert!(fp.text_hash != 0);
        assert!(fp.structure_hash != 0);
        assert!(fp.element_count > 0);
        assert!(fp.content_length > 0);
    }

    #[test]
    fn test_fingerprint_same_content() {
        let html1 = "<html><body><p>Hello world</p></body></html>";
        let html2 = "<html><body><p>Hello world</p></body></html>";

        let fp1 = generate_fingerprint(html1).unwrap();
        let fp2 = generate_fingerprint(html2).unwrap();

        assert!(!fp1.has_changed(&fp2));
        assert_eq!(fp1.similarity(&fp2), 1.0);
    }

    #[test]
    fn test_fingerprint_different_content() {
        let html1 = "<html><body><p>Hello world</p></body></html>";
        let html2 = "<html><body><p>Goodbye world</p></body></html>";

        let fp1 = generate_fingerprint(html1).unwrap();
        let fp2 = generate_fingerprint(html2).unwrap();

        assert!(fp1.has_changed(&fp2));
        // Structure should be the same
        assert!(!fp1.has_structural_changes(&fp2));
        assert!(fp1.has_minor_changes(&fp2));
    }

    #[test]
    fn test_fingerprint_structural_change() {
        let html1 = "<html><body><p>Hello</p></body></html>";
        let html2 = "<html><body><div><p>Hello</p></div></body></html>";

        let fp1 = generate_fingerprint(html1).unwrap();
        let fp2 = generate_fingerprint(html2).unwrap();

        assert!(fp1.has_structural_changes(&fp2));
    }

    #[test]
    fn test_detect_amp_page() {
        let amp_html = r#"
            <!DOCTYPE html>
            <html amp>
            <head>
                <script async src="https://cdn.ampproject.org/v0.js"></script>
            </head>
            <body></body>
            </html>
        "#;

        let doc = parse_html(amp_html);
        assert!(detect_is_amp_page(&doc));
    }

    #[test]
    fn test_detect_amp_page_lightning() {
        let amp_html = r#"
            <!DOCTYPE html>
            <html ⚡>
            <head></head>
            <body></body>
            </html>
        "#;

        let doc = parse_html(amp_html);
        assert!(detect_is_amp_page(&doc));
    }

    #[test]
    fn test_not_amp_page() {
        let html = "<html><body><p>Regular page</p></body></html>";
        let doc = parse_html(html);
        assert!(!detect_is_amp_page(&doc));
    }

    #[test]
    fn test_extract_amp_link() {
        let html = r#"
            <html>
            <head>
                <link rel="amphtml" href="https://example.com/page.amp">
            </head>
            </html>
        "#;

        let doc = parse_html(html);
        let amp_url = extract_amp_link(&doc, None);
        assert_eq!(amp_url, Some("https://example.com/page.amp".to_string()));
    }

    #[test]
    fn test_extract_amp_components() {
        let html = r#"
            <html amp>
            <head>
                <script custom-element="amp-carousel" src="..."></script>
                <script custom-element="amp-analytics" src="..."></script>
            </head>
            <body>
                <amp-img src="test.jpg"></amp-img>
            </body>
            </html>
        "#;

        let doc = parse_html(html);
        let components = extract_amp_components(&doc);

        assert!(components.contains(&"amp-carousel".to_string()));
        assert!(components.contains(&"amp-analytics".to_string()));
        assert!(components.contains(&"amp-img".to_string()));
    }

    #[test]
    fn test_extract_amp_info() {
        let html = r#"
            <html>
            <head>
                <link rel="amphtml" href="/amp/page">
                <link rel="canonical" href="/page">
            </head>
            </html>
        "#;

        let doc = parse_html(html);
        let base = url::Url::parse("https://example.com/").unwrap();
        let info = extract_amp_info(&doc, Some(&base)).unwrap();

        assert!(info.has_amp_version());
        assert_eq!(info.amp_url, Some("https://example.com/amp/page".to_string()));
    }

    #[test]
    fn test_extract_cache_hints() {
        let html = r#"
            <html>
            <head>
                <meta http-equiv="Cache-Control" content="max-age=3600, public">
            </head>
            </html>
        "#;

        let doc = parse_html(html);
        let hints = extract_cache_hints(&doc);

        assert!(!hints.no_cache);
        assert_eq!(hints.max_age, Some(3600));
    }

    #[test]
    fn test_cache_no_cache() {
        let html = r#"
            <html>
            <head>
                <meta http-equiv="Cache-Control" content="no-cache, no-store">
            </head>
            </html>
        "#;

        let doc = parse_html(html);
        let hints = extract_cache_hints(&doc);

        assert!(hints.no_cache);
    }

    #[test]
    fn test_has_content_changed() {
        let html1 = "<html><body><p>Version 1</p></body></html>";
        let html2 = "<html><body><p>Version 2</p></body></html>";

        assert!(has_content_changed(html1, html2));
        assert!(!has_content_changed(html1, html1));
    }

    #[test]
    fn test_content_similarity() {
        let html1 = "<html><body><p>Hello world</p></body></html>";
        let html2 = "<html><body><p>Hello world</p></body></html>";

        assert_eq!(content_similarity(html1, html2), 1.0);

        let html3 = "<html><body><p>Different content entirely</p></body></html>";
        let sim = content_similarity(html1, html3);
        assert!(sim < 1.0);
        assert!(sim > 0.0);
    }

    #[test]
    fn test_quick_hash() {
        let html1 = "<html><body>Test</body></html>";
        let html2 = "<html><body>Test</body></html>";
        let html3 = "<html><body>Different</body></html>";

        assert_eq!(quick_hash(html1), quick_hash(html2));
        assert_ne!(quick_hash(html1), quick_hash(html3));
    }

    #[test]
    fn test_fingerprint_similarity_range() {
        let html1 = "<html><body><div><p>Test</p></div></body></html>";
        let html2 = "<html><body><span><p>Test</p></span></body></html>";

        let fp1 = generate_fingerprint(html1).unwrap();
        let fp2 = generate_fingerprint(html2).unwrap();

        let sim = fp1.similarity(&fp2);
        assert!(sim >= 0.0 && sim <= 1.0);
    }
}