BlueGit

ict rusty-teams

Files

rusty-teams / crates / mcp / src / install / tests.rs

tests.rs

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