Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/mail_search.rs

13.8 KiB, 126 runs

created by r2968587332:77836, 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 one matcher over real message bytes, and the typed operators round trip.
2
3use oxedyne_fe2o3_core::prelude::*;
4use oxedyne_fe2o3_jdat::prelude::*;
5use oxedyne_fe2o3_mail::search::{
6 Day,
7 Filter,
8 Folders,
9 Limits,
10 Mail,
11 Matcher,
12 Mode,
13 Query,
14 Scope,
15 Term,
16};
17
18use std::time::Instant;
19
20const TODAY: Day = Day { year: 2026, month: 3, day: 4 };
21const LIM: Limits = Limits { search_regex_max_len: 200, search_max_depth: 16, search_max_terms: 64,
22 snippet_chars: 40, snippets_per_part: 2, ..Limits::DEFAULT };
23
24fn mail(raw: &[u8]) -> Mail<'_> {
25 Mail { raw, seen: false, flagged: false, size: raw.len() as u64, day: Day::parse("2026/03/04").ok() }
26}
27
28fn text_msg(subject: &str, body: &str) -> Vec<u8> {
29 format!("From: Ada <ada@x.example>\r\nTo: Bob <bob@x.example>\r\nBcc: Eve <eve@x.example>\r\n\
30 Subject: {}\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n{}\r\n", subject, body).into_bytes()
31}
32
33fn leaf(mode: Mode, text: &str) -> Term {
34 Term::Text { scope: None, mode, text: text.to_string() }
35}
36
37fn q(text: &str, mode: Mode) -> Query {
38 Query { terms: leaf(mode, text), ..Query::default() }
39}
40
41fn all(v: Vec<Term>) -> Query {
42 Query { terms: Term::All(v), ..Query::default() }
43}
44
45fn hit(q: &Query, raw: &[u8]) -> Outcome<bool> {
46 let m = res!(Matcher::new(q, &LIM, TODAY));
47 Ok(res!(m.matches(&mail(raw))).is_some())
48}
49
50#[test]
51fn word_boundaries_and_case() -> Outcome<()> {
52 let concat = text_msg("hello", "a concat b");
53 let cat = text_msg("hello", "the Cat sat");
54 assert!(!res!(hit(&q(r"\bcat\b", Mode::Regex), &concat)));
55 assert!(res!(hit(&q("cat", Mode::Word), &concat)));
56 let ww = Query { whole_word: true, ..q("cat", Mode::Word) };
57 assert!(!res!(hit(&ww, &concat)));
58 assert!(res!(hit(&ww, &cat)));
59 assert!(!res!(hit(&Query { match_case: true, ..ww.clone() }, &cat)));
60 // Whole word for a word that ends in punctuation: `C++` is a word in "use C++ here".
61 assert!(res!(hit(&Query { whole_word: true, ..q("C++", Mode::Word) }, &text_msg("x", "use C++ here"))));
62 // `-cat` under whole word keeps "concat".
63 let ex = Query { whole_word: true, ..all(vec![leaf(Mode::Word, "dog"), Term::Not(Box::new(leaf(Mode::Word, "cat")))]) };
64 assert!(res!(hit(&ex, &text_msg("x", "dog concat"))));
65 assert!(!res!(hit(&ex, &text_msg("x", "dog cat"))));
66 Ok(())
67}
68
69#[test]
70fn modes_and_scopes() -> Outcome<()> {
71 let m = text_msg("Quarterly report", "figures for march");
72 let words = |a: &str, b: &str| vec![leaf(Mode::Word, a), leaf(Mode::Word, b)];
73 assert!(res!(hit(&all(words("report", "march")), &m))); // each word in some scope
74 assert!(!res!(hit(&all(words("report", "april")), &m)));
75 assert!(res!(hit(&Query { terms: Term::Any(words("april", "march")), ..Query::default() }, &m)));
76 assert!(!res!(hit(&Query { scopes: vec![Scope::Subject], ..q("march", Mode::Word) }, &m)));
77 assert!(res!(hit(&Query { scopes: vec![Scope::Bcc], ..q("eve", Mode::Word) }, &m)));
78 let f = |s| all(vec![Term::Text { scope: Some(s), mode: Mode::Word, text: "ada".into() }]);
79 assert!(res!(hit(&f(Scope::From), &m)));
80 assert!(!res!(hit(&f(Scope::To), &m)));
81 Ok(())
82}
83
84#[test]
85fn phrase_across_folds_and_soft_breaks() -> Outcome<()> {
86 let folded = b"From: a@x\r\nSubject: the quarterly\r\n\treport is late\r\n\r\nnothing\r\n".to_vec();
87 let soft = b"From: a@x\r\nSubject: s\r\nContent-Type: text/plain\r\nContent-Transfer-Encoding: \
88 quoted-printable\r\n\r\nthe quar=\r\nterly\r\nreport is late\r\n".to_vec();
89 let p = q("quarterly report", Mode::Phrase);
90 assert!(res!(hit(&p, &folded)));
91 assert!(res!(hit(&p, &soft)));
92 assert!(!res!(hit(&p, &text_msg("x", "quarterly figures, report"))));
93 Ok(())
94}
95
96#[test]
97fn decoded_headers_and_bidi() -> Outcome<()> {
98 // Subject "Café menu" in RFC 2047; From a Hebrew name with a right-to-left mark inside it.
99 let raw = "From: =?utf-8?B?15PXldeT4oCP15M=?= <d@x>\r\nSubject: =?utf-8?Q?Caf=C3=A9_menu?=\r\n\r\nhi\r\n";
100 assert!(res!(hit(&q("café", Mode::Word), raw.as_bytes())));
101 assert!(res!(hit(&Query { scopes: vec![Scope::From], ..q("דודד", Mode::Word) }, raw.as_bytes())));
102 Ok(())
103}
104
105#[test]
106fn attachment_names_not_bytes() -> Outcome<()> {
107 let raw = b"From: a@x\r\nSubject: s\r\nContent-Type: multipart/mixed; boundary=B\r\n\r\n--B\r\n\
108 Content-Type: text/plain\r\n\r\nsee attached\r\n--B\r\nContent-Type: application/octet-stream\r\n\
109 Content-Disposition: attachment; filename=\"budget.xls\"\r\n\r\nsecretword inside\r\n--B--\r\n";
110 assert!(!res!(hit(&q("secretword", Mode::Word), raw)));
111 let h = res!(res!(Matcher::new(&q("budget", Mode::Word), &LIM, TODAY)).matches(&mail(raw)));
112 let h = res!(h.ok_or_else(|| err!("no hit on the file name"; Missing)));
113 assert_eq!(h.parts[0].scope, Scope::Attachment);
114 assert!(res!(hit(&all(vec![Term::Filter(Filter::HasAttachment)]), raw)));
115 assert!(!res!(hit(&all(vec![Term::Filter(Filter::HasAttachment)]), &text_msg("s", "b"))));
116 Ok(())
117}
118
119#[test]
120fn bounded_regex_and_large_part() -> Outcome<()> {
121 let mut big = "lorem ipsum ".repeat(5 * 1024 * 1024 / 12);
122 big.push_str("needle at the end");
123 let raw = text_msg("big", &big);
124 let t = Instant::now();
125 let h = res!(res!(Matcher::new(&Query { whole_word: true, ..q("needle", Mode::Word) }, &LIM, TODAY))
126 .matches(&mail(&raw)));
127 let h = res!(h.ok_or_else(|| err!("needle missed in 5 MB"; Missing)));
128 assert!(h.parts[0].snippets[0].text.chars().count() <= LIM.snippet_chars + "needle".len());
129 // Catastrophic nesting answers, in linear time.
130 assert!(!res!(hit(&q("(a+)+$", Mode::Regex), &text_msg("x", &format!("{}!", "a".repeat(30_000))))));
131 // A pattern too wide for the engine's visited set over 5 MB is an error, not a guess.
132 assert!(res!(Matcher::new(&q("(?:a+b+c+d+e+f+g+h+){4}x", Mode::Regex), &LIM, TODAY)).matches(&mail(&raw)).is_err());
133 assert!(t.elapsed().as_secs() < 60);
134 // A pattern over the caller's limit is refused before it runs.
135 assert!(Matcher::new(&q(&"a".repeat(201), Mode::Regex), &LIM, TODAY).is_err());
136 Ok(())
137}
138
139#[test]
140fn snippets_bounded() -> Outcome<()> {
141 let body = "x ".repeat(100) + "one fox " + &"y ".repeat(100) + "two fox " + &"z ".repeat(100) + "three fox";
142 let h = res!(res!(Matcher::new(&q("fox", Mode::Word), &LIM, TODAY)).matches(&mail(&text_msg("s", &body))));
143 let h = res!(h.ok_or_else(|| err!("fox missed"; Missing)));
144 let p = &h.parts[0];
145 assert_eq!(p.scope, Scope::Body);
146 assert_eq!(p.snippets.len(), 2);
147 for s in &p.snippets {
148 assert_eq!(s.spans.len(), 1);
149 assert_eq!(s.spans[0].as_str(&s.text), "fox");
150 }
151 Ok(())
152}
153
154#[test]
155fn filters_and_empty_query() -> Outcome<()> {
156 let raw = text_msg("s", "b");
157 let m = Mail { seen: true, flagged: false, ..mail(&raw) };
158 let e = res!(Matcher::new(&Query::default(), &LIM, TODAY));
159 assert!(res!(e.matches(&m)).is_some());
160 let check = |qq: Query| -> Outcome<bool> { Ok(res!(res!(Matcher::new(&qq, &LIM, TODAY)).matches(&m)).is_some()) };
161 assert!(res!(check(res!(Query::parse("is:read", &LIM)))));
162 assert!(!res!(check(res!(Query::parse("is:unread", &LIM)))));
163 assert!(!res!(check(res!(Query::parse("is:starred", &LIM)))));
164 assert!(!res!(check(res!(Query::parse("larger:10000", &LIM)))));
165 assert!(res!(check(res!(Query::parse("smaller:10000", &LIM)))));
166 assert!(res!(check(res!(Query::parse("after:2026-03-04", &LIM)))));
167 assert!(!res!(check(res!(Query::parse("before:2026/03/04", &LIM)))));
168 Ok(())
169}
170
171#[test]
172fn operators_round_trip() -> Outcome<()> {
173 let p = res!(Query::parse(r#"from:ada to:"Bob B" cc:c bcc:d subject:plan filename:x.pdf has:attachment is:unread
174 -is:starred after:2024/01/31 before:2024-03-01 larger:2M smaller:100 "net profit" draft -spam in:inbox"#, &LIM));
175 let v = match &p.terms { Term::All(v) => v.clone(), t => return Err(err!("not All: {:?}", t; Test)) };
176 assert_eq!(v.len(), 16);
177 assert!(v.contains(&Term::Filter(Filter::Larger(2 * 1024 * 1024))));
178 assert!(v.contains(&Term::Filter(Filter::Unseen)));
179 assert!(v.contains(&Term::Not(Box::new(Term::Filter(Filter::Flagged)))));
180 assert_eq!(p.folders, Folders::List(vec!["inbox".into()]));
181 for s in [
182 r#"from:ada to:"Bob B" subject:plan "net profit" draft -spam"#,
183 "a OR b OR c", r#""exact words""#, r"/inv(oice)?\/\d+/ match:case", r#""a b" match:substring"#,
184 "is:read is:starred -has:attachment in:anywhere", "x in:all scope:subject,body match:word", "",
185 ] {
186 let a = res!(Query::parse(s, &LIM));
187 assert_eq!(res!(Query::parse(&a.to_string(), &LIM)), a, "round trip of {:?} via {:?}", s, a.to_string());
188 }
189 let j = res!(Query::parse(r#"from:ada "a b" x -y is:unread after:2024/01/31 in:anywhere match:word"#, &LIM));
190 assert_eq!(res!(Query::from_dat(res!(j.to_dat()), &LIM)), j);
191 for junk in ["from:", r#""unclosed"#, "/(/", "is:nonsense", "a OR", "(a", "a)", "-in:x", "after:2024/02/30", "larger:big"] {
192 match Query::parse(junk, &LIM) {
193 Ok(x) => return Err(err!("junk {:?} parsed as {:?}", junk, x; Test)),
194 Err(e) => assert!(!fmt!("{}", e).is_empty()),
195 }
196 }
197 Ok(())
198}
199
200// ── Round 2: the boolean tree, by typed text only ────────────────────────
201
202fn hit_s(s: &str, raw: &[u8]) -> Outcome<bool> {
203 let m = res!(Matcher::new(&res!(Query::parse(s, &LIM)), &LIM, TODAY));
204 Ok(res!(m.matches(&mail(raw))).is_some())
205}
206
207fn round_trip(s: &str) -> Outcome<()> {
208 let a = res!(Query::parse(s, &LIM));
209 assert_eq!(res!(Query::parse(&a.to_string(), &LIM)), a, "round trip of {:?} via {:?}", s, a.to_string());
210 Ok(())
211}
212
213#[test]
214fn r2_negated_field() -> Outcome<()> {
215 let from = |who: &str| format!("From: {}\r\nSubject: invoice\r\n\r\nbody\r\n", who).into_bytes();
216 assert!(!res!(hit_s("-from:ada invoice", &from("Ada <ada@x>"))));
217 assert!(res!(hit_s("-from:ada invoice", &from("Bob <bob@x>"))));
218 round_trip("-from:ada invoice")
219}
220
221#[test]
222fn r2_or_beside_words() -> Outcome<()> {
223 assert!(res!(hit_s("invoice march OR april", &text_msg("s", "invoice for april"))));
224 assert!(!res!(hit_s("invoice march OR april", &text_msg("s", "invoice alone"))));
225 assert!(!res!(hit_s("invoice march OR april", &text_msg("s", "march and april"))));
226 round_trip("invoice march OR april")
227}
228
229#[test]
230fn r2_regex_beside_words() -> Outcome<()> {
231 assert!(res!(hit_s(r"/inv\d+/ paid", &text_msg("s", "inv42 is paid"))));
232 assert!(!res!(hit_s(r"/inv\d+/ paid", &text_msg("s", "inv42 is due"))));
233 round_trip(r"/inv\d+/ paid")
234}
235
236#[test]
237fn r2_two_regexes() -> Outcome<()> {
238 assert!(res!(hit_s(r"/inv\d+/ /due \d+/", &text_msg("s", "inv42 due 7"))));
239 assert!(!res!(hit_s(r"/inv\d+/ /due \d+/", &text_msg("s", "inv42 due soon"))));
240 round_trip(r"/inv\d+/ /due \d+/")
241}
242
243#[test]
244fn r2_groups_round_trip() -> Outcome<()> {
245 for s in ["(a b) OR c", "-(a OR b) c", "from:(a b) x", "{a b} c", "a OR (b OR c)", r#"subject:"x y" OR -to:z"#] {
246 res!(round_trip(s));
247 }
248 assert!(res!(hit_s("(quarterly figures) OR nothing", &text_msg("Quarterly report", "figures"))));
249 // A switch of the whole query (in:, scope:, match:) is refused inside OR, in words.
250 assert!(Query::parse("in:inbox OR x", &LIM).is_err());
251 Ok(())
252}
253
254#[test]
255fn r2_unknown_stays_unknown() -> Outcome<()> {
256 let mut big = "lorem ipsum ".repeat(5 * 1024 * 1024 / 12);
257 big.push_str("needle");
258 let raw = text_msg("big", &big);
259 let m = res!(Matcher::new(&res!(Query::parse("needle -/(?:a+b+c+d+e+f+g+h+){4}x/", &LIM)), &LIM, TODAY));
260 assert!(m.matches(&mail(&raw)).is_err());
261 Ok(())
262}
263
264#[test]
265fn r2_or_word_edge() -> Outcome<()> {
266 // A word "OR" and a word holding a key, as text, print quoted and read back as words.
267 let a = all(vec![leaf(Mode::Word, "salt"), leaf(Mode::Word, "OR"), leaf(Mode::Word, "from:x")]);
268 assert_eq!(res!(Query::parse(&a.to_string(), &LIM)), a, "via {:?}", a.to_string());
269 Ok(())
270}
271
272// ── Round 3: filters are leaves of the tree ──────────────────────────────
273
274fn hit_m(s: &str, m: &Mail) -> Outcome<Option<bool>> {
275 let mt = res!(Matcher::new(&res!(Query::parse(s, &LIM)), &LIM, TODAY));
276 Ok(mt.matches(m).ok().map(|h| h.is_some())) // None: the answer is unknown
277}
278
279#[test]
280fn r3_filter_beside_or() -> Outcome<()> {
281 let raw = text_msg("s", "b");
282 let s = "is:unread OR has:attachment";
283 assert_eq!(res!(hit_m(s, &Mail { seen: true, ..mail(&raw) })), Some(false));
284 assert_eq!(res!(hit_m(s, &mail(&raw))), Some(true));
285 round_trip(s)
286}
287
288#[test]
289fn r3_filter_in_group() -> Outcome<()> {
290 let raw = text_msg("s", "invoice");
291 let s = "invoice (larger:100000 OR is:starred)";
292 assert_eq!(res!(hit_m(s, &mail(&raw))), Some(false));
293 assert_eq!(res!(hit_m(s, &Mail { flagged: true, ..mail(&raw) })), Some(true));
294 round_trip(s)
295}
296
297#[test]
298fn r3_negated_filter_in_group() -> Outcome<()> {
299 let raw = text_msg("s", "b");
300 let s = "-(is:read after:2026/01/01)";
301 assert_eq!(res!(hit_m(s, &Mail { seen: true, ..mail(&raw) })), Some(false));
302 assert_eq!(res!(hit_m(s, &mail(&raw))), Some(true));
303 round_trip(s)
304}
305
306#[test]
307fn r3_unknown_day() -> Outcome<()> {
308 let raw = text_msg("s", "b");
309 let nod = |seen| Mail { seen, day: None, ..mail(&raw) };
310 // Inside Any: decided by the known side, else unknown.
311 assert_eq!(res!(hit_m("after:2026/01/01 OR is:read", &nod(true))), Some(true));
312 assert_eq!(res!(hit_m("after:2026/01/01 OR is:read", &nod(false))), None);
313 // Under Not, and alone: unknown, never a miss.
314 assert_eq!(res!(hit_m("-after:2026/01/01", &nod(false))), None);
315 assert_eq!(res!(hit_m("before:2026/01/01", &nod(false))), None);
316 // Decided without it: a known false side of All.
317 assert_eq!(res!(hit_m("before:2026/01/01 is:read", &nod(false))), Some(false));
318 round_trip("after:2026/01/01 OR is:read")
319}