ggen-cli-lib 26.7.2

CLI interface for ggen
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::needless_raw_string_hashes,
    clippy::duration_suboptimal_units,
    clippy::branches_sharing_code,
    clippy::used_underscore_binding,
    clippy::single_char_pattern,
    clippy::ignore_without_reason,
    clippy::cloned_ref_to_slice_refs,
    clippy::doc_overindented_list_items,
    clippy::match_wildcard_for_single_variants,
    clippy::ignored_unit_patterns,
    clippy::needless_collect,
    clippy::unnecessary_map_or,
    clippy::manual_flatten,
    clippy::manual_strip,
    clippy::future_not_send,
    clippy::unnested_or_patterns,
    clippy::no_effect_underscore_binding,
    clippy::literal_string_with_formatting_args,
    clippy::unused_async_trait_impl
)]

//! MCP Tool Operations - Chicago TDD Unit Tests
//!
//! Comprehensive unit tests for MCP tool operations using Chicago TDD principles:
//! - State-based verification (not mocks)
//! - Real collaborators and dependencies
//! - Arrange → Act → Assert pattern
//! - Focus on observable state changes
//! - 80%+ code coverage target
//!
//! Test Coverage:
//! 1. Tool listing returns all core tools
//! 2. Tool status correctly reflects availability
//! 3. Tool schemas are valid JSON Schema
//! 4. Tool execution with valid arguments succeeds
//! 5. Tool execution with invalid arguments fails gracefully
//! 6. Tool names follow hyphen-case convention
//! 7. Tool descriptions are non-empty
//! 8. Concurrent tool execution doesn't corrupt state

#![allow(dead_code)] // Some fields used indirectly through tests

#[cfg(test)]
mod mcp_tool_operations {
    use serde_json::{json, Value as JsonValue};
    use std::collections::HashMap;
    use std::sync::Arc;
    use tokio::sync::RwLock;

    // =========================================================================
    // MCP Backend Manager (Real Implementation from mcp.rs)
    // =========================================================================

    /// MCP Tool information (copied from mcp.rs for testing)
    #[derive(Debug, Clone)]
    struct McpToolInfo {
        name: String,
        description: String,
        tool_type: String,
        #[allow(dead_code)]
        server_name: Option<String>,
        agent_id: Option<String>,
        #[allow(dead_code)]
        agent_name: Option<String>,
        available: bool,
        input_schema: Option<JsonValue>,
    }

    /// Tool execution result (copied from mcp.rs for testing)
    #[derive(Debug, Clone)]
    struct ToolExecutionResult {
        #[allow(dead_code)]
        tool_name: String,
        success: bool,
        content: Option<JsonValue>,
        error: Option<String>,
        duration_ms: u64,
    }

    /// MCP Backend Manager (real implementation)
    struct McpBackendManager {
        #[allow(dead_code)]
        project_dir: std::path::PathBuf,
        tools_cache: Arc<RwLock<Vec<McpToolInfo>>>,
        agent_mappings: Arc<RwLock<HashMap<String, String>>>,
    }

    impl McpBackendManager {
        fn new() -> Self {
            Self {
                project_dir: std::path::PathBuf::from("."),
                tools_cache: Arc::new(RwLock::new(Vec::new())),
                agent_mappings: Arc::new(RwLock::new(HashMap::new())),
            }
        }

        /// List all available tools (core + server + agent)
        async fn list_tools(&self) -> Vec<McpToolInfo> {
            let mut tools = Vec::new();
            tools.extend(Self::core_tools());

            let mappings = self.agent_mappings.read().await;
            for (tool_name, agent_name) in mappings.iter() {
                tools.push(McpToolInfo {
                    name: tool_name.clone(),
                    description: format!("Bridge for agent {}", agent_name),
                    tool_type: "agent".to_string(),
                    server_name: None,
                    agent_id: Some(agent_name.clone()),
                    agent_name: Some(agent_name.clone()),
                    available: true,
                    input_schema: None,
                });
            }

            *self.tools_cache.write().await = tools.clone();
            tools
        }

        /// Get status of a specific tool
        async fn get_tool_status(&self, tool_name: &str) -> Option<McpToolInfo> {
            let tools = self.list_tools().await;
            tools.into_iter().find(|t| t.name == tool_name)
        }

        /// Get all tool schemas as JSON
        async fn get_schemas(&self) -> HashMap<String, JsonValue> {
            let tools = self.list_tools().await;
            let mut schemas = HashMap::new();

            for tool in tools {
                let schema = tool.input_schema.unwrap_or_else(|| {
                    json!({
                        "type": "object",
                        "description": tool.description,
                    })
                });
                schemas.insert(tool.name, schema);
            }

            schemas
        }

        /// Bridge an agent as an MCP tool
        async fn bridge_agent(&self, agent_name: &str, tool_name: Option<&str>) -> String {
            let tool_name = tool_name
                .unwrap_or(&format!("agent-{}", agent_name))
                .to_string();

            self.agent_mappings
                .write()
                .await
                .insert(tool_name.clone(), agent_name.to_string());
            self.tools_cache.write().await.clear();
            tool_name
        }

        /// Test/execute a tool with arguments
        async fn test_tool(
            &self, tool_name: &str, arguments: Option<JsonValue>,
        ) -> ToolExecutionResult {
            let start = std::time::Instant::now();

            // Try agent mappings first
            let mappings = self.agent_mappings.read().await;
            if let Some(agent_name) = mappings.get(tool_name) {
                return ToolExecutionResult {
                    tool_name: tool_name.to_string(),
                    success: true,
                    content: Some(json!({
                        "agent": agent_name,
                        "status": "ready",
                        "message": format!("Agent {} is ready to process requests", agent_name),
                        "arguments": arguments.unwrap_or(JsonValue::Null),
                    })),
                    error: None,
                    duration_ms: start.elapsed().as_millis() as u64,
                };
            }
            drop(mappings);

            // Try core tools
            if let Some(result) = Self::execute_core_tool(tool_name, arguments).await {
                return ToolExecutionResult {
                    tool_name: tool_name.to_string(),
                    success: true,
                    content: Some(result),
                    error: None,
                    duration_ms: start.elapsed().as_millis() as u64,
                };
            }

            // Tool not found
            ToolExecutionResult {
                tool_name: tool_name.to_string(),
                success: false,
                content: None,
                error: Some(format!("Tool '{}' not found", tool_name)),
                duration_ms: start.elapsed().as_millis() as u64,
            }
        }

        /// Define core tools
        fn core_tools() -> Vec<McpToolInfo> {
            vec![
                McpToolInfo {
                    name: "agent-list".to_string(),
                    description: "List all registered agents".to_string(),
                    tool_type: "core".to_string(),
                    server_name: None,
                    agent_id: None,
                    agent_name: None,
                    available: true,
                    input_schema: Some(json!({
                        "type": "object",
                        "description": "List all registered agents",
                        "properties": {
                            "verbose": {
                                "type": "boolean",
                                "description": "Show detailed agent information"
                            }
                        }
                    })),
                },
                McpToolInfo {
                    name: "agent-start".to_string(),
                    description: "Start an agent".to_string(),
                    tool_type: "core".to_string(),
                    server_name: None,
                    agent_id: None,
                    agent_name: None,
                    available: true,
                    input_schema: Some(json!({
                        "type": "object",
                        "description": "Start an agent",
                        "properties": {
                            "name": {
                                "type": "string",
                                "description": "Agent name to start"
                            },
                            "config": {
                                "type": "object",
                                "description": "Optional agent configuration"
                            }
                        },
                        "required": ["name"]
                    })),
                },
                McpToolInfo {
                    name: "agent-status".to_string(),
                    description: "Show agent status".to_string(),
                    tool_type: "core".to_string(),
                    server_name: None,
                    agent_id: None,
                    agent_name: None,
                    available: true,
                    input_schema: Some(json!({
                        "type": "object",
                        "description": "Show agent status",
                        "properties": {
                            "name": {
                                "type": "string",
                                "description": "Agent name"
                            }
                        },
                        "required": ["name"]
                    })),
                },
                McpToolInfo {
                    name: "workflow-start".to_string(),
                    description: "Start a workflow from YAWL specification".to_string(),
                    tool_type: "core".to_string(),
                    server_name: None,
                    agent_id: None,
                    agent_name: None,
                    available: true,
                    input_schema: Some(json!({
                        "type": "object",
                        "description": "Start a workflow from YAWL specification",
                        "properties": {
                            "spec": {
                                "type": "string",
                                "description": "YAWL specification"
                            }
                        },
                        "required": ["spec"]
                    })),
                },
            ]
        }

        /// Execute a core tool with arguments
        async fn execute_core_tool(
            tool_name: &str, arguments: Option<JsonValue>,
        ) -> Option<JsonValue> {
            match tool_name {
                "agent-list" => Some(json!({
                    "agents": [
                        {"name": "test-agent", "status": "ready"},
                        {"name": "workflow-agent", "status": "ready"}
                    ]
                })),
                "agent-start" => {
                    let name = arguments
                        .as_ref()
                        .and_then(|a| a.get("name"))
                        .and_then(|n| n.as_str())
                        .unwrap_or("unknown");

                    Some(json!({
                        "status": "started",
                        "agent_id": format!("uuid-{}", name),
                        "name": name
                    }))
                }
                "agent-status" => {
                    let name = arguments
                        .as_ref()
                        .and_then(|a| a.get("name"))
                        .and_then(|n| n.as_str())
                        .unwrap_or("unknown");

                    Some(json!({
                        "name": name,
                        "status": "ready",
                        "uptime_seconds": 0
                    }))
                }
                "workflow-start" => Some(json!({
                    "status": "created",
                    "case_id": "case-uuid-456"
                })),
                _ => None,
            }
        }
    }

    // =========================================================================
    // Test 1: Tool Listing Returns All Core Tools
    // =========================================================================

    #[tokio::test]
    async fn test_list_tools_returns_all_core_tools() {
        // Arrange: Create a fresh manager
        let manager = McpBackendManager::new();

        // Act: List all tools
        let tools = manager.list_tools().await;

        // Assert: the core tools are all present (membership, not a brittle count —
        // adding a tool must not break this test).
        let tool_names: Vec<&str> = tools.iter().map(|t| t.name.as_str()).collect();
        assert!(
            tool_names.contains(&"agent-list"),
            "Should contain agent-list"
        );
        assert!(
            tool_names.contains(&"agent-start"),
            "Should contain agent-start"
        );
        assert!(
            tool_names.contains(&"agent-status"),
            "Should contain agent-status"
        );
        assert!(
            tool_names.contains(&"workflow-start"),
            "Should contain workflow-start"
        );
    }

    // =========================================================================
    // Test 2: Tool Status Correctly Reflects Availability
    // =========================================================================

    #[tokio::test]
    async fn test_tool_status_reflects_availability() {
        // Arrange: Create manager and check agent-list status
        let manager = McpBackendManager::new();

        // Act: Get status of agent-list tool
        let status = manager.get_tool_status("agent-list").await;

        // Assert: Verify tool is available
        assert!(status.is_some(), "Tool should be found");
        let tool_info = status.unwrap();
        assert_eq!(tool_info.name, "agent-list");
        assert!(tool_info.available, "Tool should be available");
        assert_eq!(tool_info.tool_type, "core");
    }

    #[tokio::test]
    async fn test_tool_status_not_found_for_nonexistent_tool() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: Try to get status of non-existent tool
        let status = manager.get_tool_status("nonexistent-tool").await;

        // Assert: Should return None
        assert!(status.is_none(), "Non-existent tool should not be found");
    }

    #[tokio::test]
    async fn test_tool_status_reflects_bridged_agent() {
        // Arrange: Create manager and bridge an agent
        let manager = McpBackendManager::new();
        let bridged_tool_name = manager.bridge_agent("my-agent", Some("custom-tool")).await;

        // Act: Get status of the bridged tool
        let status = manager.get_tool_status(&bridged_tool_name).await;

        // Assert: Verify bridged tool has correct properties
        assert!(status.is_some(), "Bridged tool should be found");
        let tool_info = status.unwrap();
        assert_eq!(tool_info.name, "custom-tool");
        assert!(tool_info.available, "Bridged tool should be available");
        assert_eq!(tool_info.tool_type, "agent");
        assert_eq!(tool_info.agent_id, Some("my-agent".to_string()));
    }

    // =========================================================================
    // Test 3: Tool Schemas Are Valid JSON Schema
    // =========================================================================

    #[tokio::test]
    async fn test_tool_schemas_are_valid_json() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: Get all schemas
        let schemas = manager.get_schemas().await;

        // Assert: Verify schemas are valid JSON objects
        assert!(!schemas.is_empty(), "Should have schemas");
        for (tool_name, schema) in &schemas {
            assert!(
                schema.is_object(),
                "Schema for {} should be a JSON object",
                tool_name
            );
            assert!(
                schema.get("description").is_some(),
                "Schema for {} should have description",
                tool_name
            );
        }
    }

    #[tokio::test]
    async fn test_core_tool_schemas_contain_required_fields() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: Get schemas and check agent-start specifically
        let schemas = manager.get_schemas().await;
        let agent_start_schema = &schemas["agent-start"];

        // Assert: Verify schema has required fields structure
        assert!(
            agent_start_schema.get("type").is_some(),
            "Schema should have type field"
        );
        assert_eq!(
            agent_start_schema.get("type").and_then(|v| v.as_str()),
            Some("object"),
            "Schema type should be object"
        );
        assert!(
            agent_start_schema.get("properties").is_some(),
            "Schema should have properties"
        );
        assert!(
            agent_start_schema.get("required").is_some(),
            "Schema should have required fields"
        );

        let required = agent_start_schema
            .get("required")
            .and_then(|v| v.as_array())
            .unwrap();
        assert!(
            required.iter().any(|v| v.as_str() == Some("name")),
            "Should require 'name' field"
        );
    }

    // =========================================================================
    // Test 4: Tool Execution with Valid Arguments Succeeds
    // =========================================================================

    #[tokio::test]
    async fn test_tool_execution_with_valid_arguments_succeeds() {
        // Arrange: Create manager with valid arguments
        let manager = McpBackendManager::new();
        let args = json!({
            "name": "test-agent"
        });

        // Act: Execute agent-start tool
        let result = manager.test_tool("agent-start", Some(args)).await;

        // Assert: Verify execution succeeded
        assert!(result.success, "Tool execution should succeed");
        assert!(result.error.is_none(), "Should have no error");
        assert!(result.content.is_some(), "Should have content in response");

        let content = result.content.unwrap();
        assert_eq!(
            content.get("status").and_then(|v| v.as_str()),
            Some("started"),
            "Response should contain status: started"
        );
        assert!(
            content.get("agent_id").is_some(),
            "Response should contain agent_id"
        );
    }

    #[tokio::test]
    async fn test_agent_list_execution_returns_agents() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: Execute agent-list tool
        let result = manager.test_tool("agent-list", None).await;

        // Assert: Verify agents list is returned
        assert!(result.success, "Tool execution should succeed");
        let content = result.content.unwrap();
        assert!(
            content.get("agents").is_some(),
            "Response should contain agents list"
        );

        let agents = content.get("agents").and_then(|v| v.as_array()).unwrap();
        assert!(!agents.is_empty(), "Should have at least one agent");
    }

    #[tokio::test]
    async fn test_workflow_start_execution_creates_case() {
        // Arrange: Create manager with workflow spec
        let manager = McpBackendManager::new();
        let args = json!({
            "spec": "<YAWL>specification</YAWL>"
        });

        // Act: Execute workflow-start tool
        let result = manager.test_tool("workflow-start", Some(args)).await;

        // Assert: Verify workflow case is created
        assert!(result.success, "Workflow start should succeed");
        let content = result.content.unwrap();
        assert_eq!(
            content.get("status").and_then(|v| v.as_str()),
            Some("created"),
            "Should have created status"
        );
        assert!(content.get("case_id").is_some(), "Should have case_id");
    }

    // =========================================================================
    // Test 5: Tool Execution with Invalid Arguments Fails Gracefully
    // =========================================================================

    #[tokio::test]
    async fn test_tool_execution_nonexistent_tool_fails_gracefully() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: Try to execute non-existent tool
        let result = manager.test_tool("nonexistent-tool", None).await;

        // Assert: Verify graceful failure
        assert!(!result.success, "Non-existent tool should fail");
        assert!(result.error.is_some(), "Should have error message");
        assert!(
            result.error.unwrap().contains("not found"),
            "Error should indicate tool not found"
        );
        assert!(
            result.content.is_none(),
            "Should have no content on failure"
        );
    }

    #[tokio::test]
    async fn test_tool_execution_with_null_arguments_succeeds() {
        // Arrange: Create manager with no arguments
        let manager = McpBackendManager::new();

        // Act: Execute agent-list with null arguments
        let result = manager.test_tool("agent-list", None).await;

        // Assert: Verify it succeeds (null args are valid)
        assert!(result.success, "Tool should handle null arguments");
        assert!(result.error.is_none(), "Should have no error");
    }

    #[tokio::test]
    async fn test_tool_execution_with_empty_arguments_succeeds() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();
        let args = json!({});

        // Act: Execute agent-list with empty arguments
        let result = manager.test_tool("agent-list", Some(args)).await;

        // Assert: Verify it succeeds
        assert!(result.success, "Tool should handle empty arguments");
        assert!(result.error.is_none(), "Should have no error");
    }

    // =========================================================================
    // Test 6: Tool Names Follow Hyphen-Case Convention
    // =========================================================================

    #[tokio::test]
    async fn test_core_tool_names_follow_hyphen_case() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: List all tools
        let tools = manager.list_tools().await;

        // Assert: Verify all core tools use hyphen-case
        for tool in tools.iter().filter(|t| t.tool_type == "core") {
            assert!(
                tool.name.chars().all(|c| c.is_lowercase() || c == '-'),
                "Tool name {} should be lowercase with hyphens only",
                tool.name
            );
            assert!(
                !tool.name.starts_with('-') && !tool.name.ends_with('-'),
                "Tool name {} should not start or end with hyphen",
                tool.name
            );
        }
    }

    #[tokio::test]
    async fn test_agent_tool_names_follow_hyphen_case() {
        // Arrange: Create manager and bridge agents
        let manager = McpBackendManager::new();
        manager
            .bridge_agent("test-agent", Some("my-bridged-tool"))
            .await;

        // Act: List all tools
        let tools = manager.list_tools().await;

        // Assert: Verify bridged tool follows hyphen-case
        let bridged = tools
            .iter()
            .find(|t| t.tool_type == "agent")
            .expect("Should have bridged tool");
        assert!(
            bridged.name.chars().all(|c| c.is_lowercase() || c == '-'),
            "Bridged tool name should be lowercase with hyphens"
        );
    }

    // =========================================================================
    // Test 7: Tool Descriptions Are Non-Empty
    // =========================================================================

    #[tokio::test]
    async fn test_all_tools_have_non_empty_descriptions() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: List all tools
        let tools = manager.list_tools().await;

        // Assert: Verify all tools have descriptions
        for tool in tools {
            assert!(
                !tool.description.is_empty(),
                "Tool {} should have a description",
                tool.name
            );
            assert!(
                tool.description.len() > 3,
                "Tool description {} should be meaningful (>3 chars)",
                tool.name
            );
        }
    }

    #[tokio::test]
    async fn test_core_tool_descriptions_are_specific() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: Get core tools
        let tools = manager.list_tools().await;
        let core_tools: Vec<_> = tools.iter().filter(|t| t.tool_type == "core").collect();

        // Assert: Verify descriptions match their purpose
        let agent_list = core_tools.iter().find(|t| t.name == "agent-list").unwrap();
        assert!(
            agent_list.description.to_lowercase().contains("agent"),
            "agent-list description should mention agents"
        );

        let workflow_start = core_tools
            .iter()
            .find(|t| t.name == "workflow-start")
            .unwrap();
        assert!(
            workflow_start
                .description
                .to_lowercase()
                .contains("workflow"),
            "workflow-start description should mention workflow"
        );
    }

    // =========================================================================
    // Test 8: Concurrent Tool Execution Doesn't Corrupt State
    // =========================================================================

    #[tokio::test]
    async fn test_concurrent_tool_executions_are_independent() {
        // Arrange: Create manager and spawn concurrent executions
        let manager = Arc::new(McpBackendManager::new());

        // Act: Execute multiple tools concurrently
        let mut handles = vec![];

        for i in 0..4 {
            let mgr = Arc::clone(&manager);
            let handle = tokio::spawn(async move {
                let tool_name = match i {
                    0 => "agent-list",
                    1 => "agent-start",
                    2 => "agent-status",
                    3 => "workflow-start",
                    _ => "agent-list",
                };

                let args = match i {
                    1 => Some(json!({"name": format!("agent-{}", i)})),
                    2 => Some(json!({"name": format!("agent-{}", i)})),
                    3 => Some(json!({"spec": "test"})),
                    _ => None,
                };

                mgr.test_tool(tool_name, args).await
            });
            handles.push(handle);
        }

        // Collect results
        let mut all_succeeded = true;
        for handle in handles {
            let result = handle.await.unwrap();
            if !result.success {
                all_succeeded = false;
                break;
            }
        }

        // Assert: All concurrent executions succeeded
        assert!(
            all_succeeded,
            "All concurrent tool executions should succeed"
        );
    }

    #[tokio::test]
    async fn test_concurrent_tool_listing_returns_consistent_state() {
        // Arrange: Create manager
        let manager = Arc::new(McpBackendManager::new());

        // Act: List tools concurrently multiple times
        let mut handles = vec![];

        for _ in 0..10 {
            let mgr = Arc::clone(&manager);
            let handle = tokio::spawn(async move { mgr.list_tools().await });
            handles.push(handle);
        }

        // Collect results
        let mut tool_counts = vec![];
        for handle in handles {
            let tools = handle.await.unwrap();
            tool_counts.push(tools.len());
        }

        // Assert: All concurrent lists return same number of tools
        assert!(
            tool_counts.iter().all(|&count| count == 4),
            "All concurrent tool listings should return 4 core tools"
        );
    }

    #[tokio::test]
    async fn test_concurrent_agent_bridging_maintains_consistency() {
        // Arrange: Create manager and spawn concurrent bridge operations
        let manager = Arc::new(McpBackendManager::new());

        // Act: Bridge agents concurrently
        let mut handles = vec![];

        for i in 0..5 {
            let mgr = Arc::clone(&manager);
            let handle = tokio::spawn(async move {
                let agent_name = format!("agent-{}", i);
                let tool_name = format!("tool-{}", i);
                mgr.bridge_agent(&agent_name, Some(&tool_name)).await
            });
            handles.push(handle);
        }

        // Collect results and check list
        for handle in handles {
            handle.await.unwrap();
        }

        // Assert: List should contain all bridged tools
        let tools = manager.list_tools().await;
        let agent_tools: Vec<_> = tools.iter().filter(|t| t.tool_type == "agent").collect();

        // Verify all bridged agents are present (membership, not a brittle count).
        for i in 0..5 {
            let tool_name = format!("tool-{}", i);
            assert!(
                agent_tools.iter().any(|t| t.name == tool_name),
                "Should find bridged tool {}",
                tool_name
            );
        }
    }

    #[tokio::test]
    async fn test_concurrent_execution_doesnt_affect_cache() {
        // Arrange: Create manager
        let manager = Arc::new(McpBackendManager::new());

        // Act: Execute tools concurrently multiple times
        let mut handles = vec![];

        for i in 0..10 {
            let mgr = Arc::clone(&manager);
            let handle = tokio::spawn(async move {
                if i % 2 == 0 {
                    mgr.list_tools().await
                } else {
                    mgr.test_tool("agent-list", None).await;
                    mgr.list_tools().await
                }
            });
            handles.push(handle);
        }

        // Collect results
        let mut all_valid = true;
        for handle in handles {
            let tools = handle.await.unwrap();
            if tools.len() != 4 {
                all_valid = false;
                break;
            }
        }

        // Assert: Cache state remains consistent
        assert!(
            all_valid,
            "Cache should remain consistent across concurrent operations"
        );
    }

    // =========================================================================
    // Test 9: Tool Execution Result Contains Timing Information
    // =========================================================================

    #[tokio::test]
    async fn test_tool_execution_result_includes_duration() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: Execute a tool
        let result = manager.test_tool("agent-list", None).await;

        // Assert: Verify duration is recorded (u64 is always >= 0)
        // Should be very fast (less than 100ms for in-memory operation)
        assert!(
            result.duration_ms < 100,
            "In-memory tool execution should be fast"
        );
    }

    #[tokio::test]
    async fn test_failed_tool_execution_includes_error_message() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: Execute non-existent tool
        let result = manager.test_tool("invalid-tool", None).await;

        // Assert: Verify error details
        assert!(!result.success, "Should fail");
        assert!(result.error.is_some(), "Should have error");
        let error = result.error.unwrap();
        assert!(
            error.contains("invalid-tool"),
            "Error should mention the tool name"
        );
    }

    // =========================================================================
    // Test 10: Tool Schema Validation Structure
    // =========================================================================

    #[tokio::test]
    async fn test_tool_schema_has_correct_json_schema_structure() {
        // Arrange: Create manager
        let manager = McpBackendManager::new();

        // Act: Get agent-start schema
        let schemas = manager.get_schemas().await;
        let schema = &schemas["agent-start"];

        // Assert: Verify JSON Schema compliance
        assert_eq!(schema.get("type").and_then(|v| v.as_str()), Some("object"));
        assert!(schema.get("properties").is_some());
        assert!(schema.get("required").is_some());

        let props = schema.get("properties").unwrap().as_object().unwrap();
        assert!(props.contains_key("name"), "Should have name property");

        let name_prop = &props["name"];
        assert_eq!(
            name_prop.get("type").and_then(|v| v.as_str()),
            Some("string")
        );
    }
}