use sql_cli::history::CommandHistory;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_history_protection_integration() {
println!("Testing History Protection Integration...\n");
let temp_dir = TempDir::new().unwrap();
#[cfg(windows)]
{
std::env::set_var("APPDATA", temp_dir.path());
std::env::set_var("LOCALAPPDATA", temp_dir.path());
}
#[cfg(unix)]
{
std::env::set_var("HOME", temp_dir.path());
}
let mut history = CommandHistory::new().unwrap();
for i in 1..=5 {
let cmd = format!("SELECT * FROM table_{i}");
history.add_entry(cmd.clone(), true, Some(100)).unwrap();
}
let entries = history.get_all();
assert_eq!(entries.len(), 5, "Should have 5 entries");
let backup_dir = temp_dir.path().join("sql-cli").join("history_backups");
let history_file = temp_dir.path().join("sql-cli").join("history.json");
if history_file.exists() {
println!("History file exists at: {history_file:?}");
}
history.clear().unwrap();
if backup_dir.exists() {
let backups: Vec<_> = fs::read_dir(&backup_dir)
.unwrap()
.filter_map(std::result::Result::ok)
.collect();
assert!(
!backups.is_empty(),
"Should have created backup before clear"
);
println!("Found {} backup files", backups.len());
} else {
println!("Note: Backup directory not created (entries might be below threshold)");
}
println!("✓ History protection integration test passed!");
}