#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod coverage_tests {
use super::*;
use crate::contracts::{BaseAnalysisContract, OutputFormat, QualityProfile, SatdSeverity};
use std::path::PathBuf;
use tempfile::TempDir;
fn create_test_dir() -> TempDir {
tempfile::tempdir().expect("Failed to create temp directory")
}
fn create_test_file(dir: &TempDir) -> PathBuf {
let file_path = dir.path().join("test.rs");
std::fs::write(&file_path, "fn main() { println!(\"hello\"); }").unwrap();
file_path
}
fn create_base_contract(path: PathBuf) -> BaseAnalysisContract {
BaseAnalysisContract {
path,
format: OutputFormat::Json,
output: None,
top_files: Some(10),
include_tests: false,
timeout: 60,
}
}
#[test]
fn test_real_contract_service_new() {
let service = RealContractService::new();
assert!(service.is_ok(), "RealContractService::new() should succeed");
}
#[test]
fn test_real_contract_service_default() {
let service = RealContractService::default();
assert!(Arc::strong_count(&service.inner) >= 1);
}
#[tokio::test]
async fn test_analyze_complexity() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeComplexityContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
max_cyclomatic: Some(20),
max_cognitive: Some(15),
max_halstead: Some(50.0),
};
let result = service.analyze_complexity(contract).await;
assert!(result.is_ok(), "analyze_complexity should succeed");
let value = result.unwrap();
assert!(value.get("summary").is_some());
assert!(value.get("results").is_some());
assert!(value.get("metadata").is_some());
}
#[tokio::test]
async fn test_analyze_complexity_default_thresholds() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeComplexityContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
max_cyclomatic: None,
max_cognitive: None,
max_halstead: None,
};
let result = service.analyze_complexity(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_analyze_satd() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeSatdContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
severity: Some(SatdSeverity::Medium),
critical_only: false,
strict: true,
fail_on_violation: false,
};
let result = service.analyze_satd(contract).await;
assert!(result.is_ok(), "analyze_satd should succeed");
let value = result.unwrap();
assert!(value.get("summary").is_some());
}
#[tokio::test]
async fn test_analyze_satd_critical_only() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeSatdContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
severity: Some(SatdSeverity::Critical),
critical_only: true,
strict: false,
fail_on_violation: false,
};
let result = service.analyze_satd(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_analyze_dead_code() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeDeadCodeContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
include_unreachable: true,
min_dead_lines: 5,
max_percentage: 20.0,
fail_on_violation: false,
};
let result = service.analyze_dead_code(contract).await;
assert!(result.is_ok(), "analyze_dead_code should succeed");
let value = result.unwrap();
assert!(value.get("results").is_some());
}
#[tokio::test]
async fn test_analyze_dead_code_no_unreachable() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeDeadCodeContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
include_unreachable: false,
min_dead_lines: 1,
max_percentage: 100.0,
fail_on_violation: false,
};
let result = service.analyze_dead_code(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_analyze_tdg() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeTdgContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
threshold: 1.5,
include_components: true,
critical_only: false,
};
let result = service.analyze_tdg(contract).await;
assert!(result.is_ok(), "analyze_tdg should succeed");
let value = result.unwrap();
assert!(value.get("summary").is_some());
}
#[tokio::test]
async fn test_analyze_tdg_without_components() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeTdgContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
threshold: 2.5,
include_components: false,
critical_only: true,
};
let result = service.analyze_tdg(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_analyze_lint_hotspot() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeLintHotspotContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
file: None,
max_density: 5.0,
min_confidence: 0.8,
enforce: false,
dry_run: false,
};
let result = service.analyze_lint_hotspot(contract).await;
assert!(result.is_ok(), "analyze_lint_hotspot should succeed");
}
#[tokio::test]
async fn test_analyze_lint_hotspot_dry_run() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = AnalyzeLintHotspotContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
file: None,
max_density: 3.0,
min_confidence: 0.9,
enforce: true,
dry_run: true,
};
let result = service.analyze_lint_hotspot(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_quality_gate_standard_profile() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = QualityGateContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
profile: QualityProfile::Standard,
file: None,
fail_on_violation: false,
verbose: true,
};
let result = service.quality_gate(contract).await;
assert!(result.is_ok(), "quality_gate should succeed");
let value = result.unwrap();
assert!(value.get("passed").is_some());
}
#[tokio::test]
async fn test_quality_gate_strict_profile() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = QualityGateContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
profile: QualityProfile::Strict,
file: None,
fail_on_violation: false,
verbose: false,
};
let result = service.quality_gate(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_quality_gate_extreme_profile() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = QualityGateContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
profile: QualityProfile::Extreme,
file: None,
fail_on_violation: false,
verbose: true,
};
let result = service.quality_gate(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_quality_gate_toyota_profile() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let contract = QualityGateContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
profile: QualityProfile::Toyota,
file: None,
fail_on_violation: false,
verbose: false,
};
let result = service.quality_gate(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_refactor_auto() {
let temp_dir = create_test_dir();
let test_file = create_test_file(&temp_dir);
let service = RealContractService::new().unwrap();
let contract = RefactorAutoContract {
file: test_file,
format: OutputFormat::Json,
output: None,
target_complexity: 10,
dry_run: true,
timeout: 60,
};
let result = service.refactor_auto(contract).await;
assert!(result.is_ok(), "refactor_auto should succeed");
let value = result.unwrap();
assert!(value.get("plan").is_some());
assert!(value.get("dry_run").is_some());
}
#[tokio::test]
async fn test_refactor_auto_apply() {
let temp_dir = create_test_dir();
let test_file = create_test_file(&temp_dir);
let service = RealContractService::new().unwrap();
let contract = RefactorAutoContract {
file: test_file,
format: OutputFormat::Markdown,
output: None,
target_complexity: 5,
dry_run: false,
timeout: 120,
};
let result = service.refactor_auto(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_output_formats() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
for format in [
OutputFormat::Table,
OutputFormat::Json,
OutputFormat::Yaml,
OutputFormat::Markdown,
OutputFormat::Csv,
OutputFormat::Summary,
] {
let mut base = create_base_contract(temp_dir.path().to_path_buf());
base.format = format;
let contract = AnalyzeComplexityContract {
base,
max_cyclomatic: None,
max_cognitive: None,
max_halstead: None,
};
let result = service.analyze_complexity(contract).await;
assert!(result.is_ok(), "Should work with format {:?}", format);
}
}
#[tokio::test]
async fn test_with_output_file() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let output_path = temp_dir.path().join("output.json");
let mut base = create_base_contract(temp_dir.path().to_path_buf());
base.output = Some(output_path);
let contract = AnalyzeComplexityContract {
base,
max_cyclomatic: Some(30),
max_cognitive: None,
max_halstead: None,
};
let result = service.analyze_complexity(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_include_tests_flag() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
let mut base = create_base_contract(temp_dir.path().to_path_buf());
base.include_tests = true;
let contract = AnalyzeSatdContract {
base,
severity: None,
critical_only: false,
strict: false,
fail_on_violation: false,
};
let result = service.analyze_satd(contract).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_all_satd_severities() {
let temp_dir = create_test_dir();
let service = RealContractService::new().unwrap();
for severity in [
SatdSeverity::Low,
SatdSeverity::Medium,
SatdSeverity::High,
SatdSeverity::Critical,
] {
let contract = AnalyzeSatdContract {
base: create_base_contract(temp_dir.path().to_path_buf()),
severity: Some(severity),
critical_only: false,
strict: false,
fail_on_violation: false,
};
let result = service.analyze_satd(contract).await;
assert!(result.is_ok(), "Should work with severity {:?}", severity);
}
}
#[test]
fn test_service_inner_arc() {
let service = RealContractService::new().unwrap();
let arc_clone = Arc::clone(&service.inner);
assert_eq!(Arc::strong_count(&service.inner), 2);
drop(arc_clone);
assert_eq!(Arc::strong_count(&service.inner), 1);
}
}