midstream 0.2.0

Real-time LLM streaming with inflight analysis
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
//! 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<String> = RealtimeScheduler::default();
    let comparator: TemporalComparator<String> = TemporalComparator::new(100, 1000);

    // Historical execution patterns
    let mut seq1: Sequence<String> = Sequence::new();
    seq1.push("init".to_string(), 0);
    seq1.push("process".to_string(), 100);
    seq1.push("complete".to_string(), 200);

    let mut seq2: Sequence<String> = 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<f64> = 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<String> = TemporalComparator::new(100, 1000);
    let mut solver = TemporalNeuralSolver::new(1000, 500, VerificationStrictness::Medium);

    // Create sequences representing system states
    let mut seq1: Sequence<String> = 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<String> = 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<String> = 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<Vec<f64>> = (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<String> = 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<i32> = TemporalComparator::new(100, 100);
    let mut long_seq: Sequence<i32> = Sequence::new();
    for i in 0..200 {
        long_seq.push(i, i as u64);
    }
    let mut short_seq: Sequence<i32> = 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<u64> = 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<i32> = TemporalComparator::new(1000, 10000);

    let mut seq1: Sequence<i32> = Sequence::new();
    let mut seq2: Sequence<i32> = 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<f64> = 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<String> = 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<i32> = TemporalComparator::new(100, 1000);

    let mut seq1: Sequence<i32> = Sequence::new();
    let mut seq2: Sequence<i32> = 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<String> = 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");
    }
}