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
#![allow(clippy::unwrap_used, clippy::expect_used)]

use super::*;
use crate::types::validated::MailboxName;

#[test]
fn special_use_from_attribute() {
    let info = MailboxInfo {
        name: MailboxName::new("MyArchive").unwrap(),
        delimiter: Some('/'),
        attributes: vec![MailboxAttribute::Archive],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Archive));
}

#[test]
fn special_use_name_fallback() {
    let info = MailboxInfo {
        name: MailboxName::new("Spam").unwrap(),
        delimiter: Some('/'),
        attributes: vec![],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Junk));
}

#[test]
fn special_use_case_insensitive() {
    let info = MailboxInfo {
        name: MailboxName::new("SENT").unwrap(),
        delimiter: Some('/'),
        attributes: vec![],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Sent));
}

#[test]
fn special_use_nested_name() {
    let info = MailboxInfo {
        name: MailboxName::new("INBOX/Trash").unwrap(),
        delimiter: Some('/'),
        attributes: vec![],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Trash));
}

#[test]
fn special_use_attribute_overrides_name() {
    let info = MailboxInfo {
        name: MailboxName::new("Trash").unwrap(),
        delimiter: Some('/'),
        attributes: vec![MailboxAttribute::Sent],
        ..Default::default()
    };
    // Attribute wins over name.
    assert_eq!(info.special_use(), Some(SpecialUse::Sent));
}

#[test]
fn special_use_dot_delimiter() {
    let info = MailboxInfo {
        name: MailboxName::new("INBOX.Trash").unwrap(),
        delimiter: Some('.'),
        attributes: vec![],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Trash));
}

#[test]
fn special_use_no_delimiter() {
    let info = MailboxInfo {
        name: MailboxName::new("Sent").unwrap(),
        delimiter: None,
        attributes: vec![],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Sent));
}

#[test]
fn special_use_unknown() {
    let info = MailboxInfo {
        name: MailboxName::new("Work").unwrap(),
        delimiter: Some('/'),
        attributes: vec![],
        ..Default::default()
    };
    assert_eq!(info.special_use(), None);
}

#[test]
fn special_use_from_drafts_attribute() {
    // RFC 6154 Section 2: \Drafts attribute.
    let info = MailboxInfo {
        name: MailboxName::new("MyDrafts").unwrap(),
        delimiter: Some('/'),
        attributes: vec![MailboxAttribute::Drafts],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Drafts));
}

#[test]
fn special_use_from_junk_attribute() {
    // RFC 6154 Section 2: \Junk attribute.
    let info = MailboxInfo {
        name: MailboxName::new("SpamFolder").unwrap(),
        delimiter: Some('/'),
        attributes: vec![MailboxAttribute::Junk],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Junk));
}

#[test]
fn special_use_from_trash_attribute() {
    // RFC 6154 Section 2: \Trash attribute.
    let info = MailboxInfo {
        name: MailboxName::new("Bin").unwrap(),
        delimiter: Some('/'),
        attributes: vec![MailboxAttribute::Trash],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Trash));
}

#[test]
fn special_use_from_flagged_attribute() {
    // RFC 6154 Section 2: \Flagged attribute.
    let info = MailboxInfo {
        name: MailboxName::new("Stars").unwrap(),
        delimiter: Some('/'),
        attributes: vec![MailboxAttribute::Flagged],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Flagged));
}

#[test]
fn special_use_from_important_attribute() {
    // RFC 8457: \Important attribute.
    let info = MailboxInfo {
        name: MailboxName::new("Priority").unwrap(),
        delimiter: Some('/'),
        attributes: vec![MailboxAttribute::Important],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Important));
}

#[test]
fn special_use_name_fallback_infers_important() {
    let info = MailboxInfo {
        name: MailboxName::new("Important").unwrap(),
        delimiter: Some('/'),
        attributes: vec![],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Important));
}

#[test]
fn special_use_name_fallback_infers_all_mail() {
    let info = MailboxInfo {
        name: MailboxName::new("[Gmail]/All Mail").unwrap(),
        delimiter: Some('/'),
        attributes: vec![],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::All));
}

#[test]
fn special_use_name_fallback_infers_flagged_from_starred() {
    let info = MailboxInfo {
        name: MailboxName::new("Starred").unwrap(),
        delimiter: Some('/'),
        attributes: vec![],
        ..Default::default()
    };
    assert_eq!(info.special_use(), Some(SpecialUse::Flagged));
}

#[test]
fn special_use_name_fallback_returns_none_for_unknown_name() {
    // When no RFC 6154 attribute is present and the mailbox name does not
    // match any well-known name convention, special_use() returns None.
    let info = MailboxInfo {
        name: MailboxName::new("Projects/2024/reports").unwrap(),
        delimiter: Some('/'),
        attributes: vec![MailboxAttribute::HasNoChildren],
        ..Default::default()
    };
    assert_eq!(info.special_use(), None);
}

#[test]
fn status_item_mailboxid() {
    let item = StatusItem::MailboxId("F2212ea87-6097-4256".into());
    assert_eq!(item, StatusItem::MailboxId("F2212ea87-6097-4256".into()));
}

// --- MailboxAttribute::as_imap_str() ---

#[test]
fn as_imap_str_base_attributes() {
    assert_eq!(MailboxAttribute::NoInferiors.as_imap_str(), "\\Noinferiors");
    assert_eq!(MailboxAttribute::NoSelect.as_imap_str(), "\\Noselect");
    assert_eq!(MailboxAttribute::NonExistent.as_imap_str(), "\\NonExistent");
    assert_eq!(MailboxAttribute::HasChildren.as_imap_str(), "\\HasChildren");
    assert_eq!(
        MailboxAttribute::HasNoChildren.as_imap_str(),
        "\\HasNoChildren"
    );
    assert_eq!(MailboxAttribute::Marked.as_imap_str(), "\\Marked");
    assert_eq!(MailboxAttribute::Unmarked.as_imap_str(), "\\Unmarked");
    assert_eq!(MailboxAttribute::Subscribed.as_imap_str(), "\\Subscribed");
    assert_eq!(MailboxAttribute::Remote.as_imap_str(), "\\Remote");
}

#[test]
fn as_imap_str_special_use_attributes() {
    assert_eq!(MailboxAttribute::All.as_imap_str(), "\\All");
    assert_eq!(MailboxAttribute::Archive.as_imap_str(), "\\Archive");
    assert_eq!(MailboxAttribute::Drafts.as_imap_str(), "\\Drafts");
    assert_eq!(MailboxAttribute::Flagged.as_imap_str(), "\\Flagged");
    assert_eq!(MailboxAttribute::Junk.as_imap_str(), "\\Junk");
    assert_eq!(MailboxAttribute::Sent.as_imap_str(), "\\Sent");
    assert_eq!(MailboxAttribute::Trash.as_imap_str(), "\\Trash");
    assert_eq!(MailboxAttribute::Important.as_imap_str(), "\\Important");
}

#[test]
fn as_imap_str_non_standard_attributes() {
    // Google-origin non-standard attributes (widely seen in the wild).
    assert_eq!(MailboxAttribute::Memos.as_imap_str(), "\\Memos");
    assert_eq!(MailboxAttribute::Scheduled.as_imap_str(), "\\Scheduled");
    assert_eq!(MailboxAttribute::Snoozed.as_imap_str(), "\\Snoozed");
}

#[test]
fn as_imap_str_custom() {
    let attr = MailboxAttribute::Custom("\\MyCustom".into());
    assert_eq!(attr.as_imap_str(), "\\MyCustom");
}

// ===== Spec audit tests for L13/L7 =====

// ===== Case-insensitive Custom attribute equality (RFC 3501 Section 7.2.2) =====

#[test]
fn custom_attribute_equality_is_case_insensitive() {
    // RFC 3501 Section 7.2.2: mailbox attributes use flag-extension = "\" atom.
    // IMAP atoms are case-insensitive, so custom attributes must compare
    // case-insensitively.
    assert_eq!(
        MailboxAttribute::Custom("\\MyCustom".into()),
        MailboxAttribute::Custom("\\mycustom".into()),
        "Custom mailbox attributes must compare case-insensitively per RFC 3501 Section 7.2.2"
    );
}

#[test]
fn custom_attribute_hash_is_case_insensitive() {
    // RFC 3501 Section 7.2.2: case-insensitive custom attributes must hash equally.
    use std::collections::HashSet;
    let mut set = HashSet::new();
    set.insert(MailboxAttribute::Custom("\\MyCustom".into()));
    set.insert(MailboxAttribute::Custom("\\mycustom".into()));
    assert_eq!(
            set.len(),
            1,
            "Case-insensitively equal Custom attributes must have the same Hash per RFC 3501 Section 7.2.2"
        );
}

// ===== RFC 3501 Section 7.2.2 audit: cross-representation equality =====

#[test]
fn custom_noselect_equals_noselect_variant() {
    // RFC 3501 Section 7.2.2: mailbox attributes are case-insensitive atoms.
    // Custom("\\Noselect") represents the same attribute as NoSelect.
    assert_eq!(
        MailboxAttribute::Custom("\\Noselect".into()),
        MailboxAttribute::NoSelect,
        "Custom(\"\\\\Noselect\") must equal MailboxAttribute::NoSelect \
             per RFC 3501 Section 7.2.2"
    );
}

#[test]
fn custom_haschildren_equals_haschildren_variant() {
    // RFC 3348: \HasChildren attribute.
    assert_eq!(
        MailboxAttribute::Custom("\\HasChildren".into()),
        MailboxAttribute::HasChildren,
        "Custom(\"\\\\HasChildren\") must equal MailboxAttribute::HasChildren \
             per RFC 3501 Section 7.2.2"
    );
}

#[test]
fn custom_sent_equals_sent_variant() {
    // RFC 6154 Section 2: \Sent special-use attribute.
    assert_eq!(
        MailboxAttribute::Custom("\\Sent".into()),
        MailboxAttribute::Sent,
        "Custom(\"\\\\Sent\") must equal MailboxAttribute::Sent \
             per RFC 6154 Section 2"
    );
}

#[test]
fn custom_attribute_cross_representation_hash() {
    // RFC 3501 Section 7.2.2: equal attributes must hash identically.
    use std::collections::HashSet;
    let mut set = HashSet::new();
    set.insert(MailboxAttribute::NoSelect);
    set.insert(MailboxAttribute::Custom("\\Noselect".into()));
    assert_eq!(
        set.len(),
        1,
        "Custom(\"\\\\Noselect\") and NoSelect must hash the same \
             per RFC 3501 Section 7.2.2"
    );
}

// ===== is_special_use() regression tests (RFC 6154 Section 2) =====
//
// Custom variants whose wire form matches a known base LIST attribute
// (RFC 3501 Section 7.2.2) must NOT be classified as special-use.

#[test]
fn is_special_use_custom_noselect_is_not_special_use() {
    // RFC 3501 Section 7.2.2: \Noselect is a base LIST attribute, not a
    // special-use attribute (RFC 6154 Section 2).
    assert!(
        !MailboxAttribute::Custom("\\Noselect".into()).is_special_use(),
        "Custom(\"\\\\Noselect\") must not be special-use — it is a base LIST attribute"
    );
}

#[test]
fn is_special_use_custom_haschildren_is_not_special_use() {
    // RFC 3348: \HasChildren is a base LIST attribute.
    assert!(
        !MailboxAttribute::Custom("\\HasChildren".into()).is_special_use(),
        "Custom(\"\\\\HasChildren\") must not be special-use — it is a base LIST attribute"
    );
}

#[test]
fn is_special_use_custom_marked_is_not_special_use() {
    // RFC 3501 Section 7.2.2: \Marked is a base LIST attribute.
    assert!(
        !MailboxAttribute::Custom("\\Marked".into()).is_special_use(),
        "Custom(\"\\\\Marked\") must not be special-use — it is a base LIST attribute"
    );
}

#[test]
fn is_special_use_custom_unknown_is_special_use() {
    // RFC 6154 Section 2: genuinely unknown attributes are potential
    // use-attr-ext and should be treated as special-use.
    assert!(
        MailboxAttribute::Custom("\\MyCustomFlag".into()).is_special_use(),
        "Custom(\"\\\\MyCustomFlag\") should be special-use (potential use-attr-ext)"
    );
}

#[test]
fn is_special_use_custom_missing_backslash_is_not_special_use() {
    // RFC 6154 Section 6: use-attr-ext = "\" atom. A custom special-use
    // attribute without the leading backslash is not valid use-attr-ext.
    assert!(
            !MailboxAttribute::Custom("MyCustomFlag".into()).is_special_use(),
            "Custom(\"MyCustomFlag\") must not be special-use because use-attr-ext requires a leading backslash"
        );
}

#[test]
fn is_special_use_custom_with_invalid_atom_char_is_not_special_use() {
    // RFC 6154 Section 6 imports IMAP atom syntax from RFC 3501
    // Section 9, so SP is not legal inside the atom that follows "\".
    assert!(
        !MailboxAttribute::Custom("\\Bad Attr".into()).is_special_use(),
        "Custom(\"\\\\Bad Attr\") must not be special-use because SP is not legal in IMAP atoms"
    );
}

#[test]
fn is_special_use_known_special_use_variants() {
    // RFC 6154 Section 2 defined special-use attributes.
    assert!(MailboxAttribute::All.is_special_use());
    assert!(MailboxAttribute::Archive.is_special_use());
    assert!(MailboxAttribute::Drafts.is_special_use());
    assert!(MailboxAttribute::Flagged.is_special_use());
    assert!(MailboxAttribute::Junk.is_special_use());
    assert!(MailboxAttribute::Sent.is_special_use());
    assert!(MailboxAttribute::Trash.is_special_use());
    assert!(MailboxAttribute::Important.is_special_use());
}

#[test]
fn is_special_use_base_list_variants_are_not_special_use() {
    // RFC 3501 Section 7.2.2: base LIST attributes are never special-use.
    assert!(!MailboxAttribute::NoInferiors.is_special_use());
    assert!(!MailboxAttribute::NoSelect.is_special_use());
    assert!(!MailboxAttribute::NonExistent.is_special_use());
    assert!(!MailboxAttribute::HasChildren.is_special_use());
    assert!(!MailboxAttribute::HasNoChildren.is_special_use());
    assert!(!MailboxAttribute::Marked.is_special_use());
    assert!(!MailboxAttribute::Unmarked.is_special_use());
    assert!(!MailboxAttribute::Subscribed.is_special_use());
    assert!(!MailboxAttribute::Remote.is_special_use());
}

#[test]
fn is_special_use_custom_base_attr_case_insensitive() {
    // RFC 3501 Section 7.2.2: IMAP atoms are case-insensitive, so a Custom
    // variant with any casing of a base LIST attribute wire form must not be
    // classified as special-use.
    assert!(
        !MailboxAttribute::Custom("\\NOSELECT".into()).is_special_use(),
        "Case-insensitive match: \\NOSELECT is \\Noselect"
    );
    assert!(
        !MailboxAttribute::Custom("\\haschildren".into()).is_special_use(),
        "Case-insensitive match: \\haschildren is \\HasChildren"
    );
    assert!(
        !MailboxAttribute::Custom("\\NOINFERIORS".into()).is_special_use(),
        "Case-insensitive match: \\NOINFERIORS is \\Noinferiors"
    );
}

#[test]
fn spec_audit_l13_appendlimit_status_attribute() {
    // RFC 7889 Section 3: APPENDLIMIT is a per-mailbox STATUS attribute.
    let input = b"* STATUS \"INBOX\" (MESSAGES 10 APPENDLIMIT 1048576)\r\n";
    let (_, resp) = crate::codec::decode::parse_response(input).expect("should parse APPENDLIMIT");
    match resp {
        crate::types::response::Response::Untagged(inner) => match *inner {
            crate::types::response::UntaggedResponse::MailboxStatus { ref items, .. } => {
                let has_appendlimit = items
                    .iter()
                    .any(|item| matches!(item, StatusItem::AppendLimit(Some(1_048_576))));
                assert!(
                    has_appendlimit,
                    "STATUS response should contain AppendLimit(Some(1048576)); got {items:?}"
                );
            }
            other => panic!("expected MailboxStatus, got {other:?}"),
        },
        other => panic!("expected Untagged, got {other:?}"),
    }
}

// ===== MailboxAttribute::from_imap_str / From<String> / From<&str> =====

#[test]
fn mailbox_attr_from_str_known_variants() {
    // RFC 3501 Section 7.2.2 / RFC 6154 Section 2: known attributes.
    assert_eq!(
        MailboxAttribute::from("\\Noselect"),
        MailboxAttribute::NoSelect
    );
    assert_eq!(
        MailboxAttribute::from("\\HasChildren"),
        MailboxAttribute::HasChildren
    );
    assert_eq!(MailboxAttribute::from("\\Sent"), MailboxAttribute::Sent);
    assert_eq!(MailboxAttribute::from("\\Drafts"), MailboxAttribute::Drafts);
    assert_eq!(MailboxAttribute::from("\\Trash"), MailboxAttribute::Trash);
    assert_eq!(MailboxAttribute::from("\\Junk"), MailboxAttribute::Junk);
    assert_eq!(
        MailboxAttribute::from("\\Archive"),
        MailboxAttribute::Archive
    );
    assert_eq!(
        MailboxAttribute::from("\\Important"),
        MailboxAttribute::Important
    );
    assert_eq!(
        MailboxAttribute::from("\\NonExistent"),
        MailboxAttribute::NonExistent
    );
}

#[test]
fn mailbox_attr_from_string_known_variant() {
    // RFC 3501 Section 7.2.2: via owned String.
    assert_eq!(
        MailboxAttribute::from("\\Sent".to_owned()),
        MailboxAttribute::Sent
    );
    assert_eq!(
        MailboxAttribute::from("\\Trash".to_owned()),
        MailboxAttribute::Trash
    );
}

#[test]
fn mailbox_attr_from_str_case_insensitive() {
    // RFC 3501 Section 7.2.2: mailbox attributes are case-insensitive.
    assert_eq!(
        MailboxAttribute::from("\\NOSELECT"),
        MailboxAttribute::NoSelect
    );
    assert_eq!(
        MailboxAttribute::from("\\noselect"),
        MailboxAttribute::NoSelect
    );
    assert_eq!(
        MailboxAttribute::from("\\HASCHILDREN"),
        MailboxAttribute::HasChildren
    );
    assert_eq!(MailboxAttribute::from("\\sent"), MailboxAttribute::Sent);
}

#[test]
fn mailbox_attr_from_str_custom() {
    // Unrecognized attributes preserved verbatim.
    assert_eq!(
        MailboxAttribute::from("\\MyCustom"),
        MailboxAttribute::Custom("\\MyCustom".to_owned())
    );
}

#[test]
fn mailbox_attr_from_str_non_standard_google() {
    // Non-standard Google-origin attributes.
    assert_eq!(MailboxAttribute::from("\\Memos"), MailboxAttribute::Memos);
    assert_eq!(
        MailboxAttribute::from("\\Scheduled"),
        MailboxAttribute::Scheduled
    );
    assert_eq!(
        MailboxAttribute::from("\\Snoozed"),
        MailboxAttribute::Snoozed
    );
}