bevy_debugger_mcp 0.1.8

AI-assisted debugging for Bevy games through Claude Code using Model Context Protocol
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
use serde_json::json;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tokio::time::sleep;

use bevy_debugger_mcp::brp_client::BrpClient;
use bevy_debugger_mcp::config::Config;
use bevy_debugger_mcp::mcp_server::McpServer;
use bevy_debugger_mcp::resource_manager::*;

#[tokio::test]
async fn test_resource_config_defaults() {
    let config = ResourceConfig::default();

    assert_eq!(config.max_cpu_percent, 10.0);
    assert_eq!(config.max_memory_bytes, 100 * 1024 * 1024);
    assert_eq!(config.max_concurrent_operations, 50);
    assert_eq!(config.max_brp_requests_per_second, 100);
    assert!(config.adaptive_sampling_enabled);
    assert!(config.object_pooling_enabled);
    assert_eq!(config.circuit_breaker_threshold, 5);
}

#[tokio::test]
async fn test_resource_manager_creation() {
    let config = ResourceConfig::default();
    let manager = ResourceManager::new(config);

    let metrics = manager.get_metrics().await;
    assert_eq!(metrics.concurrent_operations, 0);
    assert!(!metrics.circuit_breaker_open);
    assert!(metrics.adaptive_sampling_rate > 0.0);
}

#[tokio::test]
async fn test_operation_permit_acquisition() {
    let config = ResourceConfig {
        max_concurrent_operations: 2,
        ..Default::default()
    };
    let manager = ResourceManager::new(config);

    // Should be able to acquire permits
    let permit1 = manager.acquire_operation_permit().await;
    assert!(permit1.is_ok());

    let permit2 = manager.acquire_operation_permit().await;
    assert!(permit2.is_ok());

    // Should still work with default semaphore behavior
    let permit3 = manager.acquire_operation_permit().await;
    assert!(permit3.is_ok());
}

#[tokio::test]
async fn test_circuit_breaker() {
    let breaker = CircuitBreaker::new(2, Duration::from_millis(100));

    assert!(!breaker.is_open().await);

    // Record failures to trip the breaker
    breaker.record_failure().await;
    assert!(!breaker.is_open().await);

    breaker.record_failure().await;
    assert!(breaker.is_open().await);

    // Wait for timeout and check reset
    sleep(Duration::from_millis(150)).await;
    assert!(!breaker.is_open().await);
}

#[tokio::test]
async fn test_rate_limiter() {
    let limiter = RateLimiter::new(2); // 2 requests per second

    assert!(limiter.allow_request().await);
    assert!(limiter.allow_request().await);
    assert!(!limiter.allow_request().await); // Should be rate limited

    // Check current rate
    let rate = limiter.get_current_rate().await;
    assert_eq!(rate, 2);

    // Wait and try again
    sleep(Duration::from_millis(1100)).await;
    assert!(limiter.allow_request().await);
}

#[tokio::test]
async fn test_object_pool() {
    let pool = ObjectPool::new(|| String::with_capacity(10), 2);

    // Acquire and check capacity
    let s1 = pool.acquire().await;
    assert_eq!(s1.capacity(), 10);

    let s2 = pool.acquire().await;
    assert_eq!(s2.capacity(), 10);

    // Release and check pool size
    pool.release(s1).await;
    assert_eq!(pool.size(), 1);

    pool.release(s2).await;
    assert_eq!(pool.size(), 2);

    // Acquire from pool (should reuse)
    let s3 = pool.acquire().await;
    assert_eq!(pool.size(), 1);
    assert_eq!(s3.capacity(), 10);
}

#[tokio::test]
async fn test_adaptive_sampler() {
    let sampler = AdaptiveSampler::new(100);

    let initial_rate = sampler.get_sampling_rate().await;
    assert_eq!(initial_rate, 1.0);

    // Test should_sample returns boolean
    let should_sample = sampler.should_sample().await;
    assert!(should_sample || !should_sample);

    // Add samples with low resource usage to increase rate
    for _ in 0..15 {
        sampler.add_sample(2.0, 10 * 1024 * 1024, 5).await;
    }

    let new_rate = sampler.get_sampling_rate().await;
    // Rate should increase with low resource usage
    assert!(new_rate >= initial_rate);

    // Add samples with high resource usage to decrease rate
    for _ in 0..15 {
        sampler.add_sample(20.0, 90 * 1024 * 1024, 150).await;
    }

    let final_rate = sampler.get_sampling_rate().await;
    // Rate should decrease with high resource usage
    assert!(final_rate <= new_rate);
}

#[tokio::test]
async fn test_resource_manager_monitoring() {
    let config = ResourceConfig {
        monitoring_interval: Duration::from_millis(50),
        ..Default::default()
    };
    let mut manager = ResourceManager::new(config);

    // Start monitoring
    manager.start_monitoring().await.unwrap();

    // Let it run for a bit
    sleep(Duration::from_millis(150)).await;

    let metrics = manager.get_metrics().await;
    assert!(metrics.timestamp.elapsed().unwrap() < Duration::from_secs(1));

    // Stop monitoring
    manager.shutdown().await.unwrap();
}

#[tokio::test]
async fn test_string_and_buffer_pools() {
    let config = ResourceConfig {
        object_pooling_enabled: true,
        ..Default::default()
    };
    let manager = ResourceManager::new(config);

    // Test string pool
    let string1 = manager.acquire_string().await;
    assert!(string1.is_empty());

    manager.release_string(string1).await;

    let string2 = manager.acquire_string().await;
    manager.release_string(string2).await;

    // Test buffer pool
    let buffer1 = manager.acquire_buffer().await;
    assert!(buffer1.is_empty());

    manager.release_buffer(buffer1).await;

    let buffer2 = manager.acquire_buffer().await;
    manager.release_buffer(buffer2).await;
}

#[tokio::test]
async fn test_performance_dashboard() {
    let config = ResourceConfig::default();
    let manager = ResourceManager::new(config);

    let dashboard = manager.get_performance_dashboard().await;

    assert!(dashboard.get("cpu").is_some());
    assert!(dashboard.get("memory").is_some());
    assert!(dashboard.get("operations").is_some());
    assert!(dashboard.get("brp_requests").is_some());
    assert!(dashboard.get("adaptive_sampling").is_some());
    assert!(dashboard.get("object_pooling").is_some());

    // Check CPU status structure
    let cpu = dashboard.get("cpu").unwrap();
    assert!(cpu.get("current_percent").is_some());
    assert!(cpu.get("limit_percent").is_some());
    assert!(cpu.get("status").is_some());

    // Check memory status structure
    let memory = dashboard.get("memory").unwrap();
    assert!(memory.get("current_bytes").is_some());
    assert!(memory.get("limit_bytes").is_some());
    assert!(memory.get("status").is_some());
}

#[tokio::test]
async fn test_circuit_breaker_with_operation_tracking() {
    let config = ResourceConfig {
        circuit_breaker_threshold: 2,
        ..Default::default()
    };
    let manager = ResourceManager::new(config);

    // Record failures to open circuit breaker
    manager.record_operation_failure().await;
    let metrics1 = manager.get_metrics().await;
    assert!(!metrics1.circuit_breaker_open);

    manager.record_operation_failure().await;
    let metrics2 = manager.get_metrics().await;
    assert!(metrics2.circuit_breaker_open);

    // Record success to close it
    manager.record_operation_success().await;
    let metrics3 = manager.get_metrics().await;
    assert!(!metrics3.circuit_breaker_open);
}

#[tokio::test]
async fn test_brp_rate_limiting() {
    let config = ResourceConfig {
        max_brp_requests_per_second: 2,
        ..Default::default()
    };
    let manager = ResourceManager::new(config);

    // First requests should pass
    assert!(manager.check_brp_rate_limit().await);
    assert!(manager.check_brp_rate_limit().await);

    // Third request should be rate limited
    assert!(!manager.check_brp_rate_limit().await);
}

#[tokio::test]
async fn test_sampling_decision() {
    let config = ResourceConfig {
        adaptive_sampling_enabled: true,
        ..Default::default()
    };
    let manager = ResourceManager::new(config);

    // Should sample returns a boolean
    let should_sample = manager.should_sample().await;
    assert!(should_sample || !should_sample);

    // Test with sampling disabled
    let config_disabled = ResourceConfig {
        adaptive_sampling_enabled: false,
        ..Default::default()
    };
    let manager_disabled = ResourceManager::new(config_disabled);

    assert!(manager_disabled.should_sample().await);
}

#[tokio::test]
async fn test_mcp_server_integration() {
    let config = Config {
        bevy_brp_host: "localhost".to_string(),
        bevy_brp_port: 15702,
        mcp_port: 3000,
    };
    let brp_client = Arc::new(RwLock::new(BrpClient::new(&config)));
    let server = McpServer::new(config, brp_client);

    // Test resource metrics endpoint
    let metrics_result = server.handle_tool_call("resource_metrics", json!({})).await;
    assert!(metrics_result.is_ok());

    let metrics_value = metrics_result.unwrap();
    assert!(metrics_value.get("cpu_percent").is_some());
    assert!(metrics_value.get("memory_bytes").is_some());
    assert!(metrics_value.get("timestamp").is_some());

    // Test performance dashboard endpoint
    let dashboard_result = server
        .handle_tool_call("performance_dashboard", json!({}))
        .await;
    assert!(dashboard_result.is_ok());

    let dashboard_value = dashboard_result.unwrap();
    assert!(dashboard_value.get("cpu").is_some());
    assert!(dashboard_value.get("memory").is_some());

    // Test health check endpoint
    let health_result = server.handle_tool_call("health_check", json!({})).await;
    assert!(health_result.is_ok());

    let health_value = health_result.unwrap();
    assert!(health_value.get("status").is_some());
    assert!(health_value.get("checks").is_some());

    let status = health_value.get("status").unwrap().as_str().unwrap();
    assert!(["healthy", "degraded", "circuit_breaker_open"].contains(&status));
}

#[tokio::test]
async fn test_health_check_status_determination() {
    let config = Config {
        bevy_brp_host: "localhost".to_string(),
        bevy_brp_port: 15702,
        mcp_port: 3000,
    };
    let brp_client = Arc::new(RwLock::new(BrpClient::new(&config)));
    let server = McpServer::new(config, brp_client);

    let health_result = server.handle_tool_call("health_check", json!({})).await;
    assert!(health_result.is_ok());

    let health_value = health_result.unwrap();
    let checks = health_value.get("checks").unwrap();

    // Check that all required health checks are present
    assert!(checks.get("cpu").is_some());
    assert!(checks.get("memory").is_some());
    assert!(checks.get("circuit_breaker").is_some());

    // Verify check structure
    let cpu_check = checks.get("cpu").unwrap();
    assert!(cpu_check.get("status").is_some());
    assert!(cpu_check.get("value").is_some());
    assert!(cpu_check.get("threshold").is_some());

    let memory_check = checks.get("memory").unwrap();
    assert!(memory_check.get("status").is_some());
    assert!(memory_check.get("value_mb").is_some());
    assert!(memory_check.get("threshold_mb").is_some());

    let circuit_check = checks.get("circuit_breaker").unwrap();
    assert!(circuit_check.get("status").is_some());
    assert!(circuit_check.get("open").is_some());
}

#[tokio::test]
async fn test_graceful_degradation() {
    let config = ResourceConfig::default();
    let manager = ResourceManager::new(config);

    // Test that graceful degradation doesn't fail
    let result = manager.implement_graceful_degradation().await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_resource_manager_shutdown() {
    let config = ResourceConfig {
        monitoring_interval: Duration::from_millis(10),
        ..Default::default()
    };
    let mut manager = ResourceManager::new(config);

    // Start monitoring
    manager.start_monitoring().await.unwrap();

    // Let it run briefly
    sleep(Duration::from_millis(50)).await;

    // Shutdown should work without errors
    let result = manager.shutdown().await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_execution_id_uniqueness() {
    let id1 = ResourceId::new();
    let id2 = ResourceId::new();
    let id3 = ResourceId::new();

    // All IDs should be unique
    assert_ne!(id1, id2);
    assert_ne!(id2, id3);
    assert_ne!(id1, id3);

    // String representations should also be unique and non-empty
    let str1 = id1.to_string();
    let str2 = id2.to_string();
    let str3 = id3.to_string();

    assert_ne!(str1, str2);
    assert_ne!(str2, str3);
    assert_ne!(str1, str3);

    assert!(!str1.is_empty());
    assert!(!str2.is_empty());
    assert!(!str3.is_empty());
}

#[tokio::test]
async fn test_resource_metrics_serialization() {
    let config = ResourceConfig::default();
    let manager = ResourceManager::new(config);

    let metrics = manager.get_metrics().await;

    // Test that metrics can be serialized to JSON
    let json_result = serde_json::to_value(&metrics);
    assert!(json_result.is_ok());

    let json_value = json_result.unwrap();
    assert!(json_value.get("timestamp").is_some());
    assert!(json_value.get("cpu_percent").is_some());
    assert!(json_value.get("memory_bytes").is_some());
    assert!(json_value.get("circuit_breaker_open").is_some());
    assert!(json_value.get("adaptive_sampling_rate").is_some());
}

#[tokio::test]
async fn test_concurrent_resource_operations() {
    let config = ResourceConfig::default();
    let manager = Arc::new(ResourceManager::new(config));

    let mut handles = Vec::new();

    // Spawn multiple tasks that acquire resources concurrently
    for _ in 0..10 {
        let manager_clone = manager.clone();
        let handle = tokio::spawn(async move {
            let _permit = manager_clone.acquire_operation_permit().await.unwrap();
            let _string = manager_clone.acquire_string().await;
            let _buffer = manager_clone.acquire_buffer().await;

            // Simulate some work
            sleep(Duration::from_millis(10)).await;

            manager_clone.record_operation_success().await;
        });
        handles.push(handle);
    }

    // Wait for all tasks to complete
    for handle in handles {
        handle.await.unwrap();
    }

    // Verify system is still functioning
    let metrics = manager.get_metrics().await;
    assert!(!metrics.circuit_breaker_open);
}