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
//! Phase 3: Custom Threshold Tests
//!
//! Comprehensive test suite for the custom threshold customization feature.
//! Tests cover Rust API, validation, edge cases, and scoring behavior.

use webpage_quality_analyzer::async_runtime::DefaultRuntime;
use webpage_quality_analyzer::{Analyzer, QualityBand};

const TEST_HTML_SHORT: &str = r#"
<!DOCTYPE html>
<html>
<head>
    <title>Short Article</title>
    <meta name="description" content="A brief description">
</head>
<body>
    <h1>Short Content</h1>
    <p>This is a very short article with minimal content.</p>
</body>
</html>
"#;

const TEST_HTML_MEDIUM: &str = r#"
<!DOCTYPE html>
<html>
<head>
    <title>Medium Length Article For Testing Purposes</title>
    <meta name="description" content="A medium length article for comprehensive testing">
</head>
<body>
    <h1>Medium Length Content</h1>
    <p>This article has a reasonable amount of content. It contains multiple sentences
    to ensure we have enough text for meaningful word count analysis.</p>
    <p>The second paragraph adds more substance to the document, making it suitable
    for testing threshold boundaries and scoring behavior across different ranges.</p>
    <p>A third paragraph ensures we're well within the medium range for most metrics,
    providing stable test conditions for threshold customization validation.</p>
</body>
</html>
"#;

const TEST_HTML_LONG: &str = r#"
<!DOCTYPE html>
<html>
<head>
    <title>Comprehensive Long Article With Extended Title For Testing</title>
    <meta name="description" content="An extensive article with substantial content for thorough threshold testing">
</head>
<body>
    <h1>Comprehensive Long Content</h1>
    <h2>Introduction</h2>
    <p>This is a long article designed to test threshold behaviors at the upper end of metric ranges.
    It contains multiple paragraphs, headings, and substantial textual content to ensure comprehensive
    coverage of all threshold scenarios.</p>
    
    <h2>Main Content Section</h2>
    <p>The main content section provides extensive detail on various topics. Each paragraph is carefully
    crafted to contribute to the overall word count while maintaining readability and structure.</p>
    <p>We include multiple sentences in each paragraph to ensure natural text flow and realistic
    content analysis. This helps validate that our threshold customizations work correctly with
    real-world content patterns.</p>
    
    <h2>Additional Details</h2>
    <p>Further elaboration on the topic demonstrates the depth of coverage this article provides.
    The extensive content allows us to test how custom thresholds affect scoring when metric values
    are well above typical optimal ranges.</p>
    <p>This comprehensive approach ensures our threshold system can handle content of various lengths
    and complexities without issues.</p>
    
    <h2>Conclusion</h2>
    <p>In conclusion, this long-form content provides an excellent test case for custom threshold
    validation and scoring behavior analysis across the full spectrum of content lengths.</p>
</body>
</html>
"#;

const TEST_URL: &str = "http://example.com/test";

#[tokio::test]
async fn test_set_metric_threshold_basic() {
    // Test basic threshold customization
    let analyzer: Analyzer = Analyzer::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            50.0,   // min - below 50 words scores poorly
            200.0,  // optimal_min - 200+ words is good
            800.0,  // optimal_max - up to 800 words is optimal
            2000.0, // max - beyond 2000 doesn't improve score
        )
        .unwrap()
        .build()
        .unwrap();

    let report = analyzer
        .run(TEST_URL, Some(TEST_HTML_MEDIUM))
        .await
        .unwrap();

    // Verify report generates successfully
    assert!(report.score > 0.0);
    assert!(matches!(
        report.verdict,
        QualityBand::VeryPoor
            | QualityBand::Poor
            | QualityBand::Fair
            | QualityBand::Good
            | QualityBand::Excellent
    ));
}

#[tokio::test]
async fn test_set_simple_threshold() {
    // Test the convenience method for single optimal value
    let analyzer: Analyzer = Analyzer::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_simple_threshold(
            "title_len",
            20.0,  // min
            50.0,  // optimal (single value)
            100.0, // max
        )
        .unwrap()
        .build()
        .unwrap();

    let report = analyzer
        .run(TEST_URL, Some(TEST_HTML_MEDIUM))
        .await
        .unwrap();
    assert!(report.score > 0.0);
}

#[tokio::test]
async fn test_multiple_threshold_customizations() {
    // Test setting thresholds for multiple metrics
    let analyzer: Analyzer = Analyzer::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_thresholds(vec![
            ("word_count", 100.0, 300.0, 1000.0, 3000.0),
            ("title_len", 20.0, 40.0, 70.0, 120.0),
            ("paragraph_count", 2.0, 4.0, 12.0, 25.0),
        ])
        .unwrap()
        .build()
        .unwrap();

    let report = analyzer.run(TEST_URL, Some(TEST_HTML_LONG)).await.unwrap();
    assert!(report.score > 0.0);
}

#[tokio::test]
async fn test_threshold_affects_scoring() {
    // Verify that custom thresholds actually change scoring behavior

    // Default thresholds
    let analyzer_default: Analyzer = Analyzer::builder()
        .with_profile_name("content_article")
        .unwrap()
        .build()
        .unwrap();

    let report_default = analyzer_default
        .run(TEST_URL, Some(TEST_HTML_SHORT))
        .await
        .unwrap();

    // Custom thresholds that are more lenient for short content
    let analyzer_custom: Analyzer = Analyzer::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            10.0,  // Very low min
            20.0,  // Low optimal_min (favors short content)
            100.0, // Low optimal_max
            500.0, // Low max
        )
        .unwrap()
        .build()
        .unwrap();

    let report_custom = analyzer_custom
        .run(TEST_URL, Some(TEST_HTML_SHORT))
        .await
        .unwrap();

    // Both should produce valid scores
    assert!(report_default.score >= 0.0);
    assert!(report_custom.score >= 0.0);

    // Scores should exist (may be different due to threshold changes)
    // We don't assert which is higher because it depends on profile configuration
}

#[tokio::test]
async fn test_threshold_validation_min_greater_than_optimal_min() {
    // Test: min must be < optimal_min
    let result = Analyzer::<DefaultRuntime>::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            500.0,  // min
            100.0,  // optimal_min - ERROR: less than min
            800.0,  // optimal_max
            2000.0, // max
        );

    assert!(result.is_err(), "Should reject min >= optimal_min");
    let err = result.unwrap_err().to_string();
    assert!(err.contains("min") && err.contains("optimal_min"));
}

#[tokio::test]
async fn test_threshold_validation_optimal_min_greater_than_optimal_max() {
    // Test: optimal_min must be <= optimal_max
    let result = Analyzer::<DefaultRuntime>::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            100.0,  // min
            800.0,  // optimal_min - ERROR: greater than optimal_max
            500.0,  // optimal_max
            2000.0, // max
        );

    assert!(result.is_err(), "Should reject optimal_min > optimal_max");
    let err = result.unwrap_err().to_string();
    assert!(err.contains("optimal_min") && err.contains("optimal_max"));
}

#[tokio::test]
async fn test_threshold_validation_optimal_max_greater_than_max() {
    // Test: optimal_max must be < max
    let result = Analyzer::<DefaultRuntime>::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            100.0,  // min
            500.0,  // optimal_min
            2000.0, // optimal_max - ERROR: equal to max
            2000.0, // max
        );

    assert!(result.is_err(), "Should reject optimal_max >= max");
    let err = result.unwrap_err().to_string();
    assert!(err.contains("optimal_max") && err.contains("max"));
}

#[tokio::test]
async fn test_threshold_validation_negative_values() {
    // Test: negative values should be rejected
    let result = Analyzer::<DefaultRuntime>::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            -100.0, // ERROR: negative min
            500.0,
            800.0,
            2000.0,
        );

    assert!(result.is_err(), "Should reject negative values");
    let err = result.unwrap_err().to_string();
    assert!(err.contains("negative") || err.contains("min"));
}

#[tokio::test]
async fn test_threshold_validation_infinite_values() {
    // Test: infinite values should be rejected
    let result = Analyzer::<DefaultRuntime>::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            100.0,
            500.0,
            f32::INFINITY, // ERROR: infinite
            2000.0,
        );

    assert!(result.is_err(), "Should reject infinite values");
    let err = result.unwrap_err().to_string();
    assert!(err.contains("finite") || err.contains("optimal_max"));
}

#[tokio::test]
async fn test_threshold_validation_nan_values() {
    // Test: NaN values should be rejected
    let result = Analyzer::<DefaultRuntime>::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            100.0,
            f32::NAN, // ERROR: NaN
            800.0,
            2000.0,
        );

    assert!(result.is_err(), "Should reject NaN values");
    let err = result.unwrap_err().to_string();
    assert!(err.contains("finite") || err.contains("optimal_min"));
}

#[tokio::test]
async fn test_invalid_metric_name() {
    // Test: unknown metric names should be rejected
    let result = Analyzer::<DefaultRuntime>::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold("nonexistent_metric_xyz", 100.0, 500.0, 800.0, 2000.0);

    assert!(result.is_err(), "Should reject unknown metric names");
    let err = result.unwrap_err().to_string();
    assert!(err.contains("nonexistent_metric_xyz") || err.contains("Unknown metric"));
}

#[tokio::test]
async fn test_threshold_chaining_with_other_operations() {
    // Test that threshold setting chains properly with other builder methods
    let analyzer: Analyzer = Analyzer::builder()
        .with_profile_name("content_article")
        .unwrap()
        .disable_metric("readability_fk")
        .unwrap()
        .set_metric_threshold("word_count", 100.0, 500.0, 2000.0, 5000.0)
        .unwrap()
        .enable_metric("paragraph_count")
        .unwrap()
        .set_simple_threshold("title_len", 20.0, 60.0, 120.0)
        .unwrap()
        .build()
        .unwrap();

    let report = analyzer
        .run(TEST_URL, Some(TEST_HTML_MEDIUM))
        .await
        .unwrap();
    assert!(report.score > 0.0);
}

#[tokio::test]
async fn test_threshold_override_profile_default() {
    // Test that custom thresholds override profile defaults

    // This test verifies the integration works, even if we can't directly observe
    // the internal threshold values, we can confirm no errors occur
    let analyzer: Analyzer = Analyzer::builder()
        .with_profile_name("news")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            50.0, // Different from profile default
            300.0,
            1500.0,
            4000.0,
        )
        .unwrap()
        .build()
        .unwrap();

    let report = analyzer
        .run(TEST_URL, Some(TEST_HTML_MEDIUM))
        .await
        .unwrap();
    assert!(report.score > 0.0);
}

#[tokio::test]
async fn test_edge_case_equal_optimal_values() {
    // Test edge case: optimal_min == optimal_max (flat optimal range)
    let analyzer: Analyzer = Analyzer::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "word_count",
            100.0,
            500.0,
            500.0, // optimal_min == optimal_max
            2000.0,
        )
        .unwrap()
        .build()
        .unwrap();

    let report = analyzer
        .run(TEST_URL, Some(TEST_HTML_MEDIUM))
        .await
        .unwrap();
    assert!(report.score > 0.0);
}

#[tokio::test]
async fn test_edge_case_very_narrow_ranges() {
    // Test edge case: very small differences between thresholds
    let analyzer: Analyzer = Analyzer::builder()
        .with_profile_name("content_article")
        .unwrap()
        .set_metric_threshold(
            "title_len",
            10.0,
            10.1, // Very close to min
            10.2, // Very narrow optimal range
            10.3, // Very close to optimal_max
        )
        .unwrap()
        .build()
        .unwrap();

    let report = analyzer
        .run(TEST_URL, Some(TEST_HTML_MEDIUM))
        .await
        .unwrap();
    assert!(report.score >= 0.0);
}

#[tokio::test]
async fn test_threshold_with_different_profiles() {
    // Test that thresholds work with various profiles
    let profiles = ["content_article", "news", "blog"];

    for profile in &profiles {
        let analyzer: Analyzer = Analyzer::builder()
            .with_profile_name(profile)
            .unwrap()
            .set_metric_threshold("word_count", 100.0, 400.0, 1500.0, 4000.0)
            .unwrap()
            .build()
            .unwrap();

        let report = analyzer
            .run(TEST_URL, Some(TEST_HTML_MEDIUM))
            .await
            .unwrap();
        assert!(
            report.score >= 0.0,
            "Profile {} should work with custom thresholds",
            profile
        );
    }
}

#[tokio::test]
async fn test_comprehensive_threshold_workflow() {
    // Comprehensive workflow test demonstrating full threshold customization
    let analyzer: Analyzer = Analyzer::builder()
        .with_profile_name("content_article")
        .unwrap()
        // Customize multiple metrics with full control
        .set_metric_threshold("word_count", 150.0, 600.0, 2500.0, 6000.0)
        .unwrap()
        .set_simple_threshold("title_len", 25.0, 55.0, 110.0)
        .unwrap()
        .set_metric_threshold("paragraph_count", 3.0, 6.0, 18.0, 35.0)
        .unwrap()
        // Mix with metric toggling
        .disable_metric("readability_fk")
        .unwrap()
        .enable_metric("headings_count")
        .unwrap()
        .build()
        .unwrap();

    // Test with different content lengths
    let report_short = analyzer.run(TEST_URL, Some(TEST_HTML_SHORT)).await.unwrap();
    let report_medium = analyzer
        .run(TEST_URL, Some(TEST_HTML_MEDIUM))
        .await
        .unwrap();
    let report_long = analyzer.run(TEST_URL, Some(TEST_HTML_LONG)).await.unwrap();

    // All should produce valid scores
    assert!(report_short.score >= 0.0);
    assert!(report_medium.score >= 0.0);
    assert!(report_long.score >= 0.0);

    println!("✅ Comprehensive threshold workflow test passed!");
    println!("   - Short content score: {:.2}", report_short.score);
    println!("   - Medium content score: {:.2}", report_medium.score);
    println!("   - Long content score: {:.2}", report_long.score);
}