ccswarm 0.5.0

AI-powered multi-agent orchestration system with proactive intelligence, security monitoring, and session management
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
//! Mockall Best Practices Tests
//!
//! This file demonstrates mockall best practices for testing ccswarm components.
//!
//! ## Best Practices Applied:
//! 1. Use `#[automock]` or manual mock implementations for traits
//! 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
//! 7. Use checkpoints for complex test scenarios
//!
//! References:
//! - https://docs.rs/mockall/latest/mockall/
//! - https://blog.logrocket.com/mocking-rust-mockall-alternatives/

use anyhow::Result;
use chrono::Utc;
use mockall::mock;
use mockall::predicate::*;

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

// Mock for ProviderExecutor trait
// Best Practice: Define mocks for key boundary interfaces
mock! {
    pub ProviderExecutor {
        fn execute_prompt(
            &self,
            prompt: &str,
            identity_role: &str,
            working_dir: &std::path::Path,
        ) -> Result<String>;

        fn health_check(&self) -> Result<bool>;

        fn get_capabilities(&self) -> ProviderCapabilities;
    }
}

// Mock for TaskHandler
mock! {
    pub TaskHandler {
        fn handle(&self, task_id: &str, prompt: &str) -> Result<TaskResult>;
        fn can_handle(&self, task_type: &str) -> bool;
    }
}

// Mock for TemplateStorage
mock! {
    pub TemplateStorage {
        fn save(&self, name: &str, content: &str) -> Result<()>;
        fn load(&self, name: &str) -> Result<Option<String>>;
        fn delete(&self, name: &str) -> Result<bool>;
        fn list(&self) -> Result<Vec<String>>;
    }
}

// Mock for SessionLifecycle
mock! {
    pub SessionLifecycle {
        fn initialize(&mut self) -> Result<()>;
        fn shutdown(&mut self) -> Result<()>;
        fn get_status(&self) -> SessionStatus;
        fn is_ready(&self) -> bool;
    }
}

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

#[derive(Debug, Clone)]
pub struct ProviderCapabilities {
    pub supports_json_output: bool,
    pub supports_streaming: bool,
    pub max_context_tokens: usize,
}

impl Default for ProviderCapabilities {
    fn default() -> Self {
        Self {
            supports_json_output: true,
            supports_streaming: false,
            max_context_tokens: 4096,
        }
    }
}

#[derive(Debug, Clone)]
pub struct TaskResult {
    pub task_id: String,
    pub success: bool,
    pub output: String,
    pub duration_ms: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionStatus {
    Active,
    Idle,
    Busy,
    Terminated,
}

// ============================================================================
// Provider Executor Tests
// ============================================================================

mod provider_executor_tests {
    use super::*;

    /// Best Practice 1: Basic mock with explicit expectation
    #[test]
    fn test_execute_prompt_success() {
        let mut mock = MockProviderExecutor::new();

        // Set expectation: expect execute_prompt to be called once
        mock.expect_execute_prompt()
            .times(1) // Best Practice: Verify call count
            .withf(|prompt, _, _| prompt.contains("test")) // Best Practice: Argument matching
            .returning(|_, _, _| Ok("Success response".to_string()));

        let result = mock.execute_prompt("test prompt", "assistant", std::path::Path::new("/tmp"));

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "Success response");
    }

    /// Best Practice 2: Testing error conditions
    #[test]
    fn test_execute_prompt_error() {
        let mut mock = MockProviderExecutor::new();

        mock.expect_execute_prompt()
            .times(1)
            .returning(|_, _, _| Err(anyhow::anyhow!("Provider unavailable")));

        let result = mock.execute_prompt("any prompt", "assistant", std::path::Path::new("/tmp"));

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("unavailable"));
    }

    /// Best Practice 3: Multiple calls with different return values
    #[test]
    fn test_multiple_prompt_executions() {
        let mut mock = MockProviderExecutor::new();

        // Use call count to return different values
        let call_count = std::sync::atomic::AtomicUsize::new(0);

        mock.expect_execute_prompt()
            .times(3)
            .returning(move |prompt, _, _| {
                let count = call_count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                Ok(format!("Response {} for: {}", count, prompt))
            });

        let r1 = mock.execute_prompt("first", "a", std::path::Path::new("/"));
        let r2 = mock.execute_prompt("second", "a", std::path::Path::new("/"));
        let r3 = mock.execute_prompt("third", "a", std::path::Path::new("/"));

        assert_eq!(r1.unwrap(), "Response 0 for: first");
        assert_eq!(r2.unwrap(), "Response 1 for: second");
        assert_eq!(r3.unwrap(), "Response 2 for: third");
    }

    /// Best Practice 4: Health check with capabilities
    #[test]
    fn test_health_check_and_capabilities() {
        let mut mock = MockProviderExecutor::new();

        mock.expect_health_check().times(1).returning(|| Ok(true));

        mock.expect_get_capabilities()
            .times(1)
            .returning(|| ProviderCapabilities {
                supports_json_output: true,
                supports_streaming: true,
                max_context_tokens: 8192,
            });

        assert!(mock.health_check().unwrap());

        let caps = mock.get_capabilities();
        assert!(caps.supports_streaming);
        assert_eq!(caps.max_context_tokens, 8192);
    }
}

// ============================================================================
// Task Handler Tests
// ============================================================================

mod task_handler_tests {
    use super::*;

    /// Best Practice 5: Using withf for complex argument matching
    #[test]
    fn test_task_handler_with_argument_matching() {
        let mut mock = MockTaskHandler::new();

        // Match tasks with specific ID patterns
        mock.expect_handle()
            .withf(|task_id, _| task_id.starts_with("task-"))
            .times(2)
            .returning(|id, prompt| {
                Ok(TaskResult {
                    task_id: id.to_string(),
                    success: true,
                    output: format!("Processed: {}", prompt),
                    duration_ms: 100,
                })
            });

        let r1 = mock.handle("task-001", "Do something");
        let r2 = mock.handle("task-002", "Do something else");

        assert!(r1.unwrap().success);
        assert!(r2.unwrap().success);
    }

    /// Best Practice 6: Conditional behavior based on input
    #[test]
    fn test_can_handle_task_types() {
        let mut mock = MockTaskHandler::new();

        mock.expect_can_handle()
            .withf(|task_type| task_type == "code_review")
            .returning(|_| true);

        mock.expect_can_handle()
            .withf(|task_type| task_type == "deployment")
            .returning(|_| false);

        assert!(mock.can_handle("code_review"));
        assert!(!mock.can_handle("deployment"));
    }
}

// ============================================================================
// Template Storage Tests
// ============================================================================

mod template_storage_tests {
    use super::*;

    /// Best Practice 7: CRUD operations with state tracking
    #[test]
    fn test_template_crud_operations() {
        let mut mock = MockTemplateStorage::new();

        // Save operation
        mock.expect_save()
            .with(eq("my_template"), eq("template content"))
            .times(1)
            .returning(|_, _| Ok(()));

        // Load operation
        mock.expect_load()
            .with(eq("my_template"))
            .times(1)
            .returning(|_| Ok(Some("template content".to_string())));

        // Delete operation
        mock.expect_delete()
            .with(eq("my_template"))
            .times(1)
            .returning(|_| Ok(true));

        // Execute CRUD operations
        assert!(mock.save("my_template", "template content").is_ok());
        assert_eq!(
            mock.load("my_template").unwrap(),
            Some("template content".to_string())
        );
        assert!(mock.delete("my_template").unwrap());
    }

    /// Best Practice 8: List operation with empty and populated states
    #[test]
    fn test_template_list() {
        let mut mock = MockTemplateStorage::new();

        // First call: empty list
        mock.expect_list().times(1).returning(|| Ok(vec![]));

        let result = mock.list().unwrap();
        assert!(result.is_empty());
    }

    /// Best Practice 9: Load non-existent template
    #[test]
    fn test_load_nonexistent_template() {
        let mut mock = MockTemplateStorage::new();

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

        let result = mock.load("nonexistent").unwrap();
        assert!(result.is_none());
    }
}

// ============================================================================
// Session Lifecycle Tests
// ============================================================================

mod session_lifecycle_tests {
    use super::*;

    /// Best Practice 10: Testing state transitions
    #[test]
    fn test_session_initialization() {
        let mut mock = MockSessionLifecycle::new();

        // Expect initialize to be called
        mock.expect_initialize().times(1).returning(|| Ok(()));

        // After init, session should be ready
        mock.expect_is_ready().times(1).returning(|| true);

        mock.expect_get_status()
            .times(1)
            .returning(|| SessionStatus::Active);

        assert!(mock.initialize().is_ok());
        assert!(mock.is_ready());
        assert_eq!(mock.get_status(), SessionStatus::Active);
    }

    /// Best Practice 11: Testing shutdown sequence
    #[test]
    fn test_session_shutdown() {
        let mut mock = MockSessionLifecycle::new();

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

        mock.expect_get_status()
            .times(1)
            .returning(|| SessionStatus::Terminated);

        assert!(mock.shutdown().is_ok());
        assert_eq!(mock.get_status(), SessionStatus::Terminated);
    }

    /// Best Practice 12: Testing error handling during initialization
    #[test]
    fn test_session_init_failure() {
        let mut mock = MockSessionLifecycle::new();

        mock.expect_initialize()
            .times(1)
            .returning(|| Err(anyhow::anyhow!("Failed to connect to backend")));

        mock.expect_is_ready().times(1).returning(|| false);

        let result = mock.initialize();
        assert!(result.is_err());
        assert!(!mock.is_ready());
    }
}

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

mod integration_pattern_tests {
    use super::*;

    /// Best Practice 13: Orchestrating multiple mocks
    #[test]
    fn test_task_execution_workflow() {
        // Create mocks for different components
        let mut session_mock = MockSessionLifecycle::new();
        let mut task_mock = MockTaskHandler::new();
        let mut storage_mock = MockTemplateStorage::new();

        // Set up session initialization
        session_mock
            .expect_initialize()
            .times(1)
            .returning(|| Ok(()));
        session_mock.expect_is_ready().times(1).returning(|| true);

        // Set up template loading
        storage_mock
            .expect_load()
            .with(eq("default_task"))
            .times(1)
            .returning(|_| Ok(Some("Task template: {prompt}".to_string())));

        // Set up task handling
        task_mock.expect_handle().times(1).returning(|id, _| {
            Ok(TaskResult {
                task_id: id.to_string(),
                success: true,
                output: "Task completed".to_string(),
                duration_ms: 50,
            })
        });

        // Execute workflow
        session_mock.initialize().unwrap();
        assert!(session_mock.is_ready());

        let template = storage_mock.load("default_task").unwrap().unwrap();
        assert!(template.contains("Task template"));

        let result = task_mock.handle("task-123", "Execute test").unwrap();
        assert!(result.success);
    }

    /// Best Practice 14: Using times(0) to verify a method is NOT called
    #[test]
    fn test_no_shutdown_on_healthy_session() {
        let mut mock = MockSessionLifecycle::new();

        // Session should NOT be shut down if it's healthy
        mock.expect_shutdown().times(0); // Verify shutdown is never called

        mock.expect_is_ready().times(1).returning(|| true);

        // In real code, this would be the logic:
        // if !session.is_ready() { session.shutdown(); }
        if !mock.is_ready() {
            let _ = mock.shutdown();
        }
        // Test passes because shutdown was not called
    }

    /// Best Practice 15: Verifying exact argument values
    #[test]
    fn test_specific_template_operations() {
        let mut mock = MockTemplateStorage::new();

        // Verify exact template name and content
        mock.expect_save()
            .with(
                eq("production_config"),
                function(|content: &str| content.len() > 10),
            )
            .times(1)
            .returning(|_, _| Ok(()));

        mock.save("production_config", "This is a long configuration content")
            .unwrap();
    }
}

// ============================================================================
// Advanced Mockall Patterns
// ============================================================================

mod advanced_patterns {
    use super::*;

    /// Best Practice 16: Using mockall::Sequence for ordered calls
    #[test]
    fn test_ordered_operations() {
        let mut mock = MockTemplateStorage::new();
        let mut seq = mockall::Sequence::new();

        // Operations must happen in this exact order
        mock.expect_save()
            .times(1)
            .in_sequence(&mut seq)
            .returning(|_, _| Ok(()));

        mock.expect_load()
            .times(1)
            .in_sequence(&mut seq)
            .returning(|_| Ok(Some("content".to_string())));

        mock.expect_delete()
            .times(1)
            .in_sequence(&mut seq)
            .returning(|_| Ok(true));

        // Execute in order
        mock.save("test", "content").unwrap();
        mock.load("test").unwrap();
        mock.delete("test").unwrap();
    }

    /// Best Practice 17: Using returning_st for returning non-Clone types
    #[test]
    fn test_returning_complex_types() {
        let mut mock = MockProviderExecutor::new();

        // Use returning for each individual call
        mock.expect_execute_prompt()
            .times(1)
            .returning(|prompt, _, _| {
                // Can return complex computed values
                let response = format!(
                    "{{\"status\": \"ok\", \"input\": \"{}\", \"timestamp\": {}}}",
                    prompt,
                    Utc::now().timestamp()
                );
                Ok(response)
            });

        let result = mock
            .execute_prompt("test", "role", std::path::Path::new("/"))
            .unwrap();
        assert!(result.contains("status"));
        assert!(result.contains("ok"));
    }

    /// Best Practice 18: Testing retry logic with call counters
    #[test]
    fn test_retry_behavior() {
        let mut mock = MockProviderExecutor::new();
        let attempt = std::sync::atomic::AtomicUsize::new(0);

        // First two attempts fail, third succeeds
        mock.expect_execute_prompt()
            .times(3)
            .returning(move |_, _, _| {
                let current = attempt.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                if current < 2 {
                    Err(anyhow::anyhow!("Temporary failure"))
                } else {
                    Ok("Success after retry".to_string())
                }
            });

        // Simulate retry logic
        let mut result = Err(anyhow::anyhow!("initial"));
        for _ in 0..3 {
            result = mock.execute_prompt("test", "role", std::path::Path::new("/"));
            if result.is_ok() {
                break;
            }
        }

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "Success after retry");
    }
}