#![allow(deprecated)]
use assert_cmd::cargo::CommandCargoExt;
use serde_json::Value;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;
#[test]
#[ignore = "requires pre-built binary, run with --ignored"]
fn test_cli_output_format_unified_produces_valid_structure() {
let temp_dir = TempDir::new().unwrap();
let output_path = temp_dir.path().join("unified_output.json");
let test_codebase =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/fixtures/sample_codebase");
let output = Command::cargo_bin("debtmap")
.unwrap()
.args([
"analyze",
"--format",
"json",
"--output",
output_path.to_str().unwrap(),
test_codebase.to_str().unwrap(),
])
.output()
.expect("Failed to execute debtmap command");
if !output.status.success() {
eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout));
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
panic!("debtmap analyze command failed");
}
let output_content = fs::read_to_string(&output_path).expect("Failed to read output file");
let json: Value = serde_json::from_str(&output_content).expect("Output is not valid JSON");
assert!(json.get("metadata").is_some(), "Missing metadata section");
assert!(json.get("items").is_some(), "Missing items section");
assert!(json.get("summary").is_some(), "Missing summary section");
let metadata = json.get("metadata").unwrap();
assert!(
metadata.get("debtmap_version").is_some(),
"Missing metadata.debtmap_version"
);
assert!(
metadata.get("generated_at").is_some(),
"Missing metadata.generated_at"
);
assert!(
metadata.get("analysis_type").is_some(),
"Missing metadata.analysis_type"
);
let items = json.get("items").unwrap();
assert!(items.is_array(), "items should be an array");
if let Some(item_array) = items.as_array() {
if !item_array.is_empty() {
let first_item = &item_array[0];
assert!(
first_item.get("type").is_some(),
"Debt item missing 'type' field"
);
assert!(
first_item.get("location").is_some(),
"Debt item missing 'location' field"
);
assert!(
first_item.get("category").is_some(),
"Debt item missing 'category' field"
);
assert!(
first_item.get("priority").is_some(),
"Debt item missing 'priority' field"
);
assert!(
first_item.get("score").is_some(),
"Debt item missing 'score' field"
);
let item_type = first_item.get("type").unwrap().as_str().unwrap();
assert!(
item_type == "File" || item_type == "Function",
"Type must be 'File' or 'Function', got: {}",
item_type
);
let location = first_item.get("location").unwrap();
assert!(location.get("file").is_some(), "Location missing 'file'");
let priority = first_item.get("priority").unwrap().as_str().unwrap();
assert!(
priority == "high" || priority == "medium" || priority == "low",
"Priority must be 'high', 'medium', or 'low', got: {}",
priority
);
assert!(
first_item.get("score").unwrap().is_number(),
"Score should be a number"
);
}
}
let summary = json.get("summary").unwrap();
assert!(
summary.get("total_items").is_some(),
"Summary missing 'total_items'"
);
if summary.get("by_category").is_some() {
assert!(
summary.get("by_category").unwrap().is_object(),
"by_category should be an object"
);
}
if summary.get("by_severity").is_some() {
assert!(
summary.get("by_severity").unwrap().is_object(),
"by_severity should be an object"
);
}
}
#[test]
#[ignore = "requires pre-built binary, run with --ignored"]
fn test_cli_unified_format_scope_filtering() {
let temp_dir = TempDir::new().unwrap();
let output_path = temp_dir.path().join("unified_output.json");
let test_codebase =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/fixtures/sample_codebase");
let output = Command::cargo_bin("debtmap")
.unwrap()
.args([
"analyze",
"--format",
"json",
"--output",
output_path.to_str().unwrap(),
test_codebase.to_str().unwrap(),
])
.output()
.expect("Failed to execute debtmap command");
assert!(output.status.success(), "Command should succeed");
let output_content = fs::read_to_string(&output_path).unwrap();
let json: Value = serde_json::from_str(&output_content).unwrap();
let items = json.get("items").unwrap().as_array().unwrap();
let file_items: Vec<_> = items
.iter()
.filter(|item| item.get("type").unwrap().as_str().unwrap() == "File")
.collect();
let function_items: Vec<_> = items
.iter()
.filter(|item| item.get("type").unwrap().as_str().unwrap() == "Function")
.collect();
assert_eq!(
file_items.len() + function_items.len(),
items.len(),
"All items should have type of 'File' or 'Function'"
);
}
#[test]
#[ignore = "requires pre-built binary, run with --ignored"]
fn test_cli_unified_format_metrics_presence() {
let temp_dir = TempDir::new().unwrap();
let output_path = temp_dir.path().join("unified_output.json");
let test_codebase =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/data/fixtures/sample_codebase");
let output = Command::cargo_bin("debtmap")
.unwrap()
.args([
"analyze",
"--format",
"json",
"--output",
output_path.to_str().unwrap(),
test_codebase.to_str().unwrap(),
])
.output()
.expect("Failed to execute debtmap command");
assert!(output.status.success(), "Command should succeed");
let output_content = fs::read_to_string(&output_path).unwrap();
let json: Value = serde_json::from_str(&output_content).unwrap();
let items = json.get("items").unwrap().as_array().unwrap();
for item in items {
let score = item.get("score").expect("Each item should have score");
assert!(score.is_number(), "Score should be a number");
if let Some(details) = item.get("details") {
assert!(details.is_object(), "Details should be an object");
}
}
}