ai-session 0.5.0

AI-optimized terminal session management library
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
//! Mockall Best Practices Tests for ai-session
//!
//! This file demonstrates mockall best practices for testing ai-session components.
//!
//! ## Best Practices Applied:
//! 1. Use `mock!` macro for defining mock types
//! 2. Set explicit expectations with `.expect_*()` methods
//! 3. Use `.times()` to verify call counts
//! 4. Use `.returning()` to define return values
//! 5. Use `.withf()` for argument matching
//! 6. Use `mockall::Sequence` for ordered call verification

use anyhow::Result;
use chrono::Utc;
use mockall::mock;
use mockall::predicate::*;
use std::path::PathBuf;
use uuid::Uuid;

// ============================================================================
// Mock Definitions
// ============================================================================

// Mock for ExternalIntegration trait
mock! {
    pub ExternalIntegration {
        fn name(&self) -> &'static str;
        fn initialize(&mut self) -> Result<()>;
        fn on_session_created(&self, session_id: &str) -> Result<()>;
        fn on_session_terminated(&self, session_id: &str) -> Result<()>;
        fn export_session_data(&self, session_id: &str) -> Result<serde_json::Value>;
    }
}

// Mock for Transport trait
mock! {
    pub Transport {
        fn send(&mut self, message: &str) -> Result<()>;
        fn receive(&mut self) -> Result<Option<String>>;
        fn close(&mut self) -> Result<()>;
    }
}

// Mock for Capabilities trait
mock! {
    pub Capabilities {
        fn request_capability(&self, capability: &Capability) -> Result<CapabilityToken>;
        fn has_capability(&self, capability: &Capability) -> bool;
        fn revoke_capability(&mut self, token: CapabilityToken) -> Result<()>;
    }
}

// Mock for SessionManager-like functionality
mock! {
    pub SessionManager {
        fn create_session(&self, config: SessionConfig) -> Result<String>;
        fn get_session(&self, id: &str) -> Option<SessionInfo>;
        fn terminate_session(&self, id: &str) -> Result<()>;
        fn list_sessions(&self) -> Vec<String>;
    }
}

// Mock for Context compression
mock! {
    pub ContextCompressor {
        fn compress(&self, input: &str) -> Result<Vec<u8>>;
        fn decompress(&self, input: &[u8]) -> Result<String>;
        fn compression_ratio(&self) -> f64;
    }
}

// ============================================================================
// Test Data Structures
// ============================================================================

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum Capability {
    FileRead(PathBuf),
    FileWrite(PathBuf),
    NetworkAccess(String, u16),
    ProcessSpawn(String),
}

#[derive(Debug, Clone)]
pub struct CapabilityToken {
    pub id: Uuid,
    pub capability: Capability,
    pub expires_at: Option<chrono::DateTime<chrono::Utc>>,
}

#[derive(Debug, Clone, Default)]
pub struct SessionConfig {
    pub name: Option<String>,
    pub working_directory: Option<PathBuf>,
    pub enable_ai_features: bool,
    pub max_tokens: usize,
}

#[derive(Debug, Clone)]
pub struct SessionInfo {
    pub id: String,
    pub status: SessionStatus,
    pub created_at: chrono::DateTime<chrono::Utc>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionStatus {
    Initializing,
    Running,
    Paused,
    Terminated,
}

// ============================================================================
// External Integration Tests
// ============================================================================

mod external_integration_tests {
    use super::*;

    /// Test integration initialization
    #[test]
    fn test_integration_initialization() {
        let mut mock = MockExternalIntegration::new();

        mock.expect_name().times(1).returning(|| "vscode");

        mock.expect_initialize().times(1).returning(|| Ok(()));

        assert_eq!(mock.name(), "vscode");
        assert!(mock.initialize().is_ok());
    }

    /// Test session lifecycle hooks
    #[test]
    fn test_session_lifecycle_hooks() {
        let mut mock = MockExternalIntegration::new();
        let session_id = "session-001";

        mock.expect_on_session_created()
            .with(eq(session_id))
            .times(1)
            .returning(|_| Ok(()));

        mock.expect_on_session_terminated()
            .with(eq(session_id))
            .times(1)
            .returning(|_| Ok(()));

        assert!(mock.on_session_created(session_id).is_ok());
        assert!(mock.on_session_terminated(session_id).is_ok());
    }

    /// Test session data export
    #[test]
    fn test_export_session_data() {
        let mut mock = MockExternalIntegration::new();

        mock.expect_export_session_data()
            .with(eq("session-test"))
            .times(1)
            .returning(|id| {
                Ok(serde_json::json!({
                    "session_id": id,
                    "commands_executed": 42,
                    "duration_seconds": 3600
                }))
            });

        let result = mock.export_session_data("session-test").unwrap();
        assert_eq!(result["commands_executed"], 42);
    }

    /// Test integration error handling
    #[test]
    fn test_integration_error_handling() {
        let mut mock = MockExternalIntegration::new();

        mock.expect_initialize()
            .times(1)
            .returning(|| Err(anyhow::anyhow!("Connection refused")));

        let result = mock.initialize();
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("refused"));
    }
}

// ============================================================================
// Transport Tests
// ============================================================================

mod transport_tests {
    use super::*;

    /// Test message send/receive cycle
    #[test]
    fn test_send_receive_cycle() {
        let mut mock = MockTransport::new();
        let mut seq = mockall::Sequence::new();

        // Send must happen before receive
        mock.expect_send()
            .with(eq(r#"{"method":"ping"}"#))
            .times(1)
            .in_sequence(&mut seq)
            .returning(|_| Ok(()));

        mock.expect_receive()
            .times(1)
            .in_sequence(&mut seq)
            .returning(|| Ok(Some(r#"{"result":"pong"}"#.to_string())));

        assert!(mock.send(r#"{"method":"ping"}"#).is_ok());
        let response = mock.receive().unwrap();
        assert!(response.unwrap().contains("pong"));
    }

    /// Test transport close
    #[test]
    fn test_transport_close() {
        let mut mock = MockTransport::new();

        mock.expect_close().times(1).returning(|| Ok(()));

        mock.expect_send().times(0); // No sends after close

        assert!(mock.close().is_ok());
    }

    /// Test receive timeout (returns None)
    #[test]
    fn test_receive_timeout() {
        let mut mock = MockTransport::new();

        mock.expect_receive().times(1).returning(|| Ok(None));

        let result = mock.receive().unwrap();
        assert!(result.is_none());
    }

    /// Test multiple message exchange
    #[test]
    fn test_multiple_messages() {
        let mut mock = MockTransport::new();
        let counter = std::sync::atomic::AtomicUsize::new(0);

        mock.expect_send().times(3).returning(|_| Ok(()));

        mock.expect_receive().times(3).returning(move || {
            let n = counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            Ok(Some(format!("response-{}", n)))
        });

        for i in 0..3 {
            mock.send(&format!("request-{}", i)).unwrap();
            let resp = mock.receive().unwrap().unwrap();
            assert!(resp.starts_with("response-"));
        }
    }
}

// ============================================================================
// Capabilities Tests
// ============================================================================

mod capabilities_tests {
    use super::*;

    /// Test capability request and check
    #[test]
    fn test_capability_request() {
        let mut mock = MockCapabilities::new();
        let cap = Capability::FileRead(PathBuf::from("/tmp/test.txt"));

        let token = CapabilityToken {
            id: Uuid::new_v4(),
            capability: cap.clone(),
            expires_at: None,
        };
        let token_clone = token.clone();

        mock.expect_request_capability()
            .withf(|c| matches!(c, Capability::FileRead(_)))
            .times(1)
            .returning(move |_| Ok(token_clone.clone()));

        mock.expect_has_capability()
            .with(eq(cap.clone()))
            .times(1)
            .returning(|_| true);

        let result = mock.request_capability(&cap).unwrap();
        assert_eq!(result.capability, cap);
        assert!(mock.has_capability(&cap));
    }

    /// Test capability revocation
    #[test]
    fn test_capability_revocation() {
        let mut mock = MockCapabilities::new();
        let cap = Capability::NetworkAccess("localhost".to_string(), 8080);

        let token = CapabilityToken {
            id: Uuid::new_v4(),
            capability: cap.clone(),
            expires_at: Some(Utc::now()),
        };

        mock.expect_revoke_capability()
            .withf(|t| matches!(t.capability, Capability::NetworkAccess(_, 8080)))
            .times(1)
            .returning(|_| Ok(()));

        mock.expect_has_capability()
            .with(eq(cap.clone()))
            .times(1)
            .returning(|_| false);

        assert!(mock.revoke_capability(token).is_ok());
        assert!(!mock.has_capability(&cap));
    }

    /// Test denied capability
    #[test]
    fn test_capability_denied() {
        let mut mock = MockCapabilities::new();
        let cap = Capability::ProcessSpawn("rm -rf /".to_string());

        mock.expect_request_capability()
            .times(1)
            .returning(|_| Err(anyhow::anyhow!("Capability denied: dangerous operation")));

        mock.expect_has_capability()
            .with(eq(cap.clone()))
            .times(1)
            .returning(|_| false);

        assert!(mock.request_capability(&cap).is_err());
        assert!(!mock.has_capability(&cap));
    }
}

// ============================================================================
// Session Manager Tests
// ============================================================================

mod session_manager_tests {
    use super::*;

    /// Test session creation and retrieval
    #[test]
    fn test_session_lifecycle() {
        let mut mock = MockSessionManager::new();
        let config = SessionConfig {
            name: Some("test-session".to_string()),
            enable_ai_features: true,
            max_tokens: 4096,
            ..Default::default()
        };

        mock.expect_create_session()
            .times(1)
            .returning(|_| Ok("session-123".to_string()));

        mock.expect_get_session()
            .with(eq("session-123"))
            .times(1)
            .returning(|id| {
                Some(SessionInfo {
                    id: id.to_string(),
                    status: SessionStatus::Running,
                    created_at: Utc::now(),
                })
            });

        mock.expect_terminate_session()
            .with(eq("session-123"))
            .times(1)
            .returning(|_| Ok(()));

        let session_id = mock.create_session(config).unwrap();
        assert_eq!(session_id, "session-123");

        let info = mock.get_session(&session_id).unwrap();
        assert_eq!(info.status, SessionStatus::Running);

        assert!(mock.terminate_session(&session_id).is_ok());
    }

    /// Test list sessions
    #[test]
    fn test_list_sessions() {
        let mut mock = MockSessionManager::new();

        mock.expect_list_sessions().times(1).returning(|| {
            vec![
                "session-1".to_string(),
                "session-2".to_string(),
                "session-3".to_string(),
            ]
        });

        let sessions = mock.list_sessions();
        assert_eq!(sessions.len(), 3);
        assert!(sessions.contains(&"session-2".to_string()));
    }

    /// Test get nonexistent session
    #[test]
    fn test_get_nonexistent_session() {
        let mut mock = MockSessionManager::new();

        mock.expect_get_session()
            .with(eq("nonexistent"))
            .times(1)
            .returning(|_| None);

        assert!(mock.get_session("nonexistent").is_none());
    }
}

// ============================================================================
// Context Compressor Tests
// ============================================================================

mod context_compressor_tests {
    use super::*;

    /// Test compression and decompression
    #[test]
    fn test_compression_roundtrip() {
        let mut mock = MockContextCompressor::new();
        let original = "This is a test message for compression";

        mock.expect_compress()
            .with(eq(original))
            .times(1)
            .returning(|_| Ok(vec![1, 2, 3, 4, 5]));

        // Use withf for dynamic matching instead of eq with borrowed data
        mock.expect_decompress()
            .withf(|input| !input.is_empty())
            .times(1)
            .returning(|_| Ok("This is a test message for compression".to_string()));

        let compressed_result = mock.compress(original).unwrap();
        assert!(!compressed_result.is_empty());

        let decompressed = mock.decompress(&compressed_result).unwrap();
        assert_eq!(decompressed, original);
    }

    /// Test compression ratio
    #[test]
    fn test_compression_ratio() {
        let mut mock = MockContextCompressor::new();

        // Expect 93% compression (0.07 ratio means 93% reduction)
        mock.expect_compression_ratio().times(1).returning(|| 0.07);

        let ratio = mock.compression_ratio();
        assert!(ratio < 0.1); // Less than 10% of original size
    }

    /// Test compression error
    #[test]
    fn test_compression_error() {
        let mut mock = MockContextCompressor::new();

        mock.expect_compress()
            .times(1)
            .returning(|_| Err(anyhow::anyhow!("Input too large")));

        assert!(mock.compress("huge input").is_err());
    }
}

// ============================================================================
// Integration Pattern Tests
// ============================================================================

mod integration_patterns {
    use super::*;

    /// Test full workflow with multiple mocks
    #[test]
    fn test_session_with_integration_workflow() {
        let mut session_mock = MockSessionManager::new();
        let mut integration_mock = MockExternalIntegration::new();
        let mut compressor_mock = MockContextCompressor::new();

        // Setup session creation
        session_mock
            .expect_create_session()
            .times(1)
            .returning(|_| Ok("workflow-session".to_string()));

        // Setup integration notification
        integration_mock
            .expect_on_session_created()
            .with(eq("workflow-session"))
            .times(1)
            .returning(|_| Ok(()));

        // Setup context compression
        compressor_mock
            .expect_compress()
            .times(1)
            .returning(|_| Ok(vec![1, 2, 3]));

        // Execute workflow
        let session_id = session_mock
            .create_session(SessionConfig::default())
            .unwrap();

        integration_mock.on_session_created(&session_id).unwrap();

        let _ = compressor_mock.compress("session context data").unwrap();
    }

    /// Test capability-based operation
    #[test]
    fn test_capability_gated_operation() {
        let mut caps_mock = MockCapabilities::new();
        let mut transport_mock = MockTransport::new();

        let network_cap = Capability::NetworkAccess("api.example.com".to_string(), 443);

        // First check capability
        caps_mock
            .expect_has_capability()
            .with(eq(network_cap.clone()))
            .times(1)
            .returning(|_| true);

        // Then allow transport operation
        transport_mock.expect_send().times(1).returning(|_| Ok(()));

        // Simulate capability check before network operation
        if caps_mock.has_capability(&network_cap) {
            transport_mock.send("api request").unwrap();
        }
    }
}