use rusty_stack::core::types::{BackendMode, ExecutorKind};
use rusty_stack::platform::wsl::{
check_wsl_health, detect_wsl2, provisioning_guidance, WslHealthResult, WslStatus,
};
use rusty_stack::platform::BackendRouter;
#[test]
fn test_wsl2_detection_returns_status() {
let status = detect_wsl2();
match status {
WslStatus::NotInstalled => {}
WslStatus::Available { distro, version } => {
assert_eq!(version, 2, "Available WSL should report version 2");
assert!(!distro.is_empty(), "Distro name should not be empty");
}
WslStatus::Error { message: msg } => {
assert!(!msg.is_empty(), "Error message should not be empty");
}
}
}
#[test]
fn test_wsl2_detection_not_installed_on_linux() {
let status = detect_wsl2();
match status {
WslStatus::NotInstalled => {
}
WslStatus::Available { .. } => {
}
WslStatus::Error { .. } => {}
}
}
#[test]
fn test_wsl2_detection_does_not_timeout() {
let start = std::time::Instant::now();
let _status = detect_wsl2();
let elapsed = start.elapsed();
assert!(
elapsed.as_secs() < 5,
"WSL2 detection took {:?}, expected < 5s",
elapsed
);
}
#[test]
fn test_provisioning_guidance_not_installed() {
let guidance = provisioning_guidance(&WslStatus::NotInstalled);
assert!(!guidance.is_empty(), "Guidance should not be empty");
assert!(
guidance.contains("wsl") || guidance.contains("WSL"),
"Guidance should mention WSL: got '{}'",
guidance
);
assert!(
guidance.contains("install") || guidance.contains("Install"),
"Guidance should mention install: got '{}'",
guidance
);
}
#[test]
fn test_provisioning_guidance_available() {
let status = WslStatus::Available {
distro: "Ubuntu".to_string(),
version: 2,
};
let guidance = provisioning_guidance(&status);
assert!(
guidance.contains("Ubuntu") || guidance.contains("available") || guidance.contains("ready"),
"Guidance for available WSL should reference the distro or state: got '{}'",
guidance
);
}
#[test]
fn test_provisioning_guidance_error() {
let status = WslStatus::Error {
message: "test error".to_string(),
};
let guidance = provisioning_guidance(&status);
assert!(!guidance.is_empty(), "Error guidance should not be empty");
}
#[test]
fn test_wsl_health_check_returns_three_results() {
let status = WslStatus::NotInstalled;
let results = check_wsl_health(&status);
assert_eq!(
results.len(),
3,
"Should produce exactly 3 health check results, got {}",
results.len()
);
}
#[test]
fn test_wsl_health_check_results_are_independent() {
let status = WslStatus::NotInstalled;
let results = check_wsl_health(&status);
let check_types: std::collections::HashSet<&str> =
results.iter().map(|r| r.check.as_str()).collect();
assert_eq!(
check_types.len(),
3,
"Each health check should have a unique type: {:?}",
check_types
);
}
#[test]
fn test_wsl_health_check_names() {
let status = WslStatus::NotInstalled;
let results = check_wsl_health(&status);
let names: Vec<&str> = results.iter().map(|r| r.check.as_str()).collect();
assert!(
names.contains(&"distro_running"),
"Should have distro_running check: {:?}",
names
);
assert!(
names.contains(&"rocm_accessible"),
"Should have rocm_accessible check: {:?}",
names
);
assert!(
names.contains(&"gpu_nodes"),
"Should have gpu_nodes check: {:?}",
names
);
}
#[test]
fn test_wsl_health_check_not_installed_all_fail() {
let status = WslStatus::NotInstalled;
let results = check_wsl_health(&status);
for result in &results {
assert!(
!result.success,
"Health check '{}' should fail when WSL not installed",
result.check
);
}
}
#[test]
fn test_backend_router_linux_native() {
let router = BackendRouter::new(BackendMode::LinuxNative);
let kind = router.executor_for("rocm");
assert_ne!(
kind,
ExecutorKind::Unsupported,
"ROCm should not be unsupported on Linux native"
);
}
#[test]
fn test_backend_router_windows_native() {
let router = BackendRouter::new(BackendMode::WindowsNative);
let kind = router.executor_for("rocm");
assert_eq!(
kind,
ExecutorKind::Unsupported,
"ROCm should be unsupported on Windows native, got {:?}",
kind
);
}
#[test]
fn test_backend_router_wsl_backed_linux() {
let router = BackendRouter::new(BackendMode::WslBackedLinux);
let kind = router.executor_for("rocm");
assert_ne!(
kind,
ExecutorKind::Unsupported,
"ROCm should be available via WSL-backed Linux"
);
}
#[test]
fn test_backend_router_unsupported_component_clear_message() {
let router = BackendRouter::new(BackendMode::WindowsNative);
let kind = router.executor_for("rocm");
assert_eq!(kind, ExecutorKind::Unsupported);
let message = router.unsupported_message("rocm");
assert!(
!message.is_empty(),
"Unsupported message should not be empty"
);
assert!(
message.contains("rocm") || message.contains("ROCm") || message.contains("Windows"),
"Message should explain why: got '{}'",
message
);
}
#[test]
fn test_backend_router_all_components_linux() {
let router = BackendRouter::new(BackendMode::LinuxNative);
let components = [
"rocm",
"pytorch",
"triton",
"deepspeed",
"vllm",
"aiter",
"onnx",
"bitsandbytes",
"migraphx",
"flash-attn",
"mpi4py",
"wandb",
"comfyui",
"vllm-studio",
"textgen",
"rocm-smi",
"permanent-env",
];
for component in &components {
let kind = router.executor_for(component);
assert_ne!(
kind,
ExecutorKind::Unsupported,
"Component '{}' should not be unsupported on Linux native",
component
);
}
}
#[test]
fn test_backend_router_unknown_component() {
let router = BackendRouter::new(BackendMode::LinuxNative);
let kind = router.executor_for("nonexistent-component");
assert_eq!(
kind,
ExecutorKind::Unsupported,
"Unknown component should be unsupported"
);
}
#[test]
fn test_backend_router_windows_python_components() {
let router = BackendRouter::new(BackendMode::WindowsNative);
let kind = router.executor_for("wandb");
assert_ne!(
kind,
ExecutorKind::Unsupported,
"wandb should be available on Windows native"
);
}
#[test]
fn test_wsl_status_serde_roundtrip_not_installed() {
let status = WslStatus::NotInstalled;
let json = serde_json::to_string(&status).unwrap();
let back: WslStatus = serde_json::from_str(&json).unwrap();
assert_eq!(status, back);
}
#[test]
fn test_wsl_status_serde_roundtrip_available() {
let status = WslStatus::Available {
distro: "Ubuntu-22.04".to_string(),
version: 2,
};
let json = serde_json::to_string(&status).unwrap();
let back: WslStatus = serde_json::from_str(&json).unwrap();
assert_eq!(status, back);
}
#[test]
fn test_wsl_status_serde_roundtrip_error() {
let status = WslStatus::Error {
message: "test error".to_string(),
};
let json = serde_json::to_string(&status).unwrap();
let back: WslStatus = serde_json::from_str(&json).unwrap();
assert_eq!(status, back);
}
#[test]
fn test_wsl_health_result_serde_roundtrip() {
let result = WslHealthResult {
check: "distro_running".to_string(),
success: true,
message: "Ubuntu-22.04 is running".to_string(),
};
let json = serde_json::to_string(&result).unwrap();
let back: WslHealthResult = serde_json::from_str(&json).unwrap();
assert_eq!(result, back);
}
#[test]
fn test_backend_mode_classify_linux_native() {
let mode = BackendMode::LinuxNative;
assert_eq!(mode.label(), "LinuxNative");
}
#[test]
fn test_backend_mode_classify_windows_native() {
let mode = BackendMode::WindowsNative;
assert_eq!(mode.label(), "WindowsNative");
}
#[test]
fn test_backend_mode_classify_wsl_backed() {
let mode = BackendMode::WslBackedLinux;
assert_eq!(mode.label(), "WslBackedLinux");
}