This commit is contained in:
Andrew Phillips
2026-02-19 13:57:39 -04:00
parent a72395fe83
commit fdeb5f7951
82 changed files with 2756 additions and 2018 deletions

View File

@@ -6,7 +6,7 @@ mod tests {
fn test_is_binary_text() {
let text_data = b"Hello, World! This is plain text.\nWith newlines and spaces.";
let result = is_binary(text_data);
// Text data should not be detected as binary
assert!(!result);
}
@@ -15,7 +15,7 @@ mod tests {
fn test_is_binary_binary() {
let binary_data = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09";
let result = is_binary(binary_data);
// Binary data should be detected as binary
assert!(result);
}
@@ -24,7 +24,7 @@ mod tests {
fn test_is_binary_png_signature() {
let png_data = b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
let result = is_binary(png_data);
// PNG signature should be detected as binary
assert!(result);
}
@@ -33,7 +33,7 @@ mod tests {
fn test_is_binary_empty() {
let empty_data = b"";
let result = is_binary(empty_data);
// Empty data should not be detected as binary
assert!(!result);
}

View File

@@ -2,7 +2,7 @@
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

View File

@@ -1,11 +1,11 @@
//! Common test utilities and helper functions to reduce duplication in tests
use tempfile::TempDir;
use crate::db;
use rusqlite::Connection;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use rusqlite::Connection;
use crate::db;
use tempfile::TempDir;
/// Create a temporary directory for testing
pub fn create_temp_dir() -> TempDir {
@@ -37,10 +37,10 @@ pub fn test_temp_dir_setup() {
pub fn test_file_creation(dir: &TempDir, filename: &str, content: &str) -> PathBuf {
let file_path = create_temp_file_with_content(dir, filename, content);
assert!(file_path.exists());
let metadata = std::fs::metadata(&file_path).expect("Failed to get file metadata");
assert!(metadata.len() > 0);
file_path
}
@@ -64,21 +64,26 @@ pub fn create_test_item(conn: &Connection) -> i64 {
}
/// Test compression and decompression with an engine
pub fn test_compression_engine(engine: &dyn crate::compression_engine::CompressionEngine, test_data: &[u8]) {
pub fn test_compression_engine(
engine: &dyn crate::compression_engine::CompressionEngine,
test_data: &[u8],
) {
let dir = create_temp_dir();
let file_path = dir.path().join("test_compression.dat");
// Test compression
{
let mut writer = engine.create(file_path.clone()).expect("Failed to create writer");
let mut writer = engine
.create(file_path.clone())
.expect("Failed to create writer");
writer.write_all(test_data).expect("Failed to write data");
}
// Test decompression
let mut reader = engine.open(file_path).expect("Failed to open reader");
let mut decompressed = Vec::new();
std::io::copy(&mut reader, &mut decompressed).expect("Failed to read data");
assert_eq!(test_data, decompressed.as_slice());
}
@@ -95,5 +100,9 @@ pub fn assert_file_exists(file_path: &PathBuf) {
/// Assert that a file does not exist
pub fn assert_file_not_exists(file_path: &PathBuf) {
assert!(!file_path.exists(), "File {:?} should not exist but it does", file_path);
assert!(
!file_path.exists(),
"File {:?} should not exist but it does",
file_path
);
}

View File

@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use crate::compression_engine::gzip::CompressionEngineGZip;
use crate::compression_engine::CompressionEngine;
use crate::compression_engine::gzip::CompressionEngineGZip;
use crate::tests::common::test_helpers::test_compression_engine;
#[test]

View File

@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use crate::compression_engine::program::CompressionEngineProgram;
use crate::compression_engine::CompressionEngine;
use crate::compression_engine::program::CompressionEngineProgram;
#[test]
fn test_compression_engine_program_creation() {
@@ -11,7 +11,7 @@ mod tests {
decompress: vec!["-d".to_string(), "-c".to_string()],
supported: true,
};
// If the program exists, it should be supported
let _ = engine.is_supported();
}
@@ -24,7 +24,7 @@ mod tests {
decompress: vec![],
supported: false,
};
// Explicitly unsupported engine should report as such
assert!(!engine.is_supported());
}

View File

@@ -12,13 +12,31 @@ mod tests {
#[test]
fn test_compression_type_from_str() {
assert_eq!(CompressionType::from_str("lz4").unwrap(), CompressionType::LZ4);
assert_eq!(CompressionType::from_str("gzip").unwrap(), CompressionType::GZip);
assert_eq!(CompressionType::from_str("none").unwrap(), CompressionType::None);
assert_eq!(
CompressionType::from_str("lz4").unwrap(),
CompressionType::LZ4
);
assert_eq!(
CompressionType::from_str("gzip").unwrap(),
CompressionType::GZip
);
assert_eq!(
CompressionType::from_str("none").unwrap(),
CompressionType::None
);
// Test case insensitivity
assert_eq!(CompressionType::from_str("LZ4").unwrap(), CompressionType::LZ4);
assert_eq!(CompressionType::from_str("GZIP").unwrap(), CompressionType::GZip);
assert_eq!(CompressionType::from_str("NONE").unwrap(), CompressionType::None);
assert_eq!(
CompressionType::from_str("LZ4").unwrap(),
CompressionType::LZ4
);
assert_eq!(
CompressionType::from_str("GZIP").unwrap(),
CompressionType::GZip
);
assert_eq!(
CompressionType::from_str("NONE").unwrap(),
CompressionType::None
);
}
#[test]

View File

@@ -5,19 +5,16 @@ mod tests {
#[test]
fn test_compression_engine_factory() {
// Test getting different compression engines
let lz4_engine = compression_engine::get_compression_engine(
CompressionType::LZ4
).expect("Failed to get LZ4 engine");
let lz4_engine = compression_engine::get_compression_engine(CompressionType::LZ4)
.expect("Failed to get LZ4 engine");
assert!(lz4_engine.is_supported());
let gzip_engine = compression_engine::get_compression_engine(
CompressionType::GZip
).expect("Failed to get GZip engine");
let gzip_engine = compression_engine::get_compression_engine(CompressionType::GZip)
.expect("Failed to get GZip engine");
assert!(gzip_engine.is_supported());
let none_engine = compression_engine::get_compression_engine(
CompressionType::None
).expect("Failed to get None engine");
let none_engine = compression_engine::get_compression_engine(CompressionType::None)
.expect("Failed to get None engine");
assert!(none_engine.is_supported());
}

View File

@@ -1,2 +1,2 @@
pub mod factory_tests;
pub mod conversion_tests;
pub mod factory_tests;

View File

@@ -1,16 +1,16 @@
#[cfg(test)]
mod tests {
use crate::tests::common::test_helpers::create_temp_db;
use crate::db;
use crate::tests::common::test_helpers::create_temp_db;
#[test]
fn test_database_connection() {
// Create a temporary database
let (_temp_dir, _conn, db_path) = create_temp_db();
// Try to open the database
let result = db::open(db_path);
// Should succeed in creating a new database
assert!(result.is_ok());
}
@@ -19,11 +19,11 @@ mod tests {
fn test_database_item_queries() {
// Create a temporary database
let (_temp_dir, conn, _db_path) = create_temp_db();
// 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);
}

View File

@@ -1,28 +1,28 @@
#[cfg(test)]
mod tests {
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
use crate::db;
use crate::db::Meta;
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
#[test]
fn test_database_meta_operations() {
// Create a temporary database
let (_temp_dir, conn, _db_path) = create_temp_db();
// First insert an item to have a valid ID
let item_id = create_test_item(&conn);
// Create a test meta with the valid item ID
let meta = Meta {
id: item_id,
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 = crate::db::Item {
id: Some(999), // Non-existent item
@@ -30,7 +30,7 @@ mod tests {
size: Some(0),
compression: crate::compression_engine::CompressionType::None.to_string(),
};
let metas = db::get_item_meta(&conn, &item);
assert!(metas.is_ok());
assert_eq!(metas.unwrap().len(), 0);

View File

@@ -3,6 +3,6 @@
#[cfg(test)]
pub mod item_tests;
#[cfg(test)]
pub mod tag_tests;
#[cfg(test)]
pub mod meta_tests;
#[cfg(test)]
pub mod tag_tests;

View File

@@ -1,23 +1,23 @@
#[cfg(test)]
mod tests {
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
use crate::db;
use crate::db::Tag;
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
#[test]
fn test_database_tag_operations() {
// Create a temporary database
let (_temp_dir, conn, _db_path) = create_temp_db();
// First insert an item to have a valid ID
let item_id = create_test_item(&conn);
// Create a test tag with the valid item ID
let tag = Tag {
id: item_id,
name: "test_tag".to_string(),
};
// Try to insert tag
let insert_result = db::insert_tag(&conn, tag.clone());
assert!(insert_result.is_ok());
@@ -27,7 +27,7 @@ mod tests {
fn test_database_item_tag_operations() {
// Create a temporary database
let (_temp_dir, conn, _db_path) = create_temp_db();
// Try to delete tags for non-existent item
let item = crate::db::Item {
id: Some(999), // Non-existent item
@@ -35,7 +35,7 @@ mod tests {
size: Some(0),
compression: crate::compression_engine::CompressionType::None.to_string(),
};
let delete_result = db::delete_item_tags(&conn, item);
assert!(delete_result.is_ok());
}

View File

@@ -1,20 +1,20 @@
#[cfg(test)]
mod tests {
use crate::meta_plugin::digest::*;
use crate::meta_plugin::MetaPlugin;
use crate::meta_plugin::digest::*;
use std::io::Write;
#[test]
fn test_digest_sha256_meta_plugin() {
let mut plugin = DigestSha256MetaPlugin::new();
assert_eq!(plugin.meta_name(), "digest_sha256");
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");
@@ -24,10 +24,10 @@ mod tests {
#[test]
fn test_read_time_meta_plugin() {
let mut plugin = ReadTimeMetaPlugin::new();
assert_eq!(plugin.meta_name(), "read_time");
assert!(plugin.is_internal());
// Creating a writer should work
let writer_result = plugin.create();
assert!(writer_result.is_ok());
@@ -36,10 +36,10 @@ mod tests {
#[test]
fn test_read_rate_meta_plugin() {
let mut plugin = ReadRateMetaPlugin::new();
assert_eq!(plugin.meta_name(), "read_rate");
assert!(plugin.is_internal());
// Creating a writer should work
let writer_result = plugin.create();
assert!(writer_result.is_ok());

View File

@@ -1,8 +1,8 @@
// Meta plugin tests module
#[cfg(test)]
pub mod system_tests;
#[cfg(test)]
pub mod digest_tests;
#[cfg(test)]
pub mod program_tests;
#[cfg(test)]
pub mod system_tests;

View File

@@ -1,17 +1,13 @@
#[cfg(test)]
mod tests {
use crate::meta_plugin::program::MetaPluginProgram;
use crate::meta_plugin::MetaPlugin;
use crate::meta_plugin::program::MetaPluginProgram;
#[test]
fn test_meta_plugin_program_creation() {
let mut plugin = MetaPluginProgram::new(
"echo",
vec!["test"],
"test_plugin".to_string(),
false,
);
let mut plugin =
MetaPluginProgram::new("echo", vec!["test"], "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
@@ -19,13 +15,8 @@ mod tests {
#[test]
fn test_meta_plugin_program_create_writer() {
let plugin = MetaPluginProgram::new(
"cat",
vec![],
"cat_plugin".to_string(),
false,
);
let plugin = MetaPluginProgram::new("cat", 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
@@ -41,7 +32,7 @@ mod tests {
"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();

View File

@@ -1,19 +1,19 @@
#[cfg(test)]
mod tests {
use crate::meta_plugin::system::*;
use crate::meta_plugin::MetaPlugin;
use crate::meta_plugin::system::*;
#[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());
@@ -22,10 +22,10 @@ mod tests {
#[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());
@@ -34,10 +34,10 @@ mod tests {
#[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());
@@ -46,10 +46,10 @@ mod tests {
#[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());

View File

@@ -1,8 +1,8 @@
pub mod common;
pub mod compression;
pub mod compression_types;
pub mod compression_engine;
pub mod compression_types;
pub mod db;
pub mod meta_plugin;
pub mod modes;
pub mod server;
pub mod common;
pub mod db;

View File

@@ -1,6 +1,8 @@
#[cfg(test)]
mod tests {
use crate::tests::common::test_helpers::{create_temp_dir, create_empty_temp_file, assert_file_exists};
use crate::tests::common::test_helpers::{
assert_file_exists, create_empty_temp_file, create_temp_dir,
};
#[test]
fn test_delete_mode_setup() {
@@ -13,7 +15,7 @@ mod tests {
// Create a temporary directory for testing
let temp_dir = create_temp_dir();
let test_file = create_empty_temp_file(&temp_dir, "test_delete.txt");
// Verify file exists before deletion test
assert_file_exists(&test_file);
}

View File

@@ -1,6 +1,8 @@
#[cfg(test)]
mod tests {
use crate::tests::common::test_helpers::{create_temp_dir, test_temp_dir_setup, assert_file_not_exists};
use crate::tests::common::test_helpers::{
assert_file_not_exists, create_temp_dir, test_temp_dir_setup,
};
#[test]
fn test_get_mode_basic_setup() {
@@ -12,7 +14,7 @@ mod tests {
// Create a temporary directory for testing
let temp_dir = create_temp_dir();
let test_file = temp_dir.path().join("test_get.txt");
// Test path creation
assert_file_not_exists(&test_file);
}

View File

@@ -1,6 +1,8 @@
#[cfg(test)]
mod tests {
use crate::tests::common::test_helpers::{create_temp_dir, test_file_creation, assert_file_not_exists, get_file_size};
use crate::tests::common::test_helpers::{
assert_file_not_exists, create_temp_dir, get_file_size, test_file_creation,
};
use std::path::PathBuf;
#[test]
@@ -9,7 +11,7 @@ mod tests {
let dir = create_temp_dir();
let content = "This is a test file for info mode\nWith multiple lines\n";
let file_path = test_file_creation(&dir, "info_test.txt", content);
// Additional verification specific to info mode
assert!(get_file_size(&file_path) > 0);
}
@@ -18,7 +20,7 @@ mod tests {
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_file_not_exists(&nonexistent_path);
}

View File

@@ -11,12 +11,12 @@ mod tests {
fn test_list_mode_directory_operations() {
// Create a temporary directory for testing
let temp_dir = create_temp_dir();
// 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);
}
}

View File

@@ -1,18 +1,18 @@
// Modes tests module
#[cfg(test)]
pub mod save_tests;
pub mod delete_tests;
#[cfg(test)]
pub mod diff_tests;
#[cfg(test)]
pub mod get_tests;
#[cfg(test)]
pub mod info_tests;
#[cfg(test)]
pub mod list_tests;
#[cfg(test)]
pub mod delete_tests;
#[cfg(test)]
pub mod update_tests;
#[cfg(test)]
pub mod info_tests;
pub mod save_tests;
#[cfg(test)]
pub mod status_tests;
#[cfg(test)]
pub mod diff_tests;
pub mod update_tests;

View File

@@ -1,16 +1,20 @@
#[cfg(test)]
mod tests {
use crate::tests::common::test_helpers::{create_temp_dir, create_temp_file_with_content, create_empty_temp_file, assert_file_exists, get_file_size};
use crate::tests::common::test_helpers::{
assert_file_exists, create_empty_temp_file, create_temp_dir, create_temp_file_with_content,
get_file_size,
};
#[test]
fn test_save_mode_basic_functionality() {
// Create a temporary directory for testing
let dir = create_temp_dir();
let file_path = create_temp_file_with_content(&dir, "test_input.txt", "test content for save mode\n");
let file_path =
create_temp_file_with_content(&dir, "test_input.txt", "test content for save mode\n");
// Verify file was created
assert_file_exists(&file_path);
// Note: Actual save mode testing would require integration with the keep database
// and compression engines, which is complex for unit tests
}
@@ -20,7 +24,7 @@ mod tests {
// Create a temporary directory for testing
let dir = create_temp_dir();
let file_path = create_empty_temp_file(&dir, "empty_test.txt");
// Verify empty file was created
assert_file_exists(&file_path);
assert_eq!(get_file_size(&file_path), 0);

View File

@@ -1,13 +1,13 @@
#[cfg(test)]
mod tests {
use axum::http::{HeaderMap, HeaderValue};
use crate::modes::server::common::check_auth;
use axum::http::{HeaderMap, HeaderValue};
#[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));
}
@@ -15,10 +15,13 @@ mod tests {
#[test]
fn test_auth_with_bearer_token() {
let mut headers = HeaderMap::new();
headers.insert("authorization", HeaderValue::from_static("Bearer secret123"));
headers.insert(
"authorization",
HeaderValue::from_static("Bearer secret123"),
);
let password = Some("secret123".to_string());
// Valid bearer token should pass
assert!(check_auth(&headers, &password));
}
@@ -26,10 +29,13 @@ mod tests {
#[test]
fn test_auth_with_invalid_bearer_token() {
let mut headers = HeaderMap::new();
headers.insert("authorization", HeaderValue::from_static("Bearer wrongtoken"));
headers.insert(
"authorization",
HeaderValue::from_static("Bearer wrongtoken"),
);
let password = Some("secret123".to_string());
// Invalid bearer token should fail
assert!(!check_auth(&headers, &password));
}
@@ -38,10 +44,13 @@ mod tests {
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="));
headers.insert(
"authorization",
HeaderValue::from_static("Basic a2VlcDpzZWNyZXQxMjM="),
);
let password = Some("secret123".to_string());
// Valid basic auth should pass
assert!(check_auth(&headers, &password));
}
@@ -50,10 +59,13 @@ mod tests {
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="));
headers.insert(
"authorization",
HeaderValue::from_static("Basic a2VlcDp3cm9uZ3Bhc3M="),
);
let password = Some("secret123".to_string());
// Invalid basic auth should fail
assert!(!check_auth(&headers, &password));
}
@@ -62,7 +74,7 @@ mod tests {
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));
}
@@ -71,9 +83,9 @@ mod tests {
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));
}