use super::*;
use serial_test::serial;
#[test]
#[serial]
fn from_env_uses_defaults_when_no_vars_set() {
let saved_max = std::env::var("TRUSTY_EMBEDDERD_MAX_RESTARTS").ok();
let saved_backoff = std::env::var("TRUSTY_EMBEDDERD_RESTART_BACKOFF_MAX_SECS").ok();
let saved_timeout = std::env::var("TRUSTY_EMBEDDERD_STARTUP_TIMEOUT_SECS").ok();
unsafe {
std::env::remove_var("TRUSTY_EMBEDDERD_MAX_RESTARTS");
std::env::remove_var("TRUSTY_EMBEDDERD_RESTART_BACKOFF_MAX_SECS");
std::env::remove_var("TRUSTY_EMBEDDERD_STARTUP_TIMEOUT_SECS");
}
let cfg = SupervisorConfig::from_env();
assert_eq!(cfg.max_restarts, 5);
assert_eq!(cfg.backoff_max_secs, 60);
assert_eq!(cfg.startup_timeout_secs, 5);
unsafe {
if let Some(v) = saved_max {
std::env::set_var("TRUSTY_EMBEDDERD_MAX_RESTARTS", v);
}
if let Some(v) = saved_backoff {
std::env::set_var("TRUSTY_EMBEDDERD_RESTART_BACKOFF_MAX_SECS", v);
}
if let Some(v) = saved_timeout {
std::env::set_var("TRUSTY_EMBEDDERD_STARTUP_TIMEOUT_SECS", v);
}
}
}
#[test]
#[serial]
fn parse_env_uses_override() {
let saved = std::env::var("TRUSTY_EMBEDDERD_MAX_RESTARTS").ok();
unsafe {
std::env::set_var("TRUSTY_EMBEDDERD_MAX_RESTARTS", "99");
}
let cfg = SupervisorConfig::from_env();
assert_eq!(cfg.max_restarts, 99);
unsafe {
if let Some(v) = saved {
std::env::set_var("TRUSTY_EMBEDDERD_MAX_RESTARTS", v);
} else {
std::env::remove_var("TRUSTY_EMBEDDERD_MAX_RESTARTS");
}
}
}
const CUDA_CAP: usize = DEFAULT_CUDA_SIDECAR_BATCH_CAP;
#[test]
fn sidecar_batch_size_cpu_passthrough() {
assert_eq!(sidecar_batch_size(128, false, 32, false, CUDA_CAP), 128);
assert_eq!(sidecar_batch_size(512, false, 32, false, CUDA_CAP), 512);
assert_eq!(sidecar_batch_size(32, false, 32, false, CUDA_CAP), 32);
}
#[test]
fn sidecar_batch_size_coreml_caps_and_passes_through() {
assert_eq!(sidecar_batch_size(256, true, 32, false, CUDA_CAP), 32);
assert_eq!(sidecar_batch_size(512, true, 64, false, CUDA_CAP), 64);
assert_eq!(sidecar_batch_size(16, true, 32, false, CUDA_CAP), 16);
assert_eq!(sidecar_batch_size(32, true, 32, false, CUDA_CAP), 32);
}
#[test]
fn sidecar_batch_size_zero_resolved_clamps_to_one() {
assert_eq!(
sidecar_batch_size(0, false, 32, false, CUDA_CAP),
1,
"zero resolved (non-coreml, non-cuda) must clamp to 1"
);
}
#[test]
fn sidecar_batch_size_zero_coreml_cap_clamps_to_one() {
assert_eq!(
sidecar_batch_size(32, true, 0, false, CUDA_CAP),
1,
"zero coreml_cap must clamp result to 1"
);
}
#[test]
fn sidecar_batch_size_both_zero_clamps_to_one() {
assert_eq!(
sidecar_batch_size(0, true, 0, false, CUDA_CAP),
1,
"resolved=0, coreml_cap=0 must clamp to 1"
);
}
#[test]
fn sidecar_batch_size_cuda_caps_at_cuda_cap() {
assert_eq!(
sidecar_batch_size(512, false, 32, true, 64),
64,
"CUDA: 512 must be capped to 64"
);
assert_eq!(
sidecar_batch_size(256, false, 32, true, 64),
64,
"CUDA: 256 must be capped to 64"
);
assert_eq!(
sidecar_batch_size(32, false, 32, true, 64),
32,
"CUDA: 32 is already below the 64 cap — passes through"
);
assert_eq!(
sidecar_batch_size(64, false, 32, true, 64),
64,
"CUDA: exactly at cap — passes through"
);
}
#[test]
fn sidecar_batch_size_cuda_zero_cap_clamps_to_one() {
assert_eq!(
sidecar_batch_size(32, false, 32, true, 0),
1,
"zero cuda_cap must clamp result to 1"
);
}
#[test]
fn sidecar_batch_size_coreml_takes_priority_over_cuda() {
assert_eq!(
sidecar_batch_size(512, true, 32, true, 64),
32,
"when both flags set, CoreML takes priority"
);
}
#[test]
#[serial]
fn locate_binary_respects_explicit_override() {
let saved = std::env::var("TRUSTY_EMBEDDERD_BIN").ok();
unsafe {
std::env::set_var("TRUSTY_EMBEDDERD_BIN", "/no/such/binary");
}
let result = locate_embedderd_binary();
assert!(result.is_err(), "must fail on non-existent override path");
let msg = result.unwrap_err().to_string();
assert!(
msg.contains("TRUSTY_EMBEDDERD_BIN"),
"error must mention the env var"
);
unsafe {
if let Some(v) = saved {
std::env::set_var("TRUSTY_EMBEDDERD_BIN", v);
} else {
std::env::remove_var("TRUSTY_EMBEDDERD_BIN");
}
}
}