1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use sql_cli::history::CommandHistory;
use std::fs;
use tempfile::TempDir;
// This test is fully isolated: it builds CommandHistory against an explicit
// history file inside a tempdir via `with_history_file`, so it never touches
// the process-global environment or the developer's real history. That removes
// the old flakiness — the previous version redirected HOME / APPDATA, which
// (a) didn't work on Windows at all (dirs::data_dir resolves via the Win32
// known-folder API, ignoring those env vars) and (b) is process-global, so
// parallel tests raced on it. No `#[serial]` needed as a result.
#[test]
fn test_history_protection_integration() {
println!("Testing History Protection Integration...\n");
// Create temp directory for test, mirroring the real layout: the app keeps
// history under a `sql-cli/` subdirectory of its data dir.
let temp_dir = TempDir::new().unwrap();
let data_dir = temp_dir.path().join("sql-cli");
fs::create_dir_all(&data_dir).unwrap();
let history_file = data_dir.join("history.json");
// Create history instance backed by the isolated file
let mut history = CommandHistory::with_history_file(history_file.clone()).unwrap();
// Add some entries
for i in 1..=5 {
let cmd = format!("SELECT * FROM table_{i}");
history.add_entry(cmd.clone(), true, Some(100)).unwrap();
}
// Get current entry count
let entries = history.get_all();
assert_eq!(entries.len(), 5, "Should have 5 entries");
// Check backup directory (sibling of the history file)
let backup_dir = data_dir.join("history_backups");
if history_file.exists() {
println!("History file exists at: {history_file:?}");
}
// Test protection by trying to clear
history.clear().unwrap();
// After clear, check if backup was created
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 {
// Backup dir might not be created if clear happened too fast
// This is OK for the test - the important thing is protection works
println!("Note: Backup directory not created (entries might be below threshold)");
}
println!("✓ History protection integration test passed!");
}