Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

100 KB
Raw Blame
//! RabbitMQ admin adapter. Communicates with the native Go agent via JSON-RPC
//! over stdin/stdout. The agent uses `amqp091-go` for AMQP operations and the
//! RabbitMQ management HTTP API for administrative operations.
//!
//! This adapter follows the same pattern as the Kafka agent:
//! 1. Spawn the native agent process via `AgentDriverClient`
//! 2. Perform JSON-RPC handshake + connect
//! 3. Delegate all `MessageQueueAdmin` trait methods to JSON-RPC calls

use std::sync::Arc;

use async_trait::async_trait;
use serde::de::DeserializeOwned;
use tokio::sync::Mutex;

use crate::db::agent_driver::{AgentDriverClient, AgentLaunchSpec};
use crate::mq::auth::MqAuth;
use crate::mq::config::MqAdminConfig;
use crate::mq::port::MessageQueueAdmin;
use crate::mq::types::*;

/// RabbitMQ capabilities - no tenants/partitions. Topics map to queues and
/// namespaces map to virtual hosts; the subscriptions panel lists queue
/// consumers; queues can be purged (clear backlog); message peeking and
/// production are supported through the agent; exchanges and bindings are
/// managed through the agent.
const RABBITMQ_CAPABILITIES: MqCapabilities = MqCapabilities {
    supports_list_topics: true,
    supports_create_topic: true,
    supports_delete_topic: true,
    supports_topic_stats: true,
    supports_broker_admin: true,
    supports_producer_listing: true,
    supports_tenants: false,
    supports_namespaces: true,
    supports_partitioned_topics: false,
    supports_subscriptions: true,
    supports_create_subscription: false,
    supports_reset_cursor: false,
    supports_skip_messages: false,
    supports_clear_backlog: true,
    supports_peek_messages: true,
    supports_expire_messages: false,
    supports_rate_limits: false,
    supports_backlog_quota: false,
    supports_retention: false,
    supports_permissions: false,
    supports_geo_replication: false,
    supports_token_management: false,
    supports_raw_admin_api: false,
    supports_send_message: true,
    supports_message_query: false,
    supports_dlq: false,
    supports_message_trace: false,
    supports_exchanges: true,
    supports_client_connections: true,
    supports_user_permissions: true,
    supports_policies: true,
    supports_cluster_monitoring: true,
};

pub struct RabbitMqAdmin {
    client: Arc<Mutex<AgentDriverClient>>,
    config: MqAdminConfig,
}

impl RabbitMqAdmin {
    /// Spawn the RabbitMQ native agent, perform handshake, and connect.
    pub async fn new(cfg: MqAdminConfig, launch: AgentLaunchSpec) -> Result<Self, String> {
        let mut client = AgentDriverClient::spawn(launch).await?;

        // Handshake
        let _: serde_json::Value =
            client.call_with_timeout("handshake", serde_json::json!({}), cfg.rpc_timeout()).await?;

        // Build the connection params from MqAdminConfig
        let conn_params = build_connection_params(&cfg)?;
        let connect_params = serde_json::json!({ "connection": conn_params });
        let _: serde_json::Value = client.call_with_timeout("connect", connect_params, cfg.rpc_timeout()).await?;

        log::info!("RabbitMQ admin connected via agent (addresses: {})", addresses(&cfg));

        Ok(Self { client: Arc::new(Mutex::new(client)), config: cfg })
    }

    /// Send a JSON-RPC call to the RabbitMQ agent and deserialize the result.
    async fn call<T: DeserializeOwned + Send + 'static>(
        &self,
        method: &str,
        params: serde_json::Value,
    ) -> Result<T, String> {
        let mut client = self.client.lock().await;
        client.call_with_timeout(method, params, self.config.rpc_timeout()).await
    }

    /// Send a JSON-RPC call that returns `{ok: true}` on success.
    async fn call_ok(&self, method: &str, params: serde_json::Value) -> Result<(), String> {
        let _: serde_json::Value = self.call(method, params).await?;
        Ok(())
    }
}

#[async_trait]
impl MessageQueueAdmin for RabbitMqAdmin {
    fn capabilities(&self) -> MqCapabilities {
        RABBITMQ_CAPABILITIES
    }

    fn system_kind(&self) -> MqSystemKind {
        MqSystemKind::RabbitMq
    }

    async fn test_connection(&self) -> Result<MqClusterInfo, String> {
        let conn_params = build_connection_params(&self.config)?;
        let result: serde_json::Value =
            self.call("test_connection", serde_json::json!({ "connection": conn_params })).await?;

        let server_version = result.get("serverVersion").and_then(|v| v.as_str()).map(String::from);
        let cluster_name = result.get("clusterName").and_then(|v| v.as_str()).map(String::from);

        Ok(MqClusterInfo {
            system_kind: MqSystemKind::RabbitMq,
            server_version,
            resolved_profile: "rabbitmq-agent".to_string(),
            version_detection: "agent".to_string(),
            capabilities: RABBITMQ_CAPABILITIES,
            extra: serde_json::json!({
                "clusterName": cluster_name,
            }),
        })
    }

    // ---- Tenants (not supported by RabbitMQ) ----

    async fn list_tenants(&self) -> Result<Vec<TenantInfo>, String> {
        Ok(Vec::new())
    }

    async fn get_tenant(&self, _name: &str) -> Result<TenantInfo, String> {
        Err("RabbitMQ does not support tenants".to_string())
    }

    async fn create_tenant(&self, _name: &str, _cfg: TenantConfig) -> Result<(), String> {
        Err("RabbitMQ does not support tenants".to_string())
    }

    async fn update_tenant(&self, _name: &str, _cfg: TenantConfig) -> Result<(), String> {
        Err("RabbitMQ does not support tenants".to_string())
    }

    async fn delete_tenant(&self, _name: &str, _force: bool) -> Result<(), String> {
        Err("RabbitMQ does not support tenants".to_string())
    }

    // ---- Namespaces (mapped to virtual hosts) ----

    async fn list_namespaces(&self, tenant: &str) -> Result<Vec<NamespaceInfo>, String> {
        let result: serde_json::Value = self.call("mq_list_namespaces", serde_json::json!({})).await?;
        let namespaces = result.get("namespaces").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        Ok(namespaces
            .into_iter()
            .map(|n| NamespaceInfo {
                tenant: tenant.to_string(),
                namespace: n.get("name").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
                ..Default::default()
            })
            .collect())
    }

    async fn create_namespace(&self, ns: &NamespaceRef, _cfg: NamespaceConfig) -> Result<(), String> {
        let vhost = namespace_vhost_name(&ns.namespace)?;
        self.call_ok("mq_create_namespace", serde_json::json!({ "namespace": vhost })).await
    }

    async fn delete_namespace(&self, ns: &NamespaceRef, _force: bool) -> Result<(), String> {
        let vhost = namespace_vhost_name(&ns.namespace)?;
        self.call_ok("mq_delete_namespace", serde_json::json!({ "namespace": vhost })).await
    }

    async fn get_namespace_policies(&self, _ns: &NamespaceRef) -> Result<serde_json::Value, String> {
        Err("RabbitMQ does not support namespaces".to_string())
    }

    // ---- Topics (mapped to queues; namespace maps to virtual host) ----

    async fn list_topics(&self, ns: &NamespaceRef, _opts: ListTopicsOpts) -> Result<Vec<TopicInfo>, String> {
        let params = with_virtual_host(serde_json::json!({}), &ns.namespace);
        let result: serde_json::Value = self.call("mq_list_topics", params).await?;
        let topics = result.get("topics").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        Ok(topics.into_iter().map(|topic| topic_info_from_agent_value(&topic)).collect())
    }

    async fn list_topics_page(
        &self,
        ns: &NamespaceRef,
        _opts: ListTopicsOpts,
        pagination: MqListPageRequest,
    ) -> Result<MqListPage<TopicInfo>, String> {
        let params = with_virtual_host(rabbitmq_page_params(&pagination), &ns.namespace);
        let result: serde_json::Value = self.call("mq_list_topics", params).await?;
        let topics = result
            .get("topics")
            .and_then(|value| value.as_array())
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|topic| topic_info_from_agent_value(&topic))
            .collect::<Vec<_>>();

        if let Some(metadata) = agent_page_metadata(&result, &pagination) {
            return Ok(metadata.with_items(topics));
        }

        // Older installed agents ignore the optional page fields and return
        // the legacy full-list envelope. Keep that wire combination working
        // and bound what crosses the command boundary after it arrives.
        Ok(paginate_topic_infos(topics, &pagination))
    }

    async fn create_topic(&self, topic: &TopicRef, _partitions: Option<u32>) -> Result<(), String> {
        require_specific_vhost(&topic.namespace)?;
        let params = with_virtual_host(
            serde_json::json!({
                "name": queue_name(topic),
                "durable": true,
            }),
            &topic.namespace,
        );
        self.call_ok("mq_create_topic", params).await
    }

    async fn delete_topic(&self, topic: &TopicRef, _force: bool) -> Result<(), String> {
        require_specific_vhost(&topic.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "name": queue_name(topic) }), &topic.namespace);
        self.call_ok("mq_delete_topic", params).await
    }

    async fn update_partitions(&self, _topic: &TopicRef, _partitions: u32) -> Result<(), String> {
        Err("RabbitMQ queues do not have partitions".to_string())
    }

    async fn get_topic_stats(&self, topic: &TopicRef) -> Result<TopicStats, String> {
        require_specific_vhost(&topic.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "name": queue_name(topic) }), &topic.namespace);
        let result: serde_json::Value = self.call("mq_get_topic_stats", params).await?;
        Ok(topic_stats_from_agent_value(result))
    }

    async fn get_topic_internal_stats(&self, topic: &TopicRef) -> Result<serde_json::Value, String> {
        require_specific_vhost(&topic.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "name": queue_name(topic) }), &topic.namespace);
        self.call("mq_get_topic_config", params).await
    }

    // ---- Exchanges / bindings ----

    async fn list_exchanges(&self, ns: &NamespaceRef) -> Result<Vec<MqExchangeInfo>, String> {
        let params = with_virtual_host(serde_json::json!({}), &ns.namespace);
        let result: serde_json::Value = self.call("mq_list_exchanges", params).await?;
        let exchanges = result.get("exchanges").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        exchanges
            .into_iter()
            .map(|e| {
                serde_json::from_value(map_item_namespace(e))
                    .map_err(|err| format!("Invalid exchange entry from agent: {err}"))
            })
            .collect()
    }

    async fn list_exchanges_page(
        &self,
        ns: &NamespaceRef,
        pagination: MqListPageRequest,
    ) -> Result<MqListPage<MqExchangeInfo>, String> {
        let params = with_virtual_host(rabbitmq_page_params(&pagination), &ns.namespace);
        let result: serde_json::Value = self.call("mq_list_exchanges", params).await?;
        let exchanges = result
            .get("exchanges")
            .and_then(|value| value.as_array())
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|exchange| {
                serde_json::from_value(map_item_namespace(exchange))
                    .map_err(|err| format!("Invalid exchange entry from agent: {err}"))
            })
            .collect::<Result<Vec<_>, _>>()?;

        if let Some(metadata) = agent_page_metadata(&result, &pagination) {
            return Ok(metadata.with_items(exchanges));
        }

        Ok(paginate_exchange_infos(exchanges, &pagination))
    }

    async fn create_exchange(
        &self,
        ns: &NamespaceRef,
        name: &str,
        exchange_type: &str,
        durable: bool,
        auto_delete: bool,
    ) -> Result<(), String> {
        require_specific_vhost(&ns.namespace)?;
        let params = with_virtual_host(
            serde_json::json!({
                "name": name,
                "type": exchange_type,
                "durable": durable,
                "autoDelete": auto_delete,
            }),
            &ns.namespace,
        );
        self.call_ok("mq_create_exchange", params).await
    }

    async fn delete_exchange(&self, ns: &NamespaceRef, name: &str) -> Result<(), String> {
        // The default exchange ("") and the built-in `amq.*` exchanges cannot be
        // deleted; refuse locally so the broker is never asked to.
        if name.is_empty() || name.starts_with("amq.") {
            return Err(format!("The built-in exchange '{name}' cannot be deleted"));
        }
        require_specific_vhost(&ns.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "name": name }), &ns.namespace);
        self.call_ok("mq_delete_exchange", params).await
    }

    async fn list_bindings(
        &self,
        ns: &NamespaceRef,
        exchange: Option<&str>,
        queue: Option<&str>,
    ) -> Result<Vec<MqBindingInfo>, String> {
        let mut params = serde_json::json!({});
        if let Some(exchange) = exchange.filter(|e| !e.is_empty()) {
            params["exchange"] = serde_json::json!(exchange);
        }
        if let Some(queue) = queue.filter(|q| !q.is_empty()) {
            params["queue"] = serde_json::json!(queue);
        }
        let params = with_virtual_host(params, &ns.namespace);
        let result: serde_json::Value = self.call("mq_list_bindings", params).await?;
        let bindings = result.get("bindings").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        bindings
            .into_iter()
            .map(|b| {
                serde_json::from_value(map_item_namespace(b))
                    .map_err(|err| format!("Invalid binding entry from agent: {err}"))
            })
            .collect()
    }

    async fn bind_queue(&self, ns: &NamespaceRef, binding: &MqBindingInfo) -> Result<(), String> {
        let params = binding_params(binding, &ns.namespace)?;
        self.call_ok("mq_bind", params).await
    }

    async fn unbind_queue(&self, ns: &NamespaceRef, binding: &MqBindingInfo) -> Result<(), String> {
        let params = binding_params(binding, &ns.namespace)?;
        self.call_ok("mq_unbind", params).await
    }

    // ---- Client connections / channels ----

    async fn list_client_connections(&self, ns: &NamespaceRef) -> Result<Vec<MqClientConnectionInfo>, String> {
        let params = with_virtual_host(serde_json::json!({}), &ns.namespace);
        let result: serde_json::Value = self.call("mq_list_connections", params).await?;
        let connections = result.get("connections").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        connections
            .into_iter()
            .map(|c| {
                serde_json::from_value(map_item_namespace(c))
                    .map_err(|err| format!("Invalid connection entry from agent: {err}"))
            })
            .collect()
    }

    async fn list_client_channels(
        &self,
        ns: &NamespaceRef,
        connection: Option<String>,
    ) -> Result<Vec<MqChannelInfo>, String> {
        let mut params = serde_json::json!({});
        if let Some(connection) = connection.filter(|c| !c.is_empty()) {
            params["connection"] = serde_json::json!(connection);
        }
        let params = with_virtual_host(params, &ns.namespace);
        let result: serde_json::Value = self.call("mq_list_channels", params).await?;
        let channels = result.get("channels").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        channels
            .into_iter()
            .map(|c| {
                serde_json::from_value(map_item_namespace(c))
                    .map_err(|err| format!("Invalid channel entry from agent: {err}"))
            })
            .collect()
    }

    async fn close_client_connection(&self, ns: &NamespaceRef, name: &str) -> Result<(), String> {
        require_specific_vhost(&ns.namespace)?;
        self.call_ok("mq_close_connection", serde_json::json!({ "name": name })).await
    }

    // ---- Users & virtual-host permissions ----
    //
    // RabbitMQ permissions are a user × vhost configure/write/read regex
    // triple. Users are broker-global (no vhost scope); permission operations
    // are scoped by the namespace (virtual host), where the all-vhosts marker
    // (`*`) requests a cross-vhost listing but is rejected for grant/revoke.

    async fn list_users(&self) -> Result<Vec<MqUserInfo>, String> {
        let result: serde_json::Value = self.call("mq_list_users", serde_json::json!({})).await?;
        let users = result.get("users").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        users
            .into_iter()
            .map(|u| serde_json::from_value(u).map_err(|err| format!("Invalid user entry from agent: {err}")))
            .collect()
    }

    async fn create_user(&self, name: &str, password: &str, tags: Vec<String>) -> Result<(), String> {
        let mut params = serde_json::json!({ "name": name, "password": password });
        if !tags.is_empty() {
            params["tags"] = serde_json::json!(tags);
        }
        self.call_ok("mq_create_user", params).await
    }

    async fn delete_user(&self, name: &str) -> Result<(), String> {
        self.call_ok("mq_delete_user", serde_json::json!({ "name": name })).await
    }

    async fn list_user_permissions(&self, ns: &NamespaceRef) -> Result<Vec<MqVhostPermission>, String> {
        let params = with_virtual_host(serde_json::json!({}), &ns.namespace);
        let result: serde_json::Value = self.call("mq_list_permissions", params).await?;
        let permissions = result.get("permissions").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        permissions
            .into_iter()
            .map(|p| serde_json::from_value(p).map_err(|err| format!("Invalid permission entry from agent: {err}")))
            .collect()
    }

    async fn grant_user_permission(
        &self,
        ns: &NamespaceRef,
        user: &str,
        configure: &str,
        write: &str,
        read: &str,
    ) -> Result<(), String> {
        require_specific_vhost(&ns.namespace)?;
        let mut params = serde_json::json!({ "user": user });
        // Empty patterns are omitted so the agent applies its `.*` default.
        for (key, value) in [("configure", configure), ("write", write), ("read", read)] {
            if !value.trim().is_empty() {
                params[key] = serde_json::json!(value);
            }
        }
        let params = with_virtual_host(params, &ns.namespace);
        self.call_ok("mq_grant_permission", params).await
    }

    async fn revoke_user_permission(&self, ns: &NamespaceRef, user: &str) -> Result<(), String> {
        require_specific_vhost(&ns.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "user": user }), &ns.namespace);
        self.call_ok("mq_revoke_permission", params).await
    }

    // ---- Policies ----
    //
    // RabbitMQ policies are scoped to one virtual host. The all-vhosts marker
    // (`*`) requests a cross-vhost listing (each policy reports its own
    // `vhost`); set/delete must name a specific virtual host.

    async fn list_policies(&self, ns: &NamespaceRef) -> Result<Vec<MqPolicyInfo>, String> {
        let params = with_virtual_host(serde_json::json!({}), &ns.namespace);
        let result: serde_json::Value = self.call("mq_list_policies", params).await?;
        let policies = result.get("policies").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        policies
            .into_iter()
            .map(|p| serde_json::from_value(p).map_err(|err| format!("Invalid policy entry from agent: {err}")))
            .collect()
    }

    async fn set_policy(&self, ns: &NamespaceRef, policy: &MqPolicyInfo) -> Result<(), String> {
        let params = policy_params(policy, &ns.namespace)?;
        self.call_ok("mq_set_policy", params).await
    }

    async fn delete_policy(&self, ns: &NamespaceRef, name: &str) -> Result<(), String> {
        require_specific_vhost(&ns.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "name": name }), &ns.namespace);
        self.call_ok("mq_delete_policy", params).await
    }

    // ---- Subscriptions ----
    //
    // RabbitMQ has no named subscriptions; consumers attach to queues directly.
    // Each queue consumer is surfaced as a subscription keyed by its consumer
    // tag (mirrors the Kafka consumer-group mapping).

    async fn list_subscriptions(&self, topic: &TopicRef) -> Result<Vec<SubscriptionInfo>, String> {
        require_specific_vhost(&topic.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "topic": queue_name(topic) }), &topic.namespace);
        let result: serde_json::Value = self.call("mq_list_consumers", params).await?;

        let consumers = result.get("consumers").and_then(|v| v.as_array()).cloned().unwrap_or_default();
        Ok(consumers.iter().map(subscription_from_consumer_json).collect())
    }

    async fn create_subscription(&self, _topic: &TopicRef, _sub: &str, _pos: ResetPosition) -> Result<(), String> {
        Err("RabbitMQ consumers are created when clients subscribe to a queue".to_string())
    }

    async fn delete_subscription(&self, _topic: &TopicRef, _sub: &str, _force: bool) -> Result<(), String> {
        Err("RabbitMQ does not support deleting subscriptions; stop the consumer client instead".to_string())
    }

    async fn skip_messages(&self, _topic: &TopicRef, _sub: &str, _count: SkipCount) -> Result<(), String> {
        Err("RabbitMQ does not support skipping messages on a subscription".to_string())
    }

    async fn reset_cursor(&self, _topic: &TopicRef, _sub: &str, _pos: ResetPosition) -> Result<(), String> {
        Err("RabbitMQ does not support resetting cursors; queues are consumed in order".to_string())
    }

    async fn clear_backlog(&self, topic: &TopicRef, _sub: &str) -> Result<(), String> {
        // RabbitMQ purges whole queues; the subscription argument is ignored.
        require_specific_vhost(&topic.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "topic": queue_name(topic) }), &topic.namespace);
        self.call_ok("mq_purge_queue", params).await
    }

    async fn peek_messages(
        &self,
        topic: &TopicRef,
        _sub: &str,
        count: u32,
        _options: PeekMessagesOptions,
    ) -> Result<PeekMessagesResult, String> {
        let conn_params = build_connection_params(&self.config)?;
        require_specific_vhost(&topic.namespace)?;
        let params = with_virtual_host(
            serde_json::json!({
                "topic": queue_name(topic),
                "count": count,
                "connection": conn_params,
            }),
            &topic.namespace,
        );
        let result: serde_json::Value = self.call("mq_peek_messages", params).await?;

        let messages = result.get("messages").and_then(|v| v.as_array()).cloned().unwrap_or_default();
        Ok(PeekMessagesResult::complete(
            messages.into_iter().enumerate().map(|(idx, m)| peeked_message_from_json(idx, &m)).collect(),
        ))
    }

    async fn expire_messages(&self, _topic: &TopicRef, _sub: &str, _expire_seconds: i64) -> Result<(), String> {
        Err("RabbitMQ does not support expiring messages on a subscription".to_string())
    }

    // ---- Producers / consumers ----
    //
    // The agent protocol has no producer listing method yet, so producers
    // return an empty list instead of failing the whole topic detail view.
    // Consumers come from the management API via `mq_list_consumers`.

    async fn list_producers(&self, _topic: &TopicRef) -> Result<Vec<ProducerInfo>, String> {
        Ok(Vec::new())
    }

    async fn list_consumers(&self, topic: &TopicRef, _sub: &str) -> Result<Vec<ConsumerInfo>, String> {
        require_specific_vhost(&topic.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "topic": queue_name(topic) }), &topic.namespace);
        let result: serde_json::Value = self.call("mq_list_consumers", params).await?;

        let consumers = result.get("consumers").and_then(|v| v.as_array()).cloned().unwrap_or_default();
        Ok(consumers.iter().map(consumer_from_json).collect())
    }

    async fn unload_topic(&self, _topic: &TopicRef) -> Result<(), String> {
        Err("RabbitMQ does not support unloading queues".to_string())
    }

    // ---- Rate limits / quotas / retention ----

    async fn set_publish_rate(&self, _scope: &PolicyScope, _rate: PublishRate) -> Result<(), String> {
        Err("RabbitMQ does not support publish rate limits via the agent".to_string())
    }

    async fn set_dispatch_rate(&self, _scope: &PolicyScope, _rate: DispatchRate) -> Result<(), String> {
        Err("RabbitMQ does not support dispatch rate limits via the agent".to_string())
    }

    async fn set_subscribe_rate(&self, _scope: &PolicyScope, _rate: SubscribeRate) -> Result<(), String> {
        Err("RabbitMQ does not support subscribe rate limits via the agent".to_string())
    }

    async fn set_backlog_quota(&self, _scope: &PolicyScope, _quota: BacklogQuota) -> Result<(), String> {
        Err("RabbitMQ does not support backlog quotas via the agent".to_string())
    }

    async fn set_retention(&self, _scope: &PolicyScope, _retention: RetentionPolicy) -> Result<(), String> {
        Err("RabbitMQ retention is managed through queue TTL policies, not supported via the agent".to_string())
    }

    async fn get_effective_policies(&self, scope: &PolicyScope) -> Result<serde_json::Value, String> {
        let (topic_name, namespace) = match scope {
            PolicyScope::Topic { topic, namespace, .. } => (topic.clone(), namespace.clone()),
            PolicyScope::Namespace { .. } => return Err("RabbitMQ does not support namespace policies".to_string()),
        };
        require_specific_vhost(&namespace)?;
        let params = with_virtual_host(serde_json::json!({ "name": topic_name }), &namespace);
        self.call("mq_get_topic_config", params).await
    }

    // ---- Permissions (not supported by the agent) ----

    async fn grant_permission(
        &self,
        _scope: &PolicyScope,
        _role: &str,
        _actions: Vec<AuthAction>,
    ) -> Result<(), String> {
        Err("RabbitMQ permissions are managed per virtual host user, not supported via the agent".to_string())
    }

    async fn revoke_permission(&self, _scope: &PolicyScope, _role: &str) -> Result<(), String> {
        Err("RabbitMQ permissions are managed per virtual host user, not supported via the agent".to_string())
    }

    async fn list_permissions(&self, _scope: &PolicyScope) -> Result<PermissionMap, String> {
        Err("RabbitMQ permissions are managed per virtual host user, not supported via the agent".to_string())
    }

    // ---- Monitoring ----

    async fn get_backlog(&self, topic: &TopicRef, _sub: Option<&str>) -> Result<BacklogStats, String> {
        // For RabbitMQ the backlog is the number of ready messages on the queue.
        require_specific_vhost(&topic.namespace)?;
        let params = with_virtual_host(serde_json::json!({ "name": queue_name(topic) }), &topic.namespace);
        let result: serde_json::Value = self.call("mq_get_topic_stats", params).await?;

        let total_messages = result.get("totalMessages").and_then(|v| v.as_i64()).unwrap_or(0);
        Ok(BacklogStats { msg_backlog: total_messages, backlog_size: total_messages, partitions: Vec::new() })
    }

    async fn get_cluster_info(&self) -> Result<ClusterInfo, String> {
        let result: serde_json::Value = self.call("mq_describe_cluster", serde_json::json!({})).await?;

        let cluster_id = result.get("clusterName").and_then(|v| v.as_str()).map(String::from);
        let brokers: Vec<BrokerNode> = result
            .get("nodes")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .enumerate()
                    .map(|(idx, node)| BrokerNode {
                        id: idx as i32,
                        host: node.get("name").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
                        port: node.get("port").and_then(|v| v.as_i64()).unwrap_or(0) as i32,
                        rack: None,
                        ..Default::default()
                    })
                    .collect()
            })
            .unwrap_or_default();
        let broker_count = brokers.len() as u32;

        Ok(ClusterInfo { cluster_id, broker_count, controller_id: None, controller_host: None, brokers, raw: result })
    }

    // ---- Cluster monitoring ----
    //
    // Overview and node listings are broker-global; there is no vhost concept.

    async fn get_overview(&self) -> Result<MqOverviewInfo, String> {
        let result: serde_json::Value = self.call("mq_overview", serde_json::json!({})).await?;
        serde_json::from_value(result).map_err(|err| format!("Invalid overview payload from agent: {err}"))
    }

    async fn list_nodes(&self) -> Result<Vec<MqNodeInfo>, String> {
        let result: serde_json::Value = self.call("mq_list_nodes", serde_json::json!({})).await?;
        let nodes = result.get("nodes").and_then(|v| v.as_array()).cloned().unwrap_or_default();

        nodes
            .into_iter()
            .map(|n| serde_json::from_value(n).map_err(|err| format!("Invalid node entry from agent: {err}")))
            .collect()
    }

    // ---- Raw request (not supported for RabbitMQ) ----

    async fn raw_request(&self, _req: MqRawRequest) -> Result<MqRawResponse, String> {
        Err("RabbitMQ raw admin requests are not supported via the agent".to_string())
    }

    // ---- Message production ----

    async fn send_message(&self, req: SendMessageRequest) -> Result<SendMessageResponse, String> {
        let mut params = serde_json::json!({
            "topic": req.topic,
            "key": req.key,
            "payloadBase64": req.payload_base64,
            "headers": req.headers,
        });
        if let Some(properties) = req.properties {
            params["properties"] = serde_json::json!(properties);
        }
        if let Some(exchange) = req.exchange.as_deref().filter(|e| !e.is_empty()) {
            params["exchange"] = serde_json::json!(exchange);
        }
        if let Some(routing_key) = req.routing_key.as_deref() {
            params["routingKey"] = serde_json::json!(routing_key);
        }
        if let Some(namespace) = req.namespace.as_deref() {
            require_specific_vhost(namespace)?;
            params = with_virtual_host(params, namespace);
        }
        let result: serde_json::Value = self.call("mq_send_message", params).await?;

        Ok(SendMessageResponse {
            topic: result.get("topic").and_then(|v| v.as_str()).unwrap_or(&req.topic).to_string(),
            partition: Some(0),
            offset: result.get("offset").and_then(|v| v.as_i64()).unwrap_or(0),
            timestamp: result.get("timestamp").and_then(|v| v.as_i64()).map(|v| v.to_string()),
        })
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

struct AgentPageMetadata {
    page: u32,
    page_size: u32,
    total_count: u64,
    has_more: bool,
}

impl AgentPageMetadata {
    fn with_items<T>(self, items: Vec<T>) -> MqListPage<T> {
        MqListPage {
            items,
            page: self.page,
            page_size: self.page_size,
            total_count: self.total_count,
            has_more: self.has_more,
        }
    }
}

fn rabbitmq_page_params(pagination: &MqListPageRequest) -> serde_json::Value {
    let sort = match pagination.sort {
        MqListSort::Name => "name",
        MqListSort::MessagesReady => "messages_ready",
    };
    let mut params = serde_json::json!({
        "page": pagination.page,
        "page_size": pagination.page_size,
        "sort": sort,
        "sort_reverse": pagination.sort_descending,
    });
    if let Some(search) = pagination.search.as_deref().map(str::trim).filter(|search| !search.is_empty()) {
        params["search"] = serde_json::json!(search);
    }
    params
}

fn agent_page_metadata(result: &serde_json::Value, request: &MqListPageRequest) -> Option<AgentPageMetadata> {
    // `totalCount` distinguishes the new bounded agent envelope from the
    // legacy `{topics:[...]}` / `{exchanges:[...]}` response.
    let total_count = result.get("totalCount")?.as_u64()?;
    let page = result
        .get("page")
        .and_then(serde_json::Value::as_u64)
        .and_then(|value| u32::try_from(value).ok())
        .unwrap_or(request.page);
    let page_size = result
        .get("pageSize")
        .and_then(serde_json::Value::as_u64)
        .and_then(|value| u32::try_from(value).ok())
        .unwrap_or(request.page_size);
    let has_more = result
        .get("hasMore")
        .and_then(serde_json::Value::as_bool)
        .unwrap_or_else(|| u64::from(page).saturating_mul(u64::from(page_size)) < total_count);
    Some(AgentPageMetadata { page, page_size, total_count, has_more })
}

fn paginate_topic_infos(mut topics: Vec<TopicInfo>, request: &MqListPageRequest) -> MqListPage<TopicInfo> {
    let search = request.search.as_deref().map(str::trim).unwrap_or_default().to_lowercase();
    if !search.is_empty() {
        topics.retain(|topic| {
            topic.name.to_lowercase().contains(&search) || topic.short_name.to_lowercase().contains(&search)
        });
    }
    topics.sort_by(|left, right| {
        let primary = match request.sort {
            MqListSort::Name => left.name.cmp(&right.name),
            MqListSort::MessagesReady => left
                .messages_ready
                .or(left.message_count)
                .unwrap_or_default()
                .cmp(&right.messages_ready.or(right.message_count).unwrap_or_default()),
        };
        let primary = if request.sort_descending { primary.reverse() } else { primary };
        primary.then_with(|| left.short_name.cmp(&right.short_name))
    });
    paginate_items(topics, request)
}

fn paginate_exchange_infos(
    mut exchanges: Vec<MqExchangeInfo>,
    request: &MqListPageRequest,
) -> MqListPage<MqExchangeInfo> {
    let search = request.search.as_deref().map(str::trim).unwrap_or_default().to_lowercase();
    if !search.is_empty() {
        exchanges.retain(|exchange| {
            let display_name = if exchange.name.is_empty() { "(AMQP default)" } else { &exchange.name };
            display_name.to_lowercase().contains(&search)
        });
    }
    exchanges.sort_by(|left, right| {
        let ordering = left.name.cmp(&right.name);
        if request.sort_descending {
            ordering.reverse()
        } else {
            ordering
        }
    });
    paginate_items(exchanges, request)
}

fn paginate_items<T>(items: Vec<T>, request: &MqListPageRequest) -> MqListPage<T> {
    let total_count = items.len() as u64;
    let start = u64::from(request.page.saturating_sub(1)).saturating_mul(u64::from(request.page_size));
    let page_items = if start >= total_count {
        Vec::new()
    } else {
        items.into_iter().skip(start as usize).take(request.page_size as usize).collect()
    };
    let has_more = start.saturating_add(page_items.len() as u64) < total_count;
    MqListPage { items: page_items, page: request.page, page_size: request.page_size, total_count, has_more }
}

/// Topic refs use flat queue names for RabbitMQ: the queue name is the short
/// topic name, the tenant is ignored. The namespace maps to a virtual host
/// and is passed through separately (see `with_virtual_host`).
fn queue_name(topic: &TopicRef) -> &str {
    &topic.topic
}

/// Namespace marker meaning "all vhosts": list methods request a cross-vhost
/// listing via `all_vhosts: true` instead of scoping to one virtual host.
const ALL_VHOSTS_NAMESPACE: &str = "*";

/// Resolve the virtual host for a namespace. The synthetic flat contexts
/// (`_flat_mq` / `_rabbitmq`), the all-vhosts marker (`*`), and an empty
/// namespace mean "no explicit vhost" — the agent then falls back to the
/// connection's configured virtual host.
fn namespace_virtual_host(namespace: &str) -> Option<&str> {
    match namespace.trim() {
        "" | ALL_VHOSTS_NAMESPACE | "_flat_mq" | "_rabbitmq" => None,
        other => Some(other),
    }
}

/// Attach the `virtual_host` param when the namespace names a real vhost. The
/// all-vhosts marker (`*`) instead requests a cross-vhost listing via
/// `all_vhosts: true` (the agent then uses the management API's no-vhost
/// variant and reports each item's vhost in the response).
fn with_virtual_host(mut params: serde_json::Value, namespace: &str) -> serde_json::Value {
    if namespace.trim() == ALL_VHOSTS_NAMESPACE {
        params["all_vhosts"] = serde_json::json!(true);
        return params;
    }
    if let Some(vhost) = namespace_virtual_host(namespace) {
        params["virtual_host"] = serde_json::json!(vhost);
    }
    params
}

/// Move the agent's per-item `vhost` field into `namespace` so the typed
/// structs pick it up; all-vhosts listings report each item's own vhost.
fn map_item_namespace(mut item: serde_json::Value) -> serde_json::Value {
    if let Some(vhost) = item.get("vhost").and_then(|v| v.as_str()).map(String::from) {
        item["namespace"] = serde_json::json!(vhost);
    }
    item
}

/// Fail fast when an operation is scoped to the all-vhosts marker (`*`).
/// Cross-vhost context only makes sense for list operations; every targeted
/// operation must name a specific virtual host instead of silently falling
/// back to the connection's default vhost.
fn require_specific_vhost(namespace: &str) -> Result<(), String> {
    if namespace.trim() == ALL_VHOSTS_NAMESPACE {
        return Err("operation requires a specific virtual host (all-vhosts context)".to_string());
    }
    Ok(())
}

/// Vhost name for namespace create/delete: these address a specific vhost by
/// name, so the all-vhosts marker and synthetic/empty namespaces are invalid.
fn namespace_vhost_name(namespace: &str) -> Result<&str, String> {
    require_specific_vhost(namespace)?;
    if namespace.trim().is_empty() || namespace.starts_with('_') {
        return Err(format!("namespace create/delete requires a real virtual host name, got {namespace:?}"));
    }
    Ok(namespace)
}

/// Build the JSON-RPC params for `mq_bind` / `mq_unbind` from a binding. The
/// serialized `MqBindingInfo` already matches the agent contract
/// (`source`/`destination`/`destinationType`/`routingKey`/`arguments`). A
/// concrete vhost carried on the binding itself (e.g. picked from an
/// all-vhosts listing) wins over the namespace argument.
fn binding_params(binding: &MqBindingInfo, namespace: &str) -> Result<serde_json::Value, String> {
    let effective = binding.namespace.as_deref().filter(|ns| namespace_virtual_host(ns).is_some()).unwrap_or(namespace);
    require_specific_vhost(effective)?;
    let params = serde_json::to_value(binding).unwrap_or_else(|_| serde_json::json!({}));
    Ok(with_virtual_host(params, effective))
}

/// Build the JSON-RPC params for `mq_set_policy` from a policy. The all-vhosts
/// marker (`*`) is rejected: policies are written to one specific virtual
/// host. `applyTo` is omitted when empty so the agent applies its `queues`
/// default; `priority` is always sent (0 matches the agent default).
fn policy_params(policy: &MqPolicyInfo, namespace: &str) -> Result<serde_json::Value, String> {
    require_specific_vhost(namespace)?;
    let mut params = serde_json::json!({
        "name": policy.name,
        "pattern": policy.pattern,
        "priority": policy.priority,
        "definition": policy.definition,
    });
    if !policy.apply_to.trim().is_empty() {
        params["applyTo"] = serde_json::json!(policy.apply_to);
    }
    Ok(with_virtual_host(params, namespace))
}

/// Map one agent consumer JSON to a `ConsumerInfo`. The management API does
/// not report rates, so only identity and prefetch (as available permits,
/// same convention as Pulsar) are filled.
fn consumer_from_json(c: &serde_json::Value) -> ConsumerInfo {
    ConsumerInfo {
        consumer_name: c
            .get("name")
            .and_then(|v| v.as_str())
            .or_else(|| c.get("tag").and_then(|v| v.as_str()))
            .unwrap_or_default()
            .to_string(),
        msg_rate_out: 0.0,
        msg_throughput_out: 0.0,
        available_permits: c.get("prefetch").and_then(|v| v.as_i64()).unwrap_or(0),
        address: String::new(),
        client_version: String::new(),
    }
}

/// Map one agent consumer JSON to a `SubscriptionInfo` keyed by consumer tag
/// (RabbitMQ has no named subscriptions; each consumer acts as one).
fn subscription_from_consumer_json(c: &serde_json::Value) -> SubscriptionInfo {
    let tag =
        c.get("tag").and_then(|v| v.as_str()).or_else(|| c.get("name").and_then(|v| v.as_str())).unwrap_or_default();
    SubscriptionInfo {
        name: tag.to_string(),
        sub_type: "consumer".to_string(),
        consumers: vec![consumer_from_json(c)],
        ..Default::default()
    }
}

/// Extract RabbitMQ addresses from MqAdminConfig.extra.
fn addresses(cfg: &MqAdminConfig) -> String {
    extra_str(&cfg.extra, "addresses").or_else(|| extra_str(&cfg.extra, "host")).unwrap_or("").to_string()
}

fn extra_str<'a>(extra: &'a serde_json::Value, key: &str) -> Option<&'a str> {
    extra.get(key).and_then(|v| v.as_str()).filter(|v| !v.trim().is_empty())
}

fn extra_port(extra: &serde_json::Value) -> Result<u16, String> {
    const DEFAULT_AMQP_PORT: u16 = 5672;
    match extra.get("port") {
        Some(v) if v.is_u64() => {
            let port = v.as_u64().unwrap_or(0);
            u16::try_from(port).map_err(|_| format!("RabbitMQ port {port} is out of range (0-65535)"))
        }
        Some(v) => match v.as_str() {
            Some(s) => s.trim().parse::<u16>().map_err(|_| format!("invalid RabbitMQ port '{s}'")),
            None => Err("RabbitMQ port must be a number or a numeric string".to_string()),
        },
        None => Ok(DEFAULT_AMQP_PORT),
    }
}

fn configured_management_port(cfg: &MqAdminConfig) -> Result<Option<u16>, String> {
    let Some(value) = cfg.extra.get("properties").and_then(|value| value.get("management_port")) else {
        return Ok(None);
    };
    let port = if let Some(port) = value.as_u64() {
        u16::try_from(port).map_err(|_| format!("RabbitMQ management port {port} is out of range (1-65535)"))?
    } else if let Some(port) = value.as_str() {
        port.trim().parse::<u16>().map_err(|_| format!("invalid RabbitMQ management port '{port}'"))?
    } else {
        return Err("RabbitMQ management port must be a number or a numeric string".to_string());
    };
    if port == 0 {
        return Err("RabbitMQ management port must be between 1 and 65535".to_string());
    }
    Ok(Some(port))
}

pub(crate) fn primary_amqp_endpoint(cfg: &MqAdminConfig) -> Result<(String, u16), String> {
    let address_list = addresses(cfg);
    let first = address_list
        .split(',')
        .map(str::trim)
        .find(|address| !address.is_empty())
        .ok_or("RabbitMQ addresses are empty")?;
    let parsed = reqwest::Url::parse(&format!("amqp://{first}"))
        .map_err(|error| format!("RabbitMQ address '{first}' is invalid: {error}"))?;
    let host = parsed
        .host_str()
        .filter(|host| !host.is_empty())
        .ok_or_else(|| format!("RabbitMQ address '{first}' has no host"))?;
    Ok((host.to_string(), parsed.port().unwrap_or(extra_port(&cfg.extra)?)))
}

/// Resolve the independently configured RabbitMQ Management HTTP endpoint.
///
/// A blank admin URL is only safe to derive for RabbitMQ's default AMQP ports.
/// Custom AMQP listeners do not define the Management listener port, so callers
/// must not silently fall back to another broker's default port 15672/15671.
pub(crate) fn management_endpoint(cfg: &MqAdminConfig) -> Result<Option<(String, u16)>, String> {
    if !cfg.admin_url.trim().is_empty() {
        let parsed = reqwest::Url::parse(cfg.admin_url.trim())
            .map_err(|error| format!("RabbitMQ Management API URL is invalid: {error}"))?;
        let host = parsed
            .host_str()
            .filter(|host| !host.is_empty())
            .ok_or("RabbitMQ Management API URL does not include a host")?;
        let port = parsed.port_or_known_default().ok_or("RabbitMQ Management API URL does not include a port")?;
        return Ok(Some((host.to_string(), port)));
    }

    let (host, amqp_port) = primary_amqp_endpoint(cfg)?;
    if let Some(port) = configured_management_port(cfg)? {
        return Ok(Some((host, port)));
    }
    let tls_enabled =
        cfg.extra.get("properties").and_then(|properties| properties.as_object()).is_some_and(|properties| {
            properties.get("ssl").and_then(|value| value.as_bool()).unwrap_or(false)
                || properties.get("tls").and_then(|value| value.as_bool()).unwrap_or(false)
        });
    let is_default_amqp_port = amqp_port == 5672 || (tls_enabled && amqp_port == 5671);
    if !is_default_amqp_port {
        return Ok(None);
    }
    Ok(Some((host, if tls_enabled { 15671 } else { 15672 })))
}

/// Build the connection params JSON from MqAdminConfig for the native agent.
/// Blank credentials are omitted so the agent falls back to its guest/guest
/// default instead of authenticating as `:`. A non-empty `admin_url` is
/// forwarded as `management_url`; otherwise the agent derives the management
/// endpoint from the AMQP addresses itself.
fn build_connection_params(cfg: &MqAdminConfig) -> Result<serde_json::Value, String> {
    let extra = &cfg.extra;
    let basic_auth = match &cfg.auth {
        MqAuth::Basic { username, password } => Some((username.as_str(), password.as_str())),
        _ => None,
    };
    let username = extra_str(extra, "username").or_else(|| basic_auth.map(|(username, _)| username));
    let password = extra_str(extra, "password").or_else(|| basic_auth.map(|(_, password)| password));
    let virtual_host = extra_str(extra, "virtualHost").or_else(|| extra_str(extra, "virtual_host")).unwrap_or("/");
    let properties =
        extra.get("properties").filter(|value| value.is_object()).cloned().unwrap_or_else(|| serde_json::json!({}));

    let mut params = serde_json::json!({
        "addresses": addresses(cfg),
        "port": extra_port(extra)?,
        "virtual_host": virtual_host,
        "tls_skip_verify": cfg.tls_skip_verify,
        "properties": properties,
        "request_timeout_ms": cfg.request_timeout_ms(),
    });
    if let Some(username) = username.filter(|value| !value.trim().is_empty()) {
        params["username"] = serde_json::json!(username);
    }
    if let Some(password) = password.filter(|value| !value.trim().is_empty()) {
        params["password"] = serde_json::json!(password);
    }
    if !cfg.admin_url.trim().is_empty() {
        params["management_url"] = serde_json::json!(cfg.admin_url);
    }
    if let Some(connect_override) = &cfg.connect_override {
        params["connect_override"] = serde_json::json!({
            "host": connect_override.host,
            "port": connect_override.port,
        });
    }
    if let Some(connect_override) = &cfg.management_connect_override {
        params["management_connect_override"] = serde_json::json!({
            "host": connect_override.host,
            "port": connect_override.port,
        });
    }
    Ok(params)
}

/// Map one agent peek message JSON to a `PeekedMessage`.
fn peeked_message_from_json(idx: usize, m: &serde_json::Value) -> PeekedMessage {
    let properties = m
        .get("properties")
        .and_then(|v| v.as_object())
        .map(|obj| obj.iter().map(|(k, v)| (k.clone(), v.as_str().unwrap_or_default().to_string())).collect())
        .unwrap_or_default();
    let payload_base64 = m.get("payloadBase64").and_then(|v| v.as_str()).unwrap_or_default().to_string();
    let payload_text = resolve_rabbitmq_payload_text(m.get("payloadText").and_then(|v| v.as_str()), &payload_base64);
    PeekedMessage {
        position: (idx + 1) as u32,
        message_id: m
            .get("messageId")
            .and_then(|v| v.as_str())
            .map(String::from)
            .or_else(|| m.get("deliveryTag").and_then(|v| v.as_i64()).map(|v| v.to_string())),
        key: m
            .get("key")
            .and_then(|v| v.as_str())
            .or_else(|| m.get("routingKey").and_then(|v| v.as_str()))
            .map(String::from),
        // The agent reports 0 for messages without a timestamp; map that to
        // `None` so the frontend does not render a 1970 date.
        publish_time: m.get("timestamp").and_then(|v| v.as_i64()).filter(|ts| *ts > 0).map(|v| v.to_string()),
        event_time: None,
        properties,
        headers: m
            .get("headers")
            .and_then(|v| v.as_object())
            .map(|obj| obj.iter().map(|(k, v)| (k.clone(), v.as_str().unwrap_or_default().to_string())).collect())
            .unwrap_or_default(),
        payload_base64,
        payload_text,
    }
}

fn is_printable_text(text: &str) -> bool {
    text.chars().all(|c| (c >= ' ' || matches!(c, '\t' | '\n' | '\r')) && c != '\u{7f}')
}

fn resolve_rabbitmq_payload_text(raw_text: Option<&str>, payload_base64: &str) -> Option<String> {
    if let Some(text) = raw_text {
        if !text.is_empty() && is_printable_text(text) {
            return Some(text.to_string());
        }
    }
    if !payload_base64.is_empty() {
        use base64::Engine as _;
        if let Ok(bytes) = base64::engine::general_purpose::STANDARD.decode(payload_base64.trim()) {
            if let Some(decoded) = decode_thrift_binary_json(&bytes) {
                return Some(decoded);
            }
        }
    }
    if let Some(text) = raw_text {
        if let Some(decoded) = decode_thrift_binary_json(text.as_bytes()) {
            return Some(decoded);
        }
    }
    raw_text.map(String::from)
}

const THRIFT_VERSION_MASK: u32 = 0xffff_0000;
const THRIFT_VERSION_1: u32 = 0x8001_0000;
const THRIFT_MAX_DEPTH: usize = 32;
const THRIFT_MAX_ITEMS: usize = 10_000;
const THRIFT_MAX_METHOD_LEN: usize = 256;

struct ThriftCursor<'a> {
    data: &'a [u8],
    pos: usize,
}

impl<'a> ThriftCursor<'a> {
    fn remaining(&self) -> usize {
        self.data.len().saturating_sub(self.pos)
    }

    fn at_end(&self) -> bool {
        self.pos >= self.data.len()
    }

    fn take(&mut self, len: usize) -> Option<&'a [u8]> {
        let end = self.pos.checked_add(len)?;
        if end > self.data.len() {
            return None;
        }
        let slice = &self.data[self.pos..end];
        self.pos = end;
        Some(slice)
    }

    fn u8(&mut self) -> Option<u8> {
        Some(self.take(1)?[0])
    }

    fn i16(&mut self) -> Option<i16> {
        Some(i16::from_be_bytes(self.take(2)?.try_into().ok()?))
    }

    fn i32(&mut self) -> Option<i32> {
        Some(i32::from_be_bytes(self.take(4)?.try_into().ok()?))
    }

    fn u32(&mut self) -> Option<u32> {
        Some(u32::from_be_bytes(self.take(4)?.try_into().ok()?))
    }

    fn i64(&mut self) -> Option<i64> {
        Some(i64::from_be_bytes(self.take(8)?.try_into().ok()?))
    }

    fn f64(&mut self) -> Option<f64> {
        let val = f64::from_be_bytes(self.take(8)?.try_into().ok()?);
        val.is_finite().then_some(val)
    }
}

fn thrift_message_type(code: u8) -> Option<&'static str> {
    match code {
        1 => Some("CALL"),
        2 => Some("REPLY"),
        3 => Some("EXCEPTION"),
        4 => Some("ONEWAY"),
        _ => None,
    }
}

fn is_valid_thrift_method(name: &str) -> bool {
    !name.is_empty() && name.len() <= THRIFT_MAX_METHOD_LEN && name.bytes().all(|b| (0x21..0x7f).contains(&b))
}

fn bytes_to_hex(bytes: &[u8]) -> String {
    let mut out = String::with_capacity(bytes.len() * 2);
    for b in bytes {
        use std::fmt::Write as _;
        let _ = write!(&mut out, "{b:02x}");
    }
    out
}

fn read_thrift_value(cur: &mut ThriftCursor<'_>, field_type: u8, depth: usize) -> Option<serde_json::Value> {
    if depth > THRIFT_MAX_DEPTH {
        return None;
    }
    match field_type {
        1 => Some(serde_json::Value::Null),
        2 => match cur.u8()? {
            0 => Some(serde_json::Value::Bool(false)),
            1 => Some(serde_json::Value::Bool(true)),
            _ => None,
        },
        3 => Some(serde_json::json!(cur.u8()? as i8)),
        4 => Some(serde_json::json!(cur.f64()?)),
        6 => Some(serde_json::json!(cur.i16()?)),
        8 => Some(serde_json::json!(cur.i32()?)),
        10 => Some(serde_json::json!(cur.i64()?)),
        11 => {
            let len = usize::try_from(cur.i32()?).ok()?;
            let raw = cur.take(len)?;
            if raw.is_empty() {
                return Some(serde_json::Value::String(String::new()));
            }
            if let Ok(text) = std::str::from_utf8(raw) {
                if is_printable_text(text) {
                    return Some(serde_json::Value::String(text.to_string()));
                }
            }
            if depth < THRIFT_MAX_DEPTH {
                if let Some(nested) = decode_thrift_raw_struct(raw, depth + 1) {
                    return Some(nested);
                }
            }
            Some(serde_json::json!({ "$bytes": bytes_to_hex(raw) }))
        }
        12 => Some(serde_json::Value::Object(read_thrift_struct(cur, depth + 1)?)),
        13 => {
            let key_type = cur.u8()?;
            let val_type = cur.u8()?;
            let size = usize::try_from(cur.i32()?).ok()?;
            if size > THRIFT_MAX_ITEMS || size > cur.remaining() {
                return None;
            }
            let mut map = serde_json::Map::with_capacity(size);
            for _ in 0..size {
                let k = read_thrift_value(cur, key_type, depth + 1)?;
                let v = read_thrift_value(cur, val_type, depth + 1)?;
                let key_str = match k {
                    serde_json::Value::String(s) => s,
                    other => other.to_string(),
                };
                map.insert(key_str, v);
            }
            Some(serde_json::Value::Object(map))
        }
        14 | 15 => {
            let elem_type = cur.u8()?;
            let size = usize::try_from(cur.i32()?).ok()?;
            if size > THRIFT_MAX_ITEMS || size > cur.remaining() {
                return None;
            }
            let mut items = Vec::with_capacity(size);
            for _ in 0..size {
                items.push(read_thrift_value(cur, elem_type, depth + 1)?);
            }
            Some(serde_json::Value::Array(items))
        }
        16 => {
            let hex = bytes_to_hex(cur.take(16)?);
            Some(serde_json::Value::String(format!(
                "{}-{}-{}-{}-{}",
                &hex[0..8],
                &hex[8..12],
                &hex[12..16],
                &hex[16..20],
                &hex[20..32]
            )))
        }
        _ => None,
    }
}

fn read_thrift_struct(cur: &mut ThriftCursor<'_>, depth: usize) -> Option<serde_json::Map<String, serde_json::Value>> {
    if depth > THRIFT_MAX_DEPTH {
        return None;
    }
    let mut fields = serde_json::Map::new();
    loop {
        let field_type = cur.u8()?;
        if field_type == 0 {
            break;
        }
        let field_id = cur.i16()?;
        if field_id < 0 {
            return None;
        }
        let key = field_id.to_string();
        if fields.contains_key(&key) {
            return None;
        }
        let val = read_thrift_value(cur, field_type, depth)?;
        fields.insert(key, val);
    }
    Some(fields)
}

fn decode_thrift_strict_message(bytes: &[u8]) -> Option<serde_json::Value> {
    let mut cur = ThriftCursor { data: bytes, pos: 0 };
    let version_and_type = cur.u32()?;
    if (version_and_type & THRIFT_VERSION_MASK) != THRIFT_VERSION_1 || (version_and_type & 0x0000_ff00) != 0 {
        return None;
    }
    let msg_type = thrift_message_type((version_and_type & 0xff) as u8)?;
    let name_len = usize::try_from(cur.i32()?).ok()?;
    let method = std::str::from_utf8(cur.take(name_len)?).ok()?;
    if !is_valid_thrift_method(method) {
        return None;
    }
    let seq_id = cur.i32()?;
    let fields = read_thrift_struct(&mut cur, 0)?;
    cur.at_end().then(|| {
        serde_json::json!({
            "method": method,
            "type": msg_type,
            "seqId": seq_id,
            "fields": fields,
        })
    })
}

fn decode_thrift_non_strict_message(bytes: &[u8]) -> Option<serde_json::Value> {
    let mut cur = ThriftCursor { data: bytes, pos: 0 };
    let name_len = usize::try_from(cur.i32()?).ok()?;
    let method = std::str::from_utf8(cur.take(name_len)?).ok()?;
    if !is_valid_thrift_method(method) {
        return None;
    }
    let msg_type = thrift_message_type(cur.u8()?)?;
    let seq_id = cur.i32()?;
    let fields = read_thrift_struct(&mut cur, 0)?;
    cur.at_end().then(|| {
        serde_json::json!({
            "method": method,
            "type": msg_type,
            "seqId": seq_id,
            "fields": fields,
        })
    })
}

fn decode_thrift_raw_struct(bytes: &[u8], depth: usize) -> Option<serde_json::Value> {
    let mut cur = ThriftCursor { data: bytes, pos: 0 };
    let fields = read_thrift_struct(&mut cur, depth)?;
    (cur.at_end() && !fields.is_empty()).then_some(serde_json::Value::Object(fields))
}

fn decode_thrift_unframed(bytes: &[u8]) -> Option<serde_json::Value> {
    if bytes.len() >= 4 && (u32::from_be_bytes(bytes[..4].try_into().ok()?) & THRIFT_VERSION_MASK) == THRIFT_VERSION_1 {
        return decode_thrift_strict_message(bytes);
    }
    decode_thrift_non_strict_message(bytes).or_else(|| decode_thrift_raw_struct(bytes, 0))
}

fn decode_thrift_binary_json(bytes: &[u8]) -> Option<String> {
    if bytes.is_empty() {
        return None;
    }
    let value = decode_thrift_unframed(bytes).or_else(|| {
        if bytes.len() > 4 {
            let frame_len = u32::from_be_bytes(bytes[..4].try_into().ok()?) as usize;
            if frame_len > 0 && frame_len == bytes.len() - 4 {
                return decode_thrift_unframed(&bytes[4..]);
            }
        }
        None
    })?;
    serde_json::to_string_pretty(&value).ok()
}

/// Map an `mq_get_topic_stats` agent payload onto the UI-friendly TopicStats.
///
/// RabbitMQ rates come from the management API's `message_stats` sample. A
/// missing sample (new/idle queue, rates_mode=basic/none, or the AMQP
/// passive-declare fallback path) must NOT be presented as a real rate of
/// zero: `rates_unavailable` is set so the UI renders "no data" instead.
fn topic_stats_from_agent_value(result: serde_json::Value) -> TopicStats {
    let total_messages = result.get("totalMessages").and_then(|v| v.as_i64()).unwrap_or(0);
    let consumer_count = result.get("consumerCount").and_then(|v| v.as_u64()).unwrap_or(0) as u32;
    let publish_rate = result.get("publishRate").and_then(|v| v.as_f64());
    let deliver_rate = result.get("deliverRate").and_then(|v| v.as_f64());
    let has_stats =
        result.get("publishRate").is_some() || result.get("deliverRate").is_some() || result.get("ackRate").is_some();

    TopicStats {
        msg_rate_in: publish_rate.unwrap_or(0.0),
        msg_rate_out: deliver_rate.unwrap_or(0.0),
        msg_throughput_in: 0.0,
        msg_throughput_out: 0.0,
        storage_size: 0,
        backlog_size: total_messages,
        msg_in_counter: result.get("publishTotal").and_then(|v| v.as_i64()).unwrap_or(0),
        msg_out_counter: result.get("deliverGetTotal").and_then(|v| v.as_i64()).unwrap_or(0),
        subscription_count: consumer_count,
        producer_count: 0,
        rates_unavailable: !has_stats,
        raw: result,
    }
}

fn topic_info_from_agent_value(topic: &serde_json::Value) -> TopicInfo {
    let name = topic.get("name").and_then(|value| value.as_str()).unwrap_or_default().to_string();
    // Older RabbitMQ versions only report the queue type in the x-queue-type
    // argument; the agent already prefers the explicit `type` field.
    let queue_type = topic
        .get("queueType")
        .and_then(|value| value.as_str())
        .or_else(|| topic.get("arguments").and_then(|args| args.get("x-queue-type")).and_then(|value| value.as_str()))
        .map(String::from);
    TopicInfo {
        name: name.clone(),
        short_name: name,
        partitioned: false,
        partitions: None,
        persistent: topic.get("durable").and_then(|value| value.as_bool()).unwrap_or(true),
        internal: topic.get("internal").and_then(|value| value.as_bool()).unwrap_or(false),
        message_type: None,
        // All-vhosts listings report each queue's own vhost.
        namespace: topic.get("vhost").and_then(|value| value.as_str()).map(String::from),
        message_count: topic.get("messages").and_then(|value| value.as_i64()),
        messages_ready: topic.get("messagesReady").and_then(|value| value.as_i64()),
        messages_unacked: topic.get("messagesUnacked").and_then(|value| value.as_i64()),
        auto_delete: topic.get("autoDelete").and_then(|value| value.as_bool()),
        exclusive: topic.get("exclusive").and_then(|value| value.as_bool()),
        state: topic.get("state").and_then(|value| value.as_str()).map(String::from),
        queue_type,
        arguments: topic.get("arguments").cloned().filter(|value| !value.is_null()),
        consumer_count: topic.get("consumerCount").or_else(|| topic.get("consumers")).and_then(|value| value.as_i64()),
        publish_rate: topic.get("publishRate").and_then(|value| value.as_f64()),
        deliver_rate: topic.get("deliverRate").and_then(|value| value.as_f64()),
        ack_rate: topic.get("ackRate").and_then(|value| value.as_f64()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mq::auth::MqAuth;
    use crate::mq::types::MqSystemKind;

    #[tokio::test]
    async fn send_message_forwards_properties_without_changing_headers_or_legacy_requests() {
        let directory = tempfile::tempdir().expect("create fake RabbitMQ agent directory");
        let script = directory.path().join("agent.py");
        let calls = directory.path().join("calls.jsonl");
        std::fs::write(
            &script,
            r#"import json
import sys

print(json.dumps({"ready": True}), flush=True)
for line in sys.stdin:
    request = json.loads(line)
    if request["method"] == "mq_send_message":
        with open(sys.argv[1], "a") as calls:
            calls.write(json.dumps(request["params"]) + "\n")
    print(json.dumps({"jsonrpc": "2.0", "id": request["id"], "result": {"ok": True}}), flush=True)
"#,
        )
        .expect("write fake RabbitMQ agent");
        let python = if cfg!(windows) { "python" } else { "python3" };
        let launch = AgentLaunchSpec::new(python)
            .with_args([script.to_string_lossy().to_string(), calls.to_string_lossy().to_string()]);
        let config = rabbitmq_config(serde_json::json!({ "addresses": "broker:5672" }), MqAuth::None, false);
        let admin = RabbitMqAdmin::new(config, launch).await.expect("connect fake RabbitMQ agent");
        let properties = serde_json::json!({ "content_type": "application/json", "priority": "0" });
        for expected in [None, Some(properties)] {
            let mut value = serde_json::json!({
                "topic": "orders", "payloadBase64": "e30=",
                "headers": { "content_type": "custom-header" },
                "exchange": "events", "routingKey": "orders.created", "namespace": "orders-vhost"
            });
            if let Some(properties) = expected.as_ref() {
                value["properties"] = properties.clone();
            }
            admin.send_message(serde_json::from_value(value).unwrap()).await.expect("send message");
            let calls = std::fs::read_to_string(&calls).expect("read agent calls");
            let actual: serde_json::Value = serde_json::from_str(calls.lines().last().unwrap()).unwrap();
            assert_eq!(actual.get("properties"), expected.as_ref());
            assert_eq!(actual["headers"], serde_json::json!({ "content_type": "custom-header" }));
            assert_eq!(actual["exchange"], "events");
            assert_eq!(actual["routingKey"], "orders.created");
            assert_eq!(actual["virtual_host"], "orders-vhost");
        }
    }

    fn rabbitmq_config(extra: serde_json::Value, auth: MqAuth, tls_skip_verify: bool) -> MqAdminConfig {
        MqAdminConfig {
            system_kind: MqSystemKind::RabbitMq,
            admin_url: String::new(),
            auth,
            tls_skip_verify,
            pinned_version: None,
            token_signing: None,
            connect_override: None,
            management_connect_override: None,
            socks_proxy: None,
            query_timeout_secs: crate::mq::config::DEFAULT_MQ_QUERY_TIMEOUT_SECS,
            connect_timeout_secs: crate::mq::config::DEFAULT_MQ_CONNECT_TIMEOUT_SECS,
            extra,
        }
    }

    #[test]
    fn connection_params_map_addresses_vhost_and_basic_auth() {
        let cfg = rabbitmq_config(
            serde_json::json!({
                "addresses": "rabbit1:5672,rabbit2:5672",
                "port": 5671,
                "virtualHost": "orders",
                "properties": {
                    "connectionName": "dbx"
                }
            }),
            MqAuth::Basic { username: "alice".to_string(), password: "secret".to_string() },
            true,
        );

        let params = build_connection_params(&cfg).expect("connection params");

        assert_eq!(params.get("addresses").and_then(|v| v.as_str()), Some("rabbit1:5672,rabbit2:5672"));
        assert_eq!(params.get("port").and_then(|v| v.as_u64()), Some(5671));
        assert_eq!(params.get("username").and_then(|v| v.as_str()), Some("alice"));
        assert_eq!(params.get("password").and_then(|v| v.as_str()), Some("secret"));
        assert_eq!(params.get("virtual_host").and_then(|v| v.as_str()), Some("orders"));
        assert_eq!(params.get("tls_skip_verify").and_then(|v| v.as_bool()), Some(true));
        assert_eq!(params.pointer("/properties/connectionName").and_then(|v| v.as_str()), Some("dbx"));
    }

    #[test]
    fn connection_params_fall_back_to_host_extra_and_defaults() {
        let cfg = rabbitmq_config(
            serde_json::json!({
                "host": "localhost"
            }),
            MqAuth::None,
            false,
        );

        let params = build_connection_params(&cfg).expect("connection params");

        assert_eq!(params.get("addresses").and_then(|v| v.as_str()), Some("localhost"));
        assert_eq!(params.get("port").and_then(|v| v.as_u64()), Some(5672));
        // Blank credentials are omitted so the agent applies its guest/guest
        // fallback instead of authenticating as ':'.
        assert!(params.get("username").is_none());
        assert!(params.get("password").is_none());
        assert_eq!(params.get("virtual_host").and_then(|v| v.as_str()), Some("/"));
        assert_eq!(params.get("tls_skip_verify").and_then(|v| v.as_bool()), Some(false));
        // Without an explicit admin URL the agent derives the management
        // endpoint from the AMQP addresses.
        assert!(params.get("management_url").is_none());
    }

    #[test]
    fn connection_params_accept_string_port_and_extra_credentials() {
        let cfg = rabbitmq_config(
            serde_json::json!({
                "addresses": "broker",
                "port": "5673",
                "username": "bob",
                "password": "pw"
            }),
            MqAuth::None,
            false,
        );

        let params = build_connection_params(&cfg).expect("connection params");

        assert_eq!(params.get("port").and_then(|v| v.as_u64()), Some(5673));
        assert_eq!(params.get("username").and_then(|v| v.as_str()), Some("bob"));
        assert_eq!(params.get("password").and_then(|v| v.as_str()), Some("pw"));
    }

    #[test]
    fn connection_params_omit_whitespace_only_credentials() {
        let cfg = rabbitmq_config(
            serde_json::json!({ "addresses": "broker" }),
            MqAuth::Basic { username: "   ".to_string(), password: "\t".to_string() },
            false,
        );

        let params = build_connection_params(&cfg).expect("connection params");

        assert!(params.get("username").is_none());
        assert!(params.get("password").is_none());
    }

    #[test]
    fn connection_params_forward_admin_url_as_management_url() {
        // An explicit management URL (e.g. behind a reverse proxy with a path
        // prefix) is forwarded as-is so the agent does not derive it from the
        // AMQP addresses.
        let mut cfg = rabbitmq_config(
            serde_json::json!({ "addresses": "broker" }),
            MqAuth::Basic { username: "alice".to_string(), password: "secret".to_string() },
            false,
        );
        cfg.admin_url = "http://rabbit.internal:15672/proxy".to_string();
        cfg = cfg.with_connect_override("127.0.0.1", 45672).with_management_connect_override("127.0.0.1", 45673);

        let params = build_connection_params(&cfg).expect("connection params");

        assert_eq!(params.get("management_url").and_then(|v| v.as_str()), Some("http://rabbit.internal:15672/proxy"));
        assert_eq!(params.pointer("/connect_override/host").and_then(|v| v.as_str()), Some("127.0.0.1"));
        assert_eq!(params.pointer("/connect_override/port").and_then(|v| v.as_u64()), Some(45672));
        assert_eq!(params.pointer("/management_connect_override/host").and_then(|v| v.as_str()), Some("127.0.0.1"));
        assert_eq!(params.pointer("/management_connect_override/port").and_then(|v| v.as_u64()), Some(45673));
    }

    #[test]
    fn management_endpoint_does_not_guess_for_custom_amqp_port() {
        let custom = rabbitmq_config(serde_json::json!({ "addresses": "rabbit.internal:5673" }), MqAuth::None, false);
        assert_eq!(primary_amqp_endpoint(&custom).unwrap(), ("rabbit.internal".to_string(), 5673));
        assert_eq!(management_endpoint(&custom).unwrap(), None);

        let configured = rabbitmq_config(
            serde_json::json!({
                "addresses": "rabbit.internal:5673",
                "properties": { "management_port": 15673 }
            }),
            MqAuth::None,
            false,
        );
        assert_eq!(management_endpoint(&configured).unwrap(), Some(("rabbit.internal".to_string(), 15673)));

        let mut explicit = custom;
        explicit.admin_url = "https://management.internal:8443/rmq".to_string();
        assert_eq!(management_endpoint(&explicit).unwrap(), Some(("management.internal".to_string(), 8443)));
    }

    #[test]
    fn connection_params_reject_out_of_range_ports() {
        for port in [serde_json::json!(70000), serde_json::json!("99999"), serde_json::json!("abc")] {
            let cfg = rabbitmq_config(serde_json::json!({ "addresses": "broker", "port": port }), MqAuth::None, false);

            assert!(build_connection_params(&cfg).is_err(), "port {port} must be rejected");
        }
    }

    #[test]
    fn queue_name_uses_flat_topic_and_ignores_namespace() {
        let topic = TopicRef {
            tenant: "_rabbitmq".to_string(),
            namespace: "_rabbitmq".to_string(),
            topic: "orders.queue".to_string(),
            persistent: true,
            partitioned: None,
            message_type: None,
            ..TopicRef::default()
        };

        assert_eq!(queue_name(&topic), "orders.queue");
    }

    #[test]
    fn topic_info_maps_queue_message_counts() {
        let topic = topic_info_from_agent_value(&serde_json::json!({
            "name": "orders",
            "durable": true,
            "vhost": "/",
            "messages": 12,
            "messagesReady": 10,
            "messagesUnacked": 2
        }));

        assert_eq!(topic.message_count, Some(12));
        assert_eq!(topic.messages_ready, Some(10));
        assert_eq!(topic.messages_unacked, Some(2));
        assert_eq!(topic.namespace.as_deref(), Some("/"));
    }

    #[test]
    fn topic_info_maps_queue_features_arguments_and_rates() {
        let topic = topic_info_from_agent_value(&serde_json::json!({
            "name": "orders",
            "durable": true,
            "autoDelete": false,
            "exclusive": true,
            "state": "running",
            "queueType": "quorum",
            "consumerCount": 4,
            "arguments": {
                "x-queue-type": "quorum",
                "x-message-ttl": 60000,
                "x-max-length": 1000
            },
            "publishRate": 12.5,
            "deliverRate": 11.8,
            "ackRate": 11.2
        }));

        assert_eq!(topic.auto_delete, Some(false));
        assert_eq!(topic.exclusive, Some(true));
        assert_eq!(topic.state.as_deref(), Some("running"));
        assert_eq!(topic.queue_type.as_deref(), Some("quorum"));
        assert_eq!(topic.consumer_count, Some(4));
        assert_eq!(topic.publish_rate, Some(12.5));
        assert_eq!(topic.deliver_rate, Some(11.8));
        assert_eq!(topic.ack_rate, Some(11.2));
        let arguments = topic.arguments.as_ref().expect("arguments");
        assert_eq!(arguments["x-message-ttl"], serde_json::json!(60000));
        assert_eq!(arguments["x-queue-type"], serde_json::json!("quorum"));
    }

    #[test]
    fn topic_info_accepts_consumers_wire_field() {
        let topic = topic_info_from_agent_value(&serde_json::json!({
            "name": "orders",
            "consumers": 3
        }));

        assert_eq!(topic.consumer_count, Some(3));
    }

    #[test]
    fn topic_info_falls_back_to_x_queue_type_argument() {
        // Older RabbitMQ versions only expose the queue type in arguments.
        let topic = topic_info_from_agent_value(&serde_json::json!({
            "name": "stream-q",
            "arguments": { "x-queue-type": "stream" }
        }));
        assert_eq!(topic.queue_type.as_deref(), Some("stream"));

        // No type anywhere: absent, never guessed.
        let plain = topic_info_from_agent_value(&serde_json::json!({"name": "plain"}));
        assert_eq!(plain.queue_type, None);
        assert_eq!(plain.arguments, None);
        assert_eq!(plain.publish_rate, None);
    }

    #[test]
    fn topic_stats_parses_rates_and_counters_from_agent_payload() {
        let stats = topic_stats_from_agent_value(serde_json::json!({
            "name": "orders",
            "totalMessages": 123,
            "consumerCount": 4,
            "publishRate": 12.5,
            "deliverRate": 11.8,
            "ackRate": 11.2,
            "publishTotal": 1000,
            "deliverGetTotal": 900,
            "ackTotal": 880
        }));

        assert_eq!(stats.msg_rate_in, 12.5);
        assert_eq!(stats.msg_rate_out, 11.8);
        assert_eq!(stats.backlog_size, 123);
        assert_eq!(stats.subscription_count, 4);
        assert_eq!(stats.msg_in_counter, 1000);
        assert_eq!(stats.msg_out_counter, 900);
        assert!(!stats.rates_unavailable, "rates sampled by the management API");
    }

    #[test]
    fn topic_stats_marks_rates_unavailable_when_message_stats_is_missing() {
        // No message_stats sample (fallback path, idle queue, rates_mode=none):
        // the zero placeholders must not be presented as real rates.
        let stats = topic_stats_from_agent_value(serde_json::json!({
            "name": "idle",
            "totalMessages": 5,
            "consumerCount": 0
        }));

        assert_eq!(stats.msg_rate_in, 0.0);
        assert!(stats.rates_unavailable, "rates must be flagged unavailable");

        // And a real sampled zero is NOT flagged: it is a genuine rate of 0.
        let zero = topic_stats_from_agent_value(serde_json::json!({
            "name": "quiet",
            "totalMessages": 0,
            "consumerCount": 0,
            "publishRate": 0.0,
            "deliverRate": 0.0,
            "ackRate": 0.0
        }));
        assert_eq!(zero.msg_rate_in, 0.0);
        assert!(!zero.rates_unavailable, "a real zero rate is not unavailable");
    }

    #[test]
    fn topic_stats_serialization_omits_rates_unavailable_when_false() {
        let stats = topic_stats_from_agent_value(serde_json::json!({
            "name": "q", "totalMessages": 1, "consumerCount": 0, "publishRate": 3.5
        }));
        let json = serde_json::to_value(&stats).expect("serialize stats");
        assert!(json.get("ratesUnavailable").is_none());

        let unavailable = topic_stats_from_agent_value(serde_json::json!({
            "name": "q", "totalMessages": 1, "consumerCount": 0
        }));
        let json = serde_json::to_value(&unavailable).expect("serialize stats");
        assert_eq!(json.get("ratesUnavailable"), Some(&serde_json::json!(true)));
    }

    #[test]
    fn peeked_message_maps_agent_fields() {
        let msg = serde_json::json!({
            "deliveryTag": 42,
            "routingKey": "orders.new",
            "timestamp": 1710000000000i64,
            "headers": { "x-trace": "abc" },
            "properties": { "contentType": "application/json" },
            "payloadBase64": "aGVsbG8=",
            "payloadText": "hello"
        });

        let peeked = peeked_message_from_json(0, &msg);

        assert_eq!(peeked.position, 1);
        assert_eq!(peeked.message_id.as_deref(), Some("42"));
        assert_eq!(peeked.key.as_deref(), Some("orders.new"));
        assert_eq!(peeked.publish_time.as_deref(), Some("1710000000000"));
        assert_eq!(peeked.headers.get("x-trace").map(String::as_str), Some("abc"));
        assert_eq!(peeked.properties.get("contentType").map(String::as_str), Some("application/json"));
        assert_eq!(peeked.payload_base64, "aGVsbG8=");
        assert_eq!(peeked.payload_text.as_deref(), Some("hello"));
    }

    #[test]
    fn peeked_message_maps_zero_timestamp_to_no_publish_time() {
        // The agent reports 0 for messages without a timestamp; the frontend
        // would render that as 1970, so it maps to `None`.
        let msg = serde_json::json!({
            "deliveryTag": 7,
            "timestamp": 0,
            "payloadBase64": "aGVsbG8="
        });

        let peeked = peeked_message_from_json(0, &msg);

        assert_eq!(peeked.message_id.as_deref(), Some("7"));
        assert!(peeked.publish_time.is_none());
    }

    #[test]
    fn peeked_message_decodes_thrift_binary_protocol() {
        use base64::Engine as _;
        let strict_call = [
            0x80u8, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, b'p', b'i', b'n', b'g', 0x00, 0x00, 0x00, 0x07, 0x0b,
            0x00, 0x01, 0x00, 0x00, 0x00, 0x02, b'o', b'k', 0x08, 0x00, 0x02, 0x00, 0x00, 0x00, 0x2a, 0x00,
        ];
        let msg = serde_json::json!({
            "deliveryTag": 9,
            "payloadBase64": base64::engine::general_purpose::STANDARD.encode(strict_call),
        });
        let peeked = peeked_message_from_json(0, &msg);
        let decoded: serde_json::Value =
            serde_json::from_str(peeked.payload_text.as_deref().expect("decoded thrift")).expect("valid json");
        assert_eq!(decoded["method"], "ping");
        assert_eq!(decoded["type"], "CALL");
        assert_eq!(decoded["seqId"], 7);
        assert_eq!(decoded["fields"]["1"], "ok");
        assert_eq!(decoded["fields"]["2"], 42);
    }

    #[test]
    #[allow(clippy::assertions_on_constants)]
    fn capabilities_enable_namespaces_and_clear_backlog() {
        assert!(RABBITMQ_CAPABILITIES.supports_namespaces);
        assert!(RABBITMQ_CAPABILITIES.supports_clear_backlog);
        assert!(RABBITMQ_CAPABILITIES.supports_subscriptions);
        assert!(RABBITMQ_CAPABILITIES.supports_exchanges);
        assert!(RABBITMQ_CAPABILITIES.supports_client_connections);
        assert!(RABBITMQ_CAPABILITIES.supports_user_permissions);
        assert!(!RABBITMQ_CAPABILITIES.supports_tenants);
        assert!(!RABBITMQ_CAPABILITIES.supports_partitioned_topics);
        assert!(!RABBITMQ_CAPABILITIES.supports_create_subscription);
    }

    #[test]
    fn user_entries_parse_from_agent_payload() {
        let payload = serde_json::json!({
            "users": [
                { "name": "dbx-app", "tags": ["management"] },
                { "name": "dbx-svc" }
            ]
        });

        let users: Vec<MqUserInfo> = payload
            .get("users")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|u| serde_json::from_value(u).expect("user entry"))
            .collect();

        assert_eq!(users.len(), 2);
        assert_eq!(users[0].name, "dbx-app");
        assert_eq!(users[0].tags, vec!["management".to_string()]);
        assert_eq!(users[1].name, "dbx-svc");
        assert!(users[1].tags.is_empty());
    }

    #[test]
    fn permission_entries_parse_from_agent_payload() {
        let payload = serde_json::json!({
            "permissions": [
                { "user": "dbx-app", "vhost": "orders", "configure": ".*", "write": "dbx-.*", "read": ".*" },
                { "user": "dbx-app", "vhost": "/", "configure": "", "write": "", "read": "" }
            ]
        });

        let permissions: Vec<MqVhostPermission> = payload
            .get("permissions")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|p| serde_json::from_value(p).expect("permission entry"))
            .collect();

        assert_eq!(permissions.len(), 2);
        assert_eq!(permissions[0].user, "dbx-app");
        assert_eq!(permissions[0].vhost, "orders");
        assert_eq!(permissions[0].write, "dbx-.*");
        assert_eq!(permissions[1].vhost, "/");
        assert!(permissions[1].configure.is_empty());
    }

    #[test]
    fn client_connection_entries_parse_from_agent_payload() {
        let payload = serde_json::json!({
            "connections": [
                {
                    "name": "192.168.1.10:52344 -> 192.168.1.126:5672",
                    "user": "jjsd",
                    "peerHost": "192.168.1.10",
                    "peerPort": 52344,
                    "state": "running",
                    "channels": 2,
                    "recvRate": 10.0,
                    "sendRate": 5.5,
                    "connectedAt": 1710000000000i64
                },
                {
                    "name": "192.168.1.11:1000 -> 192.168.1.126:5672",
                    "user": "guest",
                    "peerHost": "192.168.1.11",
                    "peerPort": 1000,
                    "state": "blocked",
                    "channels": 1
                }
            ]
        });

        let connections: Vec<MqClientConnectionInfo> = payload
            .get("connections")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|c| serde_json::from_value(c).expect("connection entry"))
            .collect();

        assert_eq!(connections.len(), 2);
        assert_eq!(connections[0].channels, 2);
        assert_eq!(connections[0].recv_rate, Some(10.0));
        assert_eq!(connections[0].connected_at, Some(1710000000000));
        assert_eq!(connections[1].state, "blocked");
        assert!(connections[1].recv_rate.is_none());
        assert!(connections[1].connected_at.is_none());
    }

    #[test]
    fn channel_entries_parse_from_agent_payload() {
        let payload = serde_json::json!({
            "channels": [
                {
                    "name": "192.168.1.10:52344 -> 192.168.1.126:5672 (1)",
                    "connectionName": "192.168.1.10:52344 -> 192.168.1.126:5672",
                    "state": "running",
                    "prefetch": 10,
                    "messagesUnacked": 3,
                    "consumerCount": 1
                },
                {
                    "name": "192.168.1.11:1000 -> 192.168.1.126:5672 (1)",
                    "state": "running"
                }
            ]
        });

        let channels: Vec<MqChannelInfo> = payload
            .get("channels")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|c| serde_json::from_value(c).expect("channel entry"))
            .collect();

        assert_eq!(channels.len(), 2);
        assert_eq!(channels[0].prefetch, Some(10));
        assert_eq!(channels[0].messages_unacked, Some(3));
        assert_eq!(channels[0].consumer_count, Some(1));
        assert_eq!(channels[0].connection_name.as_deref(), Some("192.168.1.10:52344 -> 192.168.1.126:5672"));
        assert!(channels[1].connection_name.is_none());
        assert!(channels[1].prefetch.is_none());
    }

    #[test]
    fn binding_params_match_agent_contract_and_attach_vhost() {
        let binding = MqBindingInfo {
            source: "dbx-events".to_string(),
            destination: "dbx-queue".to_string(),
            destination_type: "queue".to_string(),
            routing_key: Some("orders.*".to_string()),
            arguments: None,
            namespace: None,
        };

        let params = binding_params(&binding, "orders").expect("binding params");

        assert_eq!(params.get("source").and_then(|v| v.as_str()), Some("dbx-events"));
        assert_eq!(params.get("destination").and_then(|v| v.as_str()), Some("dbx-queue"));
        assert_eq!(params.get("destinationType").and_then(|v| v.as_str()), Some("queue"));
        assert_eq!(params.get("routingKey").and_then(|v| v.as_str()), Some("orders.*"));
        assert!(params.get("arguments").is_none());
        assert_eq!(params.get("virtual_host").and_then(|v| v.as_str()), Some("orders"));

        let bare = MqBindingInfo {
            source: "dbx-a".to_string(),
            destination: "dbx-b".to_string(),
            destination_type: "exchange".to_string(),
            routing_key: None,
            arguments: None,
            namespace: None,
        };
        let params = binding_params(&bare, "_rabbitmq").expect("binding params");
        assert!(params.get("routingKey").is_none());
        assert!(params.get("virtual_host").is_none());
    }

    #[test]
    fn require_specific_vhost_rejects_only_the_all_vhosts_marker() {
        let err = require_specific_vhost("*").expect_err("all-vhosts marker must fail");
        assert_eq!(err, "operation requires a specific virtual host (all-vhosts context)");

        // Whitespace padding does not disguise the marker.
        assert!(require_specific_vhost("  *  ").is_err());

        // Real vhosts, synthetic namespaces, and an empty namespace all pass:
        // they either name a vhost or legitimately use the connection default.
        for ns in ["orders", "_rabbitmq", "_flat_mq", ""] {
            assert!(require_specific_vhost(ns).is_ok(), "namespace {ns:?} must be allowed");
        }
    }

    #[test]
    fn namespace_vhost_name_rejects_star_and_synthetic_namespaces() {
        assert!(namespace_vhost_name("*").is_err(), "create/delete must reject the all-vhosts marker");
        assert!(namespace_vhost_name("").is_err());
        assert!(namespace_vhost_name("_rabbitmq").is_err());
        assert!(namespace_vhost_name("_flat_mq").is_err());
        assert_eq!(namespace_vhost_name("/").expect("root vhost"), "/");
        assert_eq!(namespace_vhost_name("orders").expect("named vhost"), "orders");
    }

    #[test]
    fn binding_params_prefers_binding_namespace_over_argument() {
        let binding = MqBindingInfo {
            source: "dbx-events".to_string(),
            destination: "dbx-queue".to_string(),
            destination_type: "queue".to_string(),
            routing_key: None,
            arguments: None,
            namespace: Some("orders".to_string()),
        };

        let params = binding_params(&binding, "other").expect("binding params");
        assert_eq!(params.get("virtual_host").and_then(|v| v.as_str()), Some("orders"));

        // Synthetic, empty, and wildcard binding namespaces are not real
        // vhosts and fall back to the namespace argument.
        for binding_ns in ["_rabbitmq", "_flat_mq", "", "*"] {
            let scoped = MqBindingInfo { namespace: Some(binding_ns.to_string()), ..binding.clone() };
            let params = binding_params(&scoped, "orders").expect("binding params");
            assert_eq!(
                params.get("virtual_host").and_then(|v| v.as_str()),
                Some("orders"),
                "binding namespace {binding_ns:?} must fall back to the argument"
            );
        }
    }

    #[test]
    fn binding_params_rejects_all_vhosts_context_without_a_concrete_vhost() {
        let binding = MqBindingInfo {
            source: "dbx-events".to_string(),
            destination: "dbx-queue".to_string(),
            destination_type: "queue".to_string(),
            routing_key: None,
            arguments: None,
            namespace: None,
        };

        let err = binding_params(&binding, "*").expect_err("all-vhosts bind must fail");
        assert_eq!(err, "operation requires a specific virtual host (all-vhosts context)");

        // A concrete vhost on the binding rescues an all-vhosts argument:
        // the row picked from an all-vhosts listing carries its own vhost.
        let scoped = MqBindingInfo { namespace: Some("orders".to_string()), ..binding };
        let params = binding_params(&scoped, "*").expect("binding namespace wins over the marker");
        assert_eq!(params.get("virtual_host").and_then(|v| v.as_str()), Some("orders"));
        assert!(params.get("all_vhosts").is_none());
    }

    #[test]
    fn list_helpers_keep_all_vhosts_listing_for_star() {
        // List methods never go through require_specific_vhost; the '*' marker
        // still maps to a cross-vhost listing for them.
        let params = with_virtual_host(serde_json::json!({}), "*");
        assert_eq!(params.get("all_vhosts").and_then(|v| v.as_bool()), Some(true));
        assert!(params.get("virtual_host").is_none());
    }

    #[test]
    fn namespace_virtual_host_ignores_synthetic_and_empty_namespaces() {
        assert_eq!(namespace_virtual_host(""), None);
        assert_eq!(namespace_virtual_host("_flat_mq"), None);
        assert_eq!(namespace_virtual_host("_rabbitmq"), None);
        assert_eq!(namespace_virtual_host("*"), None);
        assert_eq!(namespace_virtual_host("orders"), Some("orders"));
    }

    #[test]
    fn with_virtual_host_adds_param_only_for_real_vhosts() {
        let params = with_virtual_host(serde_json::json!({ "name": "q1" }), "orders");
        assert_eq!(params.get("virtual_host").and_then(|v| v.as_str()), Some("orders"));

        for ns in ["", "_flat_mq", "_rabbitmq"] {
            let params = with_virtual_host(serde_json::json!({ "name": "q1" }), ns);
            assert!(params.get("virtual_host").is_none(), "namespace {ns:?} must not set virtual_host");
        }
    }

    #[test]
    fn with_virtual_host_maps_star_to_all_vhosts() {
        let params = with_virtual_host(serde_json::json!({ "name": "q1" }), "*");
        assert_eq!(params.get("all_vhosts").and_then(|v| v.as_bool()), Some(true));
        assert!(params.get("virtual_host").is_none(), "all-vhosts listing must not scope to a vhost");
    }

    #[test]
    fn rabbitmq_page_params_map_search_and_message_sort_for_the_agent() {
        let params = rabbitmq_page_params(&MqListPageRequest {
            page: 3,
            page_size: 200,
            search: Some("orders".to_string()),
            sort: MqListSort::MessagesReady,
            sort_descending: true,
        });

        assert_eq!(params.get("page").and_then(|value| value.as_u64()), Some(3));
        assert_eq!(params.get("page_size").and_then(|value| value.as_u64()), Some(200));
        assert_eq!(params.get("search").and_then(|value| value.as_str()), Some("orders"));
        assert_eq!(params.get("sort").and_then(|value| value.as_str()), Some("messages_ready"));
        assert_eq!(params.get("sort_reverse").and_then(|value| value.as_bool()), Some(true));
    }

    #[test]
    fn legacy_agent_topic_result_is_filtered_sorted_and_bounded_locally() {
        let topics = vec![
            TopicInfo {
                name: "orders-low".to_string(),
                short_name: "orders-low".to_string(),
                messages_ready: Some(2),
                ..TopicInfo::default()
            },
            TopicInfo {
                name: "unrelated".to_string(),
                short_name: "unrelated".to_string(),
                messages_ready: Some(99),
                ..TopicInfo::default()
            },
            TopicInfo {
                name: "orders-high".to_string(),
                short_name: "orders-high".to_string(),
                messages_ready: Some(8),
                ..TopicInfo::default()
            },
        ];
        let page = paginate_topic_infos(
            topics,
            &MqListPageRequest {
                page: 1,
                page_size: 1,
                search: Some("orders".to_string()),
                sort: MqListSort::MessagesReady,
                sort_descending: true,
            },
        );

        assert_eq!(page.total_count, 2);
        assert!(page.has_more);
        assert_eq!(page.items[0].name, "orders-high");
    }

    #[test]
    fn map_item_namespace_moves_vhost_into_namespace_field() {
        let item = map_item_namespace(serde_json::json!({ "name": "dbx-q", "vhost": "orders" }));
        assert_eq!(item.get("namespace").and_then(|v| v.as_str()), Some("orders"));

        // Entries without a vhost stay untouched (single-vhost listings).
        let plain = map_item_namespace(serde_json::json!({ "name": "dbx-q" }));
        assert!(plain.get("namespace").is_none());

        // The typed structs pick the mapped vhost up as `namespace`.
        let exchange: MqExchangeInfo = serde_json::from_value(map_item_namespace(
            serde_json::json!({ "name": "dbx-ex", "type": "direct", "vhost": "orders" }),
        ))
        .expect("exchange entry");
        assert_eq!(exchange.namespace.as_deref(), Some("orders"));

        let channel: MqChannelInfo =
            serde_json::from_value(map_item_namespace(serde_json::json!({ "name": "ch (1)" }))).expect("channel entry");
        assert!(channel.namespace.is_none());
    }

    #[test]
    fn consumer_maps_agent_fields() {
        let consumer = serde_json::json!({
            "name": "consumer-1@host",
            "tag": "ctag1.0",
            "active": true,
            "ackRequired": true,
            "prefetch": 20
        });

        let info = consumer_from_json(&consumer);

        assert_eq!(info.consumer_name, "consumer-1@host");
        assert_eq!(info.available_permits, 20);
        assert_eq!(info.msg_rate_out, 0.0);

        let without_prefetch = consumer_from_json(&serde_json::json!({ "tag": "ctag2.0" }));
        assert_eq!(without_prefetch.consumer_name, "ctag2.0");
        assert_eq!(without_prefetch.available_permits, 0);
    }

    #[test]
    fn subscription_uses_consumer_tag_as_name() {
        let consumer = serde_json::json!({
            "name": "consumer-1@host",
            "tag": "ctag1.0",
            "active": true,
            "ackRequired": false,
            "prefetch": 5
        });

        let sub = subscription_from_consumer_json(&consumer);

        assert_eq!(sub.name, "ctag1.0");
        assert_eq!(sub.sub_type, "consumer");
        assert_eq!(sub.consumers.len(), 1);
        assert_eq!(sub.consumers[0].consumer_name, "consumer-1@host");
        assert_eq!(sub.consumers[0].available_permits, 5);
    }

    #[test]
    #[allow(clippy::assertions_on_constants)]
    fn capabilities_enable_policies_and_cluster_monitoring() {
        assert!(RABBITMQ_CAPABILITIES.supports_policies);
        assert!(RABBITMQ_CAPABILITIES.supports_cluster_monitoring);
    }

    #[test]
    fn policy_entries_parse_from_agent_payload() {
        let payload = serde_json::json!({
            "policies": [
                {
                    "name": "dbx-ttl",
                    "vhost": "orders",
                    "pattern": "^dbx-",
                    "applyTo": "queues",
                    "priority": 5,
                    "definition": { "message-ttl": 60000 }
                },
                {
                    "name": "dbx-ha",
                    "vhost": "/",
                    "pattern": ".*",
                    "applyTo": "all",
                    "priority": 0,
                    "definition": { "ha-mode": "all" }
                }
            ]
        });

        let policies: Vec<MqPolicyInfo> = payload
            .get("policies")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|p| serde_json::from_value(p).expect("policy entry"))
            .collect();

        assert_eq!(policies.len(), 2);
        assert_eq!(policies[0].name, "dbx-ttl");
        assert_eq!(policies[0].vhost, "orders");
        assert_eq!(policies[0].apply_to, "queues");
        assert_eq!(policies[0].priority, 5);
        assert_eq!(policies[0].definition.get("message-ttl").and_then(|v| v.as_i64()), Some(60000));
        assert_eq!(policies[1].definition.get("ha-mode").and_then(|v| v.as_str()), Some("all"));
    }

    #[test]
    fn policy_params_match_agent_contract_and_attach_vhost() {
        let mut definition = std::collections::HashMap::new();
        definition.insert("message-ttl".to_string(), serde_json::json!(60000));
        let policy = MqPolicyInfo {
            name: "dbx-ttl".to_string(),
            vhost: String::new(),
            pattern: "^dbx-".to_string(),
            apply_to: "queues".to_string(),
            priority: 5,
            definition,
        };

        let params = policy_params(&policy, "orders").expect("policy params");
        assert_eq!(params.get("name").and_then(|v| v.as_str()), Some("dbx-ttl"));
        assert_eq!(params.get("pattern").and_then(|v| v.as_str()), Some("^dbx-"));
        assert_eq!(params.get("applyTo").and_then(|v| v.as_str()), Some("queues"));
        assert_eq!(params.get("priority").and_then(|v| v.as_i64()), Some(5));
        assert_eq!(params.pointer("/definition/message-ttl").and_then(|v| v.as_i64()), Some(60000));
        assert_eq!(params.get("virtual_host").and_then(|v| v.as_str()), Some("orders"));
        // The vhost carried on the policy itself is not forwarded; the
        // namespace argument scopes the write.
        assert!(params.get("vhost").is_none());
    }

    #[test]
    fn policy_params_omit_empty_apply_to_so_agent_default_applies() {
        let policy = MqPolicyInfo { name: "dbx-ha".to_string(), pattern: ".*".to_string(), ..MqPolicyInfo::default() };

        let params = policy_params(&policy, "_rabbitmq").expect("policy params");
        assert!(params.get("applyTo").is_none());
        assert_eq!(params.get("priority").and_then(|v| v.as_i64()), Some(0));
        // Synthetic namespaces do not scope to a vhost; the agent falls back
        // to the connection's configured virtual host.
        assert!(params.get("virtual_host").is_none());
    }

    #[test]
    fn policy_params_reject_all_vhosts_context() {
        let policy =
            MqPolicyInfo { name: "dbx-ttl".to_string(), pattern: "^dbx-".to_string(), ..MqPolicyInfo::default() };

        let err = policy_params(&policy, "*").expect_err("all-vhosts policy write must fail");
        assert_eq!(err, "operation requires a specific virtual host (all-vhosts context)");
    }

    #[test]
    fn node_entries_parse_from_agent_payload() {
        let payload = serde_json::json!({
            "nodes": [
                {
                    "name": "rabbit@node1",
                    "running": true,
                    "memUsed": 536870912i64,
                    "memLimit": 1717986918i64,
                    "diskFree": 10737418240i64,
                    "fdUsed": 42,
                    "fdTotal": 1024,
                    "socketsUsed": 10,
                    "socketsTotal": 900,
                    "uptimeMs": 86400000i64
                },
                { "name": "rabbit@node2", "running": false }
            ]
        });

        let nodes: Vec<MqNodeInfo> = payload
            .get("nodes")
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .map(|n| serde_json::from_value(n).expect("node entry"))
            .collect();

        assert_eq!(nodes.len(), 2);
        assert!(nodes[0].running);
        assert_eq!(nodes[0].mem_used, Some(536870912));
        assert_eq!(nodes[0].fd_total, Some(1024));
        assert_eq!(nodes[0].uptime_ms, Some(86400000));
        assert!(!nodes[1].running);
        assert!(nodes[1].mem_used.is_none());
        assert!(nodes[1].uptime_ms.is_none());
    }

    #[test]
    fn overview_payload_parses_with_partial_fields() {
        let overview: MqOverviewInfo = serde_json::from_value(serde_json::json!({
            "messagesReady": 7,
            "publishRate": 3.5,
            "totalQueues": 2
        }))
        .expect("overview payload");

        assert_eq!(overview.messages_ready, Some(7));
        assert_eq!(overview.publish_rate, Some(3.5));
        assert_eq!(overview.total_queues, Some(2));
        assert!(overview.messages_unacked.is_none());
        assert!(overview.total_consumers.is_none());
    }
}