sql-cli 1.82.1

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
Documentation
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!");
}