matrixcode-core 0.4.39

MatrixCode Agent Core - Pure logic, no UI
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
//! Extension Gateway
//!
//! Unified entry point for the MatrixRPC extension system.
//! Coordinates Registry, Lifecycle, Router, and Callback components.

use std::sync::Arc;

use serde_json::Value as JsonValue;
use tokio::sync::{broadcast, RwLock};

use crate::matrixrpc::{
    callback::{CallbackConfig, CallbackHandler, CallbackResult, CallbackType, SecurityValidator},
    lifecycle::{LifecycleConfig, LifecycleManager},
    protocol::{ErrorCode, JsonRpcError, JsonRpcId, JsonRpcRequest, JsonRpcResponse},
    registry::{RegistryBuilder, RegistryService, RegistryStats},
    router::{NodeCapability, NodeContext, NodeDefinition, NodeRouter, NodeRouteResult},
    service::{ExtensionService, ServiceId, ServiceStatus},
    ToolDefinition, ToolRouter, ToolRouteResult,
};

/// Gateway configuration
#[derive(Debug, Clone)]
pub struct GatewayConfig {
    /// Registry configuration
    pub registry_heartbeat_timeout_secs: u64,

    /// Lifecycle configuration
    pub lifecycle_config: LifecycleConfig,

    /// Callback configuration
    pub callback_config: CallbackConfig,

    /// Default tool timeout (ms)
    pub default_tool_timeout_ms: u64,

    /// Default node timeout (ms)
    pub default_node_timeout_ms: u64,

    /// Enable auto-discovery
    pub auto_discovery: bool,

    /// Maximum services
    pub max_services: u32,
}

impl Default for GatewayConfig {
    fn default() -> Self {
        Self {
            registry_heartbeat_timeout_secs: 60,
            lifecycle_config: LifecycleConfig::default(),
            callback_config: CallbackConfig::default(),
            default_tool_timeout_ms: 30_000,
            default_node_timeout_ms: 60_000,
            auto_discovery: true,
            max_services: 100,
        }
    }
}

/// Gateway statistics
#[derive(Debug, Clone, Default)]
pub struct GatewayStats {
    /// Total registered services
    pub total_services: usize,

    /// Running services
    pub running_services: usize,

    /// Total registered tools
    pub total_tools: usize,

    /// Total registered nodes
    pub total_nodes: usize,

    /// Total callbacks processed
    pub total_callbacks: usize,

    /// Total errors
    pub total_errors: usize,
}

/// Gateway error
#[derive(Debug, thiserror::Error)]
pub enum GatewayError {
    /// Service not found
    #[error("Service '{0}' not found")]
    ServiceNotFound(String),

    /// Service limit exceeded
    #[error("Maximum service limit ({0}) exceeded")]
    ServiceLimitExceeded(u32),

    /// Registration failed
    #[error("Registration failed: {0}")]
    RegistrationFailed(String),

    /// Routing failed
    #[error("Routing failed: {0}")]
    RoutingFailed(String),

    /// Execution failed
    #[error("Execution failed: {0}")]
    ExecutionFailed(String),

    /// Callback failed
    #[error("Callback failed: {0}")]
    CallbackFailed(String),

    /// Internal error
    #[error("Internal error: {0}")]
    Internal(String),
}

/// Service registration request
#[derive(Debug, Clone)]
pub struct ServiceRegistrationRequest {
    /// Service name
    pub name: String,

    /// Service version
    pub version: String,

    /// Service description
    pub description: Option<String>,

    /// Tools to register
    pub tools: Vec<ToolDefinition>,

    /// Nodes to register
    pub nodes: Vec<NodeDefinition>,

    /// Transport type (stdio or tcp)
    pub transport_type: String,

    /// Command (for stdio)
    pub command: Option<String>,

    /// Arguments (for stdio)
    pub args: Vec<String>,
}

impl ServiceRegistrationRequest {
    /// Create a new registration request
    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            version: version.into(),
            description: None,
            tools: Vec::new(),
            nodes: Vec::new(),
            transport_type: "stdio".to_string(),
            command: None,
            args: Vec::new(),
        }
    }

    /// Add description
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    /// Add a tool
    pub fn tool(mut self, tool: ToolDefinition) -> Self {
        self.tools.push(tool);
        self
    }

    /// Add a node
    pub fn node(mut self, node: NodeDefinition) -> Self {
        self.nodes.push(node);
        self
    }

    /// Set transport type
    pub fn transport(mut self, transport_type: impl Into<String>) -> Self {
        self.transport_type = transport_type.into();
        self
    }

    /// Set command (for stdio)
    pub fn command(mut self, cmd: impl Into<String>, args: Vec<String>) -> Self {
        self.command = Some(cmd.into());
        self.args = args;
        self
    }
}

/// Extension Gateway
///
/// Unified entry point for the MatrixRPC extension system.
/// Coordinates all components: Registry, Lifecycle, Router, and Callback.
pub struct ExtensionGateway {
    /// Configuration
    config: GatewayConfig,

    /// Registry service
    registry: Arc<RegistryService>,

    /// Lifecycle manager
    lifecycle: Arc<LifecycleManager>,

    /// Tool router
    tool_router: Arc<ToolRouter>,

    /// Node router
    node_router: Arc<NodeRouter>,

    /// Callback handler
    callback: Arc<CallbackHandler>,

    /// Security validator
    security: Arc<SecurityValidator>,

    /// Statistics
    stats: Arc<RwLock<GatewayStats>>,

    /// Event broadcaster
    event_tx: broadcast::Sender<GatewayEvent>,
}

/// Gateway events
#[derive(Debug, Clone)]
pub enum GatewayEvent {
    /// Service registered
    ServiceRegistered(ServiceId),

    /// Service unregistered
    ServiceUnregistered(ServiceId),

    /// Service status changed
    ServiceStatusChanged {
        service_id: ServiceId,
        old_status: ServiceStatus,
        new_status: ServiceStatus,
    },

    /// Tool registered
    ToolRegistered {
        tool_name: String,
        service_id: ServiceId,
    },

    /// Node registered
    NodeRegistered {
        node_id: String,
        service_id: ServiceId,
    },

    /// Callback received
    CallbackReceived {
        callback_type: CallbackType,
        service_id: ServiceId,
    },

    /// Error occurred
    Error(String),
}

impl ExtensionGateway {
    /// Create a new extension gateway
    pub fn new() -> Self {
        Self::with_config(GatewayConfig::default())
    }

    /// Create a new extension gateway with configuration
    pub fn with_config(config: GatewayConfig) -> Self {
        let (event_tx, _) = broadcast::channel(256);

        // Create registry
        let registry = Arc::new(
            RegistryBuilder::new()
                .heartbeat_timeout(config.registry_heartbeat_timeout_secs)
                .build(),
        );

        // Create lifecycle manager
        let lifecycle = Arc::new(
            LifecycleManager::with_config(registry.clone(), config.lifecycle_config.clone()),
        );

        // Create routers
        let tool_router = Arc::new(ToolRouter::new(registry.clone()));
        let node_router = Arc::new(NodeRouter::new(registry.clone()));

        // Create security validator and callback handler
        let security = Arc::new(SecurityValidator::new());
        let callback = Arc::new(
            CallbackHandler::with_config(
                security.clone(),
                tool_router.clone(),
                node_router.clone(),
                config.callback_config.clone(),
            ),
        );

        Self {
            config,
            registry,
            lifecycle,
            tool_router,
            node_router,
            callback,
            security,
            stats: Arc::new(RwLock::new(GatewayStats::default())),
            event_tx,
        }
    }

    /// Subscribe to gateway events
    pub fn subscribe(&self) -> broadcast::Receiver<GatewayEvent> {
        self.event_tx.subscribe()
    }

    /// Register a new extension service
    pub async fn register_service(&self, request: ServiceRegistrationRequest) -> Result<ServiceId, GatewayError> {
        // Check service limit
        {
            let stats = self.stats.read().await;
            if stats.total_services >= self.config.max_services as usize {
                return Err(GatewayError::ServiceLimitExceeded(self.config.max_services));
            }
        }

        // Build extension service
        let mut service = ExtensionService::new(&request.name, &request.version);

        if let Some(desc) = &request.description {
            service = service.description(desc);
        }

        // Set transport config based on type
        let transport_config = crate::matrixrpc::service::TransportConfig {
            transport_type: if request.transport_type == "tcp" {
                crate::matrixrpc::service::TransportType::Tcp
            } else {
                crate::matrixrpc::service::TransportType::Stdio
            },
            command: request.command.clone(),
            args: request.args.clone(),
            ..Default::default()
        };
        service = service.transport(transport_config);

        // Save counts before moving
        let tools_count = request.tools.len();
        let nodes_count = request.nodes.len();

        // Start service via lifecycle manager
        let service_id = self
            .lifecycle
            .start_service(service)
            .await
            .map_err(|e| GatewayError::RegistrationFailed(e.to_string()))?;

        // Register tools
        for tool in request.tools {
            self.tool_router
                .register_tool(service_id.clone(), tool.clone())
                .await;
            let _ = self.event_tx.send(GatewayEvent::ToolRegistered {
                tool_name: tool.name,
                service_id: service_id.clone(),
            });
        }

        // Register nodes
        for node in request.nodes {
            self.node_router
                .register_node(service_id.clone(), node.clone())
                .await;
            let _ = self.event_tx.send(GatewayEvent::NodeRegistered {
                node_id: node.id,
                service_id: service_id.clone(),
            });
        }

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.total_services += 1;
            stats.total_tools += tools_count;
            stats.total_nodes += nodes_count;
        }

        // Emit event
        let _ = self.event_tx.send(GatewayEvent::ServiceRegistered(service_id.clone()));

        Ok(service_id)
    }

    /// Unregister a service
    pub async fn unregister_service(&self, service_id: &ServiceId) -> Result<(), GatewayError> {
        // Unregister tools and nodes
        self.tool_router.unregister_service_tools(service_id).await;
        self.node_router.unregister_service_nodes(service_id).await;

        // Stop service
        self.lifecycle
            .stop_service(service_id)
            .await
            .map_err(|e| GatewayError::Internal(e.to_string()))?;

        // Invalidate security tokens
        self.security.invalidate_service_tokens(service_id).await;

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.total_services -= 1;
        }

        // Emit event
        let _ = self.event_tx.send(GatewayEvent::ServiceUnregistered(service_id.clone()));

        Ok(())
    }

    /// Execute a tool
    pub async fn execute_tool(
        &self,
        tool_name: &str,
        params: JsonValue,
        request_id: JsonRpcId,
    ) -> Result<ToolRouteResult, GatewayError> {
        // Route the tool
        let route_result = self
            .tool_router
            .route(tool_name, params, request_id)
            .await
            .map_err(|e| GatewayError::RoutingFailed(e.to_string()))?;

        // In real implementation, would execute via transport
        // For now, return the route result

        Ok(route_result)
    }

    /// Execute a node
    pub async fn execute_node(
        &self,
        node_id: &str,
        context: NodeContext,
        request_id: JsonRpcId,
        required_capabilities: Vec<NodeCapability>,
    ) -> Result<NodeRouteResult, GatewayError> {
        // Save callback types before moving capabilities
        let callback_types = self.get_callback_types_for_capabilities(&required_capabilities);

        // Route the node
        let route_result = self
            .node_router
            .route(node_id, context, request_id, required_capabilities)
            .await
            .map_err(|e| GatewayError::RoutingFailed(e.to_string()))?;

        // Generate callback token
        let _token = self
            .callback
            .generate_token(route_result.service_id.clone(), route_result.request_id.to_string(), callback_types)
            .await
            .map_err(|e| GatewayError::CallbackFailed(e.to_string()))?;

        // In real implementation, would execute via transport with callback endpoint

        Ok(route_result)
    }

    /// Handle a callback request
    pub async fn handle_callback(&self, request: JsonRpcRequest) -> Result<CallbackResult, GatewayError> {
        // Track callback
        {
            let mut stats = self.stats.write().await;
            stats.total_callbacks += 1;
        }

        // Handle via callback handler
        let result = self
            .callback
            .handle_request(request.clone())
            .await;

        match result {
            Ok(res) => {
                // Emit event
                if let Some(params) = &request.params {
                    if let Some(service_id) = params.get("service_id").and_then(|v| v.as_str()) {
                        let _ = self.event_tx.send(GatewayEvent::CallbackReceived {
                            callback_type: res.callback_type(),
                            service_id: ServiceId::new(service_id),
                        });
                    }
                }
                Ok(res)
            }
            Err(e) => {
                // Update error stats
                {
                    let mut stats = self.stats.write().await;
                    stats.total_errors += 1;
                }
                Err(GatewayError::CallbackFailed(e.to_string()))
            }
        }
    }

    /// Get callback types for node capabilities
    fn get_callback_types_for_capabilities(&self, capabilities: &[NodeCapability]) -> Vec<String> {
        let mut types = Vec::new();

        for cap in capabilities {
            match cap {
                NodeCapability::AiExecution => types.push("ai".to_string()),
                NodeCapability::ToolExecution => types.push("tool".to_string()),
                NodeCapability::ContextAccess => types.push("context".to_string()),
            }
        }

        types
    }

    /// List all services
    pub async fn list_services(&self) -> Vec<ExtensionService> {
        self.registry.list_all().await
    }

    /// Get service by ID
    pub async fn get_service(&self, service_id: &ServiceId) -> Option<ExtensionService> {
        self.registry.get(service_id).await
    }

    /// Get service by name
    pub async fn get_service_by_name(&self, name: &str) -> Option<ExtensionService> {
        self.registry.get_by_name(name).await
    }

    /// List all tools
    pub async fn list_tools(&self) -> Vec<ToolDefinition> {
        self.tool_router.list_tools().await
    }

    /// List all nodes
    pub async fn list_nodes(&self) -> Vec<NodeDefinition> {
        self.node_router.list_nodes().await
    }

    /// Check if tool exists
    pub async fn has_tool(&self, tool_name: &str) -> bool {
        self.tool_router.has_tool(tool_name).await
    }

    /// Check if node exists
    pub async fn has_node(&self, node_id: &str) -> bool {
        self.node_router.has_node(node_id).await
    }

    /// Get registry statistics
    pub async fn registry_stats(&self) -> RegistryStats {
        self.registry.stats().await
    }

    /// Get gateway statistics
    pub async fn gateway_stats(&self) -> GatewayStats {
        self.stats.read().await.clone()
    }

    /// Health check all services
    pub async fn health_check(&self) -> Vec<ServiceId> {
        self.lifecycle.health_check().await
    }

    /// Send heartbeat for a service
    pub async fn heartbeat(&self, service_id: &ServiceId) -> Result<(), GatewayError> {
        self.lifecycle
            .handle_heartbeat(service_id)
            .await
            .map_err(|e| GatewayError::Internal(e.to_string()))
    }

    /// Get service status
    pub async fn get_service_status(&self, service_id: &ServiceId) -> Option<ServiceStatus> {
        self.lifecycle.get_status(service_id).await
    }

    /// Stop all services
    pub async fn stop_all(&self) {
        self.lifecycle.stop_all().await;
        self.registry.clear().await;
        self.security.cleanup_expired().await;

        let mut stats = self.stats.write().await;
        stats.total_services = 0;
        stats.total_tools = 0;
        stats.total_nodes = 0;
    }

    /// Create JSON-RPC error response
    pub fn create_error_response(&self, error: GatewayError, id: JsonRpcId) -> JsonRpcResponse {
        let (code, message, data) = match error {
            GatewayError::ServiceNotFound(id) => (
                ErrorCode::RESOURCE_NOT_FOUND,
                format!("Service '{}' not found", id),
                None,
            ),
            GatewayError::ServiceLimitExceeded(limit) => (
                ErrorCode::PERMISSION_DENIED,
                format!("Maximum service limit ({}) exceeded", limit),
                None,
            ),
            GatewayError::RegistrationFailed(msg) => (
                ErrorCode::INTERNAL_ERROR,
                "Registration failed".to_string(),
                Some(serde_json::json!({ "reason": msg })),
            ),
            GatewayError::RoutingFailed(msg) => (
                ErrorCode::INTERNAL_ERROR,
                "Routing failed".to_string(),
                Some(serde_json::json!({ "reason": msg })),
            ),
            GatewayError::ExecutionFailed(msg) => (
                ErrorCode::INTERNAL_ERROR,
                "Execution failed".to_string(),
                Some(serde_json::json!({ "reason": msg })),
            ),
            GatewayError::CallbackFailed(msg) => (
                ErrorCode::CALLBACK_ERROR,
                "Callback failed".to_string(),
                Some(serde_json::json!({ "reason": msg })),
            ),
            GatewayError::Internal(msg) => (
                ErrorCode::INTERNAL_ERROR,
                msg,
                None,
            ),
        };

        JsonRpcResponse::error(
            id,
            JsonRpcError::with_data(code, message, data.unwrap_or(JsonValue::Null)),
        )
    }

    /// Get the registry service
    pub fn registry(&self) -> Arc<RegistryService> {
        self.registry.clone()
    }

    /// Get the lifecycle manager
    pub fn lifecycle(&self) -> Arc<LifecycleManager> {
        self.lifecycle.clone()
    }

    /// Get the tool router
    pub fn tool_router(&self) -> Arc<ToolRouter> {
        self.tool_router.clone()
    }

    /// Get the node router
    pub fn node_router(&self) -> Arc<NodeRouter> {
        self.node_router.clone()
    }

    /// Get the callback handler
    pub fn callback(&self) -> Arc<CallbackHandler> {
        self.callback.clone()
    }

    /// Get the security validator
    pub fn security(&self) -> Arc<SecurityValidator> {
        self.security.clone()
    }
}

impl Default for ExtensionGateway {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_gateway_creation() {
        let gateway = ExtensionGateway::new();
        let stats = gateway.gateway_stats().await;

        assert_eq!(stats.total_services, 0);
        assert_eq!(stats.total_tools, 0);
        assert_eq!(stats.total_nodes, 0);
    }

    #[tokio::test]
    async fn test_gateway_with_config() {
        let config = GatewayConfig {
            max_services: 10,
            default_tool_timeout_ms: 10_000,
            ..Default::default()
        };

        let gateway = ExtensionGateway::with_config(config);
        assert_eq!(gateway.config.max_services, 10);
    }

    #[tokio::test]
    async fn test_register_service() {
        let gateway = ExtensionGateway::new();

        let request = ServiceRegistrationRequest::new("test-service", "1.0.0")
            .description("A test service");

        let service_id = gateway.register_service(request).await.unwrap();
        assert!(gateway.get_service(&service_id).await.is_some());
    }

    #[tokio::test]
    async fn test_unregister_service() {
        let gateway = ExtensionGateway::new();

        let request = ServiceRegistrationRequest::new("test-service", "1.0.0");
        let service_id = gateway.register_service(request).await.unwrap();

        gateway.unregister_service(&service_id).await.unwrap();
        assert!(gateway.get_service(&service_id).await.is_none());
    }

    #[tokio::test]
    async fn test_register_service_with_tools() {
        let gateway = ExtensionGateway::new();

        let request = ServiceRegistrationRequest::new("test-service", "1.0.0")
            .tool(ToolDefinition {
                name: "test_tool".to_string(),
                service_id: ServiceId::generate(),
                description: Some("Test tool".to_string()),
                risk_level: Some("safe".to_string()),
                timeout_ms: Some(5000),
            });

        let service_id = gateway.register_service(request).await.unwrap();

        // Tool should be registered
        assert!(gateway.has_tool("test_tool").await);
    }

    #[tokio::test]
    async fn test_register_service_with_nodes() {
        let gateway = ExtensionGateway::new();

        let request = ServiceRegistrationRequest::new("test-service", "1.0.0")
            .node(NodeDefinition {
                id: "test_node".to_string(),
                name: "Test Node".to_string(),
                service_id: ServiceId::generate(),
                node_type: NodeType::Task,
                description: Some("Test node".to_string()),
                capabilities: vec![NodeCapability::AiExecution],
                timeout_ms: Some(10_000),
                params_schema: None,
            });

        let service_id = gateway.register_service(request).await.unwrap();

        // Node should be registered
        assert!(gateway.has_node("test_node").await);
    }

    #[tokio::test]
    async fn test_list_services() {
        let gateway = ExtensionGateway::new();

        gateway
            .register_service(ServiceRegistrationRequest::new("service1", "1.0.0"))
            .await
            .unwrap();
        gateway
            .register_service(ServiceRegistrationRequest::new("service2", "1.0.0"))
            .await
            .unwrap();

        let services = gateway.list_services().await;
        assert_eq!(services.len(), 2);
    }

    #[tokio::test]
    async fn test_list_tools() {
        let gateway = ExtensionGateway::new();

        let request = ServiceRegistrationRequest::new("test", "1.0")
            .tool(ToolDefinition {
                name: "tool1".to_string(),
                service_id: ServiceId::generate(),
                description: None,
                risk_level: None,
                timeout_ms: None,
            })
            .tool(ToolDefinition {
                name: "tool2".to_string(),
                service_id: ServiceId::generate(),
                description: None,
                risk_level: None,
                timeout_ms: None,
            });

        gateway.register_service(request).await.unwrap();

        let tools = gateway.list_tools().await;
        assert_eq!(tools.len(), 2);
    }

    #[tokio::test]
    async fn test_service_limit() {
        let config = GatewayConfig {
            max_services: 2,
            ..Default::default()
        };

        let gateway = ExtensionGateway::with_config(config);

        // Register two services
        gateway
            .register_service(ServiceRegistrationRequest::new("s1", "1.0"))
            .await
            .unwrap();
        gateway
            .register_service(ServiceRegistrationRequest::new("s2", "1.0"))
            .await
            .unwrap();

        // Third should fail
        let result = gateway
            .register_service(ServiceRegistrationRequest::new("s3", "1.0"))
            .await;

        assert!(matches!(result, Err(GatewayError::ServiceLimitExceeded(2))));
    }

    #[tokio::test]
    async fn test_gateway_events() {
        let gateway = ExtensionGateway::new();
        let mut event_rx = gateway.subscribe();

        let request = ServiceRegistrationRequest::new("test-service", "1.0.0");
        gateway.register_service(request).await.unwrap();

        // Should receive ServiceRegistered event
        let event = event_rx.try_recv();
        assert!(event.is_ok());
    }

    #[tokio::test]
    async fn test_health_check() {
        let gateway = ExtensionGateway::new();

        let request = ServiceRegistrationRequest::new("test-service", "1.0.0");
        gateway.register_service(request).await.unwrap();

        let unhealthy = gateway.health_check().await;
        // Newly registered service should not be unhealthy initially
        // (depends on heartbeat timing)
    }

    #[tokio::test]
    async fn test_stop_all() {
        let gateway = ExtensionGateway::new();

        gateway
            .register_service(ServiceRegistrationRequest::new("s1", "1.0"))
            .await
            .unwrap();
        gateway
            .register_service(ServiceRegistrationRequest::new("s2", "1.0"))
            .await
            .unwrap();

        gateway.stop_all().await;

        let services = gateway.list_services().await;
        assert!(services.is_empty());
    }
}