use std::path::{Path, PathBuf};
pub struct TestModels;
impl TestModels {
pub fn whisper_model(name: &str) -> Option<PathBuf> {
Self::find_model(name, &[
std::env::var("WHISPER_TEST_MODEL_DIR").ok(),
Some("../whisper-cpp-plus-sys/whisper.cpp/models".to_string()),
Some("whisper-cpp-plus-sys/whisper.cpp/models".to_string()),
])
}
pub fn tiny_en() -> Option<PathBuf> {
Self::whisper_model("ggml-tiny.en.bin")
.or_else(|| Self::whisper_model("for-tests-ggml-tiny.en.bin"))
}
pub fn vad() -> Option<PathBuf> {
Self::find_model("ggml-silero-vad.bin", &[
std::env::var("WHISPER_TEST_MODEL_DIR").ok(),
Some("../whisper-cpp-plus-sys/whisper.cpp/models".to_string()),
Some("whisper-cpp-plus-sys/whisper.cpp/models".to_string()),
]).or_else(|| {
Self::find_model("for-tests-silero-v6.2.0-ggml.bin", &[
Some("../whisper-cpp-plus-sys/whisper.cpp/models".to_string()),
Some("whisper-cpp-plus-sys/whisper.cpp/models".to_string()),
])
})
}
pub fn audio(name: &str) -> Option<PathBuf> {
Self::find_model(name, &[
std::env::var("WHISPER_TEST_AUDIO_DIR").ok(),
Some("../whisper-cpp-plus-sys/whisper.cpp/samples".to_string()),
Some("whisper-cpp-plus-sys/whisper.cpp/samples".to_string()),
])
}
pub fn jfk_audio() -> Option<PathBuf> {
Self::audio("jfk.wav")
}
fn find_model(name: &str, dirs: &[Option<String>]) -> Option<PathBuf> {
for dir in dirs.iter().flatten() {
let path = Path::new(dir).join(name);
if path.exists() {
return Some(path);
}
}
None
}
}
#[macro_export]
macro_rules! skip_if_no_model {
($path:expr, $model_name:expr) => {
let Some(path) = $path else {
eprintln!(
"Skipping test: {} not found.\n\
Run `cargo xtask test-setup` to download test models.",
$model_name
);
return;
};
let path = path;
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_path_resolution() {
let tiny = TestModels::tiny_en();
println!("tiny.en model path: {:?}", tiny);
let stub_path = Path::new("../whisper-cpp-plus-sys/whisper.cpp/models/for-tests-ggml-tiny.en.bin");
let stub_exists = stub_path.exists() ||
Path::new("whisper-cpp-plus-sys/whisper.cpp/models/for-tests-ggml-tiny.en.bin").exists();
if tiny.is_none() && !stub_exists {
eprintln!("Warning: No test models found (expected in CI)");
}
}
}