use std::path::Path;
use tempfile::TempDir;
use to_markdown_gui::commands::file_ops;
#[test]
fn test_duplicate_file() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path();
let src_path = temp_path.join("test_file.txt");
let original_content = b"Hello, World!";
std::fs::write(&src_path, original_content).expect("Failed to write source file");
let dup_path = file_ops::duplicate_file_impl(&src_path).expect("duplicate_file_impl should succeed");
assert!(dup_path.exists(), "Duplicate file should exist");
assert_eq!(dup_path.file_name().unwrap(), "test_file copy.txt");
let dup_content = std::fs::read(&dup_path).expect("Failed to read duplicate file");
assert_eq!(dup_content, original_content, "Content should match");
assert!(src_path.exists(), "Original file should still exist");
assert!(dup_path.exists(), "Duplicate file should exist");
}
#[test]
fn test_duplicate_file_numbering() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path();
let src_path = temp_path.join("document.md");
std::fs::write(&src_path, b"# Original Document").expect("Failed to write original");
let dup1 = file_ops::duplicate_file_impl(&src_path).expect("First duplicate should succeed");
assert_eq!(dup1.file_name().unwrap(), "document copy.md");
assert!(dup1.exists());
let dup2 = file_ops::duplicate_file_impl(&src_path).expect("Second duplicate should succeed");
assert_eq!(dup2.file_name().unwrap(), "document copy (2).md");
assert!(dup2.exists());
let dup3 = file_ops::duplicate_file_impl(&src_path).expect("Third duplicate should succeed");
assert_eq!(dup3.file_name().unwrap(), "document copy (3).md");
assert!(dup3.exists());
let original_content = std::fs::read(&src_path).expect("Failed to read original");
let dup1_content = std::fs::read(&dup1).expect("Failed to read dup1");
let dup2_content = std::fs::read(&dup2).expect("Failed to read dup2");
let dup3_content = std::fs::read(&dup3).expect("Failed to read dup3");
assert_eq!(dup1_content, original_content);
assert_eq!(dup2_content, original_content);
assert_eq!(dup3_content, original_content);
}
#[test]
fn test_duplicate_file_missing_file() {
let nonexistent = Path::new("/tmp/surely_does_not_exist_12345.txt");
let result = file_ops::duplicate_file_impl(nonexistent);
assert!(result.is_err(), "Should error when file doesn't exist");
}
#[test]
fn test_duplicate_file_no_extension() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path();
let src_path = temp_path.join("README");
std::fs::write(&src_path, b"Read me!").expect("Failed to write file");
let dup_path = file_ops::duplicate_file_impl(&src_path).expect("Duplicate should succeed");
assert_eq!(dup_path.file_name().unwrap(), "README copy");
assert!(dup_path.exists());
}
#[test]
fn test_create_markdown_note() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path();
let note_name = "test_note.md";
let note_path = file_ops::create_markdown_note_impl(temp_path, note_name)
.expect("create_markdown_note_impl should succeed");
assert!(note_path.exists(), "Note file should exist");
assert_eq!(note_path.file_name().unwrap(), note_name);
let content = std::fs::read_to_string(¬e_path).expect("Failed to read note file");
assert_eq!(content, "", "Note should be empty");
assert_eq!(note_path.parent().unwrap(), temp_path);
}
#[test]
fn test_create_markdown_note_invalid_name() {
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_path = temp_dir.path();
let result = file_ops::create_markdown_note_impl(temp_path, "");
assert!(result.is_err(), "Should error when name is empty");
let result = file_ops::create_markdown_note_impl(temp_path, " ");
assert!(result.is_err(), "Should error when name is only whitespace");
}