mcp-gmailcal 0.10.0

A MCP server for google mail, calendar, and contacts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/// Gmail API Tests Module
///
/// This module contains comprehensive tests for the Google Gmail API functionality,
/// focusing on email operations, message parsing, and MIME handling.
use base64;
use chrono::{DateTime, TimeZone, Utc};
use mockall::mock;
use mcp_gmailcal::errors::GmailApiError;
use mcp_gmailcal::gmail_api::{DraftEmail, EmailMessage, GmailService};
use reqwest::Client;
use serde_json::{json, Value};
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

// Define a proper interface for GmailClient that we can mock
trait GmailClientInterface {
    fn list_messages(&self, max_results: u32, query: Option<&str>) -> Result<Vec<EmailMessage>, GmailApiError>;
    fn get_message_details(&self, message_id: &str) -> Result<EmailMessage, GmailApiError>;
    fn create_draft(&self, draft: &DraftEmail) -> Result<String, GmailApiError>;
    fn list_labels(&self) -> Result<Value, GmailApiError>;
    fn check_connection(&self) -> Result<(String, u64), GmailApiError>;
}

// Wrapper for GmailClient that we can test against
struct MockableGmailClient {
    client: Arc<Mutex<dyn GmailClientInterface + Send + Sync>>,
}

impl MockableGmailClient {
    fn new(client: Arc<Mutex<dyn GmailClientInterface + Send + Sync>>) -> Self {
        Self { client }
    }

    fn list_messages(&self, max_results: u32, query: Option<&str>) -> Result<Vec<EmailMessage>, GmailApiError> {
        let client = self.client.lock().unwrap();
        client.list_messages(max_results, query)
    }

    fn get_message_details(&self, message_id: &str) -> Result<EmailMessage, GmailApiError> {
        let client = self.client.lock().unwrap();
        client.get_message_details(message_id)
    }

    fn create_draft(&self, draft: &DraftEmail) -> Result<String, GmailApiError> {
        let client = self.client.lock().unwrap();
        client.create_draft(draft)
    }

    fn list_labels(&self) -> Result<Value, GmailApiError> {
        let client = self.client.lock().unwrap();
        client.list_labels()
    }

    fn check_connection(&self) -> Result<(String, u64), GmailApiError> {
        let client = self.client.lock().unwrap();
        client.check_connection()
    }
}

// Helper functions to create test data
fn create_test_email(id: &str, subject: &str, from: &str, to: &str, body_text: &str, body_html: Option<&str>) -> EmailMessage {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();
    let date = Utc.timestamp(now as i64, 0).to_rfc3339();

    EmailMessage {
        id: id.to_string(),
        thread_id: format!("thread_{}", id),
        subject: Some(subject.to_string()),
        from: Some(from.to_string()),
        to: Some(to.to_string()),
        date: Some(date),
        snippet: Some(format!("This is a snippet for email {}", id)),
        body_text: Some(body_text.to_string()),
        body_html: body_html.map(|s| s.to_string()),
    }
}

fn create_test_labels() -> Vec<(String, String)> {
    vec![
        ("INBOX".to_string(), "Inbox".to_string()),
        ("SENT".to_string(), "Sent".to_string()),
        ("DRAFT".to_string(), "Drafts".to_string()),
        ("TRASH".to_string(), "Trash".to_string()),
        ("SPAM".to_string(), "Spam".to_string()),
        ("CATEGORY_PERSONAL".to_string(), "Personal".to_string()),
        ("CATEGORY_SOCIAL".to_string(), "Social".to_string()),
        ("CATEGORY_PROMOTIONS".to_string(), "Promotions".to_string()),
        ("CATEGORY_UPDATES".to_string(), "Updates".to_string()),
        ("CATEGORY_FORUMS".to_string(), "Forums".to_string()),
    ]
}

fn labels_to_json(labels: &[(String, String)]) -> Value {
    let labels_json: Vec<Value> = labels
        .iter()
        .map(|(id, name)| {
            json!({
                "id": id,
                "name": name,
            })
        })
        .collect();

    json!({
        "labels": labels_json
    })
}

// Create sample message response in raw JSON format (similar to Gmail API response)
fn create_raw_message_response(
    id: &str,
    thread_id: &str,
    subject: &str,
    from: &str,
    to: &str,
    body_text: &str,
    body_html: Option<&str>,
) -> Value {
    let mut parts = Vec::new();
    
    // Add text part
    parts.push(json!({
        "mimeType": "text/plain",
        "body": {
            "data": base64::encode(body_text)
                .replace('+', "-")
                .replace('/', "_"),
            "size": body_text.len()
        }
    }));
    
    // Add HTML part if provided
    if let Some(html) = body_html {
        parts.push(json!({
            "mimeType": "text/html",
            "body": {
                "data": base64::encode(html)
                    .replace('+', "-")
                    .replace('/', "_"),
                "size": html.len()
            }
        }));
    }
    
    // Create headers
    let headers = vec![
        json!({"name": "From", "value": from}),
        json!({"name": "To", "value": to}),
        json!({"name": "Subject", "value": subject}),
        json!({"name": "Date", "value": format!("{} GMT", Utc::now().format("%a, %d %b %Y %H:%M:%S"))}),
    ];
    
    // Create payload
    let payload = if body_html.is_some() {
        // Multipart message
        json!({
            "mimeType": "multipart/alternative",
            "headers": headers,
            "parts": parts
        })
    } else {
        // Simple message
        json!({
            "mimeType": "text/plain",
            "headers": headers,
            "body": {
                "data": base64::encode(body_text)
                    .replace('+', "-")
                    .replace('/', "_"),
                "size": body_text.len()
            }
        })
    };
    
    // Create full message
    json!({
        "id": id,
        "threadId": thread_id,
        "labelIds": ["INBOX"],
        "snippet": format!("{:.50}", body_text),
        "payload": payload
    })
}

// Mock implementation
struct MockGmailClient {
    emails: Vec<EmailMessage>,
    labels: Vec<(String, String)>,
    should_fail: bool,
}

impl MockGmailClient {
    fn new() -> Self {
        // Create sample emails
        let emails = vec![
            create_test_email(
                "msg1",
                "Important Meeting",
                "sender@example.com",
                "recipient@example.com",
                "Let's meet tomorrow to discuss the project.",
                Some("<html><body>Let's meet tomorrow to discuss the project.</body></html>"),
            ),
            create_test_email(
                "msg2",
                "Weekly Report",
                "reports@example.com",
                "recipient@example.com",
                "Please find attached the weekly report.",
                Some("<html><body>Please find attached the weekly report.</body></html>"),
            ),
            create_test_email(
                "msg3",
                "Action Required",
                "system@example.com",
                "recipient@example.com",
                "Your action is required on the following items...",
                None,
            ),
        ];
        
        let labels = create_test_labels();
        
        Self {
            emails,
            labels,
            should_fail: false,
        }
    }
    
    fn with_failure(mut self) -> Self {
        self.should_fail = true;
        self
    }
}

impl GmailClientInterface for MockGmailClient {
    fn list_messages(&self, max_results: u32, query: Option<&str>) -> Result<Vec<EmailMessage>, GmailApiError> {
        if self.should_fail {
            return Err(GmailApiError::ApiError("Failed to list messages".to_string()));
        }
        
        // Filter by query if provided
        let filtered_emails = match query {
            Some(q) => {
                let q = q.to_lowercase();
                self.emails.iter()
                    .filter(|email| {
                        let subject = email.subject.as_ref().map_or("", |s| s.as_str()).to_lowercase();
                        let body = email.body_text.as_ref().map_or("", |s| s.as_str()).to_lowercase();
                        subject.contains(&q) || body.contains(&q)
                    })
                    .cloned()
                    .collect::<Vec<EmailMessage>>()
            },
            None => self.emails.clone(),
        };
        
        // Apply max_results
        let limited_emails = filtered_emails.into_iter()
            .take(max_results as usize)
            .collect();
        
        Ok(limited_emails)
    }
    
    fn get_message_details(&self, message_id: &str) -> Result<EmailMessage, GmailApiError> {
        if self.should_fail {
            return Err(GmailApiError::ApiError("Failed to get message".to_string()));
        }
        
        // Find the email with the given ID
        let email = self.emails.iter()
            .find(|email| email.id == message_id)
            .cloned();
        
        match email {
            Some(email) => Ok(email),
            None => Err(GmailApiError::MessageRetrievalError(format!("Message {} not found", message_id))),
        }
    }
    
    fn create_draft(&self, draft: &DraftEmail) -> Result<String, GmailApiError> {
        if self.should_fail {
            return Err(GmailApiError::ApiError("Failed to create draft".to_string()));
        }
        
        // Basic validation
        if draft.to.is_empty() {
            return Err(GmailApiError::MessageFormatError("Recipient cannot be empty".to_string()));
        }
        
        if draft.subject.is_empty() {
            return Err(GmailApiError::MessageFormatError("Subject cannot be empty".to_string()));
        }
        
        // Generate a draft ID
        let draft_id = format!("draft_{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs());
        
        Ok(draft_id)
    }
    
    fn list_labels(&self) -> Result<Value, GmailApiError> {
        if self.should_fail {
            return Err(GmailApiError::ApiError("Failed to list labels".to_string()));
        }
        
        Ok(labels_to_json(&self.labels))
    }
    
    fn check_connection(&self) -> Result<(String, u64), GmailApiError> {
        if self.should_fail {
            return Err(GmailApiError::AuthError("Authentication failed".to_string()));
        }
        
        Ok(("test@example.com".to_string(), 1000))
    }
}

#[cfg(test)]
mod comprehensive_gmail_tests {
    use super::*;
    
    // Helper to create a test client
    fn create_test_client() -> MockableGmailClient {
        let mock_client = MockGmailClient::new();
        let client = Arc::new(Mutex::new(mock_client));
        MockableGmailClient::new(client)
    }
    
    // Helper to create a failing test client
    fn create_failing_client() -> MockableGmailClient {
        let mock_client = MockGmailClient::new().with_failure();
        let client = Arc::new(Mutex::new(mock_client));
        MockableGmailClient::new(client)
    }
    
    #[test]
    fn test_list_messages_success() {
        let client = create_test_client();
        
        // Test listing all messages
        let result = client.list_messages(10, None);
        assert!(result.is_ok());
        let messages = result.unwrap();
        assert_eq!(messages.len(), 3);
        
        // Test with max_results
        let result = client.list_messages(2, None);
        assert!(result.is_ok());
        let messages = result.unwrap();
        assert_eq!(messages.len(), 2);
        
        // Test with query
        let result = client.list_messages(10, Some("report"));
        assert!(result.is_ok());
        let messages = result.unwrap();
        assert_eq!(messages.len(), 1);
        assert!(messages[0].subject.as_ref().unwrap().contains("Report"));
    }
    
    #[test]
    fn test_list_messages_failure() {
        let client = create_failing_client();
        
        let result = client.list_messages(10, None);
        assert!(result.is_err());
        match result {
            Err(GmailApiError::ApiError(msg)) => {
                assert_eq!(msg, "Failed to list messages");
            }
            _ => panic!("Expected ApiError"),
        }
    }
    
    #[test]
    fn test_get_message_details_success() {
        let client = create_test_client();
        
        // Test getting an existing message
        let result = client.get_message_details("msg1");
        assert!(result.is_ok());
        let message = result.unwrap();
        
        // Verify message details
        assert_eq!(message.id, "msg1");
        assert_eq!(message.subject.as_ref().unwrap(), "Important Meeting");
        assert_eq!(message.from.as_ref().unwrap(), "sender@example.com");
        assert_eq!(message.to.as_ref().unwrap(), "recipient@example.com");
        assert!(message.body_text.is_some());
        assert!(message.body_html.is_some());
    }
    
    #[test]
    fn test_get_message_details_not_found() {
        let client = create_test_client();
        
        // Test getting a non-existent message
        let result = client.get_message_details("nonexistent");
        assert!(result.is_err());
        match result {
            Err(GmailApiError::MessageRetrievalError(msg)) => {
                assert!(msg.contains("Message nonexistent not found"));
            }
            _ => panic!("Expected MessageRetrievalError"),
        }
    }
    
    #[test]
    fn test_get_message_details_failure() {
        let client = create_failing_client();
        
        let result = client.get_message_details("msg1");
        assert!(result.is_err());
        match result {
            Err(GmailApiError::ApiError(msg)) => {
                assert_eq!(msg, "Failed to get message");
            }
            _ => panic!("Expected ApiError"),
        }
    }
    
    #[test]
    fn test_create_draft_success() {
        let client = create_test_client();
        
        // Create a valid draft
        let draft = DraftEmail {
            to: "recipient@example.com".to_string(),
            subject: "Test Draft".to_string(),
            body: "This is a test draft email.".to_string(),
            cc: None,
            bcc: None,
            thread_id: None,
            in_reply_to: None,
            references: None,
        };
        
        let result = client.create_draft(&draft);
        assert!(result.is_ok());
        let draft_id = result.unwrap();
        assert!(draft_id.starts_with("draft_"));
    }
    
    #[test]
    fn test_create_draft_with_optional_fields() {
        let client = create_test_client();
        
        // Create a draft with optional fields
        let draft = DraftEmail {
            to: "recipient@example.com".to_string(),
            subject: "Test Draft".to_string(),
            body: "This is a test draft email.".to_string(),
            cc: Some("cc@example.com".to_string()),
            bcc: Some("bcc@example.com".to_string()),
            thread_id: Some("thread123".to_string()),
            in_reply_to: Some("msg123".to_string()),
            references: Some("ref123".to_string()),
        };
        
        let result = client.create_draft(&draft);
        assert!(result.is_ok());
    }
    
    #[test]
    fn test_create_draft_validation_failure() {
        let client = create_test_client();
        
        // Test with empty recipient
        let invalid_draft = DraftEmail {
            to: "".to_string(),
            subject: "Test Draft".to_string(),
            body: "This is a test draft email.".to_string(),
            cc: None,
            bcc: None,
            thread_id: None,
            in_reply_to: None,
            references: None,
        };
        
        let result = client.create_draft(&invalid_draft);
        assert!(result.is_err());
        match result {
            Err(GmailApiError::MessageFormatError(msg)) => {
                assert!(msg.contains("Recipient cannot be empty"));
            }
            _ => panic!("Expected MessageFormatError"),
        }
        
        // Test with empty subject
        let invalid_draft = DraftEmail {
            to: "recipient@example.com".to_string(),
            subject: "".to_string(),
            body: "This is a test draft email.".to_string(),
            cc: None,
            bcc: None,
            thread_id: None,
            in_reply_to: None,
            references: None,
        };
        
        let result = client.create_draft(&invalid_draft);
        assert!(result.is_err());
        match result {
            Err(GmailApiError::MessageFormatError(msg)) => {
                assert!(msg.contains("Subject cannot be empty"));
            }
            _ => panic!("Expected MessageFormatError"),
        }
    }
    
    #[test]
    fn test_create_draft_failure() {
        let client = create_failing_client();
        
        let draft = DraftEmail {
            to: "recipient@example.com".to_string(),
            subject: "Test Draft".to_string(),
            body: "This is a test draft email.".to_string(),
            cc: None,
            bcc: None,
            thread_id: None,
            in_reply_to: None,
            references: None,
        };
        
        let result = client.create_draft(&draft);
        assert!(result.is_err());
        match result {
            Err(GmailApiError::ApiError(msg)) => {
                assert_eq!(msg, "Failed to create draft");
            }
            _ => panic!("Expected ApiError"),
        }
    }
    
    #[test]
    fn test_list_labels_success() {
        let client = create_test_client();
        
        let result = client.list_labels();
        assert!(result.is_ok());
        
        let labels = result.unwrap();
        assert!(labels["labels"].is_array());
        
        let labels_array = labels["labels"].as_array().unwrap();
        assert!(!labels_array.is_empty());
        
        // Verify standard labels exist
        let inbox_label = labels_array.iter().find(|label| label["id"] == "INBOX").unwrap();
        assert_eq!(inbox_label["name"], "Inbox");
        
        let sent_label = labels_array.iter().find(|label| label["id"] == "SENT").unwrap();
        assert_eq!(sent_label["name"], "Sent");
    }
    
    #[test]
    fn test_list_labels_failure() {
        let client = create_failing_client();
        
        let result = client.list_labels();
        assert!(result.is_err());
        match result {
            Err(GmailApiError::ApiError(msg)) => {
                assert_eq!(msg, "Failed to list labels");
            }
            _ => panic!("Expected ApiError"),
        }
    }
    
    #[test]
    fn test_check_connection_success() {
        let client = create_test_client();
        
        let result = client.check_connection();
        assert!(result.is_ok());
        
        let (email, count) = result.unwrap();
        assert_eq!(email, "test@example.com");
        assert_eq!(count, 1000);
    }
    
    #[test]
    fn test_check_connection_failure() {
        let client = create_failing_client();
        
        let result = client.check_connection();
        assert!(result.is_err());
        match result {
            Err(GmailApiError::AuthError(msg)) => {
                assert_eq!(msg, "Authentication failed");
            }
            _ => panic!("Expected AuthError"),
        }
    }
    
    #[test]
    fn test_parsing_email_message() {
        // Test parsing a raw Gmail API message response
        let raw_message = create_raw_message_response(
            "test_id",
            "test_thread",
            "Test Subject",
            "sender@example.com",
            "recipient@example.com",
            "This is the plain text body",
            Some("<html><body>This is the HTML body</body></html>"),
        );
        
        // Verify the structure of the raw message
        assert_eq!(raw_message["id"], "test_id");
        assert_eq!(raw_message["threadId"], "test_thread");
        assert_eq!(raw_message["payload"]["headers"][2]["name"], "Subject");
        assert_eq!(raw_message["payload"]["headers"][2]["value"], "Test Subject");
        
        // Verify that base64 encoding of the body text is correct
        let encoded_body = raw_message["payload"]["parts"][0]["body"]["data"].as_str().unwrap();
        let decoded_body = base64::decode(
            encoded_body.replace('-', "+").replace('_', "/")
        ).unwrap();
        let decoded_text = String::from_utf8(decoded_body).unwrap();
        assert_eq!(decoded_text, "This is the plain text body");
    }
    
    #[test]
    fn test_mime_message_format() {
        // Test MIME message format for draft creation
        let draft = DraftEmail {
            to: "recipient@example.com".to_string(),
            subject: "Test Subject".to_string(),
            body: "This is the email body.".to_string(),
            cc: Some("cc@example.com".to_string()),
            bcc: Some("bcc@example.com".to_string()),
            thread_id: None,
            in_reply_to: None,
            references: None,
        };
        
        // Create expected MIME format
        let expected_mime = "\
            From: me\r\n\
            To: recipient@example.com\r\n\
            Subject: Test Subject\r\n\
            Cc: cc@example.com\r\n\
            Bcc: bcc@example.com\r\n\
            \r\n\
            This is the email body.";
        
        // Create actual MIME message
        let actual_mime = format!(
            "From: me\r\n\
            To: {}\r\n\
            Subject: {}\r\n\
            {}{}\r\n\
            {}",
            draft.to,
            draft.subject,
            // Add optional fields if present
            draft.cc.as_ref().map_or("".to_string(), |cc| format!("Cc: {}\r\n", cc)),
            draft.bcc.as_ref().map_or("".to_string(), |bcc| format!("Bcc: {}\r\n", bcc)),
            draft.body
        );
        
        // Verify
        assert_eq!(actual_mime.replace(" ", ""), expected_mime.replace(" ", ""));
    }
    
    #[test]
    fn test_specialized_email_formats() {
        // Test creating and working with different email formats
        
        // 1. Plain text only
        let plain_email = create_test_email(
            "plain_id",
            "Plain Text Email",
            "sender@example.com",
            "recipient@example.com",
            "This is a plain text email with no HTML part.",
            None
        );
        
        assert!(plain_email.body_html.is_none());
        assert!(plain_email.body_text.is_some());
        
        // 2. HTML and text parts
        let html_email = create_test_email(
            "html_id",
            "HTML Email",
            "sender@example.com",
            "recipient@example.com",
            "This is the plain text part.",
            Some("<html><body>This is the <b>HTML</b> part.</body></html>")
        );
        
        assert!(html_email.body_html.is_some());
        assert!(html_email.body_text.is_some());
        
        // 3. Special characters
        let special_chars_email = create_test_email(
            "special_id",
            "Email with Special Characters: πŸ˜€ € ß",
            "sender@example.com",
            "recipient@example.com",
            "This email contains emoji πŸŽ‰ and special chars: Àâüß",
            Some("<html><body>HTML with emoji πŸŽ‰ and special chars: Àâüß</body></html>")
        );
        
        assert!(special_chars_email.subject.unwrap().contains("πŸ˜€"));
        assert!(special_chars_email.body_text.unwrap().contains("πŸŽ‰"));
        assert!(special_chars_email.body_html.unwrap().contains("πŸŽ‰"));
    }
    
    #[test]
    fn test_message_thread_handling() {
        // Test creating messages in the same thread
        let draft1 = DraftEmail {
            to: "recipient@example.com".to_string(),
            subject: "Original Message".to_string(),
            body: "This is the original message.".to_string(),
            cc: None,
            bcc: None,
            thread_id: None, // Not part of a thread yet
            in_reply_to: None,
            references: None,
        };
        
        let client = create_test_client();
        let result1 = client.create_draft(&draft1);
        assert!(result1.is_ok());
        
        // Create a reply in the same thread
        let draft2 = DraftEmail {
            to: "recipient@example.com".to_string(),
            subject: "Re: Original Message".to_string(),
            body: "This is a reply to the original message.".to_string(),
            cc: None,
            bcc: None,
            thread_id: Some("thread123".to_string()), // Part of a thread
            in_reply_to: Some("msg123".to_string()), // References original message
            references: Some("msg123".to_string()), // References for threading
        };
        
        let result2 = client.create_draft(&draft2);
        assert!(result2.is_ok());
    }
}