|
| 1 | +use futures::future::join_all; |
| 2 | +use mimi_cli::ai::{ |
| 3 | + AdapterCapabilities, AdapterInitParams, AdapterRegistry, AiAdapter, AiRequest, AiResponse, |
| 4 | + SharedAdapter, |
| 5 | +}; |
| 6 | +use std::sync::Arc; |
| 7 | +use tokio::sync::Mutex; |
| 8 | + |
| 9 | +struct FullStackAdapter { |
| 10 | + name: String, |
| 11 | +} |
| 12 | + |
| 13 | +impl FullStackAdapter { |
| 14 | + fn new(name: &str) -> Self { |
| 15 | + Self { |
| 16 | + name: name.to_string(), |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +#[async_trait::async_trait] |
| 22 | +impl AiAdapter for FullStackAdapter { |
| 23 | + async fn initialize(&self, _params: AdapterInitParams) -> mimi_cli::ai::AdapterResult<()> { |
| 24 | + Ok(()) |
| 25 | + } |
| 26 | + |
| 27 | + async fn capabilities(&self) -> mimi_cli::ai::AdapterResult<AdapterCapabilities> { |
| 28 | + Ok(AdapterCapabilities { |
| 29 | + supports_streaming: true, |
| 30 | + supports_caching: true, |
| 31 | + max_context_tokens: 8192, |
| 32 | + supported_models: vec!["full-stack-model".to_string()], |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + async fn invoke(&self, request: AiRequest) -> mimi_cli::ai::AdapterResult<AiResponse> { |
| 37 | + // Simulate a full-stack response that flows through the entire pipeline |
| 38 | + let content = if request.prompt.to_lowercase().contains("rust") { |
| 39 | + "Rust is a systems programming language that emphasizes memory safety and concurrency." |
| 40 | + .to_string() |
| 41 | + } else { |
| 42 | + format!("Response to query: {}", request.prompt) |
| 43 | + }; |
| 44 | + |
| 45 | + Ok(AiResponse { |
| 46 | + content, |
| 47 | + model: "full-stack-model".to_string(), |
| 48 | + tokens_used: 150, |
| 49 | + cached: false, |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + async fn health_check(&self) -> mimi_cli::ai::AdapterResult<()> { |
| 54 | + Ok(()) |
| 55 | + } |
| 56 | + |
| 57 | + async fn cleanup(&self) -> mimi_cli::ai::AdapterResult<()> { |
| 58 | + Ok(()) |
| 59 | + } |
| 60 | + |
| 61 | + fn adapter_name(&self) -> String { |
| 62 | + self.name.clone() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +struct TestMessageBus; |
| 67 | +struct TestMimiCore; |
| 68 | + |
| 69 | +async fn start_test_message_bus() -> TestMessageBus { |
| 70 | + TestMessageBus |
| 71 | +} |
| 72 | + |
| 73 | +async fn start_mimi_core() -> TestMimiCore { |
| 74 | + TestMimiCore |
| 75 | +} |
| 76 | + |
| 77 | +async fn setup_adapters() -> Arc<AdapterRegistry> { |
| 78 | + let registry = AdapterRegistry::new(); |
| 79 | + let adapter: SharedAdapter = Arc::new(Mutex::new(FullStackAdapter::new("full-stack"))); |
| 80 | + |
| 81 | + // Register adapter for full-stack testing |
| 82 | + registry |
| 83 | + .register_with_health("full-stack".to_string(), adapter) |
| 84 | + .await |
| 85 | + .unwrap(); |
| 86 | + |
| 87 | + registry |
| 88 | +} |
| 89 | + |
| 90 | +async fn send_cli_command_end_to_end( |
| 91 | + _cmd: &str, |
| 92 | + prompt: &str, |
| 93 | + registry: &Arc<AdapterRegistry>, |
| 94 | +) -> Result<AiResponse, String> { |
| 95 | + // Simulate CLI command flowing through the entire stack: |
| 96 | + // CLI parse → Message Bus publish → Mimi core subscribe → Adapter invoke → Response publish → CLI receive |
| 97 | + |
| 98 | + let request = AiRequest { |
| 99 | + prompt: prompt.to_string(), |
| 100 | + model: None, |
| 101 | + temperature: Some(0.7), |
| 102 | + max_tokens: Some(256), |
| 103 | + system_context: Some("You are a helpful assistant".to_string()), |
| 104 | + }; |
| 105 | + |
| 106 | + // Get best available adapter from registry (simulating Mimi core adapter selection) |
| 107 | + let best = registry |
| 108 | + .get_best_available() |
| 109 | + .await |
| 110 | + .map_err(|e| format!("Adapter selection failed: {:?}", e))?; |
| 111 | + |
| 112 | + // Invoke adapter (simulating message bus invocation) |
| 113 | + let response = best |
| 114 | + .lock() |
| 115 | + .await |
| 116 | + .invoke(request) |
| 117 | + .await |
| 118 | + .map_err(|e| format!("Adapter invocation failed: {:?}", e))?; |
| 119 | + |
| 120 | + Ok(response) |
| 121 | +} |
| 122 | + |
| 123 | +#[tokio::test] |
| 124 | +async fn test_full_stack_cli_to_adapter_integration() { |
| 125 | + // Setup: Start all components |
| 126 | + let _bus = start_test_message_bus().await; |
| 127 | + let _mimi = start_mimi_core().await; |
| 128 | + let registry = setup_adapters().await; |
| 129 | + |
| 130 | + // Execute: Send CLI command that flows through entire stack |
| 131 | + // CLI parse → Message Bus publish → Mimi core subscribe → Adapter invoke → Response publish → CLI receive |
| 132 | + let response = send_cli_command_end_to_end("query", "Explain Rust", ®istry).await; |
| 133 | + |
| 134 | + // Verify: Complete response from adapter is received |
| 135 | + assert!(response.is_ok()); |
| 136 | + let resp = response.unwrap(); |
| 137 | + assert!(resp.content.contains("Rust")); |
| 138 | + assert!(!resp.model.is_empty()); |
| 139 | + assert_eq!(resp.model, "full-stack-model"); |
| 140 | + assert!(resp.tokens_used > 0); |
| 141 | +} |
| 142 | + |
| 143 | +#[tokio::test] |
| 144 | +async fn test_full_stack_concurrent_requests() { |
| 145 | + // Setup |
| 146 | + let _bus = start_test_message_bus().await; |
| 147 | + let _mimi = start_mimi_core().await; |
| 148 | + let registry = setup_adapters().await; |
| 149 | + |
| 150 | + // Execute: Send 10 concurrent requests through the full stack |
| 151 | + let mut handles = vec![]; |
| 152 | + for i in 0..10 { |
| 153 | + let reg = registry.clone(); |
| 154 | + let handle = tokio::spawn(async move { |
| 155 | + send_cli_command_end_to_end("query", &format!("Question {}", i), ®).await |
| 156 | + }); |
| 157 | + handles.push(handle); |
| 158 | + } |
| 159 | + |
| 160 | + // Wait for all to complete |
| 161 | + let results: Vec<_> = join_all(handles).await; |
| 162 | + |
| 163 | + // Verify: All requests succeeded |
| 164 | + for result in results { |
| 165 | + assert!(result.is_ok()); |
| 166 | + let resp = result.unwrap(); |
| 167 | + assert!(resp.is_ok()); |
| 168 | + let response = resp.unwrap(); |
| 169 | + assert!(!response.content.is_empty()); |
| 170 | + assert!(!response.model.is_empty()); |
| 171 | + assert!(response.tokens_used > 0); |
| 172 | + } |
| 173 | +} |
0 commit comments