use std::fs;
use std::path::Path;
use chrono::Local;
use log::LevelFilter;
use mcp_gmailcal::logging;
use std::sync::Once;
static LOGGING_INIT: Once = Once::new();
fn file_contains_text(file_path: &str, text: &str) -> bool {
match fs::read_to_string(file_path) {
Ok(content) => content.contains(text),
Err(_) => false,
}
}
fn clean_up_log_file(file_path: &str) {
let _ = fs::remove_file(file_path);
}
#[test]
fn test_setup_logging_with_memory_mode() {
let result = logging::setup_logging(LevelFilter::Info, Some("memory"));
if result.is_ok() {
let log_path = result.unwrap();
assert_eq!(log_path, "stderr-only (memory mode)");
} else {
println!("Logger already initialized, skipping assertions");
}
}
#[test]
fn test_setup_logging_with_custom_log_file() {
let custom_log_file = "test_custom_log.log";
{
let file_path = custom_log_file;
assert_eq!(mock_log_file_path(Some(file_path)), file_path);
}
clean_up_log_file(custom_log_file);
if std::env::var("TARPAULIN").is_err() {
let result = logging::setup_logging(LevelFilter::Info, Some(custom_log_file));
if result.is_ok() {
let log_path = result.unwrap();
assert_eq!(log_path, custom_log_file);
if Path::new(custom_log_file).exists() {
let contains_header = file_contains_text(custom_log_file, "GMAIL MCP SERVER LOG - Started at");
assert!(contains_header);
}
} else {
println!("Logging was already initialized, skipping file verification");
}
} else {
println!("Running under tarpaulin, skipping actual file creation to avoid conflicts");
}
clean_up_log_file(custom_log_file);
}
#[test]
fn test_default_log_filename_format() {
let current_date = Local::now().format("%Y%m%d_%H").to_string();
let expected_filename = format!("gmail_mcp_{}.log", current_date);
assert!(expected_filename.starts_with("gmail_mcp_"));
assert!(expected_filename.ends_with(".log"));
assert!(expected_filename.contains(¤t_date));
}
#[test]
fn test_invalid_log_file_path() {
if std::env::var("TARPAULIN").is_ok() {
println!("Running under tarpaulin, skipping test to avoid logger initialization issues");
return;
}
let invalid_path = "/nonexistent/directory/invalid.log";
let result = logging::setup_logging(LevelFilter::Info, Some(invalid_path));
if result.is_err() {
assert!(true);
} else {
println!("Logging was already initialized, skipping error verification");
}
}
fn mock_log_file_path(log_file: Option<&str>) -> String {
match log_file {
Some(path) => path.to_string(),
None => {
let timestamp = Local::now().format("%Y%m%d_%H").to_string();
format!("gmail_mcp_{}.log", timestamp)
}
}
}
#[test]
fn test_log_file_path_logic() {
let specified_path = "specified.log";
let result = mock_log_file_path(Some(specified_path));
assert_eq!(result, specified_path);
let result = mock_log_file_path(None);
let timestamp = Local::now().format("%Y%m%d_%H").to_string();
assert!(result.contains(×tamp));
assert!(result.starts_with("gmail_mcp_"));
assert!(result.ends_with(".log"));
}
#[test]
fn test_append_mode_logic() {
let test_file = "append_test.log";
let initial_content = "Initial content\n";
fs::write(test_file, initial_content).expect("Failed to write test file");
let mut file = fs::OpenOptions::new()
.create(true)
.append(true)
.open(test_file)
.expect("Failed to open file in append mode");
use std::io::Write;
writeln!(file, "Appended content").expect("Failed to write to file");
let content = fs::read_to_string(test_file).expect("Failed to read file");
assert!(content.contains(initial_content));
assert!(content.contains("Appended content"));
clean_up_log_file(test_file);
}
#[test]
fn test_log_level_mapping() {
let level_mappings = [
("error", LevelFilter::Error),
("warn", LevelFilter::Warn),
("info", LevelFilter::Info),
("debug", LevelFilter::Debug),
("trace", LevelFilter::Trace),
("off", LevelFilter::Off),
];
for (level_str, level_filter) in level_mappings.iter() {
match *level_str {
"error" => assert_eq!(*level_filter, LevelFilter::Error),
"warn" => assert_eq!(*level_filter, LevelFilter::Warn),
"info" => assert_eq!(*level_filter, LevelFilter::Info),
"debug" => assert_eq!(*level_filter, LevelFilter::Debug),
"trace" => assert_eq!(*level_filter, LevelFilter::Trace),
"off" => assert_eq!(*level_filter, LevelFilter::Off),
_ => panic!("Unexpected log level string"),
}
}
}