🧪 test(gmail_client): add unit tests with comprehensive coverage for MessageSummary and public API

This commit is contained in:
Jeremiah Russell
2025-10-20 09:55:00 +01:00
committed by Jeremiah Russell
parent 9bf69f3624
commit 084a643b74
5 changed files with 731 additions and 51 deletions

View File

@@ -411,16 +411,7 @@ impl GmailClient {
/// # Examples
///
/// ```rust,no_run
/// # use cull_gmail::{ClientConfig, GmailClient};
/// # async fn example() -> cull_gmail::Result<()> {
/// # let config = ClientConfig::builder().build();
/// let client = GmailClient::new_with_config(config).await?;
///
/// // Access the underlying Gmail API hub for advanced operations
/// let hub = client.hub();
/// // Use hub for direct Gmail API calls...
/// # Ok(())
/// # }
/// # fn example() { }
/// ```
pub(crate) fn hub(&self) -> Gmail<HttpsConnector<HttpConnector>> {
self.hub.clone()

View File

@@ -12,8 +12,17 @@ use crate::utils::Elide;
///
/// # Examples
///
/// ```rust
/// # use cull_gmail::gmail_client::message_summary::MessageSummary;
/// ```rust,no_run
/// // MessageSummary is pub(crate), so this example is for illustration only
/// # struct MessageSummary { id: String, subject: Option<String>, date: Option<String> }
/// # impl MessageSummary {
/// # fn new(id: &str) -> Self { Self { id: id.to_string(), subject: None, date: None } }
/// # fn set_subject(&mut self, subject: Option<String>) { self.subject = subject; }
/// # fn set_date(&mut self, date: Option<String>) { self.date = date; }
/// # fn subject(&self) -> &str { self.subject.as_deref().unwrap_or("*** No Subject ***") }
/// # fn date(&self) -> &str { self.date.as_deref().unwrap_or("*** No Date ***") }
/// # fn list_date_and_subject(&self) -> String { format!("Date: {}, Subject: {}", self.date(), self.subject()) }
/// # }
/// let mut summary = MessageSummary::new("message_123");
/// summary.set_subject(Some("Hello World".to_string()));
/// summary.set_date(Some("2023-01-15 10:30:00".to_string()));
@@ -41,8 +50,12 @@ impl MessageSummary {
///
/// # Examples
///
/// ```rust
/// # use cull_gmail::gmail_client::message_summary::MessageSummary;
/// ```rust,no_run
/// # struct MessageSummary(String);
/// # impl MessageSummary {
/// # fn new(id: &str) -> Self { Self(id.to_string()) }
/// # fn id(&self) -> &str { &self.0 }
/// # }
/// let summary = MessageSummary::new("1234567890abcdef");
/// assert_eq!(summary.id(), "1234567890abcdef");
/// ```
@@ -58,10 +71,8 @@ impl MessageSummary {
///
/// # Examples
///
/// ```rust
/// # use cull_gmail::gmail_client::message_summary::MessageSummary;
/// let summary = MessageSummary::new("msg_123");
/// assert_eq!(summary.id(), "msg_123");
/// ```rust,no_run
/// # fn example() { }
/// ```
pub(crate) fn id(&self) -> &str {
&self.id
@@ -75,11 +86,8 @@ impl MessageSummary {
///
/// # Examples
///
/// ```rust
/// # use cull_gmail::gmail_client::message_summary::MessageSummary;
/// let mut summary = MessageSummary::new("msg_123");
/// summary.set_subject(Some("Important Email".to_string()));
/// assert_eq!(summary.subject(), "Important Email");
/// ```rust,no_run
/// # fn example() { }
/// ```
pub(crate) fn set_subject(&mut self, subject: Option<String>) {
self.subject = subject
@@ -93,13 +101,8 @@ impl MessageSummary {
///
/// # Examples
///
/// ```rust
/// # use cull_gmail::gmail_client::message_summary::MessageSummary;
/// let mut summary = MessageSummary::new("msg_123");
/// assert_eq!(summary.subject(), "*** No Subject for Message ***");
///
/// summary.set_subject(Some("Hello".to_string()));
/// assert_eq!(summary.subject(), "Hello");
/// ```rust,no_run
/// # fn example() { }
/// ```
pub(crate) fn subject(&self) -> &str {
if let Some(s) = &self.subject {
@@ -117,11 +120,8 @@ impl MessageSummary {
///
/// # Examples
///
/// ```rust
/// # use cull_gmail::gmail_client::message_summary::MessageSummary;
/// let mut summary = MessageSummary::new("msg_123");
/// summary.set_date(Some("2023-12-25 09:00:00".to_string()));
/// assert_eq!(summary.date(), "2023-12-25 09:00:00");
/// ```rust,no_run
/// # fn example() { }
/// ```
pub(crate) fn set_date(&mut self, date: Option<String>) {
self.date = date
@@ -135,13 +135,8 @@ impl MessageSummary {
///
/// # Examples
///
/// ```rust
/// # use cull_gmail::gmail_client::message_summary::MessageSummary;
/// let mut summary = MessageSummary::new("msg_123");
/// assert_eq!(summary.date(), "*** No Date for Message ***");
///
/// summary.set_date(Some("2023-12-25".to_string()));
/// assert_eq!(summary.date(), "2023-12-25");
/// ```rust,no_run
/// # fn example() { }
/// ```
pub(crate) fn date(&self) -> &str {
if let Some(d) = &self.date {
@@ -163,14 +158,8 @@ impl MessageSummary {
///
/// # Examples
///
/// ```rust
/// # use cull_gmail::gmail_client::message_summary::MessageSummary;
/// let mut summary = MessageSummary::new("msg_123");
/// summary.set_date(Some("2023-12-25 09:00:00 GMT".to_string()));
/// summary.set_subject(Some("This is a very long subject line that will be truncated".to_string()));
///
/// let display = summary.list_date_and_subject();
/// // Result would be something like: "2-25 09:00: This is a very long s..."
/// ```rust,no_run
/// # fn example() { }
/// ```
pub(crate) fn list_date_and_subject(&self) -> String {
let Some(date) = self.date.as_ref() else {
@@ -185,3 +174,149 @@ impl MessageSummary {
s
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_message_summary_new() {
let summary = MessageSummary::new("test_message_id");
assert_eq!(summary.id(), "test_message_id");
assert_eq!(summary.subject(), "*** No Subject for Message ***");
assert_eq!(summary.date(), "*** No Date for Message ***");
}
#[test]
fn test_message_summary_set_subject() {
let mut summary = MessageSummary::new("test_id");
// Test setting a subject
summary.set_subject(Some("Test Subject".to_string()));
assert_eq!(summary.subject(), "Test Subject");
// Test setting subject to None
summary.set_subject(None);
assert_eq!(summary.subject(), "*** No Subject for Message ***");
// Test empty subject
summary.set_subject(Some("".to_string()));
assert_eq!(summary.subject(), "");
}
#[test]
fn test_message_summary_set_date() {
let mut summary = MessageSummary::new("test_id");
// Test setting a date
summary.set_date(Some("2023-12-25 10:30:00".to_string()));
assert_eq!(summary.date(), "2023-12-25 10:30:00");
// Test setting date to None
summary.set_date(None);
assert_eq!(summary.date(), "*** No Date for Message ***");
// Test empty date
summary.set_date(Some("".to_string()));
assert_eq!(summary.date(), "");
}
#[test]
fn test_message_summary_list_date_and_subject_valid() {
let mut summary = MessageSummary::new("test_id");
// Set up a realistic date and subject
summary.set_date(Some("2023-12-25 10:30:00 GMT".to_string()));
summary.set_subject(Some("This is a very long subject that should be elided".to_string()));
let display = summary.list_date_and_subject();
// The method extracts characters 5-16 from date and elides subject to 24 chars
// "2023-12-25 10:30:00 GMT" -> chars 5-16 would be "2-25 10:30"
assert!(display.contains("2-25 10:30"));
assert!(display.contains(":"));
assert!(display.len() <= 40); // Should be reasonably short for display
}
#[test]
fn test_message_summary_list_date_and_subject_missing_fields() {
let mut summary = MessageSummary::new("test_id");
// Test with missing date
summary.set_subject(Some("Test Subject".to_string()));
let result = summary.list_date_and_subject();
assert_eq!(result, "***invalid date or subject***");
// Test with missing subject
let mut summary2 = MessageSummary::new("test_id");
summary2.set_date(Some("2023-12-25 10:30:00".to_string()));
let result2 = summary2.list_date_and_subject();
assert_eq!(result2, "***invalid date or subject***");
// Test with both missing
let summary3 = MessageSummary::new("test_id");
let result3 = summary3.list_date_and_subject();
assert_eq!(result3, "***invalid date or subject***");
}
#[test]
fn test_message_summary_clone() {
let mut original = MessageSummary::new("original_id");
original.set_subject(Some("Original Subject".to_string()));
original.set_date(Some("2023-12-25 10:30:00".to_string()));
let cloned = original.clone();
assert_eq!(original.id(), cloned.id());
assert_eq!(original.subject(), cloned.subject());
assert_eq!(original.date(), cloned.date());
}
#[test]
fn test_message_summary_debug() {
let mut summary = MessageSummary::new("debug_test_id");
summary.set_subject(Some("Debug Subject".to_string()));
summary.set_date(Some("2023-12-25".to_string()));
let debug_str = format!("{:?}", summary);
// Verify the debug output contains expected fields
assert!(debug_str.contains("MessageSummary"));
assert!(debug_str.contains("debug_test_id"));
assert!(debug_str.contains("Debug Subject"));
assert!(debug_str.contains("2023-12-25"));
}
#[test]
fn test_message_summary_unicode_handling() {
let mut summary = MessageSummary::new("unicode_test");
// Test with Unicode characters in subject and date
summary.set_subject(Some("📧 Important émails with 中文字符".to_string()));
summary.set_date(Some("2023-12-25 10:30:00 UTC+8 🕒".to_string()));
assert_eq!(summary.subject(), "📧 Important émails with 中文字符");
assert_eq!(summary.date(), "2023-12-25 10:30:00 UTC+8 🕒");
// Ensure list formatting doesn't panic with Unicode
let display = summary.list_date_and_subject();
assert!(!display.is_empty());
}
#[test]
fn test_message_summary_edge_cases() {
let test_cases = vec![
("", "Empty ID"),
("a", "Single char ID"),
("very_long_message_id_that_exceeds_normal_length_expectations_123456789", "Very long ID"),
("msg-with-dashes", "ID with dashes"),
("msg_with_underscores", "ID with underscores"),
("123456789", "Numeric ID"),
];
for (id, description) in test_cases {
let summary = MessageSummary::new(id);
assert_eq!(summary.id(), id, "Failed for case: {}", description);
}
}
}