//! ComfyUI V2 配置测试
//! 测试配置保存和加载功能

#[cfg(test)]
mod tests {
    use crate::business::services::config_manager::ConfigManager;
    use crate::config::AppConfig;
    use crate::data::models::comfyui::ComfyUIConfig;
    use std::collections::HashMap;
    use tempfile::TempDir;

    /// 测试配置管理器的目录创建和文件保存
    #[tokio::test]
    async fn test_config_manager_directory_creation() {
        // 创建临时目录
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config").join("app_config.json");
        let config_path_str = config_path.to_string_lossy().to_string();

        // 创建配置管理器
        let app_config = AppConfig::default();
        let config_manager = ConfigManager::new(app_config, Some(config_path_str.clone()));

        // 创建测试配置
        let mut custom_headers = HashMap::new();
        custom_headers.insert("Authorization".to_string(), "Bearer test".to_string());

        let test_config = ComfyUIConfig {
            base_url: "http://192.168.0.193:8188".to_string(),
            timeout_seconds: 300,
            retry_attempts: 3,
            retry_delay_ms: 1000,
            enable_websocket: true,
            enable_cache: true,
            max_concurrency: 4,
            custom_headers: Some(custom_headers),
        };

        // 测试配置更新（这应该创建目录并保存文件）
        let result = config_manager.update_comfyui_config(test_config.clone()).await;
        
        // 验证操作成功
        assert!(result.is_ok(), "配置更新应该成功: {:?}", result.err());

        // 验证文件确实被创建
        assert!(config_path.exists(), "配置文件应该被创建");

        // 验证配置内容正确
        let loaded_config = config_manager.get_comfyui_config().await;
        assert_eq!(loaded_config.base_url, test_config.base_url);
        assert_eq!(loaded_config.timeout_seconds, test_config.timeout_seconds);
        assert_eq!(loaded_config.retry_attempts, test_config.retry_attempts);
    }

    /// 测试配置转换函数
    #[test]
    fn test_config_conversion() {
        use crate::presentation::commands::comfyui_v2_commands::convert_v2_config_to_config;
        use crate::business::services::comfyui_v2_service::ComfyUIV2Config;

        let mut custom_headers = HashMap::new();
        custom_headers.insert("Content-Type".to_string(), "application/json".to_string());

        let v2_config = ComfyUIV2Config {
            base_url: "http://192.168.0.193:8188".to_string(),
            timeout: Some(300000), // 5分钟，毫秒
            retry_attempts: Some(3),
            enable_cache: Some(true),
            max_concurrency: Some(4),
            websocket_url: Some("ws://192.168.0.193:8188/ws".to_string()),
            timeout_seconds: Some(300),
            retry_delay_ms: Some(1000),
            enable_websocket: Some(true),
            custom_headers: Some(custom_headers.clone()),
        };

        let converted = convert_v2_config_to_config(v2_config);

        assert_eq!(converted.base_url, "http://192.168.0.193:8188");
        assert_eq!(converted.timeout_seconds, 300); // 应该转换为秒
        assert_eq!(converted.retry_attempts, 3);
        assert_eq!(converted.retry_delay_ms, 1000);
        assert_eq!(converted.enable_websocket, true);
        assert_eq!(converted.enable_cache, true);
        assert_eq!(converted.max_concurrency, 4);
        assert_eq!(converted.custom_headers, Some(custom_headers));
    }

    /// 测试默认配置处理
    #[test]
    fn test_default_config_handling() {
        use crate::presentation::commands::comfyui_v2_commands::convert_v2_config_to_config;
        use crate::business::services::comfyui_v2_service::ComfyUIV2Config;

        let minimal_config = ComfyUIV2Config {
            base_url: "http://localhost:8188".to_string(),
            timeout: None,
            retry_attempts: None,
            enable_cache: None,
            max_concurrency: None,
            websocket_url: None,
            timeout_seconds: None,
            retry_delay_ms: None,
            enable_websocket: None,
            custom_headers: None,
        };

        let converted = convert_v2_config_to_config(minimal_config);

        assert_eq!(converted.base_url, "http://localhost:8188");
        assert_eq!(converted.timeout_seconds, 300); // 默认5分钟
        assert_eq!(converted.retry_attempts, 3); // 默认3次
        assert_eq!(converted.retry_delay_ms, 1000); // 默认1秒
        assert_eq!(converted.enable_websocket, true); // 默认启用
        assert_eq!(converted.enable_cache, true); // 默认启用
        assert_eq!(converted.max_concurrency, 4); // 默认4个
        assert_eq!(converted.custom_headers, None);
    }
}
