pmat 3.15.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_recovery_manager() {
        let temp_dir = TempDir::new().expect("internal error");
        let path = temp_dir.path().to_str().expect("internal error");

        let event_config = EventStoreConfig {
            persistence_enabled: false,
            ..Default::default()
        };

        let manager =
            RecoveryManager::<ExampleState>::new(event_config, SnapshotConfig::default(), path)
                .await
                .expect("internal error");

        let initial = ExampleState::default();
        let restored = manager
            .recover_state(initial, None)
            .await
            .expect("internal error");

        assert_eq!(restored.events_to_replay, 0);
    }

    #[test]
    fn test_adaptive_scheduler() {
        let scheduler = AdaptiveSnapshotScheduler::new(SnapshotSchedulerConfig::default());

        // Should not snapshot with few events
        assert!(!scheduler.should_snapshot(100, Duration::from_secs(10)));

        // Should snapshot with many events
        assert!(scheduler.should_snapshot(100_001, Duration::from_secs(10)));

        // Should snapshot after long time
        assert!(scheduler.should_snapshot(100, Duration::from_secs(3601)));
    }

    #[test]
    fn test_scheduler_adaptation() {
        let config = SnapshotSchedulerConfig {
            adaptive_enabled: true,
            recovery_time_target: Duration::from_secs(5),
            ..Default::default()
        };

        let scheduler = AdaptiveSnapshotScheduler::new(config);

        // Record slow recovery
        scheduler.record_recovery(Duration::from_secs(15));

        let new_config = scheduler.get_config();
        assert!(new_config.min_events < 1000); // Should reduce threshold

        // Record fast recovery
        scheduler.record_recovery(Duration::from_secs(1));

        let new_config = scheduler.get_config();
        assert!(new_config.min_events > 500); // Should increase threshold
    }

    #[tokio::test]
    async fn test_parallel_recovery() {
        let temp_dir = TempDir::new().expect("internal error");
        let path = temp_dir.path().to_str().expect("internal error");

        let event_config = EventStoreConfig {
            persistence_enabled: false,
            ..Default::default()
        };

        let parallel =
            ParallelRecovery::<ExampleState>::new(4, event_config, SnapshotConfig::default(), path)
                .await
                .expect("internal error");

        let states = parallel
            .recover_all_partitions(ExampleState::default)
            .await
            .expect("internal error");

        assert_eq!(states.len(), 4);

        let merged = parallel
            .merge_partitions(states)
            .await
            .expect("internal error");
        assert_eq!(merged.last_event_id, 0);
    }

    // ============ SnapshotSchedulerConfig Tests ============

    #[test]
    fn test_snapshot_scheduler_config_default() {
        let config = SnapshotSchedulerConfig::default();
        assert_eq!(config.min_events, 1000);
        assert_eq!(config.max_events, 100_000);
        assert_eq!(config.min_time_between_snapshots, Duration::from_secs(60));
        assert_eq!(config.max_time_between_snapshots, Duration::from_secs(3600));
        assert_eq!(config.recovery_time_target, Duration::from_secs(5));
        assert!(config.adaptive_enabled);
    }

    #[test]
    fn test_snapshot_scheduler_config_clone() {
        let config = SnapshotSchedulerConfig {
            min_events: 500,
            max_events: 50_000,
            min_time_between_snapshots: Duration::from_secs(30),
            max_time_between_snapshots: Duration::from_secs(1800),
            recovery_time_target: Duration::from_secs(10),
            adaptive_enabled: false,
        };
        let cloned = config.clone();
        assert_eq!(cloned.min_events, 500);
        assert_eq!(cloned.max_events, 50_000);
        assert!(!cloned.adaptive_enabled);
    }

    // ============ SnapshotSchedulerMetrics Tests ============

    #[test]
    fn test_snapshot_scheduler_metrics_creation() {
        let metrics = SnapshotSchedulerMetrics {
            total_snapshots: 10,
            avg_events_between: 5000,
            avg_time_between: Duration::from_secs(300),
            last_recovery_time: Some(Duration::from_secs(3)),
            current_thresholds: (1000, Duration::from_secs(60)),
        };
        assert_eq!(metrics.total_snapshots, 10);
        assert_eq!(metrics.avg_events_between, 5000);
    }

    #[test]
    fn test_snapshot_scheduler_metrics_clone() {
        let metrics = SnapshotSchedulerMetrics {
            total_snapshots: 5,
            avg_events_between: 2000,
            avg_time_between: Duration::from_secs(120),
            last_recovery_time: None,
            current_thresholds: (500, Duration::from_secs(30)),
        };
        let cloned = metrics.clone();
        assert_eq!(cloned.total_snapshots, metrics.total_snapshots);
    }

    #[test]
    fn test_snapshot_scheduler_metrics_debug() {
        let metrics = SnapshotSchedulerMetrics {
            total_snapshots: 3,
            avg_events_between: 1000,
            avg_time_between: Duration::from_secs(60),
            last_recovery_time: Some(Duration::from_millis(500)),
            current_thresholds: (1000, Duration::from_secs(60)),
        };
        let debug = format!("{:?}", metrics);
        assert!(debug.contains("SnapshotSchedulerMetrics"));
    }

    // ============ RecoveryStats Tests ============

    #[test]
    fn test_recovery_stats_creation() {
        let stats = RecoveryStats {
            total_events: 10000,
            total_snapshots: 5,
            compression_ratio: 0.4,
            next_snapshot_in: Duration::from_secs(60),
        };
        assert_eq!(stats.total_events, 10000);
        assert_eq!(stats.total_snapshots, 5);
        assert_eq!(stats.compression_ratio, 0.4);
    }

    #[test]
    fn test_recovery_stats_clone() {
        let stats = RecoveryStats {
            total_events: 5000,
            total_snapshots: 3,
            compression_ratio: 0.5,
            next_snapshot_in: Duration::from_secs(30),
        };
        let cloned = stats.clone();
        assert_eq!(cloned.total_events, stats.total_events);
    }

    #[test]
    fn test_recovery_stats_debug() {
        let stats = RecoveryStats {
            total_events: 1000,
            total_snapshots: 1,
            compression_ratio: 0.6,
            next_snapshot_in: Duration::from_secs(45),
        };
        let debug = format!("{:?}", stats);
        assert!(debug.contains("RecoveryStats"));
    }

    // ============ RecoveryError Tests ============

    #[test]
    fn test_recovery_error_display() {
        let err1 = RecoveryError::EventStoreError("connection failed".to_string());
        assert!(err1.to_string().contains("Event store error"));
        assert!(err1.to_string().contains("connection failed"));

        let err2 = RecoveryError::SnapshotError("disk full".to_string());
        assert!(err2.to_string().contains("Snapshot error"));
        assert!(err2.to_string().contains("disk full"));

        let err3 = RecoveryError::StateCorruption("checksum mismatch".to_string());
        assert!(err3.to_string().contains("State corruption"));
        assert!(err3.to_string().contains("checksum mismatch"));

        let err4 = RecoveryError::RecoveryFailed("timeout".to_string());
        assert!(err4.to_string().contains("Recovery failed"));
        assert!(err4.to_string().contains("timeout"));
    }

    #[test]
    fn test_recovery_error_debug() {
        let err = RecoveryError::StateCorruption("test".to_string());
        let debug = format!("{:?}", err);
        assert!(debug.contains("StateCorruption"));
    }

    // ============ AdaptiveSnapshotScheduler Additional Tests ============

    #[test]
    fn test_scheduler_record_snapshot() {
        let scheduler = AdaptiveSnapshotScheduler::new(SnapshotSchedulerConfig::default());

        scheduler.record_snapshot(5000, Duration::from_secs(300));
        scheduler.record_snapshot(3000, Duration::from_secs(200));

        let metrics = scheduler.get_metrics();
        assert_eq!(metrics.total_snapshots, 2);
        assert_eq!(metrics.avg_events_between, 4000); // (5000 + 3000) / 2
    }

    #[test]
    fn test_scheduler_get_metrics_empty() {
        let scheduler = AdaptiveSnapshotScheduler::new(SnapshotSchedulerConfig::default());

        let metrics = scheduler.get_metrics();
        assert_eq!(metrics.total_snapshots, 0);
        assert_eq!(metrics.avg_events_between, 0);
        assert_eq!(metrics.avg_time_between, Duration::ZERO);
        assert!(metrics.last_recovery_time.is_none());
    }

    #[test]
    fn test_scheduler_record_recovery() {
        let scheduler = AdaptiveSnapshotScheduler::new(SnapshotSchedulerConfig::default());

        scheduler.record_recovery(Duration::from_secs(2));

        let metrics = scheduler.get_metrics();
        assert_eq!(metrics.last_recovery_time, Some(Duration::from_secs(2)));
    }

    #[test]
    fn test_scheduler_recovery_times_capped() {
        let scheduler = AdaptiveSnapshotScheduler::new(SnapshotSchedulerConfig::default());

        // Record more than 10 recovery times
        for i in 0..15 {
            scheduler.record_recovery(Duration::from_secs(i as u64));
        }

        // Internal metrics should only keep 10
        // Just verify it doesn't panic and still works
        let metrics = scheduler.get_metrics();
        assert!(metrics.last_recovery_time.is_some());
    }

    #[test]
    fn test_scheduler_should_snapshot_non_adaptive() {
        let config = SnapshotSchedulerConfig {
            adaptive_enabled: false,
            min_events: 100,
            max_events: 1000,
            min_time_between_snapshots: Duration::from_secs(10),
            max_time_between_snapshots: Duration::from_secs(100),
            recovery_time_target: Duration::from_secs(5),
        };
        let scheduler = AdaptiveSnapshotScheduler::new(config);

        // Below min events and time
        assert!(!scheduler.should_snapshot(50, Duration::from_secs(5)));

        // At min events but below min time
        assert!(!scheduler.should_snapshot(100, Duration::from_secs(5)));

        // At min time but below min events
        assert!(!scheduler.should_snapshot(50, Duration::from_secs(10)));

        // Above both mins, should use non-adaptive logic
        // Need 100 * 10 = 1000 events or 10 * 10 = 100s
        assert!(scheduler.should_snapshot(1000, Duration::from_secs(15)));
        assert!(scheduler.should_snapshot(150, Duration::from_secs(100)));
    }

    #[test]
    fn test_scheduler_adaptive_with_history() {
        let config = SnapshotSchedulerConfig {
            adaptive_enabled: true,
            min_events: 100,
            max_events: 10000,
            min_time_between_snapshots: Duration::from_secs(10),
            max_time_between_snapshots: Duration::from_secs(1000),
            recovery_time_target: Duration::from_secs(5),
        };
        let scheduler = AdaptiveSnapshotScheduler::new(config);

        // Record some fast recovery times
        scheduler.record_recovery(Duration::from_secs(1));
        scheduler.record_recovery(Duration::from_secs(2));

        // With fast recovery, should wait longer
        // Should be false with moderate events/time
        assert!(!scheduler.should_snapshot(150, Duration::from_secs(15)));
    }

    #[test]
    fn test_scheduler_get_config() {
        let config = SnapshotSchedulerConfig {
            min_events: 200,
            max_events: 5000,
            min_time_between_snapshots: Duration::from_secs(20),
            max_time_between_snapshots: Duration::from_secs(500),
            recovery_time_target: Duration::from_secs(3),
            adaptive_enabled: false,
        };
        let scheduler = AdaptiveSnapshotScheduler::new(config.clone());

        let retrieved = scheduler.get_config();
        assert_eq!(retrieved.min_events, 200);
        assert_eq!(retrieved.max_events, 5000);
        assert!(!retrieved.adaptive_enabled);
    }

    // ============ ParallelRecovery Tests ============

    #[tokio::test]
    async fn test_parallel_recovery_merge_empty() {
        let temp_dir = TempDir::new().expect("internal error");
        let path = temp_dir.path().to_str().expect("internal error");

        let event_config = EventStoreConfig {
            persistence_enabled: false,
            ..Default::default()
        };

        let parallel =
            ParallelRecovery::<ExampleState>::new(1, event_config, SnapshotConfig::default(), path)
                .await
                .expect("internal error");

        // Empty vec should error
        let result = parallel.merge_partitions(vec![]).await;
        assert!(result.is_err());

        if let Err(RecoveryError::RecoveryFailed(msg)) = result {
            assert!(msg.contains("No partitions"));
        } else {
            panic!("Expected RecoveryFailed error");
        }
    }

    #[tokio::test]
    async fn test_recovery_manager_save_snapshot() {
        let temp_dir = TempDir::new().expect("internal error");
        let path = temp_dir.path().to_str().expect("internal error");

        let event_config = EventStoreConfig {
            persistence_enabled: false,
            ..Default::default()
        };

        let manager =
            RecoveryManager::<ExampleState>::new(event_config, SnapshotConfig::default(), path)
                .await
                .expect("internal error");

        let state = ExampleState {
            last_event_id: 100,
            event_count: 50,
            ..Default::default()
        };

        let snapshot_id = manager
            .save_snapshot(&state, None)
            .await
            .expect("internal error");

        // Verify snapshot was saved by recovering
        let initial = ExampleState::default();
        let restored = manager
            .recover_state(initial, None)
            .await
            .expect("internal error");

        // Should have loaded from snapshot
        assert!(restored.events_to_replay == 0 || snapshot_id != Uuid::nil());
    }

    #[tokio::test]
    async fn test_recovery_manager_should_snapshot() {
        let temp_dir = TempDir::new().expect("internal error");
        let path = temp_dir.path().to_str().expect("internal error");

        let event_config = EventStoreConfig {
            persistence_enabled: false,
            ..Default::default()
        };

        let manager =
            RecoveryManager::<ExampleState>::new(event_config, SnapshotConfig::default(), path)
                .await
                .expect("internal error");

        let state = ExampleState::default();

        // With default state, shouldn't need snapshot
        let should = manager.should_snapshot(&state).await;
        // Result depends on state values
        // Just verify no panic; result depends on state values
        let _ = should;
    }

    #[tokio::test]
    async fn test_recovery_manager_get_stats() {
        let temp_dir = TempDir::new().expect("internal error");
        let path = temp_dir.path().to_str().expect("internal error");

        let event_config = EventStoreConfig {
            persistence_enabled: false,
            ..Default::default()
        };

        let manager =
            RecoveryManager::<ExampleState>::new(event_config, SnapshotConfig::default(), path)
                .await
                .expect("internal error");

        let stats = manager.get_recovery_stats();
        assert_eq!(stats.total_snapshots, 0);
    }

    #[tokio::test]
    async fn test_recovery_manager_cleanup() {
        let temp_dir = TempDir::new().expect("internal error");
        let path = temp_dir.path().to_str().expect("internal error");

        let event_config = EventStoreConfig {
            persistence_enabled: false,
            ..Default::default()
        };

        let manager =
            RecoveryManager::<ExampleState>::new(event_config, SnapshotConfig::default(), path)
                .await
                .expect("internal error");

        let cleaned = manager
            .cleanup_old_snapshots()
            .await
            .expect("internal error");
        assert_eq!(cleaned, 0); // No orphans to clean
    }

    #[tokio::test]
    async fn test_recovery_manager_compact() {
        let temp_dir = TempDir::new().expect("internal error");
        let path = temp_dir.path().to_str().expect("internal error");

        let event_config = EventStoreConfig {
            persistence_enabled: false,
            ..Default::default()
        };

        let manager =
            RecoveryManager::<ExampleState>::new(event_config, SnapshotConfig::default(), path)
                .await
                .expect("internal error");

        manager.compact_events().await.expect("internal error");
    }
}