use clap::Parser;
use mcp_gmailcal::cli::{Cli, Commands};
use mcp_gmailcal::logging;
use std::env;
use std::sync::Once;
static INIT: Once = Once::new();
fn setup() {
INIT.call_once(|| {
env::set_var("GMAIL_CLIENT_ID", "test_client_id");
env::set_var("GMAIL_CLIENT_SECRET", "test_client_secret");
env::set_var("GMAIL_REFRESH_TOKEN", "test_refresh_token");
env::set_var("GMAIL_ACCESS_TOKEN", "test_access_token");
env::set_var("GMAIL_REDIRECT_URI", "test_redirect_uri");
});
}
#[test]
fn test_cli_parsing() {
setup();
let args = vec!["gmail-mcp"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(cli.command.is_none());
assert!(!cli.memory_only);
let args = vec!["gmail-mcp", "server"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(matches!(cli.command, Some(Commands::Server)));
let args = vec!["gmail-mcp", "auth"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(matches!(cli.command, Some(Commands::Auth)));
let args = vec!["gmail-mcp", "test"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(matches!(cli.command, Some(Commands::Test)));
let args = vec!["gmail-mcp", "--memory-only"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(cli.memory_only);
let args = vec!["gmail-mcp", "-m"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(cli.memory_only);
let args = vec!["gmail-mcp", "--memory-only", "server"];
let cli = Cli::try_parse_from(args).unwrap();
assert!(matches!(cli.command, Some(Commands::Server)));
assert!(cli.memory_only);
}
#[test]
fn test_environment_detection() {
setup();
env::remove_var("CLAUDE_DESKTOP");
env::remove_var("CLAUDE_AI");
env::remove_var("MCP_READ_ONLY");
env::set_var("CLAUDE_DESKTOP", "1");
let is_read_only = std::env::var("CLAUDE_DESKTOP").is_ok()
|| std::env::var("CLAUDE_AI").is_ok();
assert!(is_read_only);
assert_eq!(env::var("CLAUDE_DESKTOP").unwrap(), "1");
env::remove_var("CLAUDE_DESKTOP");
env::set_var("CLAUDE_AI", "1");
let is_read_only = std::env::var("CLAUDE_DESKTOP").is_ok()
|| std::env::var("CLAUDE_AI").is_ok();
assert!(is_read_only);
assert_eq!(env::var("CLAUDE_AI").unwrap(), "1");
env::remove_var("CLAUDE_AI");
let is_read_only = std::env::var("CLAUDE_DESKTOP").is_ok()
|| std::env::var("CLAUDE_AI").is_ok();
assert!(!is_read_only);
}
#[test]
fn test_logging_setup_based_on_environment() {
setup();
use log::LevelFilter;
match logging::setup_logging(LevelFilter::Debug, Some("memory")) {
Ok(log_file) => {
assert_eq!(log_file, "stderr-only (memory mode)");
},
Err(_) => {
assert!(true);
}
}
}
#[test]
fn test_server_initialization() {
setup();
let _server = mcp_gmailcal::GmailServer::new();
assert!(true);
}
#[test]
fn test_error_handling() {
setup();
env::set_var("RUST_LOG", "info");
assert_eq!(env::var("RUST_LOG").unwrap(), "info");
let _invalid_result = logging::setup_logging(
log::LevelFilter::Trace,
Some("/invalid/path.txt")
);
assert!(true);
}