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
//! Phase 5 Integration Tests
//!
//! Tests for the User Configuration Interface including:
//! - ProfileBuilder API with fluent interface
//! - Content type presets
//! - Custom metric weights and thresholds
//! - ProfileTemplates system
//! - Template-based customization
//! - End-to-end profile creation and usage

use webpage_quality_analyzer::config::enhanced_models::QualityBandConfig;
use webpage_quality_analyzer::config::profile_builder::ContentType;
use webpage_quality_analyzer::config::{ProfileBuilder, ProfileTemplates};
use webpage_quality_analyzer::metrics::ThresholdSet;

#[tokio::test]
async fn test_profile_builder_basic_usage() {
    // Test basic ProfileBuilder creation with minimal configuration
    let profile = ProfileBuilder::new("basic_test")
        .with_description("A basic test profile")
        .build();

    assert!(profile.is_ok(), "Profile building should succeed");
    let profile = profile.unwrap();

    assert_eq!(profile.metadata.name, "basic_test");
    assert_eq!(profile.metadata.description, "A basic test profile");
    assert!(
        !profile.category_weights.is_empty(),
        "Should have default category weights"
    );

    // Verify weights sum to 1.0 (approximately)
    let total: f32 = profile.category_weights.values().sum();
    assert!(
        (total - 1.0).abs() < 0.01,
        "Weights should sum to 1.0, got {}",
        total
    );
}

#[tokio::test]
async fn test_profile_builder_with_long_form_article() {
    // Test LongFormArticle preset
    let profile = ProfileBuilder::new("long_article")
        .for_content_type(ContentType::LongFormArticle)
        .build()
        .expect("Failed to build long-form article profile");

    // Content should be heavily weighted for long-form articles
    assert!(
        profile.category_weights.get("content").unwrap() >= &0.70,
        "Long-form articles should have high content weight"
    );

    // Should have word count expectations
    assert!(
        profile.content_expectations.word_count.is_some(),
        "Long-form articles should have word count expectations"
    );

    let word_count = profile.content_expectations.word_count.as_ref().unwrap();
    assert!(
        word_count.optimal_range.0 >= 500,
        "Long-form articles should expect substantial word count"
    );
}

#[tokio::test]
async fn test_profile_builder_with_news_article() {
    // Test NewsArticle preset
    let profile = ProfileBuilder::new("news")
        .for_content_type(ContentType::NewsArticle)
        .build()
        .expect("Failed to build news article profile");

    // News articles balance content and SEO
    let content_weight = profile.category_weights.get("content").unwrap();
    let seo_weight = profile.category_weights.get("seo").unwrap();

    assert!(
        content_weight > &0.30,
        "News should have decent content weight"
    );
    assert!(
        seo_weight > &0.20,
        "News should have significant SEO weight"
    );

    // News articles should have concise word count expectations
    let word_count = profile.content_expectations.word_count.as_ref().unwrap();
    assert!(
        word_count.optimal_range.1 < 1500,
        "News articles should be relatively concise"
    );
}

#[tokio::test]
async fn test_profile_builder_with_product_page() {
    // Test ProductPage preset
    let profile = ProfileBuilder::new("product")
        .for_content_type(ContentType::ProductPage)
        .build()
        .expect("Failed to build product page profile");

    // Product pages emphasize SEO and technical performance
    let seo_weight = profile.category_weights.get("seo").unwrap();
    let technical_weight = profile.category_weights.get("technical").unwrap();

    assert!(seo_weight > &0.25, "Products need strong SEO");
    assert!(
        technical_weight > &0.10,
        "Products need good technical performance"
    );

    // Should have minimal word count requirements
    let word_count = profile.content_expectations.word_count.as_ref().unwrap();
    assert!(
        word_count.minimum < 200,
        "Product pages can have minimal text"
    );
}

#[tokio::test]
async fn test_profile_builder_custom_weights() {
    // Test setting custom metric weights
    let profile = ProfileBuilder::new("custom_weights")
        .with_metric_weight("word_count", 0.50)
        .expect("Failed to set word_count weight")
        .with_metric_weight("title_len", 0.30)
        .expect("Failed to set title_len weight")
        .build()
        .expect("Failed to build profile with custom weights");

    // Check metric overrides were applied
    assert!(
        profile.metric_overrides.contains_key("word_count"),
        "word_count override should be present"
    );
    assert!(
        profile.metric_overrides.contains_key("title_len"),
        "title_len override should be present"
    );

    assert_eq!(
        profile.metric_overrides.get("word_count").unwrap().weight,
        0.50
    );
    assert_eq!(
        profile.metric_overrides.get("title_len").unwrap().weight,
        0.30
    );
}

#[tokio::test]
async fn test_profile_builder_custom_thresholds() {
    // Test setting custom thresholds for a metric
    let thresholds = ThresholdSet {
        excellent: 2000.0,
        good: 1000.0,
        fair: 500.0,
        poor: 200.0,
    };

    let profile = ProfileBuilder::new("custom_thresholds")
        .with_custom_threshold("word_count", thresholds.clone())
        .expect("Failed to set custom thresholds")
        .build()
        .expect("Failed to build profile with custom thresholds");

    let word_count_override = profile.metric_overrides.get("word_count").unwrap();
    assert!(
        word_count_override.thresholds.is_some(),
        "Custom thresholds should be set"
    );

    let set_thresholds = word_count_override.thresholds.as_ref().unwrap();
    assert_eq!(set_thresholds.excellent, 2000.0);
    assert_eq!(set_thresholds.good, 1000.0);
}

#[tokio::test]
async fn test_profile_builder_invalid_weight() {
    // Test that invalid weights are rejected
    let result = ProfileBuilder::new("invalid").with_metric_weight("word_count", 1.5);

    assert!(result.is_err(), "Invalid weight should be rejected");
}

#[tokio::test]
async fn test_profile_builder_all_content_types() {
    // Test that all content type presets work
    let content_types = vec![
        ContentType::LongFormArticle,
        ContentType::NewsArticle,
        ContentType::ProductPage,
        ContentType::LandingPage,
        ContentType::Portfolio,
        ContentType::Documentation,
        ContentType::AboutPage,
    ];

    for content_type in content_types {
        let profile = ProfileBuilder::new("test")
            .for_content_type(content_type)
            .build();

        assert!(
            profile.is_ok(),
            "Content type {:?} should build successfully",
            content_type
        );
    }
}

#[tokio::test]
async fn test_profile_builder_method_chaining() {
    // Test fluent interface with extensive chaining
    let profile = ProfileBuilder::new("chained")
        .for_content_type(ContentType::LongFormArticle)
        .with_description("A heavily customized profile")
        .with_tags(vec![
            "test".to_string(),
            "custom".to_string(),
            "chained".to_string(),
        ])
        .with_metric_weight("word_count", 0.40)
        .expect("Failed to set word_count weight")
        .with_metric_weight("title_len", 0.20)
        .expect("Failed to set title_len weight")
        .build()
        .expect("Failed to build chained profile");

    assert_eq!(profile.metadata.name, "chained");
    assert_eq!(profile.metadata.description, "A heavily customized profile");
    assert_eq!(profile.metadata.tags.len(), 3);
    assert!(profile.metric_overrides.contains_key("word_count"));
    assert!(profile.metric_overrides.contains_key("title_len"));
}

#[tokio::test]
async fn test_profile_templates_get_template() {
    // Test getting templates by name
    let templates = vec![
        "content_article",
        "news",
        "product",
        "landing_page",
        "portfolio",
    ];

    for template_name in templates {
        let profile = ProfileTemplates::get_template(template_name);
        assert!(
            profile.is_ok(),
            "Template '{}' should be available",
            template_name
        );

        let profile = profile.unwrap();
        assert!(
            !profile.metadata.name.is_empty(),
            "Template should have a name"
        );
        assert!(
            !profile.category_weights.is_empty(),
            "Template should have category weights"
        );
    }
}

#[tokio::test]
async fn test_profile_templates_unknown_template() {
    // Test that unknown template names are handled properly
    let result = ProfileTemplates::get_template("non_existent_template");
    assert!(result.is_err(), "Unknown template should return error");
}

#[tokio::test]
async fn test_profile_templates_list() {
    // Test listing all available templates
    let templates = ProfileTemplates::list_templates();

    assert!(!templates.is_empty(), "Should have at least one template");
    assert!(
        templates.contains(&"content_article"),
        "Should include content_article template"
    );
    assert!(templates.contains(&"news"), "Should include news template");
    assert!(
        templates.contains(&"product"),
        "Should include product template"
    );
}

#[tokio::test]
async fn test_end_to_end_custom_profile() {
    // Test creating a custom profile and using it in analysis
    let profile = ProfileBuilder::new("e2e_test")
        .for_content_type(ContentType::LongFormArticle)
        .with_description("End-to-end test profile")
        .with_metric_weight("word_count", 0.60)
        .expect("Failed to set word_count weight")
        .build()
        .expect("Failed to build e2e profile");

    // Note: Full analyzer integration with custom profiles will be added in future work
    // For now, we just verify the profile was created correctly
    assert_eq!(profile.metadata.name, "e2e_test");
    assert_eq!(
        profile.metric_overrides.get("word_count").unwrap().weight,
        0.60
    );
}

#[tokio::test]
async fn test_profile_builder_category_weights() {
    // Test setting category weights directly
    let mut weights = std::collections::HashMap::new();
    weights.insert("content".to_string(), 0.50);
    weights.insert("seo".to_string(), 0.30);
    weights.insert("technical".to_string(), 0.10);
    weights.insert("structure".to_string(), 0.05);
    weights.insert("accessibility".to_string(), 0.05);

    let profile = ProfileBuilder::new("custom_categories")
        .with_category_weights(weights.clone())
        .build()
        .expect("Failed to build profile with custom category weights");

    // Verify weights were applied (may be normalized)
    assert!(profile.category_weights.contains_key("content"));
    assert!(profile.category_weights.contains_key("seo"));
}

#[tokio::test]
async fn test_profile_builder_metric_enable_disable() {
    // Test enabling/disabling specific metrics
    let profile = ProfileBuilder::new("metric_toggle")
        .with_metric_enabled("word_count", true)
        .with_metric_enabled("title_len", false)
        .build()
        .expect("Failed to build profile with toggled metrics");

    let word_count_override = profile.metric_overrides.get("word_count").unwrap();
    let title_override = profile.metric_overrides.get("title_len").unwrap();

    assert!(word_count_override.enabled, "word_count should be enabled");
    assert!(!title_override.enabled, "title_len should be disabled");
}

#[tokio::test]
async fn test_profile_builder_quality_bands() {
    // Test setting custom quality band thresholds
    let bands = QualityBandConfig {
        excellent: 95.0,
        good: 80.0,
        fair: 60.0,
        poor: 40.0,
    };

    let profile = ProfileBuilder::new("custom_bands")
        .with_quality_bands(bands.clone())
        .build()
        .expect("Failed to build profile with custom quality bands");

    assert_eq!(profile.quality_bands.excellent, 95.0);
    assert_eq!(profile.quality_bands.good, 80.0);
    assert_eq!(profile.quality_bands.fair, 60.0);
    assert_eq!(profile.quality_bands.poor, 40.0);
}

#[tokio::test]
async fn test_profile_templates_content_article_characteristics() {
    // Test that content_article template has expected characteristics
    let profile = ProfileTemplates::get_template("content_article")
        .expect("content_article template should exist");

    // Should heavily favor content metrics
    let content_weight = profile
        .category_weights
        .get("content")
        .expect("content category should exist");
    assert!(
        content_weight >= &0.70,
        "Content articles should heavily weight content, got {}",
        content_weight
    );

    // Should have substantial word count expectations
    let word_count_exp = profile
        .content_expectations
        .word_count
        .expect("Should have word count expectations");
    assert!(
        word_count_exp.optimal_range.0 >= 500,
        "Content articles should expect substantial text"
    );
    assert!(
        word_count_exp.optimal_range.1 >= 1500,
        "Content articles should support long-form content"
    );
}

#[tokio::test]
async fn test_profile_templates_news_characteristics() {
    // Test that news template has expected characteristics
    let profile = ProfileTemplates::get_template("news").expect("news template should exist");

    // Should balance content and SEO
    let content_weight = profile
        .category_weights
        .get("content")
        .expect("content category should exist");
    let seo_weight = profile
        .category_weights
        .get("seo")
        .expect("seo category should exist");

    assert!(
        content_weight > &0.30,
        "News should have reasonable content weight"
    );
    assert!(seo_weight > &0.20, "News should have strong SEO weight");

    // Should have moderate word count expectations
    let word_count_exp = profile
        .content_expectations
        .word_count
        .expect("Should have word count expectations");
    assert!(
        word_count_exp.optimal_range.0 >= 200,
        "News articles should have minimum text"
    );
    assert!(
        word_count_exp.optimal_range.1 <= 1000,
        "News articles should be relatively concise"
    );
}

#[tokio::test]
async fn test_profile_templates_product_characteristics() {
    // Test that product template has expected characteristics
    let profile = ProfileTemplates::get_template("product").expect("product template should exist");

    // Should emphasize SEO and technical
    let seo_weight = profile
        .category_weights
        .get("seo")
        .expect("seo category should exist");
    assert!(
        seo_weight > &0.25,
        "Product pages need strong SEO, got {}",
        seo_weight
    );

    // Should have flexible word count
    let word_count_exp = profile
        .content_expectations
        .word_count
        .expect("Should have word count expectations");
    assert!(
        word_count_exp.minimum < 200,
        "Product pages can have minimal text"
    );
}