#![cfg(target_arch = "wasm32")]
use std::sync::Once;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
static INIT: Once = Once::new();
fn init_test_logging() {
INIT.call_once(|| {
#[cfg(debug_assertions)]
let log_level = log::Level::Debug;
#[cfg(not(debug_assertions))]
let log_level = log::Level::Info;
console_log::init_with_level(log_level).expect("Failed to initialize console_log");
web_sys::console::log_1(&"Test logging initialized".into());
});
}
#[wasm_bindgen_test]
fn test_log_infrastructure_initialized() {
init_test_logging();
let max_level = log::max_level();
assert_ne!(
max_level,
log::LevelFilter::Off,
"Logger should be initialized (max_level should not be Off)"
);
#[cfg(debug_assertions)]
assert!(
max_level >= log::LevelFilter::Debug,
"Debug builds should have Debug or Trace level logging"
);
#[cfg(not(debug_assertions))]
assert!(
max_level >= log::LevelFilter::Info,
"Release builds should have at least Info level logging"
);
log::trace!("Trace log works");
log::debug!("Debug log works");
log::info!("Info log works");
log::warn!("Warn log works");
log::error!("Error log works");
web_sys::console::log_1(
&format!(
"Log infrastructure initialized with max_level: {:?}",
max_level
)
.into(),
);
}
#[wasm_bindgen_test]
fn test_log_with_formatting() {
init_test_logging();
let test_value = 42;
let test_string = "test";
log::debug!(
"Formatted log: value={}, string={}",
test_value,
test_string
);
log::info!("Test with single param: {}", test_value);
assert!(true, "Formatted logging works");
}
#[wasm_bindgen_test]
async fn test_log_in_async_context() {
init_test_logging();
log::info!("Async context log test");
wasm_bindgen_futures::JsFuture::from(js_sys::Promise::resolve(&wasm_bindgen::JsValue::from(
42,
)))
.await
.expect("Promise should resolve");
log::debug!("After async operation");
assert!(true, "Async logging works");
}