daaki-imap 0.2.0

An IMAP4rev1/IMAP4rev2 async client library
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#![allow(clippy::unwrap_used, clippy::expect_used)]

use super::*;

// ---------------------------------------------------------------------------
// Basic flag criteria (RFC 3501 Section 6.4.4)
// ---------------------------------------------------------------------------

#[test]
fn all() {
    let c = SearchCriteria::new().all();
    assert_eq!(c.as_str(), "ALL");
}

#[test]
fn answered() {
    assert_eq!(SearchCriteria::new().answered().as_str(), "ANSWERED");
}

#[test]
fn deleted() {
    assert_eq!(SearchCriteria::new().deleted().as_str(), "DELETED");
}

#[test]
fn draft() {
    assert_eq!(SearchCriteria::new().draft().as_str(), "DRAFT");
}

#[test]
fn flagged() {
    assert_eq!(SearchCriteria::new().flagged().as_str(), "FLAGGED");
}

#[test]
fn seen() {
    assert_eq!(SearchCriteria::new().seen().as_str(), "SEEN");
}

#[test]
fn recent() {
    assert_eq!(SearchCriteria::new().recent().as_str(), "RECENT");
}

#[test]
fn new_messages() {
    assert_eq!(SearchCriteria::new().new_messages().as_str(), "NEW");
}

#[test]
fn old() {
    assert_eq!(SearchCriteria::new().old().as_str(), "OLD");
}

#[test]
fn unanswered() {
    assert_eq!(SearchCriteria::new().unanswered().as_str(), "UNANSWERED");
}

#[test]
fn undeleted() {
    assert_eq!(SearchCriteria::new().undeleted().as_str(), "UNDELETED");
}

#[test]
fn undraft() {
    assert_eq!(SearchCriteria::new().undraft().as_str(), "UNDRAFT");
}

#[test]
fn unflagged() {
    assert_eq!(SearchCriteria::new().unflagged().as_str(), "UNFLAGGED");
}

#[test]
fn unseen() {
    assert_eq!(SearchCriteria::new().unseen().as_str(), "UNSEEN");
}

#[test]
fn keyword() {
    assert_eq!(
        SearchCriteria::new()
            .keyword("$Important")
            .unwrap()
            .as_str(),
        "KEYWORD $Important"
    );
}

#[test]
fn unkeyword() {
    assert_eq!(
        SearchCriteria::new().unkeyword("$Junk").unwrap().as_str(),
        "UNKEYWORD $Junk"
    );
}

// ---------------------------------------------------------------------------
// Header / body criteria (RFC 3501 Section 6.4.4)
// ---------------------------------------------------------------------------

#[test]
fn bcc() {
    assert_eq!(
        SearchCriteria::new()
            .bcc("alice@example.com")
            .unwrap()
            .as_str(),
        "BCC \"alice@example.com\""
    );
}

#[test]
fn cc() {
    assert_eq!(
        SearchCriteria::new()
            .cc("bob@example.com")
            .unwrap()
            .as_str(),
        "CC \"bob@example.com\""
    );
}

#[test]
fn from() {
    assert_eq!(
        SearchCriteria::new()
            .from("alice@example.com")
            .unwrap()
            .as_str(),
        "FROM \"alice@example.com\""
    );
}

#[test]
fn to() {
    assert_eq!(
        SearchCriteria::new()
            .to("bob@example.com")
            .unwrap()
            .as_str(),
        "TO \"bob@example.com\""
    );
}

#[test]
fn subject() {
    assert_eq!(
        SearchCriteria::new()
            .subject("hello world")
            .unwrap()
            .as_str(),
        "SUBJECT \"hello world\""
    );
}

#[test]
fn header() {
    assert_eq!(
        SearchCriteria::new()
            .header("X-Mailer", "thunderbird")
            .unwrap()
            .as_str(),
        "HEADER X-Mailer \"thunderbird\""
    );
}

#[test]
fn body() {
    assert_eq!(
        SearchCriteria::new()
            .body("meeting notes")
            .unwrap()
            .as_str(),
        "BODY \"meeting notes\""
    );
}

#[test]
fn text() {
    assert_eq!(
        SearchCriteria::new().text("urgent").unwrap().as_str(),
        "TEXT \"urgent\""
    );
}

// ---------------------------------------------------------------------------
// Date criteria (RFC 3501 Section 6.4.4)
// ---------------------------------------------------------------------------

#[test]
fn before() {
    assert_eq!(
        SearchCriteria::new()
            .before("13-Feb-2025")
            .unwrap()
            .as_str(),
        "BEFORE 13-Feb-2025"
    );
}

#[test]
fn on() {
    assert_eq!(
        SearchCriteria::new().on("13-Feb-2025").unwrap().as_str(),
        "ON 13-Feb-2025"
    );
}

#[test]
fn since() {
    assert_eq!(
        SearchCriteria::new().since("1-Jan-2024").unwrap().as_str(),
        "SINCE 1-Jan-2024"
    );
}

#[test]
fn sent_before() {
    assert_eq!(
        SearchCriteria::new()
            .sent_before("1-Mar-2025")
            .unwrap()
            .as_str(),
        "SENTBEFORE 1-Mar-2025"
    );
}

#[test]
fn sent_on() {
    assert_eq!(
        SearchCriteria::new()
            .sent_on("1-Mar-2025")
            .unwrap()
            .as_str(),
        "SENTON 1-Mar-2025"
    );
}

#[test]
fn sent_since() {
    assert_eq!(
        SearchCriteria::new()
            .sent_since("1-Mar-2025")
            .unwrap()
            .as_str(),
        "SENTSINCE 1-Mar-2025"
    );
}

// ---------------------------------------------------------------------------
// Size criteria (RFC 3501 Section 6.4.4)
// ---------------------------------------------------------------------------

#[test]
fn larger() {
    assert_eq!(SearchCriteria::new().larger(10240).as_str(), "LARGER 10240");
}

#[test]
fn smaller() {
    assert_eq!(SearchCriteria::new().smaller(1024).as_str(), "SMALLER 1024");
}

// ---------------------------------------------------------------------------
// Sequence / UID criteria (RFC 3501 Section 6.4.4)
// ---------------------------------------------------------------------------

#[test]
fn uid_set() {
    assert_eq!(
        SearchCriteria::new().uid("1:100").unwrap().as_str(),
        "UID 1:100"
    );
}

#[test]
fn uid_star() {
    assert_eq!(
        SearchCriteria::new().uid("1:*").unwrap().as_str(),
        "UID 1:*"
    );
}

#[test]
fn sequence_set() {
    assert_eq!(
        SearchCriteria::new().sequence("1:50").unwrap().as_str(),
        "1:50"
    );
}

#[test]
fn sequence_comma_separated() {
    assert_eq!(
        SearchCriteria::new().sequence("1,2,5:10").unwrap().as_str(),
        "1,2,5:10"
    );
}

// ---------------------------------------------------------------------------
// Combinators (RFC 3501 Section 6.4.4)
// ---------------------------------------------------------------------------

#[test]
fn not_single_key() {
    let inner = SearchCriteria::new().seen();
    let c = SearchCriteria::new().not(&inner);
    // Single key: no parens needed.
    assert_eq!(c.as_str(), "NOT SEEN");
}

#[test]
fn not_compound() {
    let inner = SearchCriteria::new()
        .seen()
        .from("alice@example.com")
        .unwrap();
    let c = SearchCriteria::new().not(&inner);
    // Compound inner criteria gets parenthesized.
    assert_eq!(c.as_str(), "NOT (SEEN FROM \"alice@example.com\")");
}

#[test]
fn or_simple() {
    let a = SearchCriteria::new().seen();
    let b = SearchCriteria::new().flagged();
    let c = SearchCriteria::new().or(&a, &b);
    assert_eq!(c.as_str(), "OR SEEN FLAGGED");
}

#[test]
fn or_compound_operands() {
    let a = SearchCriteria::new().unseen().since("1-Jan-2025").unwrap();
    let b = SearchCriteria::new()
        .flagged()
        .from("bob@example.com")
        .unwrap();
    let c = SearchCriteria::new().or(&a, &b);
    assert_eq!(
        c.as_str(),
        "OR (UNSEEN SINCE 1-Jan-2025) (FLAGGED FROM \"bob@example.com\")"
    );
}

// ---------------------------------------------------------------------------
// Chaining (AND semantics) (RFC 3501 Section 6.4.4)
// ---------------------------------------------------------------------------

#[test]
fn chain_multiple_criteria() {
    let c = SearchCriteria::new()
        .unseen()
        .since("13-Feb-2025")
        .unwrap()
        .from("alice@example.com")
        .unwrap();
    assert_eq!(
        c.as_str(),
        "UNSEEN SINCE 13-Feb-2025 FROM \"alice@example.com\""
    );
}

#[test]
fn chain_flags_and_size() {
    let c = SearchCriteria::new()
        .undeleted()
        .unseen()
        .larger(5000)
        .smaller(1_000_000);
    assert_eq!(c.as_str(), "UNDELETED UNSEEN LARGER 5000 SMALLER 1000000");
}

#[test]
fn chain_with_or_and_not() {
    let deleted = SearchCriteria::new().deleted();
    let draft = SearchCriteria::new().draft();
    let c = SearchCriteria::new()
        .unseen()
        .or(&deleted, &draft)
        .since("1-Jan-2025")
        .unwrap();
    assert_eq!(c.as_str(), "UNSEEN OR DELETED DRAFT SINCE 1-Jan-2025");
}

// ---------------------------------------------------------------------------
// String quoting (RFC 3501 Section 9)
// ---------------------------------------------------------------------------

#[test]
fn quote_backslash_in_string() {
    let c = SearchCriteria::new().from("a\\b").unwrap();
    assert_eq!(c.as_str(), "FROM \"a\\\\b\"");
}

#[test]
fn quote_double_quote_in_string() {
    let c = SearchCriteria::new().subject("say \"hello\"").unwrap();
    assert_eq!(c.as_str(), "SUBJECT \"say \\\"hello\\\"\"");
}

#[test]
fn empty_string_value() {
    // An empty astring value is quoted as "" per RFC 3501 Section 9.
    let c = SearchCriteria::new().from("").unwrap();
    assert_eq!(c.as_str(), "FROM \"\"");
}

// ---------------------------------------------------------------------------
// Display and AsRef<str> traits
// ---------------------------------------------------------------------------

#[test]
fn display_trait() {
    let c = SearchCriteria::new().unseen().flagged();
    assert_eq!(format!("{c}"), "UNSEEN FLAGGED");
}

#[test]
fn as_ref_str() {
    let c = SearchCriteria::new().all();
    let s: &str = c.as_ref();
    assert_eq!(s, "ALL");
}

#[test]
fn as_str_method() {
    let c = SearchCriteria::new().unseen();
    assert_eq!(c.as_str(), "UNSEEN");
}

// ---------------------------------------------------------------------------
// Default trait
// ---------------------------------------------------------------------------

#[test]
fn default_is_empty() {
    let c = SearchCriteria::default();
    assert_eq!(c.as_str(), "");
}

// ---------------------------------------------------------------------------
// Clone and equality
// ---------------------------------------------------------------------------

#[test]
fn clone_and_eq() {
    let c1 = SearchCriteria::new()
        .unseen()
        .from("alice@example.com")
        .unwrap();
    let c2 = c1.clone();
    assert_eq!(c1, c2);
}

#[test]
fn ne_different_criteria() {
    let c1 = SearchCriteria::new().unseen();
    let c2 = SearchCriteria::new().seen();
    assert_ne!(c1, c2);
}

// ---------------------------------------------------------------------------
// Complex real-world examples
// ---------------------------------------------------------------------------

#[test]
fn real_world_unread_from_sender_this_month() {
    let c = SearchCriteria::new()
        .unseen()
        .since("1-Mar-2026")
        .unwrap()
        .from("notifications@github.com")
        .unwrap();
    assert_eq!(
        c.as_str(),
        "UNSEEN SINCE 1-Mar-2026 FROM \"notifications@github.com\""
    );
}

#[test]
fn real_world_large_unseen_or_flagged() {
    let unseen = SearchCriteria::new().unseen();
    let flagged = SearchCriteria::new().flagged();
    let c = SearchCriteria::new().larger(100_000).or(&unseen, &flagged);
    assert_eq!(c.as_str(), "LARGER 100000 OR UNSEEN FLAGGED");
}

#[test]
fn real_world_not_deleted_not_draft() {
    let deleted = SearchCriteria::new().deleted();
    let draft = SearchCriteria::new().draft();
    let c = SearchCriteria::new().not(&deleted).not(&draft);
    assert_eq!(c.as_str(), "NOT DELETED NOT DRAFT");
}

#[test]
fn real_world_header_search() {
    let c = SearchCriteria::new()
        .header("X-Priority", "1")
        .unwrap()
        .unseen()
        .since("1-Jan-2026")
        .unwrap();
    assert_eq!(
        c.as_str(),
        "HEADER X-Priority \"1\" UNSEEN SINCE 1-Jan-2026"
    );
}

// ---------------------------------------------------------------------------
// CONDSTORE MODSEQ criterion (RFC 7162 Section 3.1.5)
// ---------------------------------------------------------------------------

#[test]
fn mod_seq_simple() {
    // RFC 7162 Section 3.1.5: simple MODSEQ form with a mod-sequence value.
    assert_eq!(
        SearchCriteria::new().mod_seq(12345).as_str(),
        "MODSEQ 12345"
    );
}

#[test]
fn mod_seq_zero() {
    // RFC 7162 Section 3.1.5: zero is a valid mod-sequence-valzer.
    assert_eq!(SearchCriteria::new().mod_seq(0).as_str(), "MODSEQ 0");
}

#[test]
fn mod_seq_combined_with_unseen() {
    // MODSEQ combined with other criteria via AND semantics
    // (RFC 3501 Section 6.4.4).
    let c = SearchCriteria::new().unseen().mod_seq(100);
    assert_eq!(c.as_str(), "UNSEEN MODSEQ 100");
}

#[test]
fn mod_seq_large_value() {
    // RFC 7162 Section 3.1.5: mod-sequence-valzer can be up to 63 bits.
    let c = SearchCriteria::new().mod_seq(9_999_999_999_999);
    assert_eq!(c.as_str(), "MODSEQ 9999999999999");
}

#[test]
fn is_compound_modseq_single() {
    // MODSEQ <value> is a single search-key (keyword + 1 arg = 2 tokens).
    assert!(!is_compound("MODSEQ 12345"));
}

// ---------------------------------------------------------------------------
// is_compound helper tests
// ---------------------------------------------------------------------------

#[test]
fn is_compound_single_keyword() {
    assert!(!is_compound("UNSEEN"));
}

#[test]
fn is_compound_keyword_with_arg() {
    assert!(!is_compound("FROM \"alice\""));
}

#[test]
fn is_compound_multiple_keywords() {
    assert!(is_compound("UNSEEN FLAGGED"));
}

#[test]
fn is_compound_header_two_args() {
    assert!(!is_compound("HEADER X-Mailer \"thunderbird\""));
}

#[test]
fn is_compound_or_two_args() {
    assert!(!is_compound("OR SEEN FLAGGED"));
}

#[test]
fn is_compound_not_with_arg() {
    assert!(!is_compound("NOT SEEN"));
}

#[test]
fn is_compound_not_with_parenthesized_group() {
    // NOT (SEEN FLAGGED) is one search-key (NOT + 1 parenthesized group = 2 tokens).
    assert!(!is_compound("NOT (SEEN FLAGGED)"));
}

#[test]
fn is_compound_or_with_parenthesized_groups() {
    // OR (UNSEEN) (FLAGGED) is one search-key (OR + 2 parenthesized groups = 3 tokens).
    assert!(!is_compound("OR (UNSEEN) (FLAGGED)"));
}

#[test]
fn is_compound_bare_sequence_set() {
    // A bare sequence set is a single search-key.
    assert!(!is_compound("1:100"));
}

// ---------------------------------------------------------------------------
// count_top_level_tokens helper tests
// ---------------------------------------------------------------------------

#[test]
fn count_tokens_empty() {
    assert_eq!(count_top_level_tokens(""), 0);
}

#[test]
fn count_tokens_single_word() {
    assert_eq!(count_top_level_tokens("UNSEEN"), 1);
}

#[test]
fn count_tokens_two_words() {
    assert_eq!(count_top_level_tokens("UNSEEN FLAGGED"), 2);
}

#[test]
fn count_tokens_quoted_string_with_spaces() {
    // The quoted string counts as one token despite containing spaces.
    assert_eq!(count_top_level_tokens("FROM \"alice bob\""), 2);
}

#[test]
fn count_tokens_parenthesized_group() {
    // A parenthesized group counts as one token.
    assert_eq!(count_top_level_tokens("NOT (SEEN FLAGGED)"), 2);
}

#[test]
fn count_tokens_escaped_quote_in_string() {
    // The escaped quote inside the string does not terminate the quoted region.
    assert_eq!(count_top_level_tokens("SUBJECT \"say \\\"hi\\\"\""), 2);
}

// ---------------------------------------------------------------------------
// IMAP-001: quote_imap_string must reject NUL/CR/LF (RFC 3501 Section 9)
// ---------------------------------------------------------------------------

#[test]
fn quote_imap_string_rejects_nul() {
    // NUL (\0) is not a valid CHAR per RFC 3501 Section 9:
    // CHAR = <any 7-bit US-ASCII character except NUL, 0x01 - 0x7f>
    // A quoted string cannot contain NUL.
    let result = SearchCriteria::new().from("hello\0world");
    assert!(
        result.is_err(),
        "NUL byte must be rejected in quoted strings"
    );
}

#[test]
fn quote_imap_string_rejects_cr() {
    // CR (\r) is not a TEXT-CHAR per RFC 3501 Section 9:
    // TEXT-CHAR = <any CHAR except CR and LF>
    // QUOTED-CHAR = TEXT-CHAR / quoted-specials
    // A quoted string cannot contain bare CR.
    let result = SearchCriteria::new().from("hello\rworld");
    assert!(
        result.is_err(),
        "CR byte must be rejected in quoted strings"
    );
}

#[test]
fn quote_imap_string_rejects_lf() {
    // LF (\n) is not a TEXT-CHAR per RFC 3501 Section 9:
    // TEXT-CHAR = <any CHAR except CR and LF>
    // A quoted string cannot contain bare LF.
    let result = SearchCriteria::new().from("hello\nworld");
    assert!(
        result.is_err(),
        "LF byte must be rejected in quoted strings"
    );
}

#[test]
fn quote_imap_string_rejects_nul_in_header_value() {
    // The header() method also uses quote_imap_string for the value argument.
    let result = SearchCriteria::new().header("X-Test", "val\0ue");
    assert!(result.is_err(), "NUL in header value must be rejected");
}

// ---------------------------------------------------------------------------
// IMAP-002: keyword/unkeyword must validate atom chars (RFC 3501 Section 9)
// ---------------------------------------------------------------------------

#[test]
fn keyword_rejects_space() {
    // RFC 3501 Section 9: flag-keyword = atom, atom = 1*ATOM-CHAR.
    // Space is an atom-special and must be rejected.
    let result = SearchCriteria::new().keyword("my flag");
    assert!(result.is_err(), "space in keyword must be rejected");
}

#[test]
fn keyword_rejects_close_paren() {
    // ')' is an atom-special per RFC 3501 Section 9.
    let result = SearchCriteria::new().keyword("$foo)");
    assert!(result.is_err(), "closing paren in keyword must be rejected");
}

#[test]
fn keyword_rejects_empty() {
    // RFC 3501 Section 9: atom = 1*ATOM-CHAR — must be at least one character.
    let result = SearchCriteria::new().keyword("");
    assert!(result.is_err(), "empty keyword must be rejected");
}

#[test]
fn unkeyword_rejects_space() {
    let result = SearchCriteria::new().unkeyword("my flag");
    assert!(result.is_err(), "space in unkeyword must be rejected");
}

#[test]
fn unkeyword_rejects_empty() {
    let result = SearchCriteria::new().unkeyword("");
    assert!(result.is_err(), "empty unkeyword must be rejected");
}

// ---------------------------------------------------------------------------
// IMAP-003: uid/sequence must validate sequence sets (RFC 3501 Section 9)
// ---------------------------------------------------------------------------

#[test]
fn uid_rejects_non_set() {
    // "not a set" is not a valid sequence-set per RFC 3501 Section 9.
    let result = SearchCriteria::new().uid("not a set");
    assert!(
        result.is_err(),
        "invalid sequence set must be rejected by uid()"
    );
}

#[test]
fn uid_rejects_empty() {
    let result = SearchCriteria::new().uid("");
    assert!(
        result.is_err(),
        "empty sequence set must be rejected by uid()"
    );
}

#[test]
fn sequence_rejects_spaces() {
    // "1 2 3" contains spaces, which are not valid in a sequence-set.
    let result = SearchCriteria::new().sequence("1 2 3");
    assert!(
        result.is_err(),
        "spaces in sequence set must be rejected by sequence()"
    );
}

#[test]
fn sequence_rejects_empty() {
    let result = SearchCriteria::new().sequence("");
    assert!(
        result.is_err(),
        "empty sequence set must be rejected by sequence()"
    );
}

// ---------------------------------------------------------------------------
// IMAP-004: Date methods must validate IMAP date format (RFC 3501 Section 9)
// ---------------------------------------------------------------------------

#[test]
fn before_rejects_non_date() {
    // "not-a-date" doesn't match DD-Mon-YYYY.
    let result = SearchCriteria::new().before("not-a-date");
    assert!(
        result.is_err(),
        "non-date string must be rejected by before()"
    );
}

#[test]
fn before_rejects_empty() {
    let result = SearchCriteria::new().before("");
    assert!(result.is_err(), "empty date must be rejected by before()");
}

#[test]
fn before_rejects_invalid_month() {
    // "31-Foo-2025" has an invalid month abbreviation.
    let result = SearchCriteria::new().before("31-Foo-2025");
    assert!(
        result.is_err(),
        "invalid month abbreviation must be rejected"
    );
}

#[test]
fn since_rejects_wrong_format() {
    // "2025-01-13" is ISO format, not IMAP date format.
    let result = SearchCriteria::new().since("2025-01-13");
    assert!(result.is_err(), "ISO date format must be rejected");
}

#[test]
fn sent_on_rejects_garbage() {
    let result = SearchCriteria::new().sent_on("garbage");
    assert!(
        result.is_err(),
        "garbage date must be rejected by sent_on()"
    );
}

// ---------------------------------------------------------------------------
// validate_imap_date helper tests (RFC 3501 Section 9)
// ---------------------------------------------------------------------------

#[test]
fn validate_imap_date_valid_single_digit_day() {
    // Single-digit day is valid per RFC 3501 Section 9: date-day = 1*2DIGIT.
    assert!(validate_imap_date("1-Jan-2025").is_ok());
}

#[test]
fn validate_imap_date_valid_double_digit_day() {
    assert!(validate_imap_date("13-Feb-2025").is_ok());
}

#[test]
fn validate_imap_date_rejects_three_digit_day() {
    assert!(validate_imap_date("123-Jan-2025").is_err());
}

#[test]
fn validate_imap_date_rejects_two_digit_year() {
    assert!(validate_imap_date("1-Jan-25").is_err());
}

#[test]
fn validate_imap_date_all_months_valid() {
    for month in [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ] {
        let date = format!("1-{month}-2025");
        assert!(
            validate_imap_date(&date).is_ok(),
            "{month} should be accepted"
        );
    }
}