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::ApiCall(ref m) if m.status_code == Some(404) && m.message == "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::ApiCall(_)));
}
#[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::ApiCall(ref d) if d.status_code == Some(429)));
}
#[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);
let AiMuxError::ApiCall(ref detail) = err else {
panic!("expected ApiCall, got {err:?}")
};
assert_eq!(detail.message, "boom");
assert_eq!(err.to_string(), "API call error: HTTP 500: boom");
}
#[test]
fn empty_body_falls_back_to_http_status_message() {
let err = parse_provider_error(503, "", &DEFAULT_ERROR_STRUCTURE);
assert_eq!(err.status_code(), Some(503));
assert!(err.to_string().contains("503"), "{err}");
}
#[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::ApiCall(ref m) if m.status_code == Some(401) && m.message == "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::ApiCall(ref m) if m.status_code == Some(401) && m.message == "Invalid API key")
);
}
#[test]
fn url_constant_is_unused_but_documented() {
assert_eq!(TEST_URL, "test-url");
}