use std::fs;
use std::process::Command;
use probe_code::extract::{handle_extract, ExtractOptions};
#[test]
fn test_deduplication_of_nested_extractions() {
let temp_dir = tempfile::tempdir().unwrap();
let file_path = temp_dir.path().join("nested_test.rs");
let content = r#"
fn outer_function() {
let x = 10;
// This is a nested function that should be deduplicated
fn inner_function() {
let y = 20;
println!("Inner function: {}", y);
}
// Call the inner function
inner_function();
println!("Outer function: {}", x);
}
fn standalone_function() {
println!("This is standalone");
}
"#;
fs::write(&file_path, content).unwrap();
std::env::set_var("DEBUG", "1");
let options = ExtractOptions {
files: vec![
format!("{}:2", file_path.to_string_lossy()), format!("{}:6", file_path.to_string_lossy()), format!("{}:16", file_path.to_string_lossy()), ],
custom_ignores: vec![],
context_lines: 0,
format: "plain".to_string(),
from_clipboard: false,
input_file: None,
to_clipboard: false,
dry_run: true, diff: false,
allow_tests: true,
keep_input: false,
prompt: None,
instructions: None,
};
handle_extract(options).unwrap();
std::env::remove_var("DEBUG");
}
#[test]
fn test_deduplication_with_command_line_integration() {
let temp_dir = tempfile::tempdir().unwrap();
let file_path = temp_dir.path().join("nested_test.rs");
let content = r#"
fn outer_function() {
let x = 10;
// This is a nested function that should be deduplicated
fn inner_function() {
let y = 20;
println!("Inner function: {}", y);
}
// Call the inner function
inner_function();
println!("Outer function: {}", x);
}
fn standalone_function() {
println!("This is standalone");
}
"#;
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",
&format!("{}:2", file_path.to_string_lossy()), &format!("{}:6", file_path.to_string_lossy()), "--allow-tests",
"--format",
"json",
])
.env("DEBUG", "1")
.output()
.expect("Failed to execute command");
assert!(output.status.success(), "Command failed to execute");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
println!("Command stdout: {stdout}");
println!("Command stderr: {stderr}");
assert!(
stdout.contains("Before deduplication:") && stdout.contains("After deduplication:"),
"Deduplication logs not found in output"
);
assert!(
stdout.contains("\"count\": 1"),
"Should only have one result after deduplication"
);
assert!(
stdout.contains("outer_function"),
"Output should contain the outer function"
);
}