#![cfg(feature = "vad")]
use std::path::{Path, PathBuf};
use decibri::error::DecibriError;
use decibri::vad::{SileroVad, VadConfig};
fn bundled_model_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("..")
.join("models")
.join("silero_vad.onnx")
}
#[test]
fn test_ort_init_success_with_bundled_model() {
let mut config = VadConfig::default();
config.model_path = bundled_model_path();
let vad = SileroVad::new(config);
assert!(
vad.is_ok(),
"SileroVad::new should succeed with the bundled model: {:?}",
vad.err()
);
}
#[test]
fn test_vad_model_load_fails_for_missing_file() {
let bogus = PathBuf::from("does-not-exist-silero-xyz.onnx");
let mut config = VadConfig::default();
config.model_path = bogus.clone();
let err = SileroVad::new(config)
.err()
.expect("expected VadModelLoadFailed");
match err {
DecibriError::VadModelLoadFailed { path, .. } => {
assert_eq!(
path, bogus,
"VadModelLoadFailed.path should match the input PathBuf"
);
}
other => panic!("expected VadModelLoadFailed, got {other:?}"),
}
}
#[test]
fn test_vad_config_validate_typed_errors() {
let mut bad_rate = VadConfig::default();
bad_rate.sample_rate = 44_100;
match bad_rate
.validate()
.expect_err("expected VadSampleRateUnsupported")
{
DecibriError::VadSampleRateUnsupported(sr) => {
assert_eq!(sr, 44_100, "payload should carry the offending rate");
}
other => panic!("expected VadSampleRateUnsupported, got {other:?}"),
}
let mut bad_threshold = VadConfig::default();
bad_threshold.threshold = 1.5;
match bad_threshold
.validate()
.expect_err("expected VadThresholdOutOfRange")
{
DecibriError::VadThresholdOutOfRange(t) => {
assert!(
(t - 1.5).abs() < f32::EPSILON,
"payload should carry the offending threshold, got {t}"
);
}
other => panic!("expected VadThresholdOutOfRange, got {other:?}"),
}
let mut good = VadConfig::default();
good.model_path = bundled_model_path();
assert!(good.validate().is_ok(), "default config should validate");
}
#[test]
fn test_vad_end_to_end_silence_inference() {
let mut config = VadConfig::default();
config.model_path = bundled_model_path();
let mut vad = SileroVad::new(config).expect("model should load");
let silence = vec![0.0f32; 1600];
let result = vad.process(&silence).expect("inference should succeed");
assert!(
result.probability < 0.5,
"silence probability should be low, got {}",
result.probability
);
assert!(
!result.is_speech,
"silence should not be classified as speech"
);
}