use crate::server::brp_messages::{DebugCommand, ComponentValue, ComponentFilter, FilterOp, QueryFilter};
use crate::server::checkpoint::{Checkpoint, CheckpointManager, CheckpointConfig};
use crate::server::config::Config;
use crate::server::error::Result;
use crate::server::memory_profiler::{MemoryProfiler, MemoryProfilerConfig};
use crate::server::query_parser::{RegexQueryParser, QueryParser};
use crate::server::semantic_analyzer::SemanticAnalyzer;
use crate::server::state_diff::{StateDiff, StateSnapshot, FuzzyCompareConfig, GameRules};
use serde_json::json;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::time::{timeout, Duration};
#[tokio::test]
async fn test_memory_profiler_no_panic() {
let config = MemoryProfilerConfig::default();
let profiler = MemoryProfiler::new(config);
for i in 0..100 {
if let Ok(alloc_id) = profiler.record_allocation(
&format!("System{}", i),
usize::MAX / 1000, Some(vec!["backtrace".to_string(); 1000])
) {
let _ = profiler.record_deallocation(alloc_id);
}
profiler.update_entity_count(usize::MAX / 1000);
if let Ok(_) = profiler.take_snapshot().await {
}
if let Ok(_) = profiler.detect_leaks().await {
}
}
}
#[tokio::test]
async fn test_query_parser_no_panic() {
let parser = match RegexQueryParser::new() {
Ok(p) => p,
Err(_) => return, };
let malformed_queries = vec![
String::new(), "|||||||||||".to_string(), "find entities with \0\0\0".to_string(), "find entities with ".repeat(1000), "find entities with component Component".repeat(100), "show entity 18446744073709551615".to_string(), "show entity -1".to_string(), "show entity abc".to_string(), "find 999999999999999999999999999999999999 entities".to_string(), ];
for query in malformed_queries {
let _ = parser.parse(&query); }
}
#[tokio::test]
async fn test_semantic_analyzer_no_panic() {
let analyzer = match SemanticAnalyzer::new() {
Ok(a) => a,
Err(_) => return, };
let extreme_queries = vec![
"\0".repeat(10000), "🚀".repeat(1000), "find".repeat(10000), String::new(), " ".repeat(10000), "find stuck entities".repeat(1000), ];
for query in extreme_queries {
let _ = analyzer.analyze(&query); }
}
#[tokio::test]
async fn test_checkpoint_manager_no_panic() {
let config = CheckpointConfig::default();
let manager = Arc::new(CheckpointManager::new(config));
let mut handles = vec![];
for i in 0..50 {
let manager_clone = Arc::clone(&manager);
let handle = tokio::spawn(async move {
for j in 0..10 {
let checkpoint_data = json!({"iteration": i * 10 + j});
let _ = manager_clone
.create_checkpoint(Checkpoint::new(
&format!("test_checkpoint_{}", i * 10 + j),
&format!("Test description {}", i * 10 + j),
"test_operation",
"test_component",
checkpoint_data,
))
.await;
}
});
handles.push(handle);
}
for handle in handles {
let _ = timeout(Duration::from_secs(5), handle).await;
}
let _ = manager.get_statistics().await;
let _ = manager.list_checkpoints().await;
let _ = manager.list_checkpoints_by_operation("test_operation").await;
}
#[tokio::test]
async fn test_state_diff_no_panic() {
let fuzzy_config = FuzzyCompareConfig::default();
let game_rules = GameRules::default();
let diff_engine = StateDiff::with_config(fuzzy_config, game_rules);
let mut components1 = HashMap::new();
let mut components2 = HashMap::new();
components1.insert("Position".to_string(), ComponentValue::String("10,20,30".to_string()));
components1.insert("Velocity".to_string(), ComponentValue::String("1,2,3".to_string()));
components2.insert("Position".to_string(), ComponentValue::String("15,25,35".to_string()));
components2.insert("Health".to_string(), json!(100.0));
let entity1 = crate::server::brp_messages::EntityData {
id: 1,
components: components1,
};
let entity2 = crate::server::brp_messages::EntityData {
id: 1,
components: components2,
};
let snapshot1 = StateSnapshot::new(vec![entity1], 1);
let snapshot2 = StateSnapshot::new(vec![entity2], 2);
let _ = diff_engine.diff_snapshots(&snapshot1, &snapshot2);
}
#[tokio::test]
async fn test_random_stress_no_panic() {
use rand::Rng;
let mut rng = rand::thread_rng();
for _ in 0..100 {
let random_str: String = (0..rng.random_range(1..1000))
.map(|_| rng.random_range(0..127) as u8 as char)
.collect();
if let Ok(parser) = RegexQueryParser::new() {
let _ = parser.parse(&random_str);
}
if let Ok(analyzer) = SemanticAnalyzer::new() {
let _ = analyzer.analyze(&random_str);
}
let random_num = rng.random::<u64>();
let query = format!("show entity {}", random_num);
if let Ok(parser) = RegexQueryParser::new() {
let _ = parser.parse(&query);
}
}
}