|
1 | | -use super::helpers::run_mcp_commands; |
| 1 | +use super::helpers::{add_server_config, add_server_config_with_path, run_mcp_commands}; |
2 | 2 | use crate::core::app::commands::get_jan_data_folder_path; |
3 | 3 | use crate::core::state::SharedMcpServers; |
4 | 4 | use std::collections::HashMap; |
@@ -37,3 +37,140 @@ async fn test_run_mcp_commands() { |
37 | 37 | // Clean up the mock config file |
38 | 38 | std::fs::remove_file(&config_path).expect("Failed to remove config file"); |
39 | 39 | } |
| 40 | + |
| 41 | +#[test] |
| 42 | +fn test_add_server_config_new_file() { |
| 43 | + let app = mock_app(); |
| 44 | + let app_path = get_jan_data_folder_path(app.handle().clone()); |
| 45 | + let config_path = app_path.join("mcp_config_test_new.json"); |
| 46 | + |
| 47 | + // Ensure the directory exists |
| 48 | + if let Some(parent) = config_path.parent() { |
| 49 | + std::fs::create_dir_all(parent).expect("Failed to create parent directory"); |
| 50 | + } |
| 51 | + |
| 52 | + // Create initial config file with empty mcpServers |
| 53 | + let mut file = File::create(&config_path).expect("Failed to create config file"); |
| 54 | + file.write_all(b"{\"mcpServers\":{}}") |
| 55 | + .expect("Failed to write to config file"); |
| 56 | + drop(file); |
| 57 | + |
| 58 | + // Test adding a new server config |
| 59 | + let server_value = serde_json::json!({ |
| 60 | + "command": "npx", |
| 61 | + "args": ["-y", "test-server"], |
| 62 | + "env": { "TEST_API_KEY": "test_key" }, |
| 63 | + "active": false |
| 64 | + }); |
| 65 | + |
| 66 | + let result = add_server_config_with_path( |
| 67 | + app.handle().clone(), |
| 68 | + "test_server".to_string(), |
| 69 | + server_value.clone(), |
| 70 | + Some("mcp_config_test_new.json"), |
| 71 | + ); |
| 72 | + |
| 73 | + assert!(result.is_ok(), "Failed to add server config: {:?}", result); |
| 74 | + |
| 75 | + // Verify the config was added correctly |
| 76 | + let config_content = std::fs::read_to_string(&config_path) |
| 77 | + .expect("Failed to read config file"); |
| 78 | + let config: serde_json::Value = serde_json::from_str(&config_content) |
| 79 | + .expect("Failed to parse config"); |
| 80 | + |
| 81 | + assert!(config["mcpServers"]["test_server"].is_object()); |
| 82 | + assert_eq!(config["mcpServers"]["test_server"]["command"], "npx"); |
| 83 | + assert_eq!(config["mcpServers"]["test_server"]["args"][0], "-y"); |
| 84 | + assert_eq!(config["mcpServers"]["test_server"]["args"][1], "test-server"); |
| 85 | + |
| 86 | + // Clean up |
| 87 | + std::fs::remove_file(&config_path).expect("Failed to remove config file"); |
| 88 | +} |
| 89 | + |
| 90 | +#[test] |
| 91 | +fn test_add_server_config_existing_servers() { |
| 92 | + let app = mock_app(); |
| 93 | + let app_path = get_jan_data_folder_path(app.handle().clone()); |
| 94 | + let config_path = app_path.join("mcp_config_test_existing.json"); |
| 95 | + |
| 96 | + // Ensure the directory exists |
| 97 | + if let Some(parent) = config_path.parent() { |
| 98 | + std::fs::create_dir_all(parent).expect("Failed to create parent directory"); |
| 99 | + } |
| 100 | + |
| 101 | + // Create config file with existing server |
| 102 | + let initial_config = serde_json::json!({ |
| 103 | + "mcpServers": { |
| 104 | + "existing_server": { |
| 105 | + "command": "existing_command", |
| 106 | + "args": ["arg1"], |
| 107 | + "active": true |
| 108 | + } |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + let mut file = File::create(&config_path).expect("Failed to create config file"); |
| 113 | + file.write_all(serde_json::to_string_pretty(&initial_config).unwrap().as_bytes()) |
| 114 | + .expect("Failed to write to config file"); |
| 115 | + drop(file); |
| 116 | + |
| 117 | + // Add new server |
| 118 | + let new_server_value = serde_json::json!({ |
| 119 | + "command": "new_command", |
| 120 | + "args": ["new_arg"], |
| 121 | + "active": false |
| 122 | + }); |
| 123 | + |
| 124 | + let result = add_server_config_with_path( |
| 125 | + app.handle().clone(), |
| 126 | + "new_server".to_string(), |
| 127 | + new_server_value, |
| 128 | + Some("mcp_config_test_existing.json"), |
| 129 | + ); |
| 130 | + |
| 131 | + assert!(result.is_ok(), "Failed to add server config: {:?}", result); |
| 132 | + |
| 133 | + // Verify both servers exist |
| 134 | + let config_content = std::fs::read_to_string(&config_path) |
| 135 | + .expect("Failed to read config file"); |
| 136 | + let config: serde_json::Value = serde_json::from_str(&config_content) |
| 137 | + .expect("Failed to parse config"); |
| 138 | + |
| 139 | + // Check existing server is still there |
| 140 | + assert!(config["mcpServers"]["existing_server"].is_object()); |
| 141 | + assert_eq!(config["mcpServers"]["existing_server"]["command"], "existing_command"); |
| 142 | + |
| 143 | + // Check new server was added |
| 144 | + assert!(config["mcpServers"]["new_server"].is_object()); |
| 145 | + assert_eq!(config["mcpServers"]["new_server"]["command"], "new_command"); |
| 146 | + |
| 147 | + // Clean up |
| 148 | + std::fs::remove_file(&config_path).expect("Failed to remove config file"); |
| 149 | +} |
| 150 | + |
| 151 | +#[test] |
| 152 | +fn test_add_server_config_missing_config_file() { |
| 153 | + let app = mock_app(); |
| 154 | + let app_path = get_jan_data_folder_path(app.handle().clone()); |
| 155 | + let config_path = app_path.join("nonexistent_config.json"); |
| 156 | + |
| 157 | + // Ensure the file doesn't exist |
| 158 | + if config_path.exists() { |
| 159 | + std::fs::remove_file(&config_path).ok(); |
| 160 | + } |
| 161 | + |
| 162 | + let server_value = serde_json::json!({ |
| 163 | + "command": "test", |
| 164 | + "args": [], |
| 165 | + "active": false |
| 166 | + }); |
| 167 | + |
| 168 | + let result = add_server_config( |
| 169 | + app.handle().clone(), |
| 170 | + "test".to_string(), |
| 171 | + server_value, |
| 172 | + ); |
| 173 | + |
| 174 | + assert!(result.is_err(), "Expected error when config file doesn't exist"); |
| 175 | + assert!(result.unwrap_err().contains("Failed to read config file")); |
| 176 | +} |
0 commit comments