aethershell 2.0.3

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Comprehensive MCP (Model Context Protocol) Tests
//! Tests for MCP client, tool discovery, execution, and integration

use aethershell::ai::mcp::{McpClient, McpToolSchema, MCP_VERSION};

// ========== Basic MCP Client Tests ==========

#[test]
fn test_mcp_client_creation() {
    let client = McpClient::new("http://localhost:8080");
    assert_eq!(client.endpoint, "http://localhost:8080");
}

#[test]
fn test_mcp_version_constant() {
    assert_eq!(MCP_VERSION, "1.0");
}

#[test]
fn test_mcp_client_with_trailing_slash() {
    let client = McpClient::new("http://localhost:8080/");
    assert_eq!(client.endpoint, "http://localhost:8080/");
}

// ========== Tool Discovery Tests ==========

#[test]
fn test_mcp_list_tools_empty_when_server_unreachable() {
    let client = McpClient::new("http://localhost:9999"); // Non-existent server
    let tools = client.list_tools();
    assert!(tools.is_ok());
    assert_eq!(tools.unwrap(), Vec::<String>::new());
}

#[test]
fn test_mcp_discover_tools_returns_empty_on_error() {
    let client = McpClient::new("http://localhost:9999");
    let result = client.discover_tools();
    assert!(result.is_ok());
    assert!(result.unwrap().is_empty());
}

#[test]
fn test_mcp_tool_schema_serialization() {
    use serde_json::json;

    let schema = McpToolSchema {
        name: "test_tool".to_string(),
        description: "A test tool".to_string(),
        input_schema: json!({"type": "object"}),
        output_schema: Some(json!({"type": "string"})),
    };

    let serialized = serde_json::to_string(&schema);
    assert!(serialized.is_ok());
}

#[test]
fn test_mcp_tool_schema_deserialization() {
    let json_str = r#"{
        "name": "example",
        "description": "Example tool",
        "input_schema": {"type": "string"},
        "output_schema": {"type": "number"}
    }"#;

    let schema: Result<McpToolSchema, _> = serde_json::from_str(json_str);
    assert!(schema.is_ok());
    let schema = schema.unwrap();
    assert_eq!(schema.name, "example");
    assert_eq!(schema.description, "Example tool");
}

// ========== Tool Execution Tests ==========

#[test]
fn test_mcp_call_tool_with_unreachable_server() {
    let client = McpClient::new("http://localhost:9999");
    let result = client.call_tool("test", "{}");
    // Should return error when server unreachable
    assert!(result.is_err());
}

#[test]
fn test_mcp_call_tool_input_parsing() {
    let client = McpClient::new("http://localhost:9999");
    // Should handle valid JSON input
    let result = client.call_tool("test", r#"{"key": "value"}"#);
    assert!(result.is_err()); // Server unreachable, but input parsed
}

#[test]
fn test_mcp_call_tool_invalid_json_input() {
    let client = McpClient::new("http://localhost:9999");
    // Should handle invalid JSON by wrapping in string
    let result = client.call_tool("test", "not json");
    assert!(result.is_err()); // Server unreachable
}

// ========== Health Check Tests ==========

#[test]
fn test_mcp_health_check_unreachable_server() {
    let client = McpClient::new("http://localhost:9999");
    let is_healthy = client.health_check();
    assert!(!is_healthy);
}

#[test]
fn test_mcp_health_check_invalid_url() {
    let client = McpClient::new("not-a-url");
    let is_healthy = client.health_check();
    assert!(!is_healthy);
}

// ========== Tool Cache Tests ==========

#[test]
fn test_mcp_tool_description_empty_cache() {
    let client = McpClient::new("http://localhost:8080");
    let desc = client.get_tool_description("nonexistent");
    assert!(desc.is_none());
}

#[test]
fn test_mcp_validate_input_without_cache() {
    use serde_json::json;

    let client = McpClient::new("http://localhost:8080");
    let input = json!({"test": "value"});
    let result = client.validate_input("unknown_tool", &input);
    // Should succeed (no validation when tool not cached)
    assert!(result.is_ok());
}

// ========== MCP Tool Resolver Tests ==========

#[test]
fn test_mcp_resolver_creation() {
    use aethershell::ai::mcp::McpToolResolver;

    let _resolver = McpToolResolver::new("http://localhost:8080");
    // Should create successfully
    // Cannot directly test private fields, but creation should work
}

#[test]
fn test_mcp_resolver_list_tools() {
    use aethershell::ai::agents::ToolResolver;
    use aethershell::ai::mcp::McpToolResolver;

    let resolver = McpToolResolver::new("http://localhost:9999");
    let tools = resolver.list();
    // Should return empty list when server unreachable
    assert_eq!(tools.len(), 0);
}

#[test]
fn test_mcp_resolver_get_tool() {
    use aethershell::ai::agents::ToolResolver;
    use aethershell::ai::mcp::McpToolResolver;

    let resolver = McpToolResolver::new("http://localhost:8080");
    let tool = resolver.get("test_tool");
    // Should return Some(tool) even if server unreachable (stub tool)
    assert!(tool.is_some());
}

#[test]
fn test_mcp_tool_name() {
    use aethershell::ai::agents::ToolResolver;
    use aethershell::ai::mcp::McpToolResolver;

    let resolver = McpToolResolver::new("http://localhost:8080");
    if let Some(tool) = resolver.get("example_tool") {
        assert_eq!(tool.name(), "example_tool");
    }
}

#[test]
fn test_mcp_tool_description_default() {
    use aethershell::ai::agents::ToolResolver;
    use aethershell::ai::mcp::McpToolResolver;

    let resolver = McpToolResolver::new("http://localhost:8080");
    if let Some(tool) = resolver.get("test") {
        let desc = tool.description();
        // Should return default description when not cached
        assert!(desc.contains("MCP") || !desc.is_empty());
    }
}

#[test]
fn test_mcp_tool_call_error_handling() {
    use aethershell::ai::agents::ToolResolver;
    use aethershell::ai::mcp::McpToolResolver;
    use aethershell::env::Env;

    let resolver = McpToolResolver::new("http://localhost:9999");
    if let Some(tool) = resolver.get("test") {
        let mut env = Env::default();
        let result = tool.call("{}", &mut env);
        // Should return error when server unreachable
        assert!(result.is_err());
    }
}

// ========== Integration Tests ==========

#[test]
fn test_mcp_integration_with_tool_registry() {
    use aethershell::ai::agents::ToolRegistry;

    let registry = ToolRegistry::with_builtins_and_mcp("http://localhost:8080");
    let tools = registry.list();

    // Should include builtins at minimum
    assert!(!tools.is_empty());
    assert!(tools.contains(&"print".to_string()));
}

#[test]
fn test_mcp_tool_resolution_with_registry() {
    use aethershell::ai::agents::ToolRegistry;

    let registry = ToolRegistry::with_builtins_and_mcp("http://localhost:8080");
    let resolved = registry.resolve_many(&["print", "mcp_tool"]);

    // Should resolve at least builtins
    assert!(!resolved.is_empty());
}

#[test]
fn test_mcp_multiple_endpoints() {
    let client1 = McpClient::new("http://server1:8080");
    let client2 = McpClient::new("http://server2:8080");

    assert_ne!(client1.endpoint, client2.endpoint);
}

#[test]
fn test_mcp_endpoint_normalization() {
    let client = McpClient::new("http://localhost:8080/");
    let tools_url = format!("{}/mcp/v1/tools", client.endpoint.trim_end_matches('/'));
    assert_eq!(tools_url, "http://localhost:8080/mcp/v1/tools");
}

// ========== Error Handling Tests ==========

#[test]
fn test_mcp_malformed_response_handling() {
    let client = McpClient::new("http://localhost:9999");
    let result = client.discover_tools();
    // Should handle errors gracefully
    assert!(result.is_ok());
}

#[test]
fn test_mcp_concurrent_access() {
    use std::sync::Arc;
    use std::thread;

    let client = Arc::new(McpClient::new("http://localhost:8080"));
    let mut handles = vec![];

    for _ in 0..5 {
        let client = Arc::clone(&client);
        let handle = thread::spawn(move || {
            let _ = client.list_tools();
        });
        handles.push(handle);
    }

    for handle in handles {
        assert!(handle.join().is_ok());
    }
}

#[test]
fn test_mcp_cache_thread_safety() {
    use std::sync::Arc;
    use std::thread;

    let client = Arc::new(McpClient::new("http://localhost:8080"));
    let mut handles = vec![];

    for _ in 0..3 {
        let client = Arc::clone(&client);
        let handle = thread::spawn(move || {
            let _ = client.get_tool_description("test");
        });
        handles.push(handle);
    }

    for handle in handles {
        assert!(handle.join().is_ok());
    }
}

// ========== URL Handling Tests ==========

#[test]
fn test_mcp_various_endpoint_formats() {
    let endpoints = vec![
        "http://localhost:8080",
        "http://localhost:8080/",
        "https://mcp.example.com",
        "https://mcp.example.com/api",
        "http://192.168.1.100:3000",
    ];

    for endpoint in endpoints {
        let client = McpClient::new(endpoint);
        assert!(!client.endpoint.is_empty());
    }
}

#[test]
fn test_mcp_tool_execution_url_construction() {
    let client = McpClient::new("http://localhost:8080");
    // The URL construction is internal, but we can test that the method exists
    let _ = client.call_tool("test", "{}");
}

// ========== Performance Tests ==========

#[test]
fn test_mcp_client_creation_performance() {
    // This deliberately asserts no wall-clock bound. It used to require 100
    // constructions in under 5s, which passed in 2.4s alone and took 11.4s
    // under the full parallel suite — the threshold was measuring machine load,
    // not the code, so it failed on exactly the busy CI runs where a real
    // regression matters least. Any replacement threshold has the same defect.
    //
    // What is worth pinning is that construction is repeatable and each client
    // keeps its own endpoint: `McpClient::new` falls back to a default reqwest
    // client if the secure one cannot be built, and that fallback must not be
    // shared or cached across instances.
    for i in 0..100 {
        let endpoint = format!("http://localhost:{}", 8080 + i);
        let client = McpClient::new(&endpoint);
        assert_eq!(client.endpoint, endpoint);
    }
}

#[test]
fn test_mcp_cache_access_performance() {
    let client = McpClient::new("http://localhost:8080");

    // No wall-clock bound here either — a 100ms budget for 1000 lookups is the
    // most load-sensitive assertion in the suite, and it says nothing about
    // correctness. See `test_mcp_client_creation_performance` above.
    //
    // The property worth holding is that a cache miss stays a miss and does not
    // reach the network: the endpoint is unreachable, so a lookup that returned
    // `Some` — or blocked — would mean the cache had been bypassed.
    for _ in 0..1000 {
        assert!(
            client.get_tool_description("test").is_none(),
            "an empty cache must miss rather than fall through to the server"
        );
    }
}

// ========== Edge Cases ==========

#[test]
fn test_mcp_empty_endpoint() {
    let client = McpClient::new("");
    assert_eq!(client.endpoint, "");
}

#[test]
fn test_mcp_long_endpoint() {
    let long_endpoint = format!("http://localhost:8080/{}", "a".repeat(1000));
    let client = McpClient::new(&long_endpoint);
    assert_eq!(client.endpoint.len(), long_endpoint.len());
}

#[test]
fn test_mcp_tool_name_with_special_characters() {
    use aethershell::ai::agents::ToolResolver;
    use aethershell::ai::mcp::McpToolResolver;

    let resolver = McpToolResolver::new("http://localhost:8080");
    let special_names = vec!["tool-with-dash", "tool_with_underscore", "tool.with.dot"];

    for name in special_names {
        if let Some(tool) = resolver.get(name) {
            assert_eq!(tool.name(), name);
        }
    }
}

#[test]
fn test_mcp_unicode_tool_names() {
    use aethershell::ai::agents::ToolResolver;
    use aethershell::ai::mcp::McpToolResolver;

    let resolver = McpToolResolver::new("http://localhost:8080");
    let unicode_tool = resolver.get("测试工具");
    assert!(unicode_tool.is_some());
}