use super::*;
use tokio::sync::Mutex;
static ENV_LOCK: Mutex<()> = Mutex::const_new(());
struct EnvVarGuard {
key: &'static str,
prev: Option<String>,
}
impl EnvVarGuard {
fn apply(key: &'static str, value: Option<&str>) -> Self {
let prev = std::env::var(key).ok();
unsafe {
match value {
Some(v) => std::env::set_var(key, v),
None => std::env::remove_var(key),
}
}
Self { key, prev }
}
}
impl Drop for EnvVarGuard {
fn drop(&mut self) {
unsafe {
match &self.prev {
Some(v) => std::env::set_var(self.key, v),
None => std::env::remove_var(self.key),
}
}
}
}
#[tokio::test]
async fn resolve_expected_python_provider_forces_cpu() {
let _g = ENV_LOCK.lock().await;
let _d = EnvVarGuard::apply("TRUSTY_DEVICE", Some("CPU"));
assert_eq!(resolve_expected_python_provider(), ExecutionProvider::Cpu);
}
#[tokio::test]
async fn resolve_expected_python_provider_default_matches_platform() {
let _g = ENV_LOCK.lock().await;
let _d = EnvVarGuard::apply("TRUSTY_DEVICE", None);
let got = resolve_expected_python_provider();
#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
let expected = ExecutionProvider::Mps;
#[cfg(all(
not(all(target_arch = "aarch64", target_os = "macos")),
feature = "embedder-cuda"
))]
let expected = ExecutionProvider::Cuda;
#[cfg(all(
not(all(target_arch = "aarch64", target_os = "macos")),
not(feature = "embedder-cuda")
))]
let expected = ExecutionProvider::Cpu;
assert_eq!(
got, expected,
"predicted python-sidecar provider must match resolve_device's precedence \
(mps > cuda > cpu) for this build/platform"
);
}
#[test]
fn embedding_model_name_reports_resolved_variant() {
use crate::embedder::fast_embedder::embedding_model_name;
assert_eq!(
embedding_model_name(&fastembed::EmbeddingModel::AllMiniLML6V2),
"all-MiniLM-L6-v2",
"fp32 default must report the un-suffixed name, not the stale (Q) tag"
);
assert_eq!(
embedding_model_name(&fastembed::EmbeddingModel::AllMiniLML6V2Q),
"all-MiniLM-L6-v2-int8",
"the explicit INT8 opt-in must report an int8-suffixed name"
);
}
#[tokio::test]
#[ignore]
async fn fast_embedder_model_name_reports_fp32_default() {
let _g = ENV_LOCK.lock().await;
let _m = EnvVarGuard::apply("TRUSTY_EMBEDDER_MODEL", None);
let e = FastEmbedder::new().await.unwrap();
assert_eq!(e.model_name(), "all-MiniLM-L6-v2");
}
#[tokio::test]
#[ignore]
async fn fast_embedder_model_name_reports_int8_opt_in() {
let _g = ENV_LOCK.lock().await;
let _m = EnvVarGuard::apply("TRUSTY_EMBEDDER_MODEL", Some("int8"));
let e = FastEmbedder::new().await.unwrap();
assert_eq!(e.model_name(), "all-MiniLM-L6-v2-int8");
}