repotoire 0.3.112

Graph-powered code analysis CLI. 114 detectors for security, architecture, and code quality.
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
//! AI Missing Tests detector - identifies new code added without tests
//!
//! Detects functions/methods that don't have corresponding test coverage.
//! This is a common pattern when AI generates implementation code but neglects
//! to generate tests.
//!
//! Detection Strategy:
//! 1. Find all functions in non-test files (detected by path pattern)
//! 2. Exclude functions that are themselves test functions
//! 3. Check for corresponding test functions using naming conventions
//! 4. Flag functions without test coverage
//!
//! Test File Detection (by path pattern):
//! - Python: test_*.py, *_test.py, tests/*.py
//! - JavaScript/TypeScript: *.test.js, *.spec.ts, __tests__/*

#![allow(dead_code)] // Module under development - structs/helpers used in tests only

use crate::detectors::base::{Detector, DetectorConfig};
use crate::detectors::function_context::{FunctionContextMap, FunctionRole};
use crate::graph::GraphStore;
use crate::models::{Finding, Severity};
use anyhow::Result;
use regex::Regex;
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;
use tracing::debug;

/// Default configuration
const DEFAULT_MIN_FUNCTION_LOC: usize = 5;
const DEFAULT_EXCLUDE_PRIVATE: bool = true;
const DEFAULT_EXCLUDE_DUNDER: bool = true;
const DEFAULT_MAX_FINDINGS: usize = 50;

/// Test file patterns for different languages
const TEST_FILE_PATTERNS: &[&str] = &[
    // Python
    r"test_.*\.py$",
    r".*_test\.py$",
    r"tests?/.*\.py$",
    r".*tests?\.py$",
    // JavaScript/TypeScript
    r".*\.test\.[jt]sx?$",
    r".*\.spec\.[jt]sx?$",
    r"__tests__/.*\.[jt]sx?$",
];

/// Detects functions/methods that lack corresponding tests
pub struct AIMissingTestsDetector {
    config: DetectorConfig,
    min_function_loc: usize,
    exclude_private: bool,
    exclude_dunder: bool,
    max_findings: usize,
    test_file_patterns: Vec<Regex>,
}

impl AIMissingTestsDetector {
    /// Create a new detector with default settings
    pub fn new() -> Self {
        let test_file_patterns = TEST_FILE_PATTERNS
            .iter()
            .filter_map(|p| Regex::new(p).ok())
            .collect();

        Self {
            config: DetectorConfig::new(),
            min_function_loc: DEFAULT_MIN_FUNCTION_LOC,
            exclude_private: DEFAULT_EXCLUDE_PRIVATE,
            exclude_dunder: DEFAULT_EXCLUDE_DUNDER,
            max_findings: DEFAULT_MAX_FINDINGS,
            test_file_patterns,
        }
    }

    /// Create with custom config
    pub fn with_config(config: DetectorConfig) -> Self {
        let test_file_patterns = TEST_FILE_PATTERNS
            .iter()
            .filter_map(|p| Regex::new(p).ok())
            .collect();

        Self {
            min_function_loc: config.get_option_or("min_function_loc", DEFAULT_MIN_FUNCTION_LOC),
            exclude_private: config.get_option_or("exclude_private", DEFAULT_EXCLUDE_PRIVATE),
            exclude_dunder: config.get_option_or("exclude_dunder", DEFAULT_EXCLUDE_DUNDER),
            max_findings: config.get_option_or("max_findings", DEFAULT_MAX_FINDINGS),
            config,
            test_file_patterns,
        }
    }

    /// Check if a file path matches test file patterns
    fn is_test_file(&self, file_path: &str) -> bool {
        let file_lower = file_path.to_lowercase();
        self.test_file_patterns
            .iter()
            .any(|p| p.is_match(&file_lower))
    }

    /// Check if a function is a test function or fixture
    /// This is a static method for use in detect() without &self
    fn is_test_function(name: &str, file_path: &str) -> bool {
        let name_lower = name.to_lowercase();
        let path_lower = file_path.to_lowercase();

        // Check function name patterns
        if name_lower.starts_with("test_") 
            || name_lower.starts_with("test")  // testSomething (camelCase)
            || name_lower.ends_with("_test")
            || name_lower == "setup"
            || name_lower == "teardown"
            || name_lower == "setup_module"
            || name_lower == "teardown_module"
            || name_lower == "setup_class"
            || name_lower == "teardown_class"
            || name_lower == "setuptestdata"  // Django
            || name_lower.starts_with("fixture_")
        {
            return true;
        }

        // Check if in test file/directory
        if path_lower.contains("/test/")
            || path_lower.contains("/tests/")
            || path_lower.contains("/__tests__/")
            || path_lower.contains("/spec/")
            || path_lower.contains("_test.py")
            || path_lower.contains("_test.go")
            || path_lower.contains("_test.rs")
            || path_lower.contains(".test.ts")
            || path_lower.contains(".test.js")
            || path_lower.contains(".spec.ts")
            || path_lower.contains(".spec.js")
            || path_lower.starts_with("test_")
        {
            return true;
        }

        false
    }

    /// Get possible test function names for a given function
    fn get_test_function_variants(&self, func_name: &str) -> Vec<String> {
        let name_lower = func_name.to_lowercase();
        let mut variants = vec![
            format!("test_{}", name_lower),
            format!("test{}", name_lower),
            format!("{}_test", name_lower),
        ];

        // For methods, also check class-based test names
        if name_lower.contains('_') {
            for part in name_lower.split('_') {
                if part.len() > 2 {
                    variants.push(format!("test_{}", part));
                }
            }
        }

        variants
    }

    /// Get possible test file paths for a given source file
    fn get_test_file_variants(&self, file_path: &str) -> Vec<String> {
        let normalized = file_path.replace('\\', "/");
        let parts: Vec<&str> = normalized.split('/').collect();
        let filename = parts.last().unwrap_or(&"");

        let module_name = if filename.contains('.') {
            filename
                .rsplit_once('.')
                .map(|(name, _)| name)
                .unwrap_or(filename)
        } else {
            filename
        };

        if module_name.is_empty() {
            return vec![];
        }

        vec![
            format!("test_{}.py", module_name),
            format!("tests/test_{}.py", module_name),
            format!("test/test_{}.py", module_name),
            format!("{}_test.py", module_name),
            format!("tests/{}_test.py", module_name),
            format!("{}.test.js", module_name),
            format!("{}.spec.js", module_name),
            format!("{}.test.ts", module_name),
            format!("{}.spec.ts", module_name),
            format!("__tests__/{}.js", module_name),
            format!("__tests__/{}.ts", module_name),
        ]
    }

    /// Generate test suggestion for a function
    fn generate_test_suggestion(&self, func_name: &str, language: &str) -> String {
        let lang = language.to_lowercase();

        if lang == "python" || lang.is_empty() {
            format!(
                r#"Create a comprehensive test for '{}':

```python
def test_{}_success():
    """Test {} with valid input."""
    result = {}(valid_input)
    assert result is not None
    assert result == expected_value

def test_{}_edge_cases():
    """Test {} edge cases."""
    # Test boundary conditions
    assert {}(min_value) == expected_min
    assert {}(max_value) == expected_max

def test_{}_error_handling():
    """Test {} error handling."""
    with pytest.raises(ValueError):
        {}(invalid_input)
```"#,
                func_name,
                func_name,
                func_name,
                func_name,
                func_name,
                func_name,
                func_name,
                func_name,
                func_name,
                func_name,
                func_name
            )
        } else if lang == "javascript" || lang == "typescript" {
            format!(
                r#"Create a comprehensive test for '{}':

```{}
describe('{}', () => {{
  it('should handle valid input', () => {{
    const result = {}(validInput);
    expect(result).toBeDefined();
    expect(result).toEqual(expectedValue);
  }});

  it('should handle edge cases', () => {{
    expect({}(minValue)).toEqual(expectedMin);
    expect({}(maxValue)).toEqual(expectedMax);
  }});

  it('should throw on invalid input', () => {{
    expect(() => {}(invalidInput)).toThrow();
  }});
}});
```"#,
                func_name, lang, func_name, func_name, func_name, func_name, func_name
            )
        } else {
            format!(
                "Add comprehensive test coverage for '{}' with multiple assertions and error handling tests.",
                func_name
            )
        }
    }

    /// Create a finding for a function without tests
    fn create_finding(
        &self,
        _qualified_name: &str,
        name: &str,
        file_path: &str,
        line_start: Option<u32>,
        line_end: Option<u32>,
        loc: usize,
        is_method: bool,
        language: &str,
    ) -> Finding {
        let func_type = if is_method { "method" } else { "function" };

        let description = format!(
            "The {} '{}' has no corresponding test. \
             This is a common pattern when AI generates implementation code without tests.{}",
            func_type,
            name,
            if loc > 0 {
                format!(" The {} has {} lines of code.", func_type, loc)
            } else {
                String::new()
            }
        );

        Finding {
            id: String::new(),
            detector: "AIMissingTestsDetector".to_string(),
            severity: Severity::Medium,
            title: format!("Missing tests for {}: {}", func_type, name),
            description,
            affected_files: vec![PathBuf::from(file_path)],
            line_start,
            line_end,
            suggested_fix: Some(self.generate_test_suggestion(name, language)),
            estimated_effort: Some("Small (15-45 minutes)".to_string()),
            category: Some("test_coverage".to_string()),
            cwe_id: None,
            why_it_matters: Some(
                "Untested code is a risk. Tests catch bugs early, document expected behavior, \
                 and make refactoring safer. AI-generated code especially needs tests since \
                 AI may produce subtly incorrect implementations."
                    .to_string(),
            ),
            ..Default::default()
        }
    }
}

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

impl Detector for AIMissingTestsDetector {
    fn name(&self) -> &'static str {
        "AIMissingTestsDetector"
    }

    fn description(&self) -> &'static str {
        "Detects functions/methods that lack corresponding tests"
    }

    fn category(&self) -> &'static str {
        "ai_generated"
    }

    fn config(&self) -> Option<&DetectorConfig> {
        Some(&self.config)
    }
    fn detect(&self, graph: &dyn crate::graph::GraphQuery) -> Result<Vec<Finding>> {
        let mut findings = Vec::new();
        use std::collections::HashSet;

        // Get all test functions
        let test_funcs: HashSet<String> = graph
            .get_functions()
            .iter()
            .filter(|f| Self::is_test_function(&f.name, &f.file_path))
            .map(|f| f.name.clone())
            .collect();

        // Find complex public functions without tests
        for func in graph.get_functions() {
            // Skip test functions, fixtures, and test files
            if Self::is_test_function(&func.name, &func.file_path) {
                continue;
            }

            // Skip private functions
            if func.name.starts_with('_') && !func.name.starts_with("__") {
                continue;
            }

            // Skip dunder methods
            if func.name.starts_with("__") && func.name.ends_with("__") {
                continue;
            }

            let complexity = func.complexity().unwrap_or(1);
            let loc = func.loc();

            // Only flag complex/large functions
            if complexity < 5 && loc < 20 {
                continue;
            }

            // Check if there's a test for this function
            let test_name = format!("test_{}", func.name);
            if !test_funcs.contains(&test_name)
                && !test_funcs.iter().any(|t| t.contains(&func.name))
            {
                let severity = if complexity > 15 {
                    Severity::High
                } else if complexity > 10 {
                    Severity::Medium
                } else {
                    Severity::Low
                };

                findings.push(Finding {
                    id: String::new(),
                    detector: "AIMissingTestsDetector".to_string(),
                    severity,
                    title: format!("Missing Test: {}", func.name),
                    description: format!(
                        "Function '{}' (complexity: {}, {} LOC) has no test coverage.",
                        func.name, complexity, loc
                    ),
                    affected_files: vec![func.file_path.clone().into()],
                    line_start: Some(func.line_start),
                    line_end: Some(func.line_end),
                    suggested_fix: Some(format!("Add test function: test_{}", func.name)),
                    estimated_effort: Some("Small (30 min)".to_string()),
                    category: Some("ai_watchdog".to_string()),
                    cwe_id: None,
                    why_it_matters: Some("Complex untested code is a maintenance risk".to_string()),
                    ..Default::default()
                });
            }
        }

        // Limit findings
        findings.truncate(50);
        Ok(findings)
    }

    fn uses_context(&self) -> bool {
        true
    }

    fn detect_with_context(
        &self,
        graph: &dyn crate::graph::GraphQuery,
        contexts: &Arc<FunctionContextMap>,
    ) -> Result<Vec<Finding>> {
        let mut findings = Vec::new();

        // Get all test functions (from context or name/path patterns)
        let test_funcs: HashSet<String> = graph
            .get_functions()
            .iter()
            .filter(|f| {
                // Check context first
                if let Some(ctx) = contexts.get(&f.qualified_name) {
                    if ctx.is_test || ctx.role == FunctionRole::Test {
                        return true;
                    }
                }
                // Fall back to name/path patterns
                Self::is_test_function(&f.name, &f.file_path)
            })
            .map(|f| f.name.clone())
            .collect();

        debug!(
            "AIMissingTestsDetector: found {} test functions",
            test_funcs.len()
        );

        // Find complex public functions without tests
        for func in graph.get_functions() {
            // Check context first for test detection
            if let Some(ctx) = contexts.get(&func.qualified_name) {
                if ctx.is_test || ctx.role == FunctionRole::Test {
                    continue;
                }
            }

            // Fall back to name/path pattern check
            if Self::is_test_function(&func.name, &func.file_path) {
                continue;
            }

            // Skip private functions
            if func.name.starts_with('_') && !func.name.starts_with("__") {
                continue;
            }

            // Skip dunder methods
            if func.name.starts_with("__") && func.name.ends_with("__") {
                continue;
            }

            // Get complexity from context or graph
            let complexity = if let Some(ctx) = contexts.get(&func.qualified_name) {
                ctx.complexity.unwrap_or(1)
            } else {
                func.complexity().unwrap_or(1)
            };

            let loc = func.loc();

            // Only flag complex/large functions
            if complexity < 5 && loc < 20 {
                continue;
            }

            // Check if there's a test for this function
            let test_name = format!("test_{}", func.name);
            if !test_funcs.contains(&test_name)
                && !test_funcs.iter().any(|t| t.contains(&func.name))
            {
                let severity = if complexity > 15 {
                    Severity::High
                } else if complexity > 10 {
                    Severity::Medium
                } else {
                    Severity::Low
                };

                findings.push(Finding {
                    id: String::new(),
                    detector: "AIMissingTestsDetector".to_string(),
                    severity,
                    title: format!("Missing Test: {}", func.name),
                    description: format!(
                        "Function '{}' (complexity: {}, {} LOC) has no test coverage.",
                        func.name, complexity, loc
                    ),
                    affected_files: vec![func.file_path.clone().into()],
                    line_start: Some(func.line_start),
                    line_end: Some(func.line_end),
                    suggested_fix: Some(format!("Add test function: test_{}", func.name)),
                    estimated_effort: Some("Small (30 min)".to_string()),
                    category: Some("ai_watchdog".to_string()),
                    cwe_id: None,
                    why_it_matters: Some("Complex untested code is a maintenance risk".to_string()),
                    ..Default::default()
                });
            }
        }

        // Limit findings
        findings.truncate(50);
        debug!(
            "AIMissingTestsDetector: found {} missing test findings",
            findings.len()
        );
        Ok(findings)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_test_file() {
        let detector = AIMissingTestsDetector::new();

        assert!(detector.is_test_file("test_module.py"));
        assert!(detector.is_test_file("module_test.py"));
        assert!(detector.is_test_file("tests/module.py"));
        assert!(detector.is_test_file("app.test.js"));
        assert!(detector.is_test_file("app.spec.ts"));
        assert!(detector.is_test_file("__tests__/app.js"));

        assert!(!detector.is_test_file("module.py"));
        assert!(!detector.is_test_file("app.js"));
    }

    #[test]
    fn test_get_test_function_variants() {
        let detector = AIMissingTestsDetector::new();
        let variants = detector.get_test_function_variants("process_data");

        assert!(variants.contains(&"test_process_data".to_string()));
        assert!(variants.contains(&"testprocess_data".to_string()));
        assert!(variants.contains(&"process_data_test".to_string()));
        assert!(variants.contains(&"test_process".to_string()));
        assert!(variants.contains(&"test_data".to_string()));
    }
}