#![cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
use crate::stateless_server::StatelessTemplateServer;
use std::sync::Arc;
use tempfile::TempDir;
#[tokio::test]
async fn test_cli_run_generate_to_file() {
let _server = Arc::new(StatelessTemplateServer::new().unwrap());
let temp_dir = TempDir::new().unwrap();
let output_file = temp_dir.path().join("Makefile");
assert!(!output_file.exists());
}
#[tokio::test]
async fn test_cli_generate_template_direct() {
use crate::services::template_service::generate_template;
let server = StatelessTemplateServer::new().unwrap();
let mut params = serde_json::Map::new();
params.insert(
"project_name".to_string(),
serde_json::json!("test-project"),
);
let result = generate_template(&server, "template://makefile/rust/cli", params).await;
assert!(result.is_ok());
let generated = result.unwrap();
assert!(generated.content.contains("test-project"));
assert!(generated.content.contains("build:"));
assert!(generated.content.contains("test:"));
}
#[tokio::test]
async fn test_cli_list_templates_direct() {
use crate::services::template_service::list_templates;
let server = StatelessTemplateServer::new().unwrap();
let templates = list_templates(&server, None, None).await;
assert!(templates.is_ok());
let list = templates.unwrap();
assert!(list.len() >= 9);
let rust_templates = list_templates(&server, Some("rust"), None).await.unwrap();
assert!(rust_templates.iter().all(|t| {
matches!(
t.toolchain,
crate::models::template::Toolchain::RustCli { .. }
)
}));
let makefile_templates = list_templates(&server, None, Some("makefile"))
.await
.unwrap();
assert!(makefile_templates.iter().all(|t| {
matches!(
t.category,
crate::models::template::TemplateCategory::Makefile
)
}));
}
#[tokio::test]
async fn test_cli_search_templates_direct() {
use crate::services::template_service::search_templates;
let server = Arc::new(StatelessTemplateServer::new().unwrap());
let results = search_templates(server.clone(), "rust", None).await;
assert!(results.is_ok());
let search_results = results.unwrap();
assert!(!search_results.is_empty());
assert!(search_results
.iter()
.any(|r| r.template.uri.contains("rust")));
}
#[tokio::test]
async fn test_cli_validate_template_direct() {
use crate::services::template_service::validate_template;
let server = Arc::new(StatelessTemplateServer::new().unwrap());
let params = serde_json::json!({});
let result =
validate_template(server.clone(), "template://makefile/rust/cli", ¶ms).await;
assert!(result.is_ok());
let validation = result.unwrap();
assert!(!validation.valid);
assert!(!validation.errors.is_empty());
let params = serde_json::json!({
"project_name": "test-project"
});
let result =
validate_template(server.clone(), "template://makefile/rust/cli", ¶ms).await;
assert!(result.is_ok());
let validation = result.unwrap();
assert!(validation.valid);
assert!(validation.errors.is_empty());
}
#[tokio::test]
async fn test_cli_scaffold_project_direct() {
use crate::services::template_service::scaffold_project;
let server = Arc::new(StatelessTemplateServer::new().unwrap());
let templates = vec!["makefile".to_string(), "readme".to_string()];
let params = serde_json::json!({
"project_name": "test-scaffold",
"description": "A test project for scaffolding"
});
let result = scaffold_project(server.clone(), "rust", templates, params).await;
assert!(result.is_ok());
let scaffold_result = result.unwrap();
assert_eq!(scaffold_result.errors.len(), 0, "Should have no errors");
assert_eq!(scaffold_result.files.len(), 2);
assert!(scaffold_result
.files
.iter()
.any(|f| f.path.ends_with("Makefile")));
assert!(scaffold_result
.files
.iter()
.any(|f| f.path.ends_with("README.md")));
}
#[tokio::test]
#[serial_test::serial]
#[ignore] async fn test_cli_context_generation() {
if std::env::var("SKIP_SLOW_TESTS").is_ok() || std::env::var("CI").is_ok() {
return;
}
use crate::services::context::{analyze_project, format_context_as_markdown};
use std::path::PathBuf;
let project_path = PathBuf::from(".");
let context = analyze_project(&project_path, "rust").await;
assert!(context.is_ok());
let project_context = context.unwrap();
assert!(project_context.summary.total_functions > 0);
let markdown = format_context_as_markdown(&project_context);
assert!(markdown.contains("# Project Context"));
assert!(markdown.contains("## Summary"));
}
#[tokio::test]
async fn test_cli_churn_analysis() {
use crate::handlers::tools::{
format_churn_as_csv, format_churn_as_markdown, format_churn_summary,
};
use crate::services::git_analysis::GitAnalysisService;
use std::path::PathBuf;
let project_path = PathBuf::from(".");
let result = GitAnalysisService::analyze_code_churn(&project_path, 30);
if let Ok(analysis) = result {
let summary = format_churn_summary(&analysis);
assert!(summary.contains("Code Churn Analysis"));
let markdown = format_churn_as_markdown(&analysis);
assert!(markdown.contains("# Code Churn Analysis Report"));
let csv = format_churn_as_csv(&analysis);
assert!(csv.contains("file_path,commits,additions,deletions,churn_score"));
}
}
}