reovim-kernel 0.14.3

Core kernel mechanisms for reovim (Linux kernel/ equivalent)
Documentation
use {
    super::super::*,
    std::path::{Path, PathBuf},
};

#[test]
fn test_recovery_dir() {
    let dir = recovery_dir();
    assert!(dir.ends_with("recovery"));
}

#[test]
fn test_save_buffer_for_recovery() {
    let content = "line 1\nline 2\nline 3";
    let result = save_buffer_for_recovery(999, Some(Path::new("/tmp/test.txt")), content);

    assert!(result.is_ok());
    let path = result.unwrap();
    assert!(path.exists());

    // Verify content
    let saved = std::fs::read_to_string(&path).unwrap();
    assert!(saved.contains("buffer 999"));
    assert!(saved.contains("/tmp/test.txt"));
    assert!(saved.contains("Lines: 3"));
    assert!(saved.contains(content));

    // Cleanup
    std::fs::remove_file(&path).ok();
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn test_list_recovery_files_empty() {
    // Create a unique test directory to avoid interference
    let test_dir = std::env::temp_dir().join("reovim_test_recovery_list");
    if test_dir.exists() {
        std::fs::remove_dir_all(&test_dir).ok();
    }

    // Without any files, should return empty
    let files = list_recovery_files();
    // Note: This test depends on the actual recovery directory state
    assert!(files.is_ok());
}

// ========== save_buffer_for_recovery without original path ==========

#[test]
fn test_save_buffer_for_recovery_no_path() {
    let content = "no path buffer content";
    let result = save_buffer_for_recovery(888, None, content);

    assert!(result.is_ok());
    let path = result.unwrap();
    assert!(path.exists());

    let saved = std::fs::read_to_string(&path).unwrap();
    assert!(saved.contains("buffer 888"));
    // Should NOT contain "Original path" since we passed None
    assert!(!saved.contains("Original path"));
    assert!(saved.contains(content));

    // Cleanup
    std::fs::remove_file(&path).ok();
}

// ========== list_recovery_files with actual files ==========

#[test]
fn test_list_recovery_files_with_files() {
    // Save a file and then list
    let content = "test list content";
    let result = save_buffer_for_recovery(777, Some(Path::new("/tmp/list_test.rs")), content);
    assert!(result.is_ok());
    let saved_path = result.unwrap();

    let files = list_recovery_files().unwrap();
    // Should contain at least one file (the one we just saved)
    assert!(!files.is_empty());

    // Cleanup
    std::fs::remove_file(&saved_path).ok();
}

// ========== cleanup_old_recovery_files ==========

#[test]
fn test_cleanup_old_recovery_files() {
    // Save a file for cleanup test
    let content = "cleanup test content";
    let result = save_buffer_for_recovery(666, Some(Path::new("/tmp/cleanup.rs")), content);
    assert!(result.is_ok());
    let saved_path = result.unwrap();

    // Cleanup with max_age of 0 should remove everything
    // (since max_age_secs=0 means files with age > 0 are removed)
    // However, the file was JUST created, so its age might be 0.
    // Use a very large max_age to keep files and verify it works
    let removed = cleanup_old_recovery_files(999_999).unwrap();
    // We don't know the exact count, but it should not error
    let _ = removed;

    // If the file still exists, clean it up manually
    std::fs::remove_file(&saved_path).ok();
}

// ========== RecoverySnapshot and UnsavedBuffer ==========

#[test]
fn test_recovery_snapshot_debug() {
    let snapshot = RecoverySnapshot {
        unsaved_buffers: vec![UnsavedBuffer {
            id: 1,
            path: Some(PathBuf::from("/test.rs")),
            content_hash: 12345,
            line_count: 42,
        }],
        timestamp: std::time::SystemTime::now(),
    };
    let debug = format!("{snapshot:?}");
    assert!(debug.contains("RecoverySnapshot"));
    assert!(debug.contains("UnsavedBuffer"));
}

// === MC/DC: save_buffer_for_recovery() with original_path = Some(...) (line 89) ===

#[test]
fn test_save_buffer_with_original_path_exercises_some_branch() {
    // Explicitly exercises the `if let Some(orig) = original_path` true branch
    // (line 89) by passing Some(path). The content must contain "Original path".
    // Uses the standard recovery dir; skips gracefully if the dir is not writable.
    let content = "fn hello() {}";
    let result =
        save_buffer_for_recovery(111, Some(Path::new("/home/user/project/hello.rs")), content);

    if result.is_err() {
        // Recovery directory not writable in this environment - skip.
        return;
    }

    let path = result.unwrap();
    let saved = std::fs::read_to_string(&path).unwrap();

    // True branch: "Original path" header must be present
    assert!(saved.contains("# Original path: /home/user/project/hello.rs"));
    assert!(saved.contains("buffer 111"));
    assert!(saved.contains(content));

    std::fs::remove_file(&path).ok();
}

// === MC/DC: save_buffer_for_recovery() with original_path = None (line 89 false branch) ===

#[test]
fn test_save_buffer_without_original_path_exercises_none_branch() {
    // Explicitly exercises the `if let Some(orig) = original_path` false branch
    // (None case) so the "Original path" line is NOT written.
    let content = "scratch content";
    let result = save_buffer_for_recovery(222, None, content);

    if result.is_err() {
        return;
    }

    let path = result.unwrap();
    let saved = std::fs::read_to_string(&path).unwrap();

    // False branch: "Original path" must NOT appear
    assert!(!saved.contains("# Original path:"));
    assert!(saved.contains("buffer 222"));
    assert!(saved.contains(content));

    std::fs::remove_file(&path).ok();
}