Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/mail_search_lang.rs

8.9 KiB, 3 runs

created by r2946818238:78185, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `search`, the language: Gmail's operators read or refused, empty text refused, a scope
2//! taking text only, the round trip over generated trees, and accents in two Unicode forms.
3
4use oxedyne_fe2o3_core::prelude::*;
5use oxedyne_fe2o3_mail::search::{
6 Day,
7 Folders,
8 Limits,
9 Mail,
10 Matcher,
11 Mode,
12 Query,
13 Scope,
14 Term,
15};
16
17const LIM: Limits = Limits { search_regex_max_len: 200, search_max_depth: 16, search_max_terms: 500,
18 snippet_chars: 40, snippets_per_part: 2, ..Limits::DEFAULT };
19
20// Where the API meets these cases.
21fn parse(s: &str) -> Outcome<Query> { Query::parse(s, &LIM) }
22fn matcher(q: &Query, today: &str) -> Outcome<Matcher> { Matcher::new(q, &LIM, res!(Day::parse(today))) }
23
24fn msg(subject: &str, body: &str) -> Vec<u8> {
25 format!("From: Ada <ada@x.example>\r\nTo: Bob <bob@x.example>\r\nSubject: {}\r\n\
26 Content-Type: text/plain; charset=utf-8\r\n\r\n{}\r\n", subject, body).into_bytes()
27}
28
29fn on<'a>(raw: &'a [u8], day: &str) -> Mail<'a> {
30 Mail { raw, seen: false, flagged: false, size: raw.len() as u64, day: Day::parse(day).ok() }
31}
32
33/// Hit, miss, or `None` where unknown.
34fn ans(s: &str, m: &Mail, today: &str) -> Outcome<Option<bool>> {
35 let mt = res!(matcher(&res!(parse(s)), today));
36 Ok(mt.matches(m).ok().map(|h| h.is_some()))
37}
38
39fn text(scope: Option<Scope>, mode: Mode, t: &str) -> Term {
40 Term::Text { scope, mode, text: t.to_string() }
41}
42
43fn refused(s: &str, says: &[&str]) {
44 match parse(s) {
45 Ok(q) => panic!("{:?} accepted as {:?}", s, q.to_string()),
46 Err(e) => {
47 let e = e.to_string();
48 for w in says { assert!(e.contains(w), "{:?} refused without {:?}: {}", s, w, e); }
49 }
50 }
51}
52
53#[test]
54fn lang_relative_dates() -> Outcome<()> {
55 let raw = msg("s", "b");
56 for (s, today, day, want) in [
57 ("newer_than:1m", "2026/03/31", "2026/02/28", true), // a month back from the 31st: the 28th
58 ("newer_than:1m", "2026/03/31", "2026/02/27", false),
59 ("older_than:1m", "2026/03/31", "2026/02/27", true),
60 ("older_than:1m", "2026/03/31", "2026/02/28", false),
61 ("older_than:1y", "2024/02/29", "2023/02/27", true), // a year back from a leap day
62 ("older_than:1y", "2024/02/29", "2023/02/28", false),
63 ("newer_than:1d", "2024/03/01", "2024/02/29", true),
64 ("newer_than:1d", "2024/03/01", "2024/02/28", false),
65 ("newer_than:1d", "2025/03/01", "2025/02/28", true),
66 ("newer_than:10d", "2026/01/05", "2025/12/26", true),
67 ("newer_than:10d", "2026/01/05", "2025/12/25", false),
68 ("newer_than:7d", "2026/03/10", "2026/03/04", true), // the same search, a later day
69 ("newer_than:7d", "2026/03/20", "2026/03/04", false),
70 ("from:ada newer_than:2y", "2026/03/04", "2024/03/04", true),
71 ] {
72 assert_eq!(res!(ans(s, &on(&raw, day), today)), Some(want), "{} on {} for a mail of {}", s, today, day);
73 }
74 for s in ["older_than:7d", "newer_than:2m", "-older_than:1y", "older_than:0d"] {
75 assert_eq!(res!(parse(s)).to_string(), s, "a relative date prints as written");
76 }
77 for s in ["older_than:7", "older_than:d", "newer_than:7w", "newer_than:-1d"] {
78 assert!(parse(s).is_err(), "{:?} accepted", s);
79 }
80 Ok(())
81}
82
83#[test]
84fn lang_gmail_size() -> Outcome<()> {
85 let raw = msg("s", "b");
86 let big = Mail { size: 2_000_000, ..on(&raw, "2026/03/04") };
87 assert_eq!(res!(ans("size:1000000", &big, "2026/03/04")), Some(true));
88 assert_eq!(res!(ans("size:1000000", &on(&raw, "2026/03/04"), "2026/03/04")), Some(false));
89 assert_eq!(res!(parse("size:1000000")).to_string(), "larger:1000000");
90 Ok(())
91}
92
93#[test]
94fn lang_gmail_refused() -> Outcome<()> {
95 refused("label:work", &["label:", "in:"]);
96 for (s, k) in [("deliveredto:me@x", "deliveredto:"), ("list:info@x", "list:"), ("category:primary", "category:"),
97 ("rfc822msgid:abc", "rfc822msgid:"), ("foo:bar", "foo:"), ("-label:x", "label:")]
98 {
99 refused(s, &[k, "quotes"]);
100 }
101 assert_eq!(res!(parse("http://x.example/a")).terms, text(None, Mode::Word, "http://x.example/a"), "a URL is text");
102 refused("a AROUND 3 b", &["AROUND", "quotes"]);
103 refused("+cat", &["+cat", "quotes"]);
104 // In quotes each is text, and text shaped so prints in quotes.
105 let raw = msg("label:work +cat AROUND", "b");
106 for s in [r#""label:work""#, r#""+cat""#, r#""AROUND""#, r#"subject:"label:work""#] {
107 assert_eq!(res!(ans(s, &on(&raw, "2026/03/04"), "2026/03/04")), Some(true), "{}", s);
108 }
109 for w in ["label:work", "+cat", "AROUND", "a:b"] {
110 let q = Query { terms: text(None, Mode::Word, w), ..Query::default() };
111 assert_eq!(res!(parse(&q.to_string())), q, "{:?} printed {:?}", w, q.to_string());
112 }
113 Ok(())
114}
115
116#[test]
117fn lang_empty_text_refused() {
118 for s in [r#"from:"""#, r#""""#, r#"" ""#, r#"-"""#, r#"a OR """#] {
119 refused(s, &["empty"]);
120 }
121 for t in ["", " ", "\t\r\n"] {
122 for mode in [Mode::Word, Mode::Phrase, Mode::Regex] {
123 let q = Query { terms: text(Some(Scope::From), mode, t), ..Query::default() };
124 assert!(matcher(&q, "2026/03/04").is_err(), "{:?} {:?} compiled", mode, t);
125 }
126 }
127}
128
129#[test]
130fn lang_scope_takes_text_only() -> Outcome<()> {
131 for s in ["from:is:read", "to:larger:1", "from:-is:read", "from:(a is:read)", "from:to:x", "subject:in:x"] {
132 refused(s, &["takes text"]);
133 }
134 assert!(parse("from:(ada OR bob)").is_ok());
135 Ok(())
136}
137
138#[test]
139fn lang_accents_two_forms() -> Outcome<()> {
140 let att = |name: &str| fmt!("From: a@x\r\nSubject: s\r\nContent-Type: multipart/mixed; boundary=B\r\n\r\n--B\r\n\
141 Content-Type: text/plain\r\n\r\nsee\r\n--B\r\nContent-Type: application/pdf\r\n\
142 Content-Disposition: attachment; filename=\"{}\"\r\n\r\nxx\r\n--B--\r\n", name).into_bytes();
143 let (nfc, nfd) = ("r\u{e9}sum\u{e9}", "re\u{301}sume\u{301}");
144 for (q, name) in [(nfc, nfd), (nfd, nfc), (nfc, nfc), (nfd, nfd)] {
145 let raw = att(&fmt!("{}.pdf", name));
146 let mt = res!(matcher(&res!(parse(&fmt!("filename:{}", q))), "2026/03/04"));
147 let h = res!(mt.matches(&on(&raw, "2026/03/04")));
148 let h = res!(h.ok_or_else(|| err!("filename:{:?} missed {:?}", q, name; Missing)));
149 let s = &h.parts[0].snippets[0];
150 assert_eq!(s.spans[0].as_str(&s.text), nfc, "the mark is taken from the composed text");
151 }
152 assert_eq!(res!(ans("e\u{301}cole", &on(&msg("x", "\u{c9}COLE"), "2026/03/04"), "2026/03/04")), Some(true));
153 Ok(())
154}
155
156#[test]
157fn lang_round_trip_property() -> Outcome<()> {
158 let mut r = Rng(0x9E37_79B9_7F4A_7C15);
159 for i in 0..3000 {
160 let depth = r.n(4);
161 let q = Query {
162 terms: res!(gen(&mut r, depth)),
163 whole_word: r.n(2) == 0,
164 match_case: r.n(2) == 0,
165 substring: r.n(2) == 0,
166 scopes: (0..r.n(3)).map(|_| SCOPES[r.n(7)]).collect(),
167 folders: match r.n(4) {
168 0 => Folders::List(vec!["inbox".into(), "a b".into()]),
169 1 => Folders::All { junk_trash: r.n(2) == 0 },
170 _ => Folders::Selected,
171 },
172 };
173 let s = q.to_string();
174 let back = res!(parse(&s).map_err(|e| err!("tree {} {:?} printed {:?}: {}", i, q, s, e; Test)));
175 assert_eq!(back, q, "tree {} printed {:?}", i, s);
176 }
177 Ok(())
178}
179
180struct Rng(u64);
181
182impl Rng {
183 fn n(&mut self, k: usize) -> usize {
184 self.0 ^= self.0 << 13;
185 self.0 ^= self.0 >> 7;
186 self.0 ^= self.0 << 17;
187 (self.0 % k as u64) as usize
188 }
189}
190
191const WORDS: [&str; 12] = ["cat", r"a\b", r#"say"hi""#, "in:x", "label:x", "OR", "-x", "+x", "AROUND", "x)", "(y", "/z"];
192const PHRASES: [&str; 3] = ["net profit", "a b", "x\ty \"q\""];
193const REGEXES: [&str; 4] = [r"a\/b", r"a\\", r"\d+\/x", r"inv(oice)?"];
194const FILTERS: [&str; 11] = ["after:2024/02/29", "before:2026/01/01", "larger:2M", "smaller:100", "is:read", "is:unread",
195 "is:starred", "has:attachment", "older_than:7d", "newer_than:2m", "older_than:1y"];
196const SCOPES: [Scope; 7] = [Scope::From, Scope::To, Scope::Cc, Scope::Bcc, Scope::Subject, Scope::Body, Scope::Attachment];
197
198fn gen_leaf(r: &mut Rng) -> Outcome<Term> {
199 let scope = match r.n(3) { 0 => Some(SCOPES[r.n(7)]), _ => None };
200 Ok(match r.n(4) {
201 0 => text(scope, Mode::Word, WORDS[r.n(WORDS.len())]),
202 1 => text(scope, Mode::Phrase, PHRASES[r.n(PHRASES.len())]),
203 2 => text(scope, Mode::Regex, REGEXES[r.n(REGEXES.len())]),
204 _ => res!(parse(FILTERS[r.n(FILTERS.len())])).terms,
205 })
206}
207
208fn gen(r: &mut Rng, depth: usize) -> Outcome<Term> {
209 if depth == 0 { return gen_leaf(r); }
210 let kids = |r: &mut Rng| -> Outcome<Vec<Term>> {
211 let mut v = Vec::new();
212 for _ in 0..r.n(4) { v.push(res!(gen(r, depth - 1))); }
213 Ok(v)
214 };
215 Ok(match r.n(4) {
216 0 => Term::Not(Box::new(res!(gen(r, depth - 1)))),
217 1 => Term::All(res!(kids(r))),
218 2 => Term::Any(res!(kids(r))),
219 _ => res!(gen_leaf(r)),
220 })
221}