aethershell 0.3.1

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
Documentation
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Test suite for MCP tool use functionality
//!
//! Tests the Model Context Protocol implementation including:
//! - Tool listing and filtering
//! - Tool execution via call_tool
//! - Resources and prompts
//! - Category and safety level filtering

use aethershell::mcp::{McpConfig, McpServer, McpToolCall};
use aethershell::os_tools::{OSToolsDatabase, SafetyLevel, ToolCategory};
use std::collections::HashMap;

// ============================================================================
// Server Creation Tests
// ============================================================================

#[test]
fn test_mcp_server_default_creation() {
    let server = McpServer::new();
    let info = server.initialize();

    assert_eq!(info.server_info.name, "aethershell-mcp");
    assert_eq!(info.protocol_version, "2024-11-05");
    assert!(server.tool_count() > 0, "Server should have tools");
}

#[test]
fn test_mcp_server_with_config() {
    let config = McpConfig {
        max_safety_level: SafetyLevel::Safe,
        allow_admin_tools: false,
        allowed_categories: None,
        blocked_tools: vec!["nmap".to_string()],
        execution_timeout: 30,
    };
    let server = McpServer::with_config(config);
    let tools = server.list_tools();

    // Should not contain blocked tools
    assert!(!tools.iter().any(|t| t.name == "nmap"));
}

#[test]
fn test_mcp_server_default_config() {
    let server = McpServer::default();
    let tools = server.list_tools();

    // Default config blocks dangerous tools
    assert!(!tools.iter().any(|t| t.name == "rm"));
    assert!(!tools.iter().any(|t| t.name == "dd"));
}

// ============================================================================
// Tool Listing Tests
// ============================================================================

#[test]
fn test_list_tools_returns_tools() {
    let server = McpServer::new();
    let tools = server.list_tools();

    assert!(!tools.is_empty(), "Tool list should not be empty");
    assert!(tools.len() > 50, "Should have substantial number of tools");
}

#[test]
fn test_list_tools_have_required_fields() {
    let server = McpServer::new();
    let tools = server.list_tools();

    for tool in tools.iter().take(10) {
        assert!(!tool.name.is_empty(), "Tool name should not be empty");
        assert!(
            !tool.description.is_empty(),
            "Tool description should not be empty"
        );
        assert!(
            tool.input_schema.is_object(),
            "Tool input_schema should be a JSON object"
        );
    }
}

#[test]
fn test_get_tool_by_name() {
    let server = McpServer::new();

    // Test getting a known tool
    let git_tool = server.get_tool("git");
    assert!(git_tool.is_some(), "Should find 'git' tool");

    let git = git_tool.unwrap();
    assert_eq!(git.name, "git");
}

#[test]
fn test_get_nonexistent_tool() {
    let server = McpServer::new();

    let tool = server.get_tool("nonexistent_tool_xyz");
    assert!(tool.is_none(), "Should not find nonexistent tool");
}

// ============================================================================
// Tool Filtering Tests
// ============================================================================

#[test]
fn test_filter_tools_by_category() {
    let server = McpServer::new();

    let dev_tools = server.get_tools_by_category(&ToolCategory::Development);
    assert!(!dev_tools.is_empty(), "Should have development tools");

    // Known dev tools should be present
    let tool_names: Vec<&str> = dev_tools.iter().map(|t| t.name.as_str()).collect();
    assert!(
        tool_names.contains(&"cargo") || tool_names.contains(&"npm") || tool_names.contains(&"git"),
        "Development category should contain common dev tools"
    );
}

#[test]
fn test_filter_tools_by_multiple_categories() {
    let server = McpServer::new();

    let fs_tools = server.get_tools_by_category(&ToolCategory::FileSystem);
    let net_tools = server.get_tools_by_category(&ToolCategory::NetworkTools);
    let dev_tools = server.get_tools_by_category(&ToolCategory::Development);

    // Each category should have tools
    assert!(!fs_tools.is_empty(), "Should have filesystem tools");
    assert!(!net_tools.is_empty(), "Should have network tools");
    assert!(!dev_tools.is_empty(), "Should have development tools");

    // Categories should be different
    let total = fs_tools.len() + net_tools.len() + dev_tools.len();
    let all_tools = server.list_tools();
    assert!(
        total <= all_tools.len(),
        "Category totals should not exceed total tools"
    );
}

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

#[test]
fn test_call_tool_basic() {
    let server = McpServer::new();

    // Call a simple tool that should work on all systems
    let call = McpToolCall {
        name: "git".to_string(),
        arguments: {
            let mut args = HashMap::new();
            args.insert(
                "command".to_string(),
                serde_json::Value::String("--version".to_string()),
            );
            args
        },
    };
    let result = server.call_tool(call);

    // Should return a result
    assert!(!result.content.is_empty(), "Result should have content");
}

#[test]
fn test_call_tool_returns_content() {
    let server = McpServer::new();

    let call = McpToolCall {
        name: "git".to_string(),
        arguments: {
            let mut args = HashMap::new();
            args.insert(
                "command".to_string(),
                serde_json::Value::String("--version".to_string()),
            );
            args
        },
    };
    let result = server.call_tool(call);

    // Result should have content
    assert!(!result.content.is_empty(), "Should have content");
}

#[test]
fn test_call_nonexistent_tool() {
    let server = McpServer::new();

    let call = McpToolCall {
        name: "nonexistent_tool_xyz".to_string(),
        arguments: HashMap::new(),
    };
    let result = server.call_tool(call);

    // Should return error
    assert!(
        result.is_error.unwrap_or(false),
        "Calling nonexistent tool should return error"
    );
}

#[test]
fn test_call_blocked_tool() {
    let server = McpServer::new();

    // rm is blocked by default
    let call = McpToolCall {
        name: "rm".to_string(),
        arguments: HashMap::new(),
    };
    let result = server.call_tool(call);

    // Should return error (blocked by security policy)
    assert!(
        result.is_error.unwrap_or(false),
        "Calling blocked tool should return error"
    );
}

// ============================================================================
// Resources Tests
// ============================================================================

#[test]
fn test_list_resources() {
    let server = McpServer::new();
    let resources = server.list_resources();

    assert!(!resources.is_empty(), "Should have resources");
}

#[test]
fn test_resources_have_required_fields() {
    let server = McpServer::new();
    let resources = server.list_resources();

    for resource in &resources {
        assert!(!resource.uri.is_empty(), "Resource URI should not be empty");
        assert!(
            !resource.name.is_empty(),
            "Resource name should not be empty"
        );
    }
}

#[test]
fn test_read_resource_tools() {
    let server = McpServer::new();

    let content = server.read_resource("aethershell://tools");
    assert!(content.is_ok(), "Should read tools resource");
}

#[test]
fn test_read_resource_categories() {
    let server = McpServer::new();

    let content = server.read_resource("aethershell://categories");
    assert!(content.is_ok(), "Should read categories resource");
}

#[test]
fn test_read_resource_system_info() {
    let server = McpServer::new();

    let content = server.read_resource("aethershell://system-info");
    assert!(content.is_ok(), "Should read system-info resource");
}

#[test]
fn test_read_nonexistent_resource() {
    let server = McpServer::new();

    let content = server.read_resource("aethershell://nonexistent");
    assert!(content.is_err(), "Should not find nonexistent resource");
}

// ============================================================================
// Prompts Tests
// ============================================================================

#[test]
fn test_list_prompts() {
    let server = McpServer::new();
    let prompts = server.list_prompts();

    assert!(!prompts.is_empty(), "Should have prompts");
}

#[test]
fn test_prompts_have_required_fields() {
    let server = McpServer::new();
    let prompts = server.list_prompts();

    for prompt in &prompts {
        assert!(!prompt.name.is_empty(), "Prompt name should not be empty");
    }
}

#[test]
fn test_get_prompt_find_tool() {
    let server = McpServer::new();

    let mut args = HashMap::new();
    args.insert("task".to_string(), "compress files".to_string());

    let prompt = server.get_prompt("find-tool", &args);
    assert!(prompt.is_ok(), "Should get find-tool prompt");
}

#[test]
fn test_get_prompt_explain_tool() {
    let server = McpServer::new();

    let mut args = HashMap::new();
    args.insert("tool_name".to_string(), "git".to_string());

    let prompt = server.get_prompt("explain-tool", &args);
    assert!(prompt.is_ok(), "Should get explain-tool prompt");
}

#[test]
fn test_get_prompt_missing_args() {
    let server = McpServer::new();

    let prompt = server.get_prompt("find-tool", &HashMap::new());
    assert!(prompt.is_err(), "Should fail without required args");
}

// ============================================================================
// OS Tools Integration Tests
// ============================================================================

#[test]
fn test_os_tools_integration() {
    let os_tools = OSToolsDatabase::new();

    // Should have tools
    assert!(
        !os_tools.tools.is_empty(),
        "OSToolsDatabase should have tools"
    );

    // Should have categories
    assert!(!os_tools.categories.is_empty(), "Should have categories");
}

#[test]
fn test_os_tools_get_by_name() {
    let os_tools = OSToolsDatabase::new();

    let git = os_tools.get_tool("git");
    assert!(git.is_some(), "Should find git tool");
}

#[test]
fn test_os_tools_search() {
    let os_tools = OSToolsDatabase::new();

    let results = os_tools.search_tools("kubernetes");
    assert!(!results.is_empty(), "Should find kubernetes-related tools");
}

#[test]
fn test_os_tools_by_category() {
    let os_tools = OSToolsDatabase::new();

    let dev_tools = os_tools.get_tools_by_category(&ToolCategory::Development);
    assert!(!dev_tools.is_empty(), "Should have development tools");
}

// ============================================================================
// Safety Level Tests
// ============================================================================

#[test]
fn test_safety_levels_exist() {
    // Test that safety levels are distinct
    let safe = SafetyLevel::Safe;
    let caution = SafetyLevel::Caution;
    let dangerous = SafetyLevel::Dangerous;
    let critical = SafetyLevel::Critical;

    assert!(safe != caution);
    assert!(caution != dangerous);
    assert!(dangerous != critical);
}

#[test]
fn test_safe_config_restricts_tools() {
    let safe_config = McpConfig {
        max_safety_level: SafetyLevel::Safe,
        allow_admin_tools: false,
        allowed_categories: None,
        blocked_tools: vec![],
        execution_timeout: 30,
    };
    let safe_server = McpServer::with_config(safe_config);
    let safe_tools = safe_server.list_tools();

    let full_config = McpConfig {
        max_safety_level: SafetyLevel::Critical,
        allow_admin_tools: true,
        allowed_categories: None,
        blocked_tools: vec![],
        execution_timeout: 30,
    };
    let full_server = McpServer::with_config(full_config);
    let full_tools = full_server.list_tools();

    assert!(
        safe_tools.len() <= full_tools.len(),
        "Safe config ({}) should have <= tools than full config ({})",
        safe_tools.len(),
        full_tools.len()
    );
}

#[test]
fn test_blocked_tools_config() {
    let config = McpConfig {
        max_safety_level: SafetyLevel::Critical,
        allow_admin_tools: true,
        allowed_categories: None,
        blocked_tools: vec!["git".to_string(), "cargo".to_string()],
        execution_timeout: 30,
    };
    let server = McpServer::with_config(config);
    let tools = server.list_tools();

    // Blocked tools should not be present
    assert!(
        !tools.iter().any(|t| t.name == "git"),
        "git should be blocked"
    );
    assert!(
        !tools.iter().any(|t| t.name == "cargo"),
        "cargo should be blocked"
    );
}

#[test]
fn test_category_restriction_config() {
    let config = McpConfig {
        max_safety_level: SafetyLevel::Critical,
        allow_admin_tools: true,
        allowed_categories: Some(vec![ToolCategory::Development]),
        blocked_tools: vec![],
        execution_timeout: 30,
    };
    let server = McpServer::with_config(config);
    let tools = server.list_tools();

    // Should only have development tools
    assert!(!tools.is_empty(), "Should have some tools");

    // All tools should be in development category
    for tool in &tools {
        let os_tool = server.tools_db().get_tool(&tool.name);
        if let Some(t) = os_tool {
            assert_eq!(
                t.category,
                ToolCategory::Development,
                "Tool {} should be in Development category",
                tool.name
            );
        }
    }
}

// ============================================================================
// Tool Count and Category Tests
// ============================================================================

#[test]
fn test_tool_count_consistency() {
    let server = McpServer::new();
    let tools = server.list_tools();

    assert_eq!(
        server.tool_count(),
        tools.len(),
        "Tool count should match actual tool count"
    );
}

#[test]
fn test_expected_categories_present() {
    let server = McpServer::new();

    let expected_categories = [
        ToolCategory::FileSystem,
        ToolCategory::Development,
        ToolCategory::NetworkTools,
        ToolCategory::Security,
        ToolCategory::Containers,
        ToolCategory::MachineLearning,
    ];

    for category in expected_categories {
        let cat_tools = server.get_tools_by_category(&category);
        assert!(
            !cat_tools.is_empty(),
            "Should have tools in category: {:?}",
            category
        );
    }
}

#[test]
fn test_common_tools_available() {
    let server = McpServer::new();

    let common_tools = ["git", "curl", "jq", "docker", "npm", "cargo"];

    for tool_name in common_tools {
        let tool = server.get_tool(tool_name);
        assert!(
            tool.is_some(),
            "Common tool '{}' should be available",
            tool_name
        );
    }
}

// ============================================================================
// Edge Cases and Robustness Tests
// ============================================================================

#[test]
fn test_get_tool_case_sensitivity() {
    let server = McpServer::new();

    // Tool names should be case-sensitive (lowercase)
    let git_lower = server.get_tool("git");
    let git_upper = server.get_tool("GIT");

    assert!(git_lower.is_some(), "Should find 'git' with lowercase");
    assert!(git_upper.is_none(), "Should not find 'GIT' with uppercase");
}

#[test]
fn test_empty_tool_call_arguments() {
    let server = McpServer::new();

    // Some tools work without arguments
    let call = McpToolCall {
        name: "git".to_string(),
        arguments: HashMap::new(),
    };
    let result = server.call_tool(call);

    // Should return some result (might be error or success)
    assert!(!result.content.is_empty(), "Should have some response");
}

#[test]
fn test_tools_db_accessor() {
    let server = McpServer::new();

    let db = server.tools_db();
    assert!(!db.tools.is_empty(), "Tools DB should have tools");
}

#[test]
fn test_initialize_returns_correct_info() {
    let server = McpServer::new();
    let info = server.initialize();

    assert_eq!(info.protocol_version, "2024-11-05");
    assert!(info.capabilities.tools.is_some());
    assert!(info.capabilities.resources.is_some());
    assert!(info.capabilities.prompts.is_some());
}

#[test]
fn test_concurrent_server_access() {
    use std::thread;

    let handles: Vec<_> = (0..4)
        .map(|_| {
            thread::spawn(|| {
                let server = McpServer::new();
                let tools = server.list_tools();
                tools.len()
            })
        })
        .collect();

    let counts: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();

    // All threads should get same tool count
    assert!(
        counts.iter().all(|&c| c == counts[0]),
        "Concurrent access should be consistent"
    );
}