use std::fs;
use std::path::Path;
use tempfile::TempDir;
use probe_code::search::query::create_query_plan;
use probe_code::search::{perform_probe, SearchOptions};
#[test]
fn test_negative_compound_words() {
let temp_dir = TempDir::new().unwrap();
let temp_path = temp_dir.path();
create_test_files(temp_path);
test_basic_negative_compound_word(temp_path);
test_complex_negative_compound_word(temp_path);
test_excluded_terms_extraction();
}
fn create_test_files(temp_dir: &Path) {
let file1_path = temp_dir.join("network_firewall.go");
let file1_content = r#"
package security
// NetworkConfig configures the network settings
type NetworkConfig struct {
FirewallEnabled bool
}
// FirewallRule represents a firewall rule
type FirewallRule struct {
Name string
Action string
}
"#;
let file2_path = temp_dir.join("network_only.go");
let file2_content = r#"
package network
// NetworkSettings configures the network settings
type NetworkSettings struct {
Enabled bool
Settings string
}
"#;
let file3_path = temp_dir.join("networkfirewall.go");
let file3_content = r#"
package security
// NetworkFirewall configures the network firewall
type NetworkFirewall struct {
Enabled bool
}
"#;
fs::write(file1_path, file1_content).unwrap();
fs::write(file2_path, file2_content).unwrap();
fs::write(file3_path, file3_content).unwrap();
}
fn test_basic_negative_compound_word(temp_path: &Path) {
println!("\n=== Testing basic negative compound word: -networkfirewall ===");
std::env::set_var("DEBUG", "1");
let query = "-networkfirewall";
let queries = vec![query.to_string()];
let custom_ignores: Vec<String> = vec![];
let options = SearchOptions {
path: temp_path,
queries: &queries,
files_only: false,
custom_ignores: &custom_ignores,
exclude_filenames: false,
language: None,
reranker: "hybrid",
frequency_search: true,
max_results: None,
max_bytes: None,
max_tokens: None,
allow_tests: true,
no_merge: true,
merge_threshold: None,
dry_run: false,
session: None,
timeout: 30,
exact: false,
};
let search_results = perform_probe(&options).unwrap();
let found_networkfirewall = search_results
.results
.iter()
.any(|r| r.file.contains("networkfirewall.go"));
assert!(
!found_networkfirewall,
"Should NOT find networkfirewall.go with negative compound word"
);
let found_network_firewall = search_results
.results
.iter()
.any(|r| r.file.contains("network_firewall.go"));
assert!(
!found_network_firewall,
"Should NOT find network_firewall.go with negative compound word"
);
let found_network_only = search_results
.results
.iter()
.any(|r| r.file.contains("network_only.go"));
assert!(
!found_network_only,
"Should NOT find network_only.go with negative compound word"
);
}
fn test_complex_negative_compound_word(temp_path: &Path) {
println!("\n=== Testing complex query with multiple negative terms: settings AND -network AND -firewall ===");
let query = "settings AND -network AND -firewall";
let queries = vec![query.to_string()];
let custom_ignores: Vec<String> = vec![];
let options = SearchOptions {
path: temp_path,
queries: &queries,
files_only: false,
custom_ignores: &custom_ignores,
exclude_filenames: false,
language: None,
reranker: "hybrid",
frequency_search: true,
max_results: None,
max_bytes: None,
max_tokens: None,
allow_tests: true,
no_merge: true,
merge_threshold: None,
dry_run: false,
session: None,
timeout: 30,
exact: false,
};
let search_results = perform_probe(&options).unwrap();
let found_network_only = search_results
.results
.iter()
.any(|r| r.file.contains("network_only.go"));
assert!(
!found_network_only,
"Should NOT find network_only.go with 'settings' but excluding 'network'"
);
let found_networkfirewall = search_results
.results
.iter()
.any(|r| r.file.contains("networkfirewall.go"));
assert!(
!found_networkfirewall,
"Should NOT find networkfirewall.go with negative terms '-network' and '-firewall'"
);
let found_network_firewall = search_results
.results
.iter()
.any(|r| r.file.contains("network_firewall.go"));
assert!(
!found_network_firewall,
"Should NOT find network_firewall.go with negative terms '-network' and '-firewall'"
);
}
fn test_excluded_terms_extraction() {
println!("\n=== Testing excluded terms extraction ===");
let query = "-networkfirewall";
let plan = create_query_plan(query, false).unwrap();
assert!(
plan.excluded_terms.contains("networkfirewall"),
"Original term 'networkfirewall' should be in excluded_terms"
);
let complex_query = "settings AND -networkfirewall";
let complex_plan = create_query_plan(complex_query, false).unwrap();
assert!(
complex_plan.excluded_terms.contains("networkfirewall"),
"Original term 'networkfirewall' should be in excluded_terms"
);
std::env::remove_var("DEBUG");
}