use std::fs;
use std::process::Command;
#[test]
fn test_extract_with_prompt_and_instructions() {
let temp_dir = tempfile::tempdir().unwrap();
let file_path = temp_dir.path().join("test_file.rs");
let content = r#"
fn test() {
println!("Hello, world!");
}
"#;
fs::write(&file_path, content).unwrap();
let project_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let output = Command::new("cargo")
.args([
"run",
"--manifest-path",
project_dir.join("Cargo.toml").to_string_lossy().as_ref(),
"--",
"extract",
file_path.to_string_lossy().as_ref(),
"--format",
"json",
"--prompt",
"engineer",
"--instructions",
"Explain what this function does",
"--allow-tests", ])
.output()
.expect("Failed to execute command");
assert!(output.status.success(), "Command failed to execute");
let stdout = String::from_utf8_lossy(&output.stdout);
fn extract_json_from_output(output: &str) -> &str {
if let Some(start_index) = output.find('{') {
&output[start_index..]
} else {
output
}
}
let json_str = extract_json_from_output(&stdout);
let json_value: serde_json::Value =
serde_json::from_str(json_str).expect("Failed to parse JSON output");
assert!(json_value.is_object(), "JSON output should be an object");
assert!(
json_value.get("results").is_some(),
"JSON output should have a 'results' field"
);
let results = json_value.get("results").unwrap().as_array().unwrap();
assert!(!results.is_empty(), "Results array should not be empty");
assert!(
json_value.get("system_prompt").is_some(),
"JSON output should have a 'system_prompt' field"
);
let system_prompt = json_value.get("system_prompt").unwrap().as_str().unwrap();
assert!(
system_prompt.contains("senior software engineer"),
"System prompt should contain the engineer template"
);
assert!(
json_value.get("user_instructions").is_some(),
"JSON output should have a 'user_instructions' field"
);
let user_instructions = json_value
.get("user_instructions")
.unwrap()
.as_str()
.unwrap();
assert_eq!(
user_instructions, "Explain what this function does",
"User instructions should match the input"
);
assert_eq!(
user_instructions, "Explain what this function does",
"User instructions should match the input"
);
}