diff --git a/src/tests/common/is_binary_tests.rs b/src/tests/common/is_binary_tests.rs index 4a61cf9..f9b82d9 100644 --- a/src/tests/common/is_binary_tests.rs +++ b/src/tests/common/is_binary_tests.rs @@ -1 +1,39 @@ -// Common is_binary tests +#[cfg(test)] +mod tests { + use keep::common::is_binary::calculate_printable_ratio; + + #[test] + fn test_calculate_printable_ratio_text() { + let text_data = b"Hello, World! This is plain text.\nWith newlines and spaces."; + let ratio = calculate_printable_ratio(text_data); + + // Text data should have high printable ratio + assert!(ratio > 0.8); + } + + #[test] + fn test_calculate_printable_ratio_binary() { + let binary_data = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"; + let ratio = calculate_printable_ratio(binary_data); + + // Binary data should have low printable ratio + assert!(ratio < 0.5); + } + + #[test] + fn test_calculate_printable_ratio_mixed() { + let mixed_data = b"Text\xff\xfe\xfdMore text\x00\x01"; + let ratio = calculate_printable_ratio(mixed_data); + + // Mixed data should have medium printable ratio + assert!(ratio > 0.3 && ratio < 0.8); + } + + #[test] + fn test_calculate_printable_ratio_empty() { + let empty_data = b""; + // Empty data would cause division by zero, but the function should handle it + // Note: This test might need adjustment based on actual implementation + let _ = calculate_printable_ratio(empty_data); + } +} diff --git a/src/tests/common/status_tests.rs b/src/tests/common/status_tests.rs index dc9bd5b..9f3b3ca 100644 --- a/src/tests/common/status_tests.rs +++ b/src/tests/common/status_tests.rs @@ -1 +1,11 @@ -// Common status tests +#[cfg(test)] +mod tests { + // TODO: Add tests for common status functionality once implemented + // This would test functions related to status checking in the common module + + #[test] + fn test_status_placeholder() { + // Placeholder test - to be implemented when status functionality is added + assert!(true); + } +} diff --git a/src/tests/compression_engine/program_tests.rs b/src/tests/compression_engine/program_tests.rs index 38376be..fa9e181 100644 --- a/src/tests/compression_engine/program_tests.rs +++ b/src/tests/compression_engine/program_tests.rs @@ -1 +1,30 @@ -// Compression engine program tests +#[cfg(test)] +mod tests { + use keep::compression_engine::program::CompressionEngineProgram; + + #[test] + fn test_compression_engine_program_creation() { + let engine = CompressionEngineProgram { + program: "gzip".to_string(), + compress: vec!["-c".to_string()], + decompress: vec!["-d".to_string(), "-c".to_string()], + supported: true, + }; + + // If the program exists, it should be supported + let _ = engine.is_supported(); + } + + #[test] + fn test_compression_engine_program_unsupported() { + let engine = CompressionEngineProgram { + program: "nonexistent_compression_program".to_string(), + compress: vec![], + decompress: vec![], + supported: false, + }; + + // Explicitly unsupported engine should report as such + assert!(!engine.is_supported()); + } +} diff --git a/src/tests/db/item_tests.rs b/src/tests/db/item_tests.rs index 91532c6..6bdedbc 100644 --- a/src/tests/db/item_tests.rs +++ b/src/tests/db/item_tests.rs @@ -1 +1,36 @@ -// Database item tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + use std::path::PathBuf; + use keep::db; + + #[test] + fn test_database_connection() { + // Create a temporary directory for the database + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + let db_path = temp_dir.path().join("test.db"); + + // Try to open the database + let result = db::open(db_path); + + // Should succeed in creating a new database + assert!(result.is_ok()); + } + + #[test] + fn test_database_item_queries() { + // Create a temporary directory for the database + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + let db_path = temp_dir.path().join("test_items.db"); + + // Open the database + let conn = db::open(db_path).expect("Failed to open database"); + + // Try to query all items (should be empty in new DB) + let items = db::query_all_items(&conn); + assert!(items.is_ok()); + + // Should start with no items + assert_eq!(items.unwrap().len(), 0); + } +} diff --git a/src/tests/db/meta_tests.rs b/src/tests/db/meta_tests.rs index d604359..7d2c221 100644 --- a/src/tests/db/meta_tests.rs +++ b/src/tests/db/meta_tests.rs @@ -1 +1,39 @@ -// Database meta tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + use keep::db; + use keep::db::Meta; + + #[test] + fn test_database_meta_operations() { + // Create a temporary directory for the database + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + let db_path = temp_dir.path().join("test_meta.db"); + + // Open the database + let conn = db::open(db_path).expect("Failed to open database"); + + // Create a test meta + let meta = Meta { + id: 1, + name: "test_key".to_string(), + value: "test_value".to_string(), + }; + + // Try to insert meta + let insert_result = db::query_upsert_meta(&conn, meta.clone()); + assert!(insert_result.is_ok()); + + // Try to get meta for non-existent item + let item = keep::db::Item { + id: 999, // Non-existent item + ts: chrono::Utc::now(), + size: 0, + compression: keep::compression_engine::CompressionType::None, + }; + + let metas = db::get_item_meta(&conn, &item); + assert!(metas.is_ok()); + assert_eq!(metas.unwrap().len(), 0); + } +} diff --git a/src/tests/db/tag_tests.rs b/src/tests/db/tag_tests.rs index c11736c..dcc3e6e 100644 --- a/src/tests/db/tag_tests.rs +++ b/src/tests/db/tag_tests.rs @@ -1 +1,47 @@ -// Database tag tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + use keep::db; + use keep::db::Tag; + + #[test] + fn test_database_tag_operations() { + // Create a temporary directory for the database + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + let db_path = temp_dir.path().join("test_tags.db"); + + // Open the database + let conn = db::open(db_path).expect("Failed to open database"); + + // Create a test tag + let tag = Tag { + id: 1, + name: "test_tag".to_string(), + }; + + // Try to insert tag + let insert_result = db::insert_tag(&conn, tag.clone()); + assert!(insert_result.is_ok()); + } + + #[test] + fn test_database_item_tag_operations() { + // Create a temporary directory for the database + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + let db_path = temp_dir.path().join("test_item_tags.db"); + + // Open the database + let conn = db::open(db_path).expect("Failed to open database"); + + // Try to delete tags for non-existent item + let item = keep::db::Item { + id: 999, // Non-existent item + ts: chrono::Utc::now(), + size: 0, + compression: keep::compression_engine::CompressionType::None, + }; + + let delete_result = db::delete_item_tags(&conn, item); + assert!(delete_result.is_ok()); + } +} diff --git a/src/tests/meta_plugin/digest_tests.rs b/src/tests/meta_plugin/digest_tests.rs index 2b8e23f..f0b2803 100644 --- a/src/tests/meta_plugin/digest_tests.rs +++ b/src/tests/meta_plugin/digest_tests.rs @@ -1 +1,47 @@ -// Meta plugin digest tests +#[cfg(test)] +mod tests { + use keep::meta_plugin::digest::*; + use keep::meta_plugin::MetaPlugin; + use std::io::Write; + + #[test] + fn test_digest_sha256_meta_plugin() { + let mut plugin = DigestSha256MetaPlugin::new("test_digest".to_string()); + + assert_eq!(plugin.meta_name(), "test_digest"); + assert!(plugin.is_internal()); + + // Creating a writer should work + let writer_result = plugin.create(); + assert!(writer_result.is_ok()); + + // Writing some data + let mut writer = writer_result.unwrap(); + let write_result = writer.write(b"test data"); + assert!(write_result.is_ok()); + } + + #[test] + fn test_read_time_meta_plugin() { + let mut plugin = ReadTimeMetaPlugin::new("read_time_test".to_string()); + + assert_eq!(plugin.meta_name(), "read_time_test"); + assert!(plugin.is_internal()); + + // Creating a writer should work + let writer_result = plugin.create(); + assert!(writer_result.is_ok()); + } + + #[test] + fn test_read_rate_meta_plugin() { + let mut plugin = ReadRateMetaPlugin::new("read_rate_test".to_string()); + + assert_eq!(plugin.meta_name(), "read_rate_test"); + assert!(plugin.is_internal()); + + // Creating a writer should work + let writer_result = plugin.create(); + assert!(writer_result.is_ok()); + } +} diff --git a/src/tests/meta_plugin/program_tests.rs b/src/tests/meta_plugin/program_tests.rs index 2f8e061..525a457 100644 --- a/src/tests/meta_plugin/program_tests.rs +++ b/src/tests/meta_plugin/program_tests.rs @@ -1 +1,50 @@ -// Meta plugin program tests +#[cfg(test)] +mod tests { + use keep::meta_plugin::program::MetaPluginProgram; + use keep::meta_plugin::MetaPlugin; + use std::io::Write; + + #[test] + fn test_meta_plugin_program_creation() { + let plugin = MetaPluginProgram::new( + "echo".to_string(), + vec!["test".to_string()], + "test_plugin".to_string(), + false, + ); + + assert_eq!(plugin.meta_name(), "test_plugin"); + // If echo is available, it should be supported + // We don't assert on is_supported() as it depends on system availability + } + + #[test] + fn test_meta_plugin_program_create_writer() { + let plugin = MetaPluginProgram::new( + "cat".to_string(), + vec![].to_vec(), + "cat_plugin".to_string(), + false, + ); + + // Creating a writer should work for valid programs + let result = plugin.create(); + // We don't assert success as it depends on system availability + // but we ensure it doesn't panic + let _ = result; + } + + #[test] + fn test_meta_plugin_program_unsupported() { + let plugin = MetaPluginProgram::new( + "nonexistent_program_xyz".to_string(), + vec![].to_vec(), + "bad_plugin".to_string(), + false, + ); + + // An unsupported plugin should report as such + // Note: This might still be supported if the program exists + let _ = plugin.is_supported(); + } +} diff --git a/src/tests/meta_plugin/system_tests.rs b/src/tests/meta_plugin/system_tests.rs index 032a92d..24f4d45 100644 --- a/src/tests/meta_plugin/system_tests.rs +++ b/src/tests/meta_plugin/system_tests.rs @@ -1 +1,57 @@ -// Meta plugin system tests +#[cfg(test)] +mod tests { + use keep::meta_plugin::system::*; + use keep::meta_plugin::MetaPlugin; + + #[test] + fn test_cwd_meta_plugin() { + let mut plugin = CwdMetaPlugin::new(); + + assert_eq!(plugin.meta_name(), "cwd"); + assert!(plugin.is_internal()); + + // Creating a writer should work + let writer_result = plugin.create(); + assert!(writer_result.is_ok()); + + // Finalize should return current working directory + let result = plugin.finalize(); + assert!(result.is_ok()); + } + + #[test] + fn test_binary_meta_plugin() { + let mut plugin = BinaryMetaPlugin::new(); + + assert_eq!(plugin.meta_name(), "binary"); + assert!(plugin.is_internal()); + + // Creating a writer should work + let writer_result = plugin.create(); + assert!(writer_result.is_ok()); + } + + #[test] + fn test_uid_meta_plugin() { + let mut plugin = UidMetaPlugin::new(); + + assert_eq!(plugin.meta_name(), "uid"); + assert!(plugin.is_internal()); + + // Creating a writer should work + let writer_result = plugin.create(); + assert!(writer_result.is_ok()); + } + + #[test] + fn test_user_meta_plugin() { + let mut plugin = UserMetaPlugin::new(); + + assert_eq!(plugin.meta_name(), "user"); + assert!(plugin.is_internal()); + + // Creating a writer should work + let writer_result = plugin.create(); + assert!(writer_result.is_ok()); + } +} diff --git a/src/tests/modes/delete_tests.rs b/src/tests/modes/delete_tests.rs index 66a3051..242e6ff 100644 --- a/src/tests/modes/delete_tests.rs +++ b/src/tests/modes/delete_tests.rs @@ -1 +1,29 @@ -// Modes delete tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + use std::fs::File; + + #[test] + fn test_delete_mode_setup() { + // Create a temporary directory for testing + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + + // Test that we can work with temporary directories + assert!(temp_dir.path().exists()); + } + + #[test] + fn test_delete_mode_file_creation_and_deletion() { + // Create a temporary directory for testing + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + let test_file = temp_dir.path().join("test_delete.txt"); + + // Create a test file + { + let _file = File::create(&test_file).expect("Failed to create test file"); + } + + // Verify file exists before deletion test + assert!(test_file.exists()); + } +} diff --git a/src/tests/modes/diff_tests.rs b/src/tests/modes/diff_tests.rs index 952caee..9f7ee93 100644 --- a/src/tests/modes/diff_tests.rs +++ b/src/tests/modes/diff_tests.rs @@ -1 +1,13 @@ -// Modes diff tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + + #[test] + fn test_diff_mode_basic_setup() { + // Create a temporary directory for testing + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + + // Test that we can work with temporary directories + assert!(temp_dir.path().exists()); + } +} diff --git a/src/tests/modes/get_tests.rs b/src/tests/modes/get_tests.rs index afd67f9..a972d13 100644 --- a/src/tests/modes/get_tests.rs +++ b/src/tests/modes/get_tests.rs @@ -1 +1,23 @@ -// Modes get tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + + #[test] + fn test_get_mode_basic_setup() { + // Create a temporary directory for testing + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + + // Test that we can work with temporary directories + assert!(temp_dir.path().exists()); + } + + #[test] + fn test_get_mode_path_operations() { + // Create a temporary directory for testing + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + let test_file = temp_dir.path().join("test_get.txt"); + + // Test path creation + assert!(!test_file.exists()); + } +} diff --git a/src/tests/modes/info_tests.rs b/src/tests/modes/info_tests.rs index 28a3c44..16cfc8e 100644 --- a/src/tests/modes/info_tests.rs +++ b/src/tests/modes/info_tests.rs @@ -1 +1,35 @@ -// Modes info tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + use std::fs::File; + use std::io::Write; + + #[test] + fn test_info_mode_file_operations() { + // Create a temporary directory for testing + let dir = TempDir::new().expect("Failed to create temp directory"); + let file_path = dir.path().join("info_test.txt"); + + // Create a test file + { + let mut file = File::create(&file_path).expect("Failed to create test file"); + writeln!(file, "This is a test file for info mode").expect("Failed to write to test file"); + writeln!(file, "With multiple lines").expect("Failed to write second line"); + } + + // Verify file exists and has content + assert!(file_path.exists()); + + let metadata = std::fs::metadata(&file_path).expect("Failed to get file metadata"); + assert!(metadata.len() > 0); + } + + #[test] + fn test_info_mode_nonexistent_file() { + // Create a path to a file that doesn't exist + let nonexistent_path = PathBuf::from("/nonexistent/file/path.txt"); + + // Verify the file doesn't exist + assert!(!nonexistent_path.exists()); + } +} diff --git a/src/tests/modes/list_tests.rs b/src/tests/modes/list_tests.rs index 855bfe3..1735687 100644 --- a/src/tests/modes/list_tests.rs +++ b/src/tests/modes/list_tests.rs @@ -1 +1,26 @@ -// Modes list tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + + #[test] + fn test_list_mode_basic_setup() { + // Create a temporary directory for testing + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + + // Test that we can work with temporary directories + assert!(temp_dir.path().exists()); + } + + #[test] + fn test_list_mode_directory_operations() { + // Create a temporary directory for testing + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + + // Test reading directory contents (should be empty) + let entries: Vec<_> = std::fs::read_dir(temp_dir.path()) + .expect("Failed to read directory") + .collect(); + + assert_eq!(entries.len(), 0); + } +} diff --git a/src/tests/modes/save_tests.rs b/src/tests/modes/save_tests.rs index 666d1aa..bb918eb 100644 --- a/src/tests/modes/save_tests.rs +++ b/src/tests/modes/save_tests.rs @@ -1 +1,43 @@ -// Modes save tests +#[cfg(test)] +mod tests { + use tempfile::tempdir; + use std::fs::File; + use std::io::Write; + + #[test] + fn test_save_mode_basic_functionality() { + // Create a temporary directory for testing + let dir = tempdir().expect("Failed to create temporary directory"); + let file_path = dir.path().join("test_input.txt"); + + // Create a test file + { + let mut file = File::create(&file_path).expect("Failed to create test file"); + writeln!(file, "test content for save mode").expect("Failed to write to test file"); + } + + // Verify file was created + assert!(file_path.exists()); + + // Note: Actual save mode testing would require integration with the keep database + // and compression engines, which is complex for unit tests + } + + #[test] + fn test_save_mode_empty_file() { + // Create a temporary directory for testing + let dir = tempdir().expect("Failed to create temporary directory"); + let file_path = dir.path().join("empty_test.txt"); + + // Create an empty test file + { + let file = File::create(&file_path).expect("Failed to create empty test file"); + // File is automatically closed when it goes out of scope + } + + // Verify empty file was created + assert!(file_path.exists()); + let metadata = std::fs::metadata(&file_path).expect("Failed to get file metadata"); + assert_eq!(metadata.len(), 0); + } +} diff --git a/src/tests/modes/status_tests.rs b/src/tests/modes/status_tests.rs index 20328e3..87d9530 100644 --- a/src/tests/modes/status_tests.rs +++ b/src/tests/modes/status_tests.rs @@ -1 +1,13 @@ -// Modes status tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + + #[test] + fn test_status_mode_basic_setup() { + // Create a temporary directory for testing + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + + // Test that we can work with temporary directories + assert!(temp_dir.path().exists()); + } +} diff --git a/src/tests/modes/update_tests.rs b/src/tests/modes/update_tests.rs index 3bfceda..cf40285 100644 --- a/src/tests/modes/update_tests.rs +++ b/src/tests/modes/update_tests.rs @@ -1 +1,13 @@ -// Modes update tests +#[cfg(test)] +mod tests { + use tempfile::TempDir; + + #[test] + fn test_update_mode_basic_setup() { + // Create a temporary directory for testing + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + + // Test that we can work with temporary directories + assert!(temp_dir.path().exists()); + } +} diff --git a/src/tests/server/api_tests.rs b/src/tests/server/api_tests.rs index 59a14eb..7722f5f 100644 --- a/src/tests/server/api_tests.rs +++ b/src/tests/server/api_tests.rs @@ -1 +1,14 @@ -// Server API tests +#[cfg(test)] +mod tests { + #[test] + fn test_api_basic_setup() { + // Placeholder for API tests + assert!(true); + } + + #[test] + fn test_api_endpoints() { + // Placeholder for testing server API endpoints + assert!(true); + } +} diff --git a/src/tests/server/auth_tests.rs b/src/tests/server/auth_tests.rs index 1faefcc..32b064d 100644 --- a/src/tests/server/auth_tests.rs +++ b/src/tests/server/auth_tests.rs @@ -1 +1,80 @@ -// Server authentication tests +#[cfg(test)] +mod tests { + use axum::http::{HeaderMap, HeaderValue}; + use keep::modes::server::common::check_auth; + + #[test] + fn test_auth_with_no_password_required() { + let headers = HeaderMap::new(); + let password = None; + + // When no password is required, auth should pass + assert!(check_auth(&headers, &password)); + } + + #[test] + fn test_auth_with_bearer_token() { + let mut headers = HeaderMap::new(); + headers.insert("authorization", HeaderValue::from_static("Bearer secret123")); + + let password = Some("secret123".to_string()); + + // Valid bearer token should pass + assert!(check_auth(&headers, &password)); + } + + #[test] + fn test_auth_with_invalid_bearer_token() { + let mut headers = HeaderMap::new(); + headers.insert("authorization", HeaderValue::from_static("Bearer wrongtoken")); + + let password = Some("secret123".to_string()); + + // Invalid bearer token should fail + assert!(!check_auth(&headers, &password)); + } + + #[test] + fn test_auth_with_basic_auth() { + let mut headers = HeaderMap::new(); + // Basic auth for "keep:secret123" base64 encoded + headers.insert("authorization", HeaderValue::from_static("Basic a2VlcDpzZWNyZXQxMjM=")); + + let password = Some("secret123".to_string()); + + // Valid basic auth should pass + assert!(check_auth(&headers, &password)); + } + + #[test] + fn test_auth_with_invalid_basic_auth() { + let mut headers = HeaderMap::new(); + // Basic auth for "keep:wrongpass" base64 encoded + headers.insert("authorization", HeaderValue::from_static("Basic a2VlcDp3cm9uZ3Bhc3M=")); + + let password = Some("secret123".to_string()); + + // Invalid basic auth should fail + assert!(!check_auth(&headers, &password)); + } + + #[test] + fn test_auth_with_missing_auth_header() { + let headers = HeaderMap::new(); + let password = Some("secret123".to_string()); + + // Missing auth header should fail when password is required + assert!(!check_auth(&headers, &password)); + } + + #[test] + fn test_auth_with_malformed_auth_header() { + let mut headers = HeaderMap::new(); + headers.insert("authorization", HeaderValue::from_static("Invalid header")); + + let password = Some("secret123".to_string()); + + // Malformed auth header should fail + assert!(!check_auth(&headers, &password)); + } +}