#![cfg(all(feature = "wasm", target_arch = "wasm32"))]
use js_sys::{Object, Reflect};
use wasm_bindgen::JsValue;
use wasm_bindgen_test::*;
use ries_rs::{init, list_presets, version, wasm_search as search};
fn search_options(level: u32, max_matches: u32) -> JsValue {
let options = Object::new();
Reflect::set(
&options,
&JsValue::from_str("level"),
&JsValue::from_f64(level.into()),
)
.expect("level should be set");
Reflect::set(
&options,
&JsValue::from_str("maxMatches"),
&JsValue::from_f64(max_matches.into()),
)
.expect("maxMatches should be set");
options.into()
}
#[wasm_bindgen_test]
fn test_wasm_init() {
init();
}
#[wasm_bindgen_test]
fn test_wasm_version() {
let v = version();
assert!(!v.is_empty());
assert!(v.contains('.'));
}
#[wasm_bindgen_test]
fn test_wasm_search_rejects_non_finite_target() {
for bad_target in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
let result = search(bad_target, None);
assert!(
result.is_err(),
"search should reject non-finite target {bad_target}"
);
}
}
#[wasm_bindgen_test]
fn test_wasm_basic_search() {
let result = search(2.0, None).expect("search should succeed");
assert!(!result.is_empty(), "search should return results for 2.0");
}
#[wasm_bindgen_test]
fn test_wasm_search_with_options() {
let limited = search(2.0, Some(search_options(3, 1)))
.expect("search with limited options should succeed");
let expanded = search(2.0, Some(search_options(3, 5)))
.expect("search with expanded options should succeed");
assert_eq!(limited.len(), 1, "maxMatches=1 should cap the result count");
assert!(
expanded.len() > limited.len(),
"raising maxMatches should allow more results"
);
}
#[wasm_bindgen_test]
fn test_wasm_search_result_properties() {
let result = search(1.618033988, None).expect("search should succeed");
assert!(
!result.is_empty(),
"search should return results for golden ratio"
);
let first = &result[0];
assert!(!first.lhs.is_empty(), "lhs should not be empty");
assert!(!first.rhs.is_empty(), "rhs should not be empty");
assert!(first.complexity > 0, "complexity should be positive");
}
#[wasm_bindgen_test]
fn test_wasm_match_to_string() {
let result = search(2.0, None).expect("search should succeed");
let first = &result[0];
let s = first.to_string();
assert!(s.contains("="), "to_string should contain '='");
assert!(s.contains("error"), "to_string should contain 'error'");
}
#[wasm_bindgen_test]
fn test_wasm_match_to_json() {
let result = search(2.0, None).expect("search should succeed");
let first = &result[0];
let json = first.to_json().expect("to_json should succeed");
assert!(!json.is_null(), "to_json should return non-null value");
}
#[wasm_bindgen_test]
fn test_wasm_list_presets() {
let presets = list_presets().expect("list_presets should succeed");
assert!(!presets.is_undefined(), "presets should be defined");
}
#[wasm_bindgen_test]
fn test_wasm_search_exact_value() {
let result = search(std::f64::consts::PI, None).expect("search should succeed");
assert!(!result.is_empty(), "search should return results for pi");
let has_small_error = result.iter().any(|m| m.error.abs() < 1e-10);
assert!(has_small_error, "should find very close matches for pi");
}