#[cfg(test)]
mod rag_grounding_commands_tests {
    use super::*;
    use crate::app_state::AppState;
    use crate::infrastructure::gemini_service::{RagGroundingRequest, RagGroundingConfig};
    use tauri::State;
    use std::sync::Arc;

    /// 创建测试用的 AppState
    fn create_test_app_state() -> AppState {
        AppState::new()
    }

    #[test]
    fn test_rag_grounding_request_validation() {
        let request = RagGroundingRequest {
            user_input: "测试查询".to_string(),
            config: Some(RagGroundingConfig::default()),
            session_id: Some("test-session".to_string()),
        };

        // 验证请求结构
        assert!(!request.user_input.is_empty());
        assert!(request.config.is_some());
        assert!(request.session_id.is_some());
    }

    #[test]
    fn test_rag_grounding_config_serialization() {
        let config = RagGroundingConfig::default();
        
        // 测试序列化
        let serialized = serde_json::to_string(&config).expect("Failed to serialize config");
        assert!(serialized.contains("gen-lang-client-0413414134"));
        assert!(serialized.contains("gemini-2.5-flash"));
        
        // 测试反序列化
        let deserialized: RagGroundingConfig = serde_json::from_str(&serialized)
            .expect("Failed to deserialize config");
        assert_eq!(deserialized.project_id, config.project_id);
        assert_eq!(deserialized.model_id, config.model_id);
    }

    #[test]
    fn test_empty_user_input_handling() {
        let request = RagGroundingRequest {
            user_input: "".to_string(),
            config: None,
            session_id: None,
        };

        // 空输入应该被正确处理
        assert!(request.user_input.is_empty());
        assert!(request.config.is_none());
        assert!(request.session_id.is_none());
    }

    #[test]
    fn test_custom_config_override() {
        let custom_config = RagGroundingConfig {
            project_id: "custom-project".to_string(),
            location: "us-central1".to_string(),
            data_store_id: "custom-datastore".to_string(),
            model_id: "gemini-pro".to_string(),
            temperature: 0.5,
            max_output_tokens: 4096,
            system_prompt: Some("自定义提示".to_string()),
        };

        let request = RagGroundingRequest {
            user_input: "测试查询".to_string(),
            config: Some(custom_config.clone()),
            session_id: Some("custom-session".to_string()),
        };

        assert_eq!(request.config.as_ref().unwrap().project_id, "custom-project");
        assert_eq!(request.config.as_ref().unwrap().temperature, 0.5);
        assert_eq!(request.config.as_ref().unwrap().system_prompt, Some("自定义提示".to_string()));
    }

    #[test]
    fn test_session_id_generation() {
        let session_id = format!("session-{}", chrono::Utc::now().timestamp());
        
        let request = RagGroundingRequest {
            user_input: "测试查询".to_string(),
            config: None,
            session_id: Some(session_id.clone()),
        };

        assert_eq!(request.session_id, Some(session_id));
        assert!(request.session_id.as_ref().unwrap().starts_with("session-"));
    }

    #[test]
    fn test_config_json_structure() {
        let config = RagGroundingConfig::default();
        let json_value = serde_json::to_value(&config).expect("Failed to convert to JSON");

        // 验证必要字段存在
        assert!(json_value.get("project_id").is_some());
        assert!(json_value.get("location").is_some());
        assert!(json_value.get("data_store_id").is_some());
        assert!(json_value.get("model_id").is_some());
        assert!(json_value.get("temperature").is_some());
        assert!(json_value.get("max_output_tokens").is_some());

        // 验证字段类型
        assert!(json_value["project_id"].is_string());
        assert!(json_value["temperature"].is_number());
        assert!(json_value["max_output_tokens"].is_number());
    }

    #[test]
    fn test_request_with_minimal_data() {
        let request = RagGroundingRequest {
            user_input: "简单查询".to_string(),
            config: None,
            session_id: None,
        };

        // 最小数据请求应该有效
        assert_eq!(request.user_input, "简单查询");
        assert!(request.config.is_none());
        assert!(request.session_id.is_none());
    }

    #[test]
    fn test_unicode_input_handling() {
        let unicode_input = "测试中文输入 🔍 emoji 和特殊字符 @#$%";
        
        let request = RagGroundingRequest {
            user_input: unicode_input.to_string(),
            config: None,
            session_id: None,
        };

        assert_eq!(request.user_input, unicode_input);
        
        // 测试序列化包含Unicode字符的请求
        let serialized = serde_json::to_string(&request).expect("Failed to serialize Unicode request");
        assert!(serialized.contains("测试中文输入"));
        assert!(serialized.contains("🔍"));
    }

    #[test]
    fn test_long_input_handling() {
        let long_input = "这是一个很长的输入".repeat(100);
        
        let request = RagGroundingRequest {
            user_input: long_input.clone(),
            config: None,
            session_id: None,
        };

        assert_eq!(request.user_input.len(), long_input.len());
        assert!(request.user_input.len() > 1000); // 确保输入确实很长
    }

    #[test]
    fn test_config_temperature_bounds() {
        let mut config = RagGroundingConfig::default();
        
        // 测试不同的温度值
        config.temperature = 0.0;
        assert_eq!(config.temperature, 0.0);
        
        config.temperature = 1.0;
        assert_eq!(config.temperature, 1.0);
        
        config.temperature = 2.0;
        assert_eq!(config.temperature, 2.0);
    }

    #[test]
    fn test_config_token_limits() {
        let mut config = RagGroundingConfig::default();
        
        // 测试不同的令牌限制
        config.max_output_tokens = 1024;
        assert_eq!(config.max_output_tokens, 1024);
        
        config.max_output_tokens = 8192;
        assert_eq!(config.max_output_tokens, 8192);
        
        config.max_output_tokens = 32768;
        assert_eq!(config.max_output_tokens, 32768);
    }
}
