#![cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
use webpage_quality_analyzer::wasm::*;
wasm_bindgen_test_configure!(run_in_browser);
const TEST_HTML: &str = r#"<!DOCTYPE html>
<html lang="en">
<head>
<title>WASM Test Page</title>
<meta name="description" content="Testing WASM bindings">
</head>
<body>
<h1>Main Heading</h1>
<p>This is test content for WASM API testing.</p>
<p>Second paragraph with more content.</p>
</body>
</html>"#;
#[wasm_bindgen_test]
fn test_wasm_analyzer_new() {
let analyzer = WasmAnalyzer::new();
assert!(analyzer.is_ok(), "WasmAnalyzer::new() should succeed");
}
#[wasm_bindgen_test]
fn test_wasm_analyzer_with_config() {
let analyzer = WasmAnalyzer::with_config(Some("general".to_string()), Some(true), Some(true));
assert!(
analyzer.is_ok(),
"WasmAnalyzer::with_config() should succeed"
);
}
#[wasm_bindgen_test]
async fn test_wasm_analyze() {
let analyzer = WasmAnalyzer::new().unwrap();
let result = analyzer.analyze(TEST_HTML.to_string()).await;
assert!(result.is_ok(), "analyze() should succeed");
}
#[wasm_bindgen_test]
async fn test_wasm_analyze_with_url() {
let analyzer = WasmAnalyzer::new().unwrap();
let result = analyzer
.analyze_with_url(
"https://example.com/test".to_string(),
TEST_HTML.to_string(),
)
.await;
assert!(result.is_ok(), "analyze_with_url() should succeed");
}
#[wasm_bindgen_test]
fn test_wasm_get_available_profiles() {
let profiles = WasmAnalyzer::get_available_profiles();
assert!(profiles.len() >= 8, "Should have at least 8 profiles");
assert!(profiles.contains(&"general".to_string()));
assert!(profiles.contains(&"content_article".to_string()));
assert!(profiles.contains(&"news".to_string()));
}
#[wasm_bindgen_test]
fn test_wasm_get_version() {
let version = WasmAnalyzer::get_version();
assert!(!version.is_empty(), "Version should not be empty");
assert!(version.contains('.'), "Version should have format X.Y.Z");
}
#[wasm_bindgen_test]
fn test_wasm_batch_analyzer_new() {
let batch_analyzer = WasmBatchAnalyzer::new();
assert!(
batch_analyzer.is_ok(),
"WasmBatchAnalyzer::new() should succeed"
);
}
#[wasm_bindgen_test]
fn test_wasm_field_selector() {
let _selector = WasmFieldSelector::new()
.include_fields(vec!["score".to_string(), "url".to_string()])
.exclude_fields(vec!["metadata".to_string()]);
assert!(true, "WasmFieldSelector builder should work");
}
#[wasm_bindgen_test]
fn test_wasm_analyzer_builder() {
let builder = WasmAnalyzerBuilder::new()
.with_profile("news".to_string())
.enable_nlp(true)
.add_report(true);
let analyzer = builder.build();
assert!(
analyzer.is_ok(),
"WasmAnalyzerBuilder should build successfully"
);
}
#[wasm_bindgen_test]
fn test_wasm_builder_metric_customization() {
let builder = WasmAnalyzerBuilder::new()
.enable_metric("word_count".to_string())
.disable_metric("some_metric".to_string())
.set_metric_weight("title_length".to_string(), 2.0);
let analyzer = builder.build();
assert!(
analyzer.is_ok(),
"Builder with metric customization should work"
);
}
#[wasm_bindgen_test]
fn test_wasm_penalties_below() {
let analyzer = WasmAnalyzerBuilder::new()
.with_profile("general".to_string())
.add_penalty_below(
"short_content".to_string(),
"word_count".to_string(),
100.0,
10.0,
"Too short".to_string(),
)
.build();
assert!(analyzer.is_ok(), "add_penalty_below should work");
}
#[wasm_bindgen_test]
fn test_wasm_bonuses_above() {
let analyzer = WasmAnalyzerBuilder::new()
.with_profile("general".to_string())
.add_bonus_above(
"long_content".to_string(),
"word_count".to_string(),
500.0,
5.0,
"Long content".to_string(),
)
.build();
assert!(analyzer.is_ok(), "add_bonus_above should work");
}
#[wasm_bindgen_test]
fn test_wasm_config_json() {
let config = r#"{"analyzer": {"profile": "general"}}"#;
let analyzer = WasmAnalyzerBuilder::from_config_json(config);
assert!(analyzer.is_ok(), "from_config_json should work");
}
#[wasm_bindgen_test]
fn test_wasm_config_yaml() {
let config = "analyzer:\n profile: news\n";
let analyzer = WasmAnalyzerBuilder::from_config_yaml(config);
assert!(analyzer.is_ok(), "from_config_yaml should work");
}
#[wasm_bindgen_test]
fn test_wasm_config_toml() {
let config = "[analyzer]\nprofile = \"content_article\"\n";
let analyzer = WasmAnalyzerBuilder::from_config_toml(config);
assert!(analyzer.is_ok(), "from_config_toml should work");
}
#[wasm_bindgen_test]
async fn test_wasm_all_profiles() {
let profiles = vec![
"content_article",
"news",
"product",
"homepage",
"portfolio",
"login_page",
"general",
"blog",
];
for profile in profiles {
let analyzer =
WasmAnalyzer::with_config(Some(profile.to_string()), Some(false), Some(false)).unwrap();
let result = analyzer.analyze(TEST_HTML.to_string()).await;
assert!(result.is_ok(), "Profile {} should work", profile);
}
}
#[wasm_bindgen_test]
fn test_wasm_builder_chaining() {
let analyzer = WasmAnalyzerBuilder::new()
.with_profile("news".to_string())
.enable_nlp(true)
.add_report(true)
.set_metric_weight("word_count".to_string(), 1.5)
.add_penalty_below(
"short_content".to_string(),
"word_count".to_string(),
50.0,
5.0,
"Too short".to_string(),
)
.add_bonus_above(
"good_length".to_string(),
"word_count".to_string(),
500.0,
5.0,
"Good length".to_string(),
)
.build();
assert!(analyzer.is_ok(), "Complex builder chain should work");
}
#[wasm_bindgen_test]
fn test_wasm_error_invalid_profile() {
let analyzer = WasmAnalyzer::with_config(
Some("nonexistent_profile".to_string()),
Some(false),
Some(false),
);
assert!(analyzer.is_err(), "Invalid profile should fail");
}
#[wasm_bindgen_test]
async fn test_wasm_batch_analysis() {
let batch_analyzer = WasmBatchAnalyzer::new().unwrap();
let html_docs = vec![TEST_HTML.to_string(), TEST_HTML.to_string()];
let result = batch_analyzer.analyze_batch(html_docs).await;
assert!(result.is_ok(), "Batch analysis should succeed");
let batch_result = result.unwrap();
assert_eq!(batch_result.get_count(), 2, "Should analyze 2 documents");
}
#[wasm_bindgen_test]
async fn test_wasm_analyze_with_fields() {
let analyzer = WasmAnalyzer::new().unwrap();
let fields = vec!["score".to_string(), "url".to_string()];
let result = analyzer
.analyze_with_fields(TEST_HTML.to_string(), fields)
.await;
assert!(result.is_ok(), "analyze_with_fields should succeed");
}
#[wasm_bindgen_test]
async fn test_wasm_analyze_with_selector() {
let analyzer = WasmAnalyzer::new().unwrap();
let selector =
WasmFieldSelector::new().include_fields(vec!["score".to_string(), "verdict".to_string()]);
let result = analyzer
.analyze_with_selector(TEST_HTML.to_string(), &selector)
.await;
assert!(result.is_ok(), "analyze_with_selector should succeed");
}
#[wasm_bindgen_test]
async fn test_wasm_final_verification() {
web_sys::console::log_1(&"=== WASM API Final Verification ===".into());
let analyzer = WasmAnalyzer::new().unwrap();
let _result1 = analyzer.analyze(TEST_HTML.to_string()).await.unwrap();
web_sys::console::log_1(&"✅ Basic analyzer works".into());
let analyzer2 = WasmAnalyzerBuilder::new()
.with_profile("news".to_string())
.build()
.unwrap();
let _result2 = analyzer2.analyze(TEST_HTML.to_string()).await.unwrap();
web_sys::console::log_1(&"✅ Builder pattern works".into());
let analyzer3 =
WasmAnalyzerBuilder::from_config_json(r#"{"analyzer": {"profile": "general"}}"#).unwrap();
let _result3 = analyzer3.analyze(TEST_HTML.to_string()).await.unwrap();
web_sys::console::log_1(&"✅ Config loading works".into());
let batch = WasmBatchAnalyzer::new().unwrap();
let _batch_result = batch
.analyze_batch(vec![TEST_HTML.to_string()])
.await
.unwrap();
web_sys::console::log_1(&"✅ Batch processing works".into());
let selector = WasmFieldSelector::new().include_fields(vec!["score".to_string()]);
let _result5 = analyzer
.analyze_with_selector(TEST_HTML.to_string(), &selector)
.await
.unwrap();
web_sys::console::log_1(&"✅ Field selection works".into());
web_sys::console::log_1(&"=== ✅ ALL WASM FEATURES VERIFIED ===".into());
}