openrouter-rust 0.1.0

A modular, type-safe Rust client for the OpenRouter API
Documentation
#[cfg(test)]
mod tests {
    use openrouter_rust::client::{OpenRouterClient, OpenRouterClientBuilder};
    use openrouter_rust::error::OpenRouterError;

    #[tokio::test]
    async fn test_client_builder_success() {
        let client = OpenRouterClient::builder()
            .api_key("test-api-key")
            .build();

        assert!(client.is_ok());
        let client = client.unwrap();
        assert_eq!(client.api_key, "test-api-key");
        assert_eq!(client.base_url, "https://openrouter.ai/api/v1");
    }

    #[tokio::test]
    async fn test_client_builder_without_api_key() {
        let result = OpenRouterClient::builder().build();
        
        assert!(result.is_err());
        match result.unwrap_err() {
            OpenRouterError::ConfigError(msg) => {
                assert!(msg.contains("API key is required"));
            }
            _ => panic!("Expected ConfigError"),
        }
    }

    #[tokio::test]
    async fn test_client_builder_with_custom_base_url() {
        let client = OpenRouterClient::builder()
            .api_key("test-key")
            .base_url("https://custom.openrouter.ai/api/v1")
            .build()
            .unwrap();

        assert_eq!(client.base_url, "https://custom.openrouter.ai/api/v1");
    }

    #[tokio::test]
    async fn test_client_builder_with_optional_headers() {
        let client = OpenRouterClient::builder()
            .api_key("test-key")
            .http_referer("https://example.com")
            .x_title("My Test App")
            .build()
            .unwrap();

        assert_eq!(client.http_referer, Some("https://example.com".to_string()));
        assert_eq!(client.x_title, Some("My Test App".to_string()));
    }

    #[tokio::test]
    async fn test_client_builder_headers_generation() {
        let client = OpenRouterClient::builder()
            .api_key("test-key")
            .http_referer("https://example.com")
            .x_title("My App")
            .build()
            .unwrap();

        let headers = client.build_headers().unwrap();
        
        // Check Authorization header
        let auth_header = headers.get("authorization").unwrap();
        assert_eq!(auth_header.to_str().unwrap(), "Bearer test-key");
        
        // Check Content-Type header
        let content_type = headers.get("content-type").unwrap();
        assert_eq!(content_type.to_str().unwrap(), "application/json");
        
        // Check optional headers
        let referer = headers.get("HTTP-Referer").unwrap();
        assert_eq!(referer.to_str().unwrap(), "https://example.com");
        
        let title = headers.get("X-Title").unwrap();
        assert_eq!(title.to_str().unwrap(), "My App");
    }

    #[tokio::test]
    async fn test_client_builder_headers_without_optional() {
        let client = OpenRouterClient::builder()
            .api_key("test-key")
            .build()
            .unwrap();

        let headers = client.build_headers().unwrap();
        
        // Should have Authorization and Content-Type
        assert!(headers.contains_key("authorization"));
        assert!(headers.contains_key("content-type"));
        
        // Should NOT have optional headers
        assert!(!headers.contains_key("HTTP-Referer"));
        assert!(!headers.contains_key("X-Title"));
    }

    #[tokio::test]
    async fn test_client_clone() {
        let client = OpenRouterClient::builder()
            .api_key("test-key")
            .build()
            .unwrap();

        let cloned = client.clone();
        
        assert_eq!(client.api_key, cloned.api_key);
        assert_eq!(client.base_url, cloned.base_url);
    }

    #[tokio::test]
    async fn test_client_builder_default() {
        let builder = OpenRouterClientBuilder::default();
        // Just verify default() works
        let _ = builder;
    }
}