diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..44e8cf9 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,284 @@ +#[cfg(test)] +mod tests { + use std::fs; + use std::path::Path; + use std::process::Command; + use tempfile::tempdir; + + // Helper function to run the keep command with given arguments + fn run_keep(args: &[&str]) -> std::process::Output { + let output = Command::new("cargo") + .args(["run", "--", args]) + .output() + .expect("Failed to execute command"); + output + } + + // Helper function to create a temporary test environment + fn with_temp_env(f: F) + where + F: FnOnce(&Path), + { + let dir = tempdir().expect("Failed to create temporary directory"); + let db_path = dir.path().join("keep-1.db"); + + // Create the data directory structure + fs::create_dir_all(dir.path()).expect("Failed to create directory"); + + // Run the test + f(dir.path()); + + // Clean up + dir.close().expect("Failed to remove temporary directory"); + } + + // Helper function to check if a file exists in the data directory + fn file_exists(data_dir: &Path, id: i64) -> bool { + let path = data_dir.join(id.to_string()); + path.exists() + } + + // Helper function to get the content of a file + fn file_content(data_dir: &Path, id: i64) -> String { + let path = data_dir.join(id.to_string()); + std::fs::read_to_string(path).expect("Failed to read file") + } + + // Test: Save an item and verify it's stored + #[test] + fn test_save_item() { + with_temp_env(|data_dir| { + // Set the data directory for this test + let env = format!("KEEP_DIR={}", data_dir.display()); + + // Save an item with some content + let input = "test content"; + let mut child = Command::new("sh") + .arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save")) + .spawn() + .expect("Failed to spawn process"); + + child.wait().expect("Failed to wait for child process"); + + // Verify the item was saved + let items: Vec = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --list", env)) + .output() + .expect("Failed to execute command") + .stdout; + + assert!(String::from_utf8_lossy(&items).contains(input)); + }); + } + + // Test: Get an item and verify the content + #[test] + fn test_get_item() { + with_temp_env(|data_dir| { + // Set the data directory for this test + let env = format!("KEEP_DIR={}", data_dir.display()); + + // Save an item with some content + let input = "test content"; + let mut child = Command::new("sh") + .arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save")) + .spawn() + .expect("Failed to spawn process"); + + child.wait().expect("Failed to wait for child process"); + + // Get the item and verify the content + let output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --get", env)) + .output() + .expect("Failed to execute command"); + + assert_eq!(String::from_utf8_lossy(&output.stdout), input); + }); + } + + // Test: List items and verify the output + #[test] + fn test_list_items() { + with_temp_env(|data_dir| { + // Set the data directory for this test + let env = format!("KEEP_DIR={}", data_dir.display()); + + // Save an item with some content + let input = "test content"; + let mut child = Command::new("sh") + .arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save")) + .spawn() + .expect("Failed to spawn process"); + + child.wait().expect("Failed to wait for child process"); + + // List items and verify the output + let output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --list", env)) + .output() + .expect("Failed to execute command"); + + assert!(String::from_utf8_lossy(&output.stdout).contains(input)); + }); + } + + // Test: Delete an item and verify it's removed + #[test] + fn test_delete_item() { + with_temp_env(|data_dir| { + // Set the data directory for this test + let env = format!("KEEP_DIR={}", data_dir.display()); + + // Save an item with some content + let input = "test content"; + let mut child = Command::new("sh") + .arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save")) + .spawn() + .expect("Failed to spawn process"); + + child.wait().expect("Failed to wait for child process"); + + // Get the item ID + let list_output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --list", env)) + .output() + .expect("Failed to execute command"); + let list_output_str = String::from_utf8_lossy(&list_output.stdout); + let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::().expect("Invalid ID"); + + // Delete the item + let output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --delete {}", env, id)) + .output() + .expect("Failed to execute command"); + + // Verify the item was deleted + let list_output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --list", env)) + .output() + .expect("Failed to execute command"); + + assert_eq!(String::from_utf8_lossy(&list_output.stdout).trim(), ""); + }); + } + + // Test: Add metadata to an item and verify it's stored + #[test] + fn test_metadata() { + with_temp_env(|data_dir| { + // Set the data directory for this test + let env = format!("KEEP_DIR={}", data_dir.display()); + + // Save an item with some content and metadata + let input = "test content"; + let mut child = Command::new("sh") + .arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save --meta key=value")) + .spawn() + .expect("Failed to spawn process"); + + child.wait().expect("Failed to wait for child process"); + + // Get the item ID + let list_output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --list", env)) + .output() + .expect("Failed to execute command"); + let list_output_str = String::from_utf8_lossy(&list_output.stdout); + let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::().expect("Invalid ID"); + + // Get the item and verify the metadata + let output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --info {}", env, id)) + .output() + .expect("Failed to execute command"); + + assert!(String::from_utf8_lossy(&output.stdout).contains("key: value")); + }); + } + + // Test: Use tags to organize items + #[test] + fn test_tags() { + with_temp_env(|data_dir| { + // Set the data directory for this test + let env = format!("KEEP_DIR={}", data_dir.display()); + + // Save an item with some content and tags + let input = "test content"; + let mut child = Command::new("sh") + .arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save --tag tag1 --tag tag2")) + .spawn() + .expect("Failed to spawn process"); + + child.wait().expect("Failed to wait for child process"); + + // List items with a specific tag + let output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --list --tag tag1", env)) + .output() + .expect("Failed to execute command"); + + assert!(String::from_utf8_lossy(&output.stdout).contains("test content")); + }); + } + + // Test: Use compression + #[test] + fn test_compression() { + with_temp_env(|data_dir| { + // Set the data directory for this test + let env = format!("KEEP_DIR={}", data_dir.display()); + + // Save an item with some content and compression + let input = "test content"; + let mut child = Command::new("sh") + .arg("-c") + .arg(format!("echo {} | {}", input, env)) + .arg("cargo run -- --save --compression gzip")) + .spawn() + .expect("Failed to spawn process"); + + child.wait().expect("Failed to wait for child process"); + + // Get the item ID + let list_output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --list", env)) + .output() + .expect("Failed to execute command"); + let list_output_str = String::from_utf8_lossy(&list_output.stdout); + let id = list_output_str.lines().next().expect("No items found").split_whitespace().next().expect("No ID found").parse::().expect("Invalid ID"); + + // Get the item and verify the content + let output = Command::new("sh") + .arg("-c") + .arg(format!("{} cargo run -- --get {}", env, id)) + .output() + .expect("Failed to execute command"); + + assert_eq!(String::from_utf8_lossy(&output.stdout), input); + }); + } +}