zilliz 1.4.2

TUI and CLI tool for managing Zilliz Cloud clusters and Milvus operations
Documentation
use serde_json::json;

/// Test model parsing from JSON dict.
mod model_parsing {
    use super::*;

    fn sample_model_json() -> serde_json::Value {
        json!({
            "version": "2026-01-26",
            "minCliVersion": "0.1.0",
            "endpoint": zilliz::model::DEFAULT_CONTROL_PLANE_ENDPOINT,
            "resources": {
                "cluster": {
                    "operations": {
                        "list": {
                            "http": {"method": "GET", "path": "/v2/clusters"},
                            "params": [],
                            "output": {"dataField": "data"}
                        },
                        "describe": {
                            "http": {"method": "GET", "path": "/v2/clusters/{clusterId}"},
                            "params": [{
                                "name": "clusterId",
                                "type": "string",
                                "required": true,
                                "cli": "--cluster-id",
                                "position": "path"
                            }]
                        },
                        "create": {
                            "http": {"method": "POST", "path": "/v2/clusters/createServerless"},
                            "params": [
                                {"name": "clusterName", "type": "string", "required": true, "cli": "--name"},
                                {"name": "projectId", "type": "string", "required": true, "cli": "--project-id"},
                                {"name": "regionId", "type": "string", "required": true, "cli": "--region"}
                            ]
                        }
                    }
                }
            }
        })
    }

    fn parse_model() -> zilliz::model::types::CliModel {
        serde_json::from_value(sample_model_json()).expect("Failed to parse model")
    }

    #[test]
    fn test_parse_from_dict() {
        let model = parse_model();
        assert_eq!(model.version, "2026-01-26");
        assert_eq!(
            model.endpoint.as_deref(),
            Some(zilliz::model::DEFAULT_CONTROL_PLANE_ENDPOINT)
        );
        assert!(model.resources.contains_key("cluster"));
    }

    #[test]
    fn test_resource_operations() {
        let model = parse_model();
        let cluster = &model.resources["cluster"];
        assert!(cluster.operations.contains_key("list"));
        assert!(cluster.operations.contains_key("describe"));
        assert!(cluster.operations.contains_key("create"));
    }

    #[test]
    fn test_operation_http() {
        let model = parse_model();
        let list_op = &model.resources["cluster"].operations["list"];
        assert_eq!(list_op.method(), "GET");
        assert_eq!(list_op.path(), "/v2/clusters");
    }

    #[test]
    fn test_operation_params() {
        let model = parse_model();
        let describe_op = &model.resources["cluster"].operations["describe"];
        assert_eq!(describe_op.params.len(), 1);
        let p = &describe_op.params[0];
        assert_eq!(p.name, "clusterId");
        assert_eq!(p.param_type, "string");
        assert!(p.required);
        assert_eq!(p.cli_name.as_deref(), Some("--cluster-id"));
        assert_eq!(p.position.as_deref(), Some("path"));
        assert!(p.is_path_param());
    }

    #[test]
    fn test_param_defaults() {
        let model = parse_model();
        let create_op = &model.resources["cluster"].operations["create"];
        let p = &create_op.params[0];
        assert!(p.position.is_none());
        assert!(p.default.is_none());
    }

    #[test]
    fn test_cli_flag() {
        let model = parse_model();
        let describe_op = &model.resources["cluster"].operations["describe"];
        assert_eq!(describe_op.params[0].cli_flag(), "--cluster-id");

        let create_op = &model.resources["cluster"].operations["create"];
        assert_eq!(create_op.params[0].cli_flag(), "--name");
    }

    #[test]
    fn test_operation_body_param() {
        let model_json = json!({
            "version": "1",
            "resources": {
                "collection": {
                    "operations": {
                        "create": {
                            "http": {"method": "POST", "path": "/v2/vectordb/collections/create"},
                            "params": [{"name": "collectionName", "type": "string", "required": true, "cli": "--name"}],
                            "bodyParam": "--body"
                        }
                    }
                }
            }
        });
        let model: zilliz::model::types::CliModel = serde_json::from_value(model_json).unwrap();
        let op = &model.resources["collection"].operations["create"];
        assert_eq!(op.body_param.as_deref(), Some("--body"));
    }

    #[test]
    fn test_output_data_field() {
        let model = parse_model();
        let list_op = &model.resources["cluster"].operations["list"];
        assert_eq!(list_op.output.data_field.as_deref(), Some("data"));
    }
}