agcodex-core 0.1.0

Core business logic with AST-RAG engine and tree-sitter integration
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
//! Test Writer Agent - Generates comprehensive tests
//!
//! This agent creates thorough test suites:
//! - Unit tests for functions
//! - Integration tests for modules
//! - Property-based tests
//! - Edge case coverage
//! - Test data generation

use crate::code_tools::ast_agent_tools::ASTAgentTools;
use crate::code_tools::ast_agent_tools::AgentToolOp;
use crate::code_tools::ast_agent_tools::AgentToolResult;
use crate::modes::OperatingMode;
use crate::subagents::AgentResult;
use crate::subagents::AgentStatus;
use crate::subagents::Finding;
use crate::subagents::Severity;
use crate::subagents::Subagent;
use crate::subagents::SubagentContext;
use crate::subagents::SubagentError;
use crate::subagents::SubagentResult;
use std::collections::HashMap;
use std::future::Future;
use std::path::Path;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::SystemTime;

/// Test Writer Agent implementation
#[derive(Debug)]
pub struct TestWriterAgent {
    name: String,
    description: String,
    _mode_override: Option<OperatingMode>,
    _tool_permissions: Vec<String>,
    _prompt_template: String,
    test_strategy: TestStrategy,
}

/// Test generation strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TestStrategy {
    Basic,         // Essential test cases only
    Comprehensive, // Full coverage with edge cases
    PropertyBased, // Property-based testing with generators
}

impl Default for TestWriterAgent {
    fn default() -> Self {
        Self::new()
    }
}

impl TestWriterAgent {
    /// Create a new test writer agent
    pub fn new() -> Self {
        Self {
            name: "test-writer".to_string(),
            description: "Generates comprehensive test suites with high coverage".to_string(),
            _mode_override: Some(OperatingMode::Build),
            _tool_permissions: vec![
                "search".to_string(),
                "edit".to_string(),
                "tree".to_string(),
                "think".to_string(),
            ],
            _prompt_template: r#"
You are an expert test engineer focused on:
- Achieving high code coverage (>90%)
- Testing edge cases and error conditions
- Creating maintainable test suites
- Using appropriate testing patterns
- Generating realistic test data

Write tests that are:
- Isolated and independent
- Fast and deterministic
- Clear and well-documented
- Comprehensive yet focused
"#
            .to_string(),
            test_strategy: TestStrategy::Comprehensive,
        }
    }

    /// Set test strategy
    pub const fn with_strategy(mut self, strategy: TestStrategy) -> Self {
        self.test_strategy = strategy;
        self
    }

    /// Analyze test coverage
    async fn analyze_coverage(&self, ast_tools: &mut ASTAgentTools, file: &Path) -> Vec<Finding> {
        let mut findings = Vec::new();

        // Extract functions and check for existing tests
        if let Ok(AgentToolResult::Functions(functions)) =
            ast_tools.execute(AgentToolOp::ExtractFunctions {
                file: file.to_path_buf(),
                language: self.detect_language(file),
            })
        {
            for func in functions {
                // Check if function has tests
                let test_pattern = format!("test.*{}", func.name);
                if let Ok(AgentToolResult::SearchResults(results)) =
                    ast_tools.execute(AgentToolOp::Search {
                        query: test_pattern,
                        scope: crate::code_tools::search::SearchScope::Directory(
                            file.parent().unwrap_or(Path::new(".")).to_path_buf(),
                        ),
                    })
                    && results.is_empty()
                {
                    findings.push(Finding {
                            category: "test-coverage".to_string(),
                            severity: Severity::Medium,
                            title: format!("Missing Tests: {}", func.name),
                            description: format!(
                                "Function '{}' has no test coverage. This could lead to undetected bugs.",
                                func.name
                            ),
                            location: Some(crate::code_tools::ast_agent_tools::Location {
                                file: file.to_path_buf(),
                                line: func.start_line,
                                column: 0,
                                byte_offset: 0,
                            }),
                            suggestion: Some("Create unit tests covering normal, edge, and error cases".to_string()),
                            metadata: HashMap::from([
                                ("function_name".to_string(), serde_json::json!(func.name)),
                                ("needs_tests".to_string(), serde_json::json!(true)),
                            ]),
                        });
                }
            }
        }

        findings
    }

    /// Generate test cases for a function
    async fn generate_test_cases(
        &self,
        _ast_tools: &mut ASTAgentTools,
        function_name: &str,
        file: &Path,
    ) -> String {
        let mut test_code = String::new();
        let lang = self.detect_language(file);

        // Generate test structure based on language
        match lang.as_str() {
            "rust" => {
                test_code.push_str(&format!(
                    r#"
#[cfg(test)]
mod test_{} {{
    use super::*;

    #[test]
    fn test_{}_normal_case() {{
        // Arrange
        let input = /* TODO: Add test input */;
        
        // Act
        let result = {}(input);
        
        // Assert
        assert_eq!(result, /* expected value */);
    }}

    #[test]
    fn test_{}_edge_case() {{
        // Test with boundary values
        let edge_input = /* TODO: Add edge case input */;
        let result = {}(edge_input);
        assert!(/* validation */);
    }}

    #[test]
    #[should_panic(expected = "error message")]
    fn test_{}_error_case() {{
        // Test error handling
        let invalid_input = /* TODO: Add invalid input */;
        {}(invalid_input); // Should panic
    }}
}}"#,
                    function_name,
                    function_name,
                    function_name,
                    function_name,
                    function_name,
                    function_name,
                    function_name
                ));
            }
            "python" => {
                test_code.push_str(&format!(
                    r#"
import unittest
from unittest.mock import Mock, patch

class Test{}(unittest.TestCase):
    
    def test_{}_normal_case(self):
        # Arrange
        input_data = # TODO: Add test input
        
        # Act
        result = {}(input_data)
        
        # Assert
        self.assertEqual(result, # expected value)
    
    def test_{}_edge_case(self):
        # Test with boundary values
        edge_input = # TODO: Add edge case input
        result = {}(edge_input)
        self.assertTrue(# validation)
    
    def test_{}_error_case(self):
        # Test error handling
        invalid_input = # TODO: Add invalid input
        with self.assertRaises(Exception):
            {}(invalid_input)

if __name__ == '__main__':
    unittest.main()"#,
                    to_pascal_case(function_name),
                    function_name,
                    function_name,
                    function_name,
                    function_name,
                    function_name,
                    function_name
                ));
            }
            "javascript" | "typescript" => {
                test_code.push_str(&format!(
                    r#"
describe('{}', () => {{
    
    test('should handle normal case', () => {{
        // Arrange
        const input = /* TODO: Add test input */;
        
        // Act
        const result = {}(input);
        
        // Assert
        expect(result).toBe(/* expected value */);
    }});
    
    test('should handle edge case', () => {{
        // Test with boundary values
        const edgeInput = /* TODO: Add edge case input */;
        const result = {}(edgeInput);
        expect(result).toBeTruthy();
    }});
    
    test('should throw error for invalid input', () => {{
        // Test error handling
        const invalidInput = /* TODO: Add invalid input */;
        expect(() => {{
            {}(invalidInput);
        }}).toThrow('error message');
    }});
}});"#,
                    function_name, function_name, function_name, function_name
                ));
            }
            _ => {
                test_code.push_str(&format!(
                    "// TODO: Generate tests for function '{}'\n",
                    function_name
                ));
            }
        }

        test_code
    }

    /// Detect language from file extension
    fn detect_language(&self, file: &Path) -> String {
        file.extension()
            .and_then(|ext| ext.to_str())
            .map(|ext| match ext {
                "rs" => "rust",
                "py" => "python",
                "js" => "javascript",
                "ts" => "typescript",
                "go" => "go",
                "java" => "java",
                _ => "text",
            })
            .unwrap_or("text")
            .to_string()
    }
}

impl Subagent for TestWriterAgent {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn execute<'a>(
        &'a self,
        context: &'a SubagentContext,
        ast_tools: &'a mut ASTAgentTools,
        cancel_flag: Arc<AtomicBool>,
    ) -> Pin<Box<dyn Future<Output = SubagentResult<AgentResult>> + Send + 'a>> {
        Box::pin(async move {
            let start_time = SystemTime::now();
            let mut all_findings = Vec::new();
            let mut analyzed_files = Vec::new();
            let mut modified_files = Vec::new();
            let mut tests_generated = 0;

            // Get files to test from context
            let files = self.get_test_targets(context)?;

            for file in &files {
                if cancel_flag.load(Ordering::Acquire) {
                    return Err(SubagentError::ExecutionFailed(
                        "Test generation cancelled".to_string(),
                    ));
                }

                analyzed_files.push(file.clone());

                // Analyze test coverage
                let coverage_findings = self.analyze_coverage(ast_tools, file).await;

                // Generate tests for uncovered functions
                for finding in &coverage_findings {
                    if let Some(function_name) = finding.metadata.get("function_name")
                        && let Some(name) = function_name.as_str()
                    {
                        let test_code = self.generate_test_cases(ast_tools, name, file).await;

                        // Save test file (in Build mode)
                        if context.mode == OperatingMode::Build && !test_code.is_empty() {
                            let test_file = self.get_test_file_path(file);
                            // Here you would write the test file
                            modified_files.push(test_file);
                            tests_generated += 1;
                        }
                    }
                }

                all_findings.extend(coverage_findings);
            }

            let summary = format!(
                "Test generation completed: {} files analyzed, {} missing tests found, {} test files generated",
                analyzed_files.len(),
                all_findings.len(),
                tests_generated
            );

            // Store the length before moving all_findings
            let missing_tests = all_findings.len();

            let execution_time = SystemTime::now()
                .duration_since(start_time)
                .unwrap_or_else(|_| Duration::from_secs(0));

            Ok(AgentResult {
                agent_name: self.name.clone(),
                status: AgentStatus::Completed,
                findings: all_findings,
                analyzed_files,
                modified_files,
                execution_time,
                summary,
                metrics: HashMap::from([
                    (
                        "missing_tests".to_string(),
                        serde_json::json!(missing_tests),
                    ),
                    (
                        "tests_generated".to_string(),
                        serde_json::json!(tests_generated),
                    ),
                    (
                        "test_strategy".to_string(),
                        serde_json::json!(format!("{:?}", self.test_strategy)),
                    ),
                ]),
            })
        })
    }

    fn capabilities(&self) -> Vec<String> {
        vec![
            "test-generation".to_string(),
            "coverage-analysis".to_string(),
            "edge-case-generation".to_string(),
            "mock-generation".to_string(),
            "test-data-generation".to_string(),
        ]
    }

    fn supports_file_type(&self, file_path: &Path) -> bool {
        let supported = ["rs", "py", "js", "ts", "go", "java"];
        file_path
            .extension()
            .and_then(|ext| ext.to_str())
            .map(|ext| supported.contains(&ext))
            .unwrap_or(false)
    }

    fn execution_time_estimate(&self) -> Duration {
        match self.test_strategy {
            TestStrategy::Basic => Duration::from_secs(45),
            TestStrategy::Comprehensive => Duration::from_secs(90),
            TestStrategy::PropertyBased => Duration::from_secs(120),
        }
    }
}

impl TestWriterAgent {
    fn get_test_targets(&self, context: &SubagentContext) -> Result<Vec<PathBuf>, SubagentError> {
        if let Some(files) = context.parameters.get("files") {
            Ok(files.split(',').map(|s| PathBuf::from(s.trim())).collect())
        } else {
            Ok(vec![context.working_directory.clone()])
        }
    }

    fn get_test_file_path(&self, source_file: &Path) -> PathBuf {
        let stem = source_file.file_stem().unwrap_or_default();
        let ext = source_file.extension().unwrap_or_default();
        let parent = source_file.parent().unwrap_or(Path::new("."));

        parent.join(format!(
            "{}_test.{}",
            stem.to_string_lossy(),
            ext.to_string_lossy()
        ))
    }
}

/// Convert snake_case to PascalCase
fn to_pascal_case(s: &str) -> String {
    s.split('_')
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                None => String::new(),
                Some(first) => first.to_uppercase().chain(chars).collect(),
            }
        })
        .collect()
}