use aimux_core::AiMuxError;
use aimux_provider_utils::{DEFAULT_ERROR_STRUCTURE, ErrorStructure, parse_provider_error};
const TEST_URL: &str = "test-url";
#[test]
fn status_code_error_uses_status_text_for_non_json_body() {
let body = "Error message";
let err = parse_provider_error(404, body, &DEFAULT_ERROR_STRUCTURE);
assert!(matches!(err, AiMuxError::ModelNotFound(ref m) if m == "Error message"));
}
#[test]
fn status_code_500_maps_to_provider_error() {
let body = "Internal Server Error";
let err = parse_provider_error(500, body, &DEFAULT_ERROR_STRUCTURE);
assert!(matches!(err, AiMuxError::Provider(_)));
}
#[test]
fn json_error_response_extracts_message_from_default_structure() {
let body = r#"{"error":{"message":"Rate limit reached for requests","type":"requests"}}"#;
let err = parse_provider_error(429, body, &DEFAULT_ERROR_STRUCTURE);
assert!(matches!(err, AiMuxError::RateLimited { .. }));
}
#[test]
fn json_error_response_extracts_message_from_custom_structure() {
let structure = ErrorStructure {
message_path: &["error", "msg"],
type_path: &["error", "kind"],
};
let body = r#"{"error":{"msg":"boom","kind":"failure"}}"#;
let err = parse_provider_error(500, body, &structure);
assert!(matches!(err, AiMuxError::Provider(ref m) if m == "HTTP 500: boom"));
}
#[test]
fn empty_body_falls_back_to_http_status_message() {
let err = parse_provider_error(503, "", &DEFAULT_ERROR_STRUCTURE);
assert!(matches!(err, AiMuxError::Provider(ref m) if m.contains("503")));
}
#[test]
fn non_json_body_is_used_as_the_message() {
let err = parse_provider_error(401, "plain text problem", &DEFAULT_ERROR_STRUCTURE);
assert!(matches!(err, AiMuxError::Auth(ref m) if m == "plain text problem"));
}
#[test]
fn auth_status_maps_to_auth_variant() {
let body = r#"{"error":{"message":"Invalid API key","type":"auth"}}"#;
let err = parse_provider_error(401, body, &DEFAULT_ERROR_STRUCTURE);
assert!(matches!(err, AiMuxError::Auth(ref m) if m == "Invalid API key"));
}
#[test]
fn url_constant_is_unused_but_documented() {
assert_eq!(TEST_URL, "test-url");
}