| 1 | use std::fs; |
| 2 | use std::path::{Path, PathBuf}; |
| 3 | |
| 4 | use percent_encoding::percent_decode_str; |
| 5 | use serde_json::Value; |
| 6 | |
| 7 | use super::*; |
| 8 | |
| 9 | fn fixture(name: &str) -> String { |
| 10 | let path = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 11 | .join("tests/fixtures/install") |
| 12 | .join(name); |
| 13 | fs::read_to_string(path).unwrap() |
| 14 | } |
| 15 | |
| 16 | fn endpoint(token: &str) -> McpEndpoint { |
| 17 | McpEndpoint { |
| 18 | url: "http://127.0.0.1:8765/mcp".to_string(), |
| 19 | token: token.to_string(), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | fn homes_in(directory: &Path) -> Homes { |
| 24 | Homes { |
| 25 | entries: vec![(Location::Native, directory.to_path_buf())], |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn config(target: Target, directory: &Path) -> PathBuf { |
| 30 | target.config_path(&Location::Native, directory) |
| 31 | } |
| 32 | |
| 33 | fn seed(target: Target, directory: &Path, contents: &str) -> PathBuf { |
| 34 | let path = config(target, directory); |
| 35 | fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 36 | fs::write(&path, contents).unwrap(); |
| 37 | path |
| 38 | } |
| 39 | |
| 40 | /// Marks the agent as installed in the fake home: most targets with their config |
| 41 | /// directory, Claude Code with ~/.claude. Without one of these install() falls |
| 42 | /// back to the agent's binary on the host's PATH, and the test would depend on |
| 43 | /// what happens to be installed on the machine running it. |
| 44 | fn make_available(target: Target, home: &Path) { |
| 45 | fs::create_dir_all(config(target, home).parent().unwrap()).unwrap(); |
| 46 | fs::create_dir_all(home.join(".claude")).unwrap(); |
| 47 | } |
| 48 | |
| 49 | fn backup_of(path: &Path) -> PathBuf { |
| 50 | let mut name = path.file_name().unwrap().to_os_string(); |
| 51 | name.push(".bak-rusty-teams"); |
| 52 | path.with_file_name(name) |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn install_into_missing_file_creates_it() { |
| 57 | for target in [ |
| 58 | Target::ClaudeCode, |
| 59 | Target::Codex, |
| 60 | Target::CopilotCli, |
| 61 | Target::Cursor, |
| 62 | ] { |
| 63 | let directory = tempfile::tempdir().unwrap(); |
| 64 | let home = directory.path(); |
| 65 | make_available(target, home); |
| 66 | let homes = homes_in(home); |
| 67 | let report = install(target, &endpoint("one"), &homes).unwrap(); |
| 68 | assert_eq!(report, InstallReport::Written(vec![Location::Native])); |
| 69 | let state = status(target, &endpoint("one"), &homes); |
| 70 | assert_eq!(state.installed_at, vec![Location::Native]); |
| 71 | assert!(!state.stale); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn install_without_agent_is_not_available() { |
| 77 | let directory = tempfile::tempdir().unwrap(); |
| 78 | let homes = Homes { |
| 79 | entries: vec![( |
| 80 | Location::Wsl { |
| 81 | distro: "Ubuntu".to_string(), |
| 82 | }, |
| 83 | directory.path().to_path_buf(), |
| 84 | )], |
| 85 | }; |
| 86 | let result = install(Target::Cursor, &endpoint("one"), &homes); |
| 87 | assert!(matches!( |
| 88 | result, |
| 89 | Err(InstallError::NotAvailable(Target::Cursor)) |
| 90 | )); |
| 91 | assert!(!status(Target::Cursor, &endpoint("one"), &homes).available); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn install_json_keeps_unrelated_keys_and_order() { |
| 96 | let directory = tempfile::tempdir().unwrap(); |
| 97 | let path = seed( |
| 98 | Target::ClaudeCode, |
| 99 | directory.path(), |
| 100 | &fixture("claude_with_keys.json"), |
| 101 | ); |
| 102 | install( |
| 103 | Target::ClaudeCode, |
| 104 | &endpoint("one"), |
| 105 | &homes_in(directory.path()), |
| 106 | ) |
| 107 | .unwrap(); |
| 108 | let text = fs::read_to_string(&path).unwrap(); |
| 109 | let value: Value = serde_json::from_str(&text).unwrap(); |
| 110 | assert_eq!(value["numStartups"], 12); |
| 111 | assert_eq!(value["mcpServers"]["other"]["command"], "npx"); |
| 112 | assert_eq!(value["mcpServers"]["teams"]["type"], "http"); |
| 113 | assert_eq!( |
| 114 | value["mcpServers"]["teams"]["headers"]["Authorization"], |
| 115 | "Bearer one" |
| 116 | ); |
| 117 | let num_startups = text.find("numStartups").unwrap(); |
| 118 | let theme = text.find("\"theme\"").unwrap(); |
| 119 | let servers = text.find("mcpServers").unwrap(); |
| 120 | let projects = text.find("projects").unwrap(); |
| 121 | assert!(num_startups < theme && theme < servers && servers < projects); |
| 122 | assert!(text.find("\"other\"").unwrap() < text.find("\"teams\"").unwrap()); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn install_toml_keeps_comments() { |
| 127 | let directory = tempfile::tempdir().unwrap(); |
| 128 | let path = seed( |
| 129 | Target::Codex, |
| 130 | directory.path(), |
| 131 | &fixture("codex_with_comments.toml"), |
| 132 | ); |
| 133 | install(Target::Codex, &endpoint("one"), &homes_in(directory.path())).unwrap(); |
| 134 | let text = fs::read_to_string(&path).unwrap(); |
| 135 | assert!(text.contains("# my codex settings")); |
| 136 | assert!(text.contains("model = \"gpt-5\" # default model")); |
| 137 | assert!(text.contains("[mcp_servers.teams]")); |
| 138 | assert!(text.contains("tool_timeout_sec = 300")); |
| 139 | assert!(text.contains("http_headers = { Authorization = \"Bearer one\" }")); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn install_twice_changes_nothing() { |
| 144 | for target in [Target::ClaudeCode, Target::Codex, Target::CopilotCli] { |
| 145 | let directory = tempfile::tempdir().unwrap(); |
| 146 | let fixture_name = match target { |
| 147 | Target::Codex => "codex_with_comments.toml", |
| 148 | _ => "claude_with_keys.json", |
| 149 | }; |
| 150 | let path = seed(target, directory.path(), &fixture(fixture_name)); |
| 151 | let homes = homes_in(directory.path()); |
| 152 | install(target, &endpoint("one"), &homes).unwrap(); |
| 153 | let first = fs::read_to_string(&path).unwrap(); |
| 154 | install(target, &endpoint("one"), &homes).unwrap(); |
| 155 | assert_eq!(first, fs::read_to_string(&path).unwrap()); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn token_change_is_stale_until_reinstall() { |
| 161 | for target in [Target::ClaudeCode, Target::Codex, Target::Cursor] { |
| 162 | let directory = tempfile::tempdir().unwrap(); |
| 163 | let homes = homes_in(directory.path()); |
| 164 | make_available(target, directory.path()); |
| 165 | install(target, &endpoint("one"), &homes).unwrap(); |
| 166 | assert!(status(target, &endpoint("two"), &homes).stale); |
| 167 | let results = reinstall_all(&endpoint("two"), &homes); |
| 168 | assert!(results.iter().all(|(_, result)| result.is_ok())); |
| 169 | let state = status(target, &endpoint("two"), &homes); |
| 170 | assert!(!state.stale); |
| 171 | assert_eq!(state.installed_at, vec![Location::Native]); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn uninstall_removes_only_teams() { |
| 177 | let directory = tempfile::tempdir().unwrap(); |
| 178 | let homes = homes_in(directory.path()); |
| 179 | let json_path = seed( |
| 180 | Target::ClaudeCode, |
| 181 | directory.path(), |
| 182 | &fixture("claude_with_keys.json"), |
| 183 | ); |
| 184 | let toml_path = seed( |
| 185 | Target::Codex, |
| 186 | directory.path(), |
| 187 | &fixture("codex_with_comments.toml"), |
| 188 | ); |
| 189 | for target in [Target::ClaudeCode, Target::Codex] { |
| 190 | install(target, &endpoint("one"), &homes).unwrap(); |
| 191 | uninstall(target, &homes).unwrap(); |
| 192 | assert!( |
| 193 | status(target, &endpoint("one"), &homes) |
| 194 | .installed_at |
| 195 | .is_empty() |
| 196 | ); |
| 197 | } |
| 198 | let value: Value = serde_json::from_str(&fs::read_to_string(&json_path).unwrap()).unwrap(); |
| 199 | assert_eq!(value["mcpServers"]["other"]["command"], "npx"); |
| 200 | assert!(value["mcpServers"].get("teams").is_none()); |
| 201 | let toml_text = fs::read_to_string(&toml_path).unwrap(); |
| 202 | assert!(toml_text.contains("[mcp_servers.other]")); |
| 203 | assert!(toml_text.contains("# my codex settings")); |
| 204 | assert!(!toml_text.contains("teams")); |
| 205 | } |
| 206 | |
| 207 | #[test] |
| 208 | fn malformed_file_is_refused_and_untouched() { |
| 209 | for (target, name) in [ |
| 210 | (Target::ClaudeCode, "malformed.json"), |
| 211 | (Target::Codex, "malformed.toml"), |
| 212 | ] { |
| 213 | let directory = tempfile::tempdir().unwrap(); |
| 214 | let original = fixture(name); |
| 215 | let path = seed(target, directory.path(), &original); |
| 216 | let homes = homes_in(directory.path()); |
| 217 | let result = install(target, &endpoint("one"), &homes); |
| 218 | assert!(matches!(result, Err(InstallError::Malformed { .. }))); |
| 219 | assert!(matches!( |
| 220 | uninstall(target, &homes), |
| 221 | Err(InstallError::Malformed { .. }) |
| 222 | )); |
| 223 | assert_eq!(fs::read_to_string(&path).unwrap(), original); |
| 224 | assert!(!backup_of(&path).exists()); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn backup_is_created_once_with_original_content() { |
| 230 | let directory = tempfile::tempdir().unwrap(); |
| 231 | let original = fixture("claude_with_keys.json"); |
| 232 | let path = seed(Target::ClaudeCode, directory.path(), &original); |
| 233 | let homes = homes_in(directory.path()); |
| 234 | install(Target::ClaudeCode, &endpoint("one"), &homes).unwrap(); |
| 235 | install(Target::ClaudeCode, &endpoint("two"), &homes).unwrap(); |
| 236 | assert_eq!(fs::read_to_string(backup_of(&path)).unwrap(), original); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn vscode_install_returns_deeplink_that_round_trips() { |
| 241 | let directory = tempfile::tempdir().unwrap(); |
| 242 | let report = install( |
| 243 | Target::VsCode, |
| 244 | &endpoint("tok en&1"), |
| 245 | &homes_in(directory.path()), |
| 246 | ) |
| 247 | .unwrap(); |
| 248 | let InstallReport::OpenUrl(url) = report else { |
| 249 | panic!("expected a deeplink"); |
| 250 | }; |
| 251 | let encoded = url.strip_prefix("vscode:mcp/install?").unwrap(); |
| 252 | assert!(!encoded.contains(['{', '"', ' ', '&'])); |
| 253 | let decoded = percent_decode_str(encoded).decode_utf8().unwrap(); |
| 254 | let value: Value = serde_json::from_str(&decoded).unwrap(); |
| 255 | assert_eq!(value["name"], "teams"); |
| 256 | assert_eq!(value["type"], "http"); |
| 257 | assert_eq!(value["url"], "http://127.0.0.1:8765/mcp"); |
| 258 | assert_eq!(value["headers"]["Authorization"], "Bearer tok en&1"); |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn vscode_status_reads_servers_key() { |
| 263 | let directory = tempfile::tempdir().unwrap(); |
| 264 | seed( |
| 265 | Target::VsCode, |
| 266 | directory.path(), |
| 267 | r#"{"servers":{"teams":{"type":"http","url":"http://127.0.0.1:8765/mcp","headers":{"Authorization":"Bearer one"}}}}"#, |
| 268 | ); |
| 269 | let state = status( |
| 270 | Target::VsCode, |
| 271 | &endpoint("one"), |
| 272 | &homes_in(directory.path()), |
| 273 | ); |
| 274 | assert!(state.available); |
| 275 | assert_eq!(state.installed_at, vec![Location::Native]); |
| 276 | assert!(!state.stale); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn wsl_output_decodes_utf16_with_bom() { |
| 281 | let text = "Ubuntu\r\ndocker-desktop\r\n"; |
| 282 | let mut bytes = vec![0xff, 0xfe]; |
| 283 | bytes.extend(text.encode_utf16().flat_map(u16::to_le_bytes)); |
| 284 | assert_eq!(wsl::decode_output(&bytes), text); |
| 285 | let without_bom: Vec<u8> = text.encode_utf16().flat_map(u16::to_le_bytes).collect(); |
| 286 | assert_eq!(wsl::decode_output(&without_bom), text); |
| 287 | assert_eq!(wsl::decode_output(b"plain\n"), "plain\n"); |
| 288 | } |
| 289 | |
| 290 | #[test] |
| 291 | fn wsl_default_distro_skips_docker_and_markers() { |
| 292 | assert_eq!( |
| 293 | wsl::default_distro("docker-desktop\r\nUbuntu-24.04 (Default)\r\nDebian\r\n"), |
| 294 | Some("Ubuntu-24.04".to_string()) |
| 295 | ); |
| 296 | assert_eq!( |
| 297 | wsl::default_distro("Ubuntu\r\nDebian\r\n"), |
| 298 | Some("Ubuntu".to_string()) |
| 299 | ); |
| 300 | assert_eq!(wsl::default_distro("\r\n"), None); |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn wsl_unc_home_is_built_from_distro_and_linux_home() { |
| 305 | assert_eq!( |
| 306 | wsl::unc_home("Ubuntu", "/home/daniel\n").to_string_lossy(), |
| 307 | "\\\\wsl.localhost\\Ubuntu\\home\\daniel" |
| 308 | ); |
| 309 | } |
| 310 | |
| 311 | #[test] |
| 312 | fn wslconfig_mirrored_mode_is_detected() { |
| 313 | assert!(wsl::wslconfig_is_mirrored( |
| 314 | "[wsl2]\nmemory=8GB\nNetworkingMode = Mirrored\n" |
| 315 | )); |
| 316 | assert!(!wsl::wslconfig_is_mirrored("[wsl2]\nnetworkingMode=nat\n")); |
| 317 | assert!(!wsl::wslconfig_is_mirrored( |
| 318 | "[wsl2]\n# networkingMode=mirrored\n" |
| 319 | )); |
| 320 | } |