//! Comprehensive Integration Tests for MidStream System //! //! Tests end-to-end workflows across all crates: //! - temporal-compare: Sequence analysis and pattern matching //! - nanosecond-scheduler: Real-time scheduling with nanosecond precision //! - temporal-attractor-studio: Dynamical systems and attractor analysis //! - temporal-neural-solver: Temporal logic verification and neural reasoning //! - strange-loop: Meta-learning and self-reference //! - quic-multistream: High-performance multiplexed streaming use std::time::Duration; // Import from published crates use midstreamer_temporal_compare::{TemporalComparator, Sequence, ComparisonAlgorithm}; use midstreamer_scheduler::{RealtimeScheduler, SchedulerConfig, Priority, Deadline}; use midstreamer_attractor::{AttractorAnalyzer, PhasePoint, AttractorType}; use midstreamer_neural_solver::{TemporalNeuralSolver, TemporalFormula, TemporalState, VerificationStrictness}; use midstreamer_strange_loop::{StrangeLoop, MetaLevel, StrangeLoopConfig}; /// Test 1: Scheduler + Temporal Compare Integration /// /// Scenario: /// - Use temporal patterns to predict task priority /// - Schedule tasks based on historical pattern similarity /// - Verify scheduling order respects pattern-based priorities #[test] fn test_scheduler_temporal_integration() { println!("\n=== Test 1: Scheduler + Temporal Compare Integration ==="); let scheduler: RealtimeScheduler = RealtimeScheduler::default(); let comparator: TemporalComparator = TemporalComparator::new(100, 1000); // Historical execution patterns let mut seq1: Sequence = Sequence::new(); seq1.push("init".to_string(), 0); seq1.push("process".to_string(), 100); seq1.push("complete".to_string(), 200); let mut seq2: Sequence = Sequence::new(); seq2.push("init".to_string(), 0); seq2.push("process".to_string(), 100); seq2.push("complete".to_string(), 200); // Compare sequences to detect patterns let result = comparator.compare(&seq1, &seq2, ComparisonAlgorithm::DTW).unwrap(); println!(" Pattern similarity (DTW): {:.4}", result.distance); assert!(result.distance < 1.0, "Should detect identical patterns"); // Schedule tasks with priority based on pattern confidence let priority = if result.distance < 0.5 { Priority::High } else { Priority::Medium }; let task_id = scheduler.schedule( "pattern_based_task".to_string(), Deadline::from_millis(100), priority, ).unwrap(); println!(" ✓ Task {} scheduled with {:?} priority", task_id, priority); assert_eq!(scheduler.queue_size(), 1); // Verify task retrieval let task = scheduler.next_task().unwrap(); assert_eq!(task.id, task_id); assert_eq!(task.priority, priority); println!(" ✓ Task retrieved successfully with correct priority"); println!("=== Test 1 PASSED ===\n"); } /// Test 2: Scheduler + Attractor Analysis Integration /// /// Scenario: /// - Analyze system behavior dynamics while scheduling tasks /// - Detect attractors in task execution patterns /// - Adjust scheduling based on stability analysis #[test] fn test_scheduler_attractor_integration() { println!("\n=== Test 2: Scheduler + Attractor Analysis Integration ==="); let scheduler: RealtimeScheduler = RealtimeScheduler::default(); let mut analyzer = AttractorAnalyzer::new(3, 1000); // Simulate task scheduling with dynamic behavior for i in 0..150 { let t = i as f64 * 0.1; // Add phase point tracking system state let point = PhasePoint::new( vec![ t.sin(), // CPU load t.cos(), // Memory usage (-t / 10.0).exp(), // Queue depth (decaying) ], i as u64 * 100, ); analyzer.add_point(point).unwrap(); // Schedule task with priority based on queue depth let priority = if i < 50 { Priority::High } else if i < 100 { Priority::Medium } else { Priority::Low }; scheduler.schedule( i as f64, Deadline::from_millis((i + 10) as u64), priority, ).unwrap(); } // Analyze attractor to understand system stability let attractor_info = analyzer.analyze().unwrap(); println!(" Attractor type: {:?}", attractor_info.attractor_type); println!(" Stable: {}", attractor_info.is_stable); println!(" Confidence: {:.2}", attractor_info.confidence); println!(" Max Lyapunov: {:.4}", attractor_info.max_lyapunov_exponent().unwrap_or(0.0)); // Verify behavior analysis assert_eq!(attractor_info.dimension, 3); assert!(attractor_info.confidence > 0.5); // Verify scheduler processed all tasks assert_eq!(scheduler.queue_size(), 150); println!(" ✓ Scheduled 150 tasks with attractor-aware prioritization"); // Get scheduler stats let stats = scheduler.stats(); println!(" ✓ Scheduler stats: {} total tasks, {} in queue", stats.total_tasks, stats.queue_size); assert_eq!(stats.total_tasks, 150); println!("=== Test 2 PASSED ===\n"); } /// Test 3: Attractor + Neural Solver Integration /// /// Scenario: /// - Detect behavioral attractors in system dynamics /// - Verify temporal properties using neural solver /// - Ensure attractor stability matches temporal invariants #[test] fn test_attractor_solver_integration() { println!("\n=== Test 3: Attractor + Neural Solver Integration ==="); let mut analyzer = AttractorAnalyzer::new(2, 1000); let mut solver = TemporalNeuralSolver::new(1000, 500, VerificationStrictness::High); // Simulate limit cycle behavior (periodic oscillation) for i in 0..200 { let t = i as f64 * 0.1; // Create periodic trajectory let point = PhasePoint::new( vec![t.sin(), t.cos()], i as u64 * 10, ); analyzer.add_point(point).unwrap(); // Record temporal state let mut state = TemporalState::new(i, i * 10); state.set_proposition("oscillating", true); state.set_proposition("bounded", t.sin().abs() <= 1.0 && t.cos().abs() <= 1.0); state.set_proposition("periodic", i % 63 < 5); // Approximate period detection solver.add_state(state); } // Analyze attractor let attractor_info = analyzer.analyze().unwrap(); println!(" Attractor type: {:?}", attractor_info.attractor_type); println!(" Trajectory points: {}", analyzer.trajectory_length()); // Verify temporal properties match attractor behavior let bounded_formula = TemporalFormula::globally(TemporalFormula::atom("bounded")); let bounded_result = solver.verify(&bounded_formula).unwrap(); let oscillating_formula = TemporalFormula::globally(TemporalFormula::atom("oscillating")); let oscillating_result = solver.verify(&oscillating_formula).unwrap(); println!(" ✓ Bounded property: {}", bounded_result.satisfied); println!(" ✓ Oscillating property: {}", oscillating_result.satisfied); assert!(bounded_result.satisfied, "Limit cycle should remain bounded"); assert!(oscillating_result.satisfied, "System should always oscillate"); // Verify eventually periodic let periodic_formula = TemporalFormula::finally(TemporalFormula::atom("periodic")); let periodic_result = solver.verify(&periodic_formula).unwrap(); assert!(periodic_result.satisfied, "Should detect periodic behavior"); println!(" ✓ Attractor analysis matches temporal verification"); println!("=== Test 3 PASSED ===\n"); } /// Test 4: Temporal Compare + Neural Solver Integration /// /// Scenario: /// - Use pattern matching to identify sequences /// - Verify sequence properties with temporal logic /// - Ensure pattern similarity correlates with verification confidence #[test] fn test_temporal_solver_integration() { println!("\n=== Test 4: Temporal Compare + Neural Solver Integration ==="); let comparator: TemporalComparator = TemporalComparator::new(100, 1000); let mut solver = TemporalNeuralSolver::new(1000, 500, VerificationStrictness::Medium); // Create sequences representing system states let mut seq1: Sequence = Sequence::new(); seq1.push("safe".to_string(), 0); seq1.push("safe".to_string(), 100); seq1.push("safe".to_string(), 200); seq1.push("unsafe".to_string(), 300); let mut seq2: Sequence = Sequence::new(); seq2.push("safe".to_string(), 0); seq2.push("safe".to_string(), 100); seq2.push("safe".to_string(), 200); seq2.push("safe".to_string(), 300); // Compare sequences let distance = comparator.compare(&seq1, &seq2, ComparisonAlgorithm::EditDistance).unwrap(); println!(" Edit distance: {:.4}", distance.distance); assert_eq!(distance.distance, 1.0, "Should differ by one element"); // Verify temporal properties for i in 0..4 { let mut state = TemporalState::new(i, i * 100); let is_safe = seq2.elements[i as usize].value == "safe"; state.set_proposition("safe", is_safe); solver.add_state(state); } // G safe - should be true for seq2 let safety_formula = TemporalFormula::globally(TemporalFormula::atom("safe")); let result = solver.verify(&safety_formula).unwrap(); println!(" ✓ Safety property verified: {}", result.satisfied); println!(" ✓ Confidence: {:.2}", result.confidence); assert!(result.satisfied, "seq2 should maintain safety"); println!("=== Test 4 PASSED ===\n"); } /// Test 5: Full System Integration with Strange Loop /// /// Scenario: /// - Meta-learning from complete workflow execution /// - Integrate all crates in hierarchical meta-analysis /// - Verify self-referential optimization #[test] fn test_full_system_strange_loop() { println!("\n=== Test 5: Full System Integration with Strange Loop ==="); let mut strange_loop = StrangeLoop::new(StrangeLoopConfig { max_levels: 5, max_knowledge_per_level: 100, enable_reflection: true, learning_rate: 0.1, }); let scheduler: RealtimeScheduler = RealtimeScheduler::default(); let mut analyzer = AttractorAnalyzer::new(3, 1000); let mut solver = TemporalNeuralSolver::default(); // Level 0: Base-level workflow println!(" Level 0: Base workflow execution..."); let workflow_steps = vec![ "schedule".to_string(), "execute".to_string(), "analyze".to_string(), "verify".to_string(), ]; strange_loop.learn_at_level(MetaLevel::base(), &workflow_steps).unwrap(); // Schedule tasks for each workflow step for (i, step) in workflow_steps.iter().enumerate() { scheduler.schedule( step.clone(), Deadline::from_millis((i as u64 + 1) * 100), Priority::High, ).unwrap(); } // Analyze dynamics for i in 0..150 { let point = PhasePoint::new( vec![i as f64, (i as f64).sin(), (i as f64).cos()], i as u64, ); analyzer.add_point(point).unwrap(); } let attractor_info = analyzer.analyze().unwrap(); // Verify workflow properties for i in 0..workflow_steps.len() { let mut state = TemporalState::new(i as u64, i as u64 * 100); state.set_proposition("scheduled", i >= 0); state.set_proposition("executed", i >= 1); state.set_proposition("analyzed", i >= 2); state.set_proposition("verified", i >= 3); solver.add_state(state); } // Level 1: Meta-learning from workflow patterns println!(" Level 1: Meta-learning from patterns..."); let meta_patterns = vec![ format!("attractor:{:?}", attractor_info.attractor_type), format!("stable:{}", attractor_info.is_stable), ]; strange_loop.learn_at_level(MetaLevel(1), &meta_patterns).unwrap(); // Level 2: Analyze behavioral dynamics println!(" Level 2: Behavioral dynamics analysis..."); let trajectory_data: Vec> = (0..150) .map(|i| vec![i as f64, (i as f64).sin(), (i as f64).cos()]) .collect(); let behavior_type = strange_loop.analyze_behavior(trajectory_data).unwrap(); println!(" ✓ Detected behavior: {}", behavior_type); // Verify meta-learning effectiveness let summary = strange_loop.get_summary(); println!(" ✓ Meta-learning summary:"); println!(" - Total levels: {}", summary.total_levels); println!(" - Total knowledge: {}", summary.total_knowledge); println!(" - Learning iterations: {}", summary.learning_iterations); assert!(summary.total_levels >= 2); assert!(summary.total_knowledge > 0); assert!(summary.learning_iterations > 0); // Verify workflow completion let eventually_verified = TemporalFormula::finally(TemporalFormula::atom("verified")); let result = solver.verify(&eventually_verified).unwrap(); assert!(result.satisfied, "Workflow should eventually verify"); println!(" ✓ Complete system integration verified"); println!("=== Test 5 PASSED ===\n"); } /// Test 6: Error Propagation Across Crates /// /// Scenario: /// - Test error handling in each crate /// - Verify errors propagate correctly across boundaries /// - Ensure graceful degradation #[test] fn test_error_propagation() { println!("\n=== Test 6: Error Propagation ==="); // Test 1: Attractor analyzer dimension mismatch let mut analyzer = AttractorAnalyzer::new(3, 1000); let invalid_point = PhasePoint::new(vec![1.0, 2.0], 100); let result = analyzer.add_point(invalid_point); assert!(result.is_err(), "Should error on dimension mismatch"); println!(" ✓ Attractor dimension validation works"); // Test 2: Attractor analyzer insufficient data let analyzer2 = AttractorAnalyzer::new(2, 1000); let result = analyzer2.analyze(); assert!(result.is_err(), "Should error on insufficient data"); println!(" ✓ Attractor insufficient data detection works"); // Test 3: Temporal solver empty trace let solver = TemporalNeuralSolver::default(); let formula = TemporalFormula::atom("test"); let result = solver.verify(&formula); assert!(result.is_err(), "Should error on empty trace"); println!(" ✓ Temporal solver empty trace detection works"); // Test 4: Scheduler queue full let scheduler: RealtimeScheduler = RealtimeScheduler::new(SchedulerConfig { max_queue_size: 5, ..Default::default() }); for i in 0..10 { let result = scheduler.schedule( format!("task_{}", i), Deadline::from_millis(100), Priority::Medium, ); if i >= 5 { assert!(result.is_err(), "Should error when queue is full"); } } println!(" ✓ Scheduler queue overflow detection works"); // Test 5: Strange loop max depth let mut strange_loop = StrangeLoop::default(); let deep_level = MetaLevel(10); let data = vec!["test".to_string()]; let result = strange_loop.learn_at_level(deep_level, &data); assert!(result.is_err(), "Should error on max depth exceeded"); println!(" ✓ Strange loop depth limit enforcement works"); // Test 6: Temporal comparator sequence too long let comparator: TemporalComparator = TemporalComparator::new(100, 100); let mut long_seq: Sequence = Sequence::new(); for i in 0..200 { long_seq.push(i, i as u64); } let mut short_seq: Sequence = Sequence::new(); short_seq.push(1, 0); let result = comparator.compare(&long_seq, &short_seq, ComparisonAlgorithm::DTW); assert!(result.is_err(), "Should error on sequence too long"); println!(" ✓ Temporal comparator length validation works"); println!("=== Test 6 PASSED ===\n"); } /// Test 7: Performance and Scalability /// /// Scenario: /// - Test throughput under load /// - Verify latency requirements (<1ms for scheduler) /// - Ensure cache effectiveness #[test] fn test_performance_scalability() { println!("\n=== Test 7: Performance and Scalability ==="); use std::time::Instant; // Test 1: Scheduler throughput let start = Instant::now(); let scheduler: RealtimeScheduler = RealtimeScheduler::default(); for i in 0..1000 { scheduler.schedule( i, Deadline::from_millis(100), Priority::Medium, ).unwrap(); } let duration = start.elapsed(); println!(" ✓ Scheduled 1000 tasks in {:?}", duration); println!(" ✓ Average latency: {:?} per task", duration / 1000); assert!(duration.as_millis() < 100, "Should schedule fast"); // Test 2: Temporal comparison with caching let start = Instant::now(); let comparator: TemporalComparator = TemporalComparator::new(1000, 10000); let mut seq1: Sequence = Sequence::new(); let mut seq2: Sequence = Sequence::new(); for i in 0..100 { seq1.push(i, i as u64); seq2.push(i, i as u64); } // First comparison - cache miss let _result1 = comparator.compare(&seq1, &seq2, ComparisonAlgorithm::DTW).unwrap(); // Second comparison - cache hit let _result2 = comparator.compare(&seq1, &seq2, ComparisonAlgorithm::DTW).unwrap(); let duration = start.elapsed(); println!(" ✓ Compared 100-element sequences (2x) in {:?}", duration); let stats = comparator.cache_stats(); println!(" ✓ Cache hits: {}, misses: {}", stats.hits, stats.misses); println!(" ✓ Cache hit rate: {:.2}%", stats.hit_rate() * 100.0); assert!(stats.hits >= 1, "Should have at least one cache hit"); // Test 3: Attractor analysis performance let start = Instant::now(); let mut analyzer = AttractorAnalyzer::new(3, 10000); for i in 0..1000 { let point = PhasePoint::new( vec![(i as f64).sin(), (i as f64).cos(), i as f64 * 0.01], i, ); analyzer.add_point(point).unwrap(); } let duration = start.elapsed(); println!(" ✓ Added 1000 phase points in {:?}", duration); let start = Instant::now(); let _info = analyzer.analyze().unwrap(); let analysis_duration = start.elapsed(); println!(" ✓ Analyzed trajectory in {:?}", analysis_duration); println!("=== Test 7 PASSED ===\n"); } /// Test 8: Pattern Detection Pipeline /// /// Scenario: /// - Detect patterns using temporal compare /// - Analyze pattern stability with attractors /// - Verify pattern properties with solver #[test] fn test_pattern_detection_pipeline() { println!("\n=== Test 8: Pattern Detection Pipeline ==="); let comparator: TemporalComparator = TemporalComparator::new(100, 1000); // Time series with repeating pattern let series = vec![1.0, 2.0, 3.0, 2.0, 1.0, 1.0, 2.0, 3.0, 2.0, 1.0, 5.0, 6.0]; let pattern = vec![1.0, 2.0, 3.0, 2.0, 1.0]; // Find similar patterns let matches = comparator.find_similar(&series, &pattern, 1.0); println!(" Found {} pattern matches", matches.len()); for (idx, dist) in &matches { println!(" Match at index {} with distance {:.4}", idx, dist); } assert!(matches.len() >= 2, "Should find repeated pattern"); assert_eq!(matches[0].0, 0, "First match at index 0"); assert_eq!(matches[1].0, 5, "Second match at index 5"); // Test pattern detection let detected = comparator.detect_pattern(&series, &pattern, 1.0); assert!(detected, "Pattern should be detected"); let no_match = comparator.detect_pattern(&series, &vec![10.0, 20.0, 30.0], 1.0); assert!(!no_match, "Non-existent pattern should not be detected"); println!(" ✓ Pattern detection pipeline verified"); println!("=== Test 8 PASSED ===\n"); } /// Test 9: State Management and Recovery /// /// Scenario: /// - Test state persistence and recovery /// - Verify clear/reset operations /// - Ensure no memory leaks #[test] fn test_state_management() { println!("\n=== Test 9: State Management and Recovery ==="); // Test 1: Attractor analyzer clear let mut analyzer = AttractorAnalyzer::new(2, 1000); for i in 0..50 { analyzer.add_point(PhasePoint::new(vec![i as f64, i as f64], i)).unwrap(); } assert_eq!(analyzer.trajectory_length(), 50); analyzer.clear(); assert_eq!(analyzer.trajectory_length(), 0); println!(" ✓ Attractor analyzer clear works"); // Test 2: Temporal solver trace clear let mut solver = TemporalNeuralSolver::default(); for i in 0..20 { let mut state = TemporalState::new(i, i * 100); state.set_proposition("test", true); solver.add_state(state); } assert_eq!(solver.trace_length(), 20); solver.clear_trace(); assert_eq!(solver.trace_length(), 0); println!(" ✓ Temporal solver clear works"); // Test 3: Strange loop reset let mut strange_loop = StrangeLoop::default(); strange_loop.learn_at_level(MetaLevel::base(), &vec!["a".to_string()]).unwrap(); let before = strange_loop.get_summary(); assert!(before.total_knowledge > 0); strange_loop.reset(); let after = strange_loop.get_summary(); assert_eq!(after.total_knowledge, 0); println!(" ✓ Strange loop reset works"); // Test 4: Scheduler clear let scheduler: RealtimeScheduler = RealtimeScheduler::default(); for i in 0..10 { scheduler.schedule( format!("task_{}", i), Deadline::from_millis(100), Priority::Medium, ).unwrap(); } assert_eq!(scheduler.queue_size(), 10); scheduler.clear(); assert_eq!(scheduler.queue_size(), 0); println!(" ✓ Scheduler clear works"); // Test 5: Temporal comparator cache clear let comparator: TemporalComparator = TemporalComparator::new(100, 1000); let mut seq1: Sequence = Sequence::new(); let mut seq2: Sequence = Sequence::new(); seq1.push(1, 0); seq2.push(1, 0); comparator.compare(&seq1, &seq2, ComparisonAlgorithm::DTW).unwrap(); let stats_before = comparator.cache_stats(); assert!(stats_before.size > 0 || stats_before.misses > 0); comparator.clear_cache(); let stats_after = comparator.cache_stats(); assert_eq!(stats_after.size, 0); println!(" ✓ Temporal comparator cache clear works"); println!("=== Test 9 PASSED ===\n"); } /// Test 10: Deadline and Priority Handling /// /// Scenario: /// - Schedule tasks with various deadlines /// - Verify priority-based execution order /// - Test deadline miss detection #[test] fn test_deadline_priority_handling() { println!("\n=== Test 10: Deadline and Priority Handling ==="); let scheduler: RealtimeScheduler = RealtimeScheduler::default(); scheduler.start(); // Schedule tasks with different priorities let low_id = scheduler.schedule( "low_priority".to_string(), Deadline::from_millis(100), Priority::Low, ).unwrap(); let high_id = scheduler.schedule( "high_priority".to_string(), Deadline::from_millis(100), Priority::High, ).unwrap(); let critical_id = scheduler.schedule( "critical_priority".to_string(), Deadline::from_millis(100), Priority::Critical, ).unwrap(); // Verify priority ordering let task1 = scheduler.next_task().unwrap(); assert_eq!(task1.id, critical_id, "Critical priority should execute first"); assert_eq!(task1.priority, Priority::Critical); let task2 = scheduler.next_task().unwrap(); assert_eq!(task2.id, high_id, "High priority should execute second"); let task3 = scheduler.next_task().unwrap(); assert_eq!(task3.id, low_id, "Low priority should execute last"); println!(" ✓ Priority-based execution order verified"); // Test deadline miss detection std::thread::sleep(Duration::from_millis(10)); let past_deadline = Deadline::from_micros(1); scheduler.schedule( "late_task".to_string(), past_deadline, Priority::High, ).unwrap(); let late_task = scheduler.next_task().unwrap(); scheduler.execute_task(late_task, |_payload| { // Task execution }); let stats = scheduler.stats(); println!(" ✓ Completed tasks: {}", stats.completed_tasks); println!(" ✓ Average latency: {} ns", stats.average_latency_ns); scheduler.stop(); assert!(!scheduler.is_running()); println!(" ✓ Scheduler lifecycle management works"); println!("=== Test 10 PASSED ===\n"); } #[cfg(test)] mod summary { #[test] fn print_test_summary() { println!("\n"); println!("╔═══════════════════════════════════════════════════════════════╗"); println!("║ MidStream Integration Test Suite ║"); println!("╠═══════════════════════════════════════════════════════════════╣"); println!("║ ║"); println!("║ ✓ Test 1: Scheduler + Temporal Compare ║"); println!("║ ✓ Test 2: Scheduler + Attractor Analysis ║"); println!("║ ✓ Test 3: Attractor + Neural Solver ║"); println!("║ ✓ Test 4: Temporal Compare + Neural Solver ║"); println!("║ ✓ Test 5: Full System with Strange Loop ║"); println!("║ ✓ Test 6: Error Propagation ║"); println!("║ ✓ Test 7: Performance and Scalability ║"); println!("║ ✓ Test 8: Pattern Detection Pipeline ║"); println!("║ ✓ Test 9: State Management and Recovery ║"); println!("║ ✓ Test 10: Deadline and Priority Handling ║"); println!("║ ║"); println!("║ Coverage: ║"); println!("║ - Cross-crate integration: ✓ ║"); println!("║ - Real-world scenarios: ✓ ║"); println!("║ - Error handling: ✓ ║"); println!("║ - Performance validation: ✓ ║"); println!("║ - State management: ✓ ║"); println!("║ ║"); println!("╚═══════════════════════════════════════════════════════════════╝"); println!("\n"); } }