webpage_quality_analyzer 1.0.2

High-performance webpage quality analyzer with 115 comprehensive metrics - Rust library with WASM, C++, and Python bindings
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
/// Phase 3 Integration Tests - Profile-Aware Scoring Engine
/// Comprehensive testing of all Phase 3 components and their integration

#[cfg(test)]
mod phase3_integration_tests {
    use crate::config::enhanced_models::*;
    use crate::models::models::{PageMetrics, ProcessedDocument};
    use crate::scoring::{
        ProfileCompiler, ProfileAwareScorer, ContentValidator, Phase3ScoringSystem,
        CompiledProfile, ScoringResult, ContentValidationResult, ViolationSeverity,
    };
    use std::collections::HashMap;

    /// Test data builder for Phase 3 testing
    struct Phase3TestData {
        profile_config: ProfileConfig,
        metrics: PageMetrics,
        document: ProcessedDocument,
    }

    impl Phase3TestData {
        fn new() -> Self {
            Self {
                profile_config: Self::create_test_profile(),
                metrics: Self::create_test_metrics(),
                document: Self::create_test_document(),
            }
        }

        fn create_test_profile() -> ProfileConfig {
            ProfileConfig {
                name: "test_profile".to_string(),
                description: Some("Test profile for Phase 3 validation".to_string()),
                target_content_type: ContentType::Article,
                metric_weights: Self::create_metric_weights(),
                category_weights: Self::create_category_weights(),
                global_penalties: vec![
                    GlobalPenalty {
                        id: "low_word_count".to_string(),
                        description: "Penalty for insufficient content".to_string(),
                        trigger_condition: PenaltyTrigger::MetricThreshold {
                            metric_name: "word_count".to_string(),
                            operator: ComparisonOperator::LessThan,
                            threshold: MetricValue::Integer(300),
                        },
                        penalty_amount: PenaltyAmount::Fixed(20.0),
                        severity: PenaltySeverity::High,
                        applies_to: vec![MetricCategory::Content],
                        max_applications: Some(1),
                    },
                ],
                global_bonuses: vec![
                    GlobalBonus {
                        id: "excellent_readability".to_string(),
                        description: "Bonus for excellent readability".to_string(),
                        trigger_condition: BonusTrigger::MetricThreshold {
                            metric_name: "readability_score".to_string(),
                            operator: ComparisonOperator::GreaterThan,
                            threshold: MetricValue::Float(80.0),
                        },
                        bonus_amount: BonusAmount::Fixed(10.0),
                        applies_to: vec![MetricCategory::Content],
                        max_applications: Some(1),
                    },
                ],
                content_expectations: Some(ContentExpectations {
                    word_count: Some(WordCountExpectation {
                        minimum: 300,
                        optimal_range: (500, 2000),
                        maximum_useful: Some(5000),
                        penalty_curve: PenaltyCurve::Linear,
                    }),
                    heading_structure: Some(HeadingExpectation {
                        require_h1: true,
                        minimum_headings: 3,
                        maximum_heading_depth: 4,
                        logical_hierarchy: true,
                    }),
                    media_requirements: Some(MediaExpectation {
                        minimum_images: 1,
                        alt_text_coverage: 0.9,
                        image_to_text_ratio: Some((0.01, 0.1)),
                        require_video: Some(false),
                        require_audio: Some(false),
                    }),
                    technical_requirements: Some(TechnicalExpectation {
                        ssl_required: true,
                        required_meta_tags: vec!["description".to_string(), "viewport".to_string()],
                    }),
                    seo_requirements: Some(SeoExpectation {
                        title_length_range: Some((30, 60)),
                        meta_description_required: true,
                        meta_description_length_range: Some((120, 160)),
                        canonical_url_required: true,
                        structured_data_required: false,
                        open_graph_required: false,
                    }),
                }),
            }
        }

        fn create_metric_weights() -> HashMap<String, f32> {
            let mut weights = HashMap::new();
            weights.insert("word_count".to_string(), 1.5);
            weights.insert("readability_score".to_string(), 2.0);
            weights.insert("title_len".to_string(), 1.2);
            weights.insert("meta_desc_len".to_string(), 1.1);
            weights.insert("heading_count".to_string(), 1.0);
            weights.insert("image_count".to_string(), 0.8);
            weights
        }

        fn create_category_weights() -> HashMap<MetricCategory, f32> {
            let mut weights = HashMap::new();
            weights.insert(MetricCategory::Content, 3.0);
            weights.insert(MetricCategory::Structure, 2.5);
            weights.insert(MetricCategory::SEO, 2.0);
            weights.insert(MetricCategory::Technical, 1.5);
            weights.insert(MetricCategory::Accessibility, 1.8);
            weights.insert(MetricCategory::Media, 1.0);
            weights
        }

        fn create_test_metrics() -> PageMetrics {
            use crate::models::models::*;
            
            PageMetrics {
                html_analysis: HtmlAnalysis {
                    content: ContentMetrics {
                        word_count: 800,
                        paragraph_count: 12,
                        sentence_count: 45,
                        readability_score: Some(75.5),
                        reading_time_minutes: Some(4),
                        text_density: Some(0.65),
                        avg_words_per_sentence: Some(17.8),
                        avg_sentences_per_paragraph: Some(3.75),
                    },
                    structure: StructureMetrics {
                        heading_count: 5,
                        list_count: 3,
                        table_count: 1,
                        nav_count: 1,
                        main_count: 1,
                        aside_count: 2,
                        footer_count: 1,
                        header_count: 1,
                    },
                    media: MediaMetrics {
                        image_count: 4,
                        video_count: 0,
                        audio_count: 0,
                        interactive_count: 1,
                    },
                    seo: SeoMetrics {
                        title_len: Some(45),
                        meta_desc_len: Some(145),
                        h1_count: 1,
                        canonical_url: Some("https://example.com/article".to_string()),
                        meta_keywords: None,
                        og_title: Some("Test Article".to_string()),
                        og_description: Some("A test article for validation".to_string()),
                        og_image: Some("https://example.com/image.jpg".to_string()),
                        schema_markup_types: vec!["Article".to_string()],
                    },
                    accessibility: AccessibilityMetrics {
                        alt_text_coverage: 0.95,
                        aria_label_coverage: 0.8,
                        color_contrast_issues: 1,
                        keyboard_navigation_score: Some(85.0),
                        screen_reader_score: Some(90.0),
                    },
                    technical: TechnicalMetrics {
                        page_size_kb: Some(245),
                        load_time_ms: Some(1200),
                        dom_elements: Some(156),
                        critical_css_coverage: Some(0.85),
                        js_errors: Some(0),
                        console_warnings: Some(2),
                    },
                    links_social: LinksSocialMetrics {
                        internal_links: 8,
                        external_links: 3,
                        social_shares: Some(15),
                        backlink_estimate: Some(42),
                    },
                    mobile_usability: MobileUsabilityMetrics {
                        mobile_friendly: Some(true),
                        viewport_configured: Some(true),
                        touch_targets_sized: Some(true),
                        responsive_images: Some(0.9),
                    },
                    performance: PerformanceMetrics {
                        core_web_vitals_score: Some(85.0),
                        lighthouse_performance: Some(88),
                        first_contentful_paint_ms: Some(800),
                        largest_contentful_paint_ms: Some(1100),
                        cumulative_layout_shift: Some(0.05),
                        first_input_delay_ms: Some(45),
                    },
                    language_nlp: LanguageNlpMetrics {
                        detected_language: Some("en".to_string()),
                        language_confidence: Some(0.95),
                        sentiment_score: Some(0.2),
                        topic_categories: vec!["Technology".to_string(), "Web Development".to_string()],
                        keyword_density: Some(0.02),
                        duplicate_content_percentage: Some(0.05),
                    },
                },
            }
        }

        fn create_test_document() -> ProcessedDocument {
            ProcessedDocument {
                title: Some("Test Article: Phase 3 Validation".to_string()),
                content: "This is a comprehensive test article designed to validate the Phase 3 profile-aware scoring engine. The article contains multiple paragraphs with structured content, headings, and rich media elements.".to_string(),
                headings: vec![
                    "Test Article: Phase 3 Validation".to_string(),
                    "Introduction".to_string(),
                    "Methodology".to_string(),
                    "Results".to_string(),
                    "Conclusion".to_string(),
                ],
                links: vec![
                    "https://example.com/internal".to_string(),
                    "https://external-site.com/reference".to_string(),
                ],
                images: vec![
                    "https://example.com/chart.png".to_string(),
                    "https://example.com/diagram.jpg".to_string(),
                    "https://example.com/photo.webp".to_string(),
                    "https://example.com/illustration.svg".to_string(),
                ],
                meta_description: Some("A comprehensive test article for Phase 3 profile-aware scoring engine validation with detailed content analysis.".to_string()),
                word_count: 800,
                reading_time: 4,
                language: Some("en".to_string()),
            }
        }
    }

    #[test]
    fn test_profile_compiler_basic_functionality() {
        let test_data = Phase3TestData::new();
        let compiler = ProfileCompiler::new();
        
        // Test profile compilation
        let compiled_profile = compiler.compile_profile(&test_data.profile_config);
        assert!(compiled_profile.is_ok(), "Profile compilation should succeed");
        
        let compiled = compiled_profile.unwrap();
        assert_eq!(compiled.name, "test_profile");
        assert_eq!(compiled.target_content_type, ContentType::Article);
        assert!(!compiled.metric_rules.is_empty(), "Should have compiled metric rules");
        assert!(!compiled.global_penalties.is_empty(), "Should have compiled penalties");
        assert!(!compiled.global_bonuses.is_empty(), "Should have compiled bonuses");
    }

    #[test]
    fn test_profile_compiler_metric_rules() {
        let test_data = Phase3TestData::new();
        let compiler = ProfileCompiler::new();
        let compiled = compiler.compile_profile(&test_data.profile_config).unwrap();
        
        // Verify metric rules compilation
        let word_count_rule = compiled.metric_rules.iter()
            .find(|rule| rule.metric_name == "word_count");
        assert!(word_count_rule.is_some(), "Should have word count rule");
        
        let word_count_rule = word_count_rule.unwrap();
        assert_eq!(word_count_rule.weight, 1.5);
        assert_eq!(word_count_rule.category, MetricCategory::Content);
    }

    #[test]
    fn test_profile_aware_scorer_basic_scoring() {
        let test_data = Phase3TestData::new();
        let compiler = ProfileCompiler::new();
        let scorer = ProfileAwareScorer::new();
        
        let compiled_profile = compiler.compile_profile(&test_data.profile_config).unwrap();
        
        // Test async scoring
        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(async {
            scorer.calculate_score(&compiled_profile, &test_data.metrics, &test_data.document).await
        });
        
        assert!(result.is_ok(), "Scoring should succeed");
        let scoring_result = result.unwrap();
        
        assert!(scoring_result.final_score >= 0.0 && scoring_result.final_score <= 100.0);
        assert!(!scoring_result.category_scores.is_empty(), "Should have category scores");
        assert!(scoring_result.metrics_processed > 0, "Should have processed metrics");
    }

    #[test]
    fn test_profile_aware_scorer_category_scoring() {
        let test_data = Phase3TestData::new();
        let compiler = ProfileCompiler::new();
        let scorer = ProfileAwareScorer::new();
        
        let compiled_profile = compiler.compile_profile(&test_data.profile_config).unwrap();
        
        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(async {
            scorer.calculate_score(&compiled_profile, &test_data.metrics, &test_data.document).await
        }).unwrap();
        
        // Verify category scoring
        assert!(result.category_scores.contains_key(&MetricCategory::Content));
        assert!(result.category_scores.contains_key(&MetricCategory::Structure));
        assert!(result.category_scores.contains_key(&MetricCategory::SEO));
        
        // Content should have highest weight and significant impact
        let content_score = result.category_scores.get(&MetricCategory::Content).unwrap();
        assert!(content_score.raw_score > 0.0);
        assert_eq!(content_score.weight, 3.0);
    }

    #[test]
    fn test_content_validator_basic_functionality() {
        let test_data = Phase3TestData::new();
        let validator = ContentValidator::new();
        
        let expectations = test_data.profile_config.content_expectations.as_ref().unwrap();
        let result = validator.validate_content_expectations(
            expectations,
            &test_data.metrics,
            &test_data.document
        );
        
        assert!(result.is_ok(), "Content validation should succeed");
        let validation_result = result.unwrap();
        
        assert!(validation_result.compliance_score >= 0.0 && validation_result.compliance_score <= 100.0);
        // With good test data, we should have high compliance
        assert!(validation_result.compliance_score > 80.0, "Should have high compliance with good test data");
    }

    #[test]
    fn test_content_validator_word_count_validation() {
        let mut test_data = Phase3TestData::new();
        let validator = ContentValidator::new();
        
        // Test with insufficient word count
        test_data.metrics.html_analysis.content.word_count = 200; // Below minimum of 300
        
        let expectations = test_data.profile_config.content_expectations.as_ref().unwrap();
        let result = validator.validate_content_expectations(
            expectations,
            &test_data.metrics,
            &test_data.document
        ).unwrap();
        
        // Should have violations for insufficient word count
        let has_word_count_violation = result.violations.iter().any(|v| {
            matches!(v, crate::scoring::ContentViolation::InsufficientWordCount { .. })
        });
        assert!(has_word_count_violation, "Should detect insufficient word count");
        
        // Should have penalties
        assert!(!result.penalties.is_empty(), "Should have penalties for violations");
        
        // Compliance score should be reduced
        assert!(result.compliance_score < 80.0, "Compliance should be reduced due to violations");
    }

    #[test]
    fn test_content_validator_heading_structure() {
        let mut test_data = Phase3TestData::new();
        let validator = ContentValidator::new();
        
        // Test with insufficient headings
        test_data.metrics.html_analysis.structure.heading_count = 1; // Below minimum of 3
        
        let expectations = test_data.profile_config.content_expectations.as_ref().unwrap();
        let result = validator.validate_content_expectations(
            expectations,
            &test_data.metrics,
            &test_data.document
        ).unwrap();
        
        // Should detect heading structure issues
        let has_heading_violation = result.violations.iter().any(|v| {
            matches!(v, crate::scoring::ContentViolation::InvalidHeadingStructure { .. })
        });
        assert!(has_heading_violation, "Should detect heading structure issues");
    }

    #[test]
    fn test_phase3_system_complete_analysis() {
        let test_data = Phase3TestData::new();
        let system = Phase3ScoringSystem::new();
        
        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(async {
            system.complete_analysis(
                &test_data.profile_config,
                &test_data.metrics,
                &test_data.document
            ).await
        });
        
        assert!(result.is_ok(), "Complete analysis should succeed");
        let analysis_result = result.unwrap();
        
        // Verify all components are present
        assert!(!analysis_result.compiled_profile.metric_rules.is_empty());
        assert!(analysis_result.scoring_result.final_score > 0.0);
        assert!(analysis_result.content_validation.is_some());
        
        let content_validation = analysis_result.content_validation.unwrap();
        assert!(content_validation.compliance_score >= 0.0);
    }

    #[test]
    fn test_penalty_application() {
        let mut test_data = Phase3TestData::new();
        let system = Phase3ScoringSystem::new();
        
        // Set up conditions to trigger penalty (low word count)
        test_data.metrics.html_analysis.content.word_count = 250; // Below threshold of 300
        
        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(async {
            system.complete_analysis(
                &test_data.profile_config,
                &test_data.metrics,
                &test_data.document
            ).await
        }).unwrap();
        
        // Should have applied penalties
        assert!(!result.scoring_result.applied_penalties.is_empty(), "Should have applied penalties");
        
        let word_count_penalty = result.scoring_result.applied_penalties.iter()
            .find(|p| p.penalty_id == "low_word_count");
        assert!(word_count_penalty.is_some(), "Should have low word count penalty");
        
        let penalty = word_count_penalty.unwrap();
        assert_eq!(penalty.amount, 20.0);
        assert_eq!(penalty.reason, "Penalty for insufficient content");
    }

    #[test]
    fn test_bonus_application() {
        let mut test_data = Phase3TestData::new();
        let system = Phase3ScoringSystem::new();
        
        // Set up conditions to trigger bonus (high readability)
        test_data.metrics.html_analysis.content.readability_score = Some(85.0); // Above threshold of 80.0
        
        let rt = tokio::runtime::Runtime::new().unwrap();
        let result = rt.block_on(async {
            system.complete_analysis(
                &test_data.profile_config,
                &test_data.metrics,
                &test_data.document
            ).await
        }).unwrap();
        
        // Should have applied bonuses
        assert!(!result.scoring_result.applied_bonuses.is_empty(), "Should have applied bonuses");
        
        let readability_bonus = result.scoring_result.applied_bonuses.iter()
            .find(|b| b.bonus_id == "excellent_readability");
        assert!(readability_bonus.is_some(), "Should have excellent readability bonus");
        
        let bonus = readability_bonus.unwrap();
        assert_eq!(bonus.amount, 10.0);
        assert_eq!(bonus.reason, "Bonus for excellent readability");
    }

    #[test]
    fn test_performance_characteristics() {
        let test_data = Phase3TestData::new();
        let system = Phase3ScoringSystem::new();
        
        // Test multiple scoring operations to verify performance
        let start_time = std::time::Instant::now();
        
        let rt = tokio::runtime::Runtime::new().unwrap();
        for _ in 0..10 {
            let result = rt.block_on(async {
                system.complete_analysis(
                    &test_data.profile_config,
                    &test_data.metrics,
                    &test_data.document
                ).await
            });
            assert!(result.is_ok(), "All scoring operations should succeed");
        }
        
        let elapsed = start_time.elapsed();
        
        // Performance should be reasonable (less than 1 second for 10 operations)
        assert!(elapsed.as_millis() < 1000, "Performance should be acceptable: {:?}", elapsed);
    }

    #[test]
    fn test_caching_effectiveness() {
        let test_data = Phase3TestData::new();
        let compiler = ProfileCompiler::new();
        let scorer = ProfileAwareScorer::new();
        
        let compiled_profile = compiler.compile_profile(&test_data.profile_config).unwrap();
        
        let rt = tokio::runtime::Runtime::new().unwrap();
        
        // First scoring operation (cache miss)
        let start1 = std::time::Instant::now();
        let result1 = rt.block_on(async {
            scorer.calculate_score(&compiled_profile, &test_data.metrics, &test_data.document).await
        }).unwrap();
        let time1 = start1.elapsed();
        
        // Second scoring operation (should benefit from cache)
        let start2 = std::time::Instant::now();
        let result2 = rt.block_on(async {
            scorer.calculate_score(&compiled_profile, &test_data.metrics, &test_data.document).await
        }).unwrap();
        let time2 = start2.elapsed();
        
        // Results should be consistent
        assert!((result1.final_score - result2.final_score).abs() < 0.01, "Results should be consistent");
        
        // Second operation should not be significantly slower (caching working)
        assert!(time2.as_millis() <= time1.as_millis() + 50, "Caching should maintain performance");
    }

    #[test]
    fn test_error_handling() {
        let compiler = ProfileCompiler::new();
        
        // Test with invalid profile configuration
        let mut invalid_profile = Phase3TestData::create_test_profile();
        invalid_profile.metric_weights.clear(); // Remove all weights
        
        // Should handle gracefully or provide meaningful error
        let result = compiler.compile_profile(&invalid_profile);
        // Result may succeed with default weights or fail with clear error
        match result {
            Ok(compiled) => {
                // If it succeeds, should use defaults
                assert!(!compiled.metric_rules.is_empty(), "Should have default metric rules");
            },
            Err(e) => {
                // If it fails, should have meaningful error message
                assert!(!e.to_string().is_empty(), "Error should have meaningful message");
            }
        }
    }

    #[test]
    fn test_content_type_specific_scoring() {
        let mut test_data = Phase3TestData::new();
        let system = Phase3ScoringSystem::new();
        
        // Test with Article content type
        test_data.profile_config.target_content_type = ContentType::Article;
        let rt = tokio::runtime::Runtime::new().unwrap();
        let article_result = rt.block_on(async {
            system.complete_analysis(
                &test_data.profile_config,
                &test_data.metrics,
                &test_data.document
            ).await
        }).unwrap();
        
        // Test with Product content type
        test_data.profile_config.target_content_type = ContentType::Product;
        let product_result = rt.block_on(async {
            system.complete_analysis(
                &test_data.profile_config,
                &test_data.metrics,
                &test_data.document
            ).await
        }).unwrap();
        
        // Different content types should potentially yield different scores
        // (depending on the specific scoring logic and content characteristics)
        assert!(article_result.scoring_result.final_score >= 0.0);
        assert!(product_result.scoring_result.final_score >= 0.0);
    }
}