agenterra 0.2.2

Generate production-ready MCP (Model Context Protocol) servers and clients from OpenAPI specs
use std::collections::HashMap;

#[test]
fn test_array_parameter_serialization() {
    // Test the logic we're generating in handler files

    // Simulate what the generated code does
    let tags: Option<Vec<String>> = Some(vec![
        "tag1".to_string(),
        "tag2".to_string(),
        "tag3".to_string(),
    ]);
    let mut params = HashMap::new();

    if let Some(val) = &tags {
        // This is what our generated code does
        params.insert("tags".to_string(), val.join(","));
    }

    // Verify the result
    assert_eq!(params.get("tags"), Some(&"tag1,tag2,tag3".to_string()));

    // Test empty array
    let empty_tags: Option<Vec<String>> = Some(vec![]);
    let mut params2 = HashMap::new();

    if let Some(val) = &empty_tags {
        params2.insert("tags".to_string(), val.join(","));
    }

    assert_eq!(params2.get("tags"), Some(&"".to_string()));

    // Test None case
    let no_tags: Option<Vec<String>> = None;
    let mut params3 = HashMap::new();

    if let Some(val) = &no_tags {
        params3.insert("tags".to_string(), val.join(","));
    }

    assert_eq!(params3.get("tags"), None);
}

#[test]
fn test_reqwest_query_with_comma_separated_values() {
    // Test how reqwest actually handles comma-separated values
    use url::Url;

    let mut params = HashMap::new();
    params.insert("tags", "tag1,tag2,tag3");

    // Simulate what reqwest does with query parameters
    let base_url = Url::parse("https://api.example.com/pets").unwrap();
    let url_with_params = Url::parse_with_params(base_url.as_str(), &params).unwrap();

    // The URL should encode the comma-separated string as a single parameter
    assert_eq!(
        url_with_params.as_str(),
        "https://api.example.com/pets?tags=tag1%2Ctag2%2Ctag3"
    );

    // Note: %2C is the URL-encoded comma
}

#[test]
fn test_alternative_array_formats() {
    // Document how different array formats would be serialized

    // Format 1: Comma-separated (current approach)
    // URL: ?tags=tag1,tag2,tag3
    // Server needs to split by comma

    // Format 2: Repeated parameters (not supported with HashMap<String, String>)
    // URL: ?tags=tag1&tags=tag2&tags=tag3
    // Would need HashMap<String, Vec<String>> or similar

    // Format 3: Bracket notation (not supported with HashMap<String, String>)
    // URL: ?tags[]=tag1&tags[]=tag2&tags[]=tag3
    // Would need special handling

    // Format 4: JSON array (would need to be URL-encoded)
    // URL: ?tags=%5B%22tag1%22%2C%22tag2%22%2C%22tag3%22%5D
    // Would be: ?tags=["tag1","tag2","tag3"]

    // Test JSON array encoding (would need urlencoding crate)
    let tags_json = serde_json::to_string(&vec!["tag1", "tag2", "tag3"]).unwrap();
    // Would be URL encoded as: %5B%22tag1%22%2C%22tag2%22%2C%22tag3%22%5D
    assert_eq!(tags_json, r#"["tag1","tag2","tag3"]"#);
}