Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/mail_search_f1.rs

7.1 KiB, 21 runs

created by r2968587332:77964, 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`, fix rounds 1 and 2: the depth and terms bounds, the stop once decided, the clipped snippet, one
2//! operand a scope, every holding alternative marked.
3
4use oxedyne_fe2o3_core::prelude::*;
5use oxedyne_fe2o3_mail::search::{
6 Day,
7 Limits,
8 Mail,
9 Matcher,
10 Mode,
11 Query,
12 Snippet,
13 Term,
14};
15
16use std::time::{
17 Duration,
18 Instant,
19};
20
21const MAX_DEPTH: usize = 16;
22const MAX_TERMS: usize = 50;
23const TODAY: Day = Day { year: 2026, month: 3, day: 4 };
24const LIM: Limits = Limits { search_regex_max_len: 200, search_max_depth: MAX_DEPTH, search_max_terms: MAX_TERMS,
25 snippet_chars: 40, snippets_per_part: 2, ..Limits::DEFAULT };
26
27// Where the API meets these cases.
28fn parse(s: &str) -> Outcome<Query> { Query::parse(s, &LIM) }
29fn matcher(q: &Query) -> Outcome<Matcher> { Matcher::new(q, &LIM, TODAY) }
30fn cut(s: &Snippet) -> bool { s.cut }
31
32fn msg(body: &str) -> Vec<u8> {
33 format!("From: Ada <ada@x.example>\r\nSubject: x\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n{}\r\n", body)
34 .into_bytes()
35}
36
37fn on(raw: &[u8]) -> Mail<'_> {
38 Mail { raw, seen: false, flagged: false, size: raw.len() as u64, day: Day::parse("2026/03/04").ok() }
39}
40
41/// Hit, miss, or `None` where unknown.
42fn ans(s: &str, m: &Mail) -> Outcome<Option<bool>> {
43 Ok(res!(matcher(&res!(parse(s)))).matches(m).ok().map(|h| h.is_some()))
44}
45
46fn refused<T: std::fmt::Debug>(r: Outcome<T>, bound: usize) {
47 match r {
48 Ok(x) => panic!("accepted past the bound of {}: {:?}", bound, x),
49 Err(e) => assert!(e.to_string().contains(&bound.to_string()), "refused without naming {}: {}", bound, e),
50 }
51}
52
53fn word(t: &str) -> Term { Term::Text { scope: None, mode: Mode::Word, text: t.to_string() } }
54
55#[test]
56fn f1_terms_bound() {
57 let words = |n: usize| (0..n).map(|i| fmt!("w{}", i)).collect::<Vec<_>>().join(" OR ");
58 assert!(parse(&words(MAX_TERMS)).is_ok());
59 refused(parse(&words(MAX_TERMS + 1)), MAX_TERMS);
60 refused(parse(&vec!["is:read"; MAX_TERMS + 1].join(" ")), MAX_TERMS);
61 let built = Term::Any((0..=MAX_TERMS).map(|i| word(&fmt!("w{}", i))).collect());
62 refused(matcher(&Query { terms: built, ..Query::default() }).map(|_| ()), MAX_TERMS);
63}
64
65#[test]
66fn f1_depth_bound() {
67 let parens = |n: usize| "(".repeat(n) + "a" + &")".repeat(n);
68 assert!(parse(&parens(MAX_DEPTH)).is_ok() && parse(&("-".repeat(MAX_DEPTH) + "a")).is_ok());
69 refused(parse(&parens(MAX_DEPTH + 1)), MAX_DEPTH);
70 refused(parse(&("-".repeat(MAX_DEPTH + 1) + "a")), MAX_DEPTH);
71 let mut t = word("a");
72 for _ in 0..=MAX_DEPTH { t = Term::Not(Box::new(Term::All(vec![t]))); }
73 refused(matcher(&Query { terms: t, ..Query::default() }).map(|_| ()), MAX_DEPTH);
74}
75
76// A group is decided by its first deciding child; on a miss the rest are not searched, and an unknown
77// child, before or after, never outvotes the deciding one.
78#[test]
79fn f1_stop_once_decided() -> Outcome<()> {
80 let raw = msg(&"lorem ipsum ".repeat(256 * 1024 / 12));
81 let m = on(&raw);
82 let zq = (0..40).map(|i| fmt!("zq{}", i)).collect::<Vec<_>>();
83 let mut t = Vec::new();
84 for s in ["-lorem".to_string(), fmt!("-lorem {}", zq.join(" ")), fmt!("lorem OR {}", zq.join(" OR "))] {
85 let at = Instant::now();
86 t.push((res!(ans(&s, &m)), at.elapsed()));
87 }
88 eprintln!("F1 decided: -lorem {:?}; and 40 more {:?}; lorem OR 40 more {:?}", t[0].1, t[1].1, t[2].1);
89 assert_eq!((t[0].0, t[1].0, t[2].0), (Some(false), Some(false), Some(true)));
90 // A miss stops at its deciding child; a hit searches the rest of an any-of for its marks.
91 let bound = t[0].1 * 5 + Duration::from_millis(200);
92 assert!(t[1].1 < bound, "a decided group searched on: {:?} against {:?}", t, bound);
93 let slow = "/(?:a+b+c+d+e+f+g+h+){4}x/";
94 let big = msg(&"abcdefgh".repeat(5 * 1024 * 1024 / 8));
95 let mb = on(&big);
96 assert_eq!(res!(ans(&fmt!("{} -abcdefgh", slow), &mb)), Some(false));
97 assert_eq!(res!(ans(&fmt!("{} OR abcdefgh", slow), &mb)), Some(true));
98 assert_eq!(res!(ans(&fmt!("{} abcdefgh", slow), &mb)), None);
99 Ok(())
100}
101
102// A wide match is clipped to its window and shown as running on.
103#[test]
104fn f1_snippet_clipped() -> Outcome<()> {
105 let raw = msg(&"word ".repeat(20_000));
106 let h = res!(res!(res!(matcher(&res!(parse(r"/w[\s\S]+/")))).matches(&on(&raw))).ok_or_else(|| err!("no hit"; Missing)));
107 let all: Vec<&Snippet> = h.parts.iter().flat_map(|p| p.snippets.iter()).collect();
108 for s in &all {
109 assert!(s.text.chars().count() <= LIM.snippet_chars, "a snippet of {} chars", s.text.chars().count());
110 assert!(s.spans.iter().all(|x| x.end <= s.text.len()));
111 }
112 assert!(all.iter().any(|s| cut(s)), "a clipped mark not shown as running on");
113 Ok(())
114}
115
116// Fix round 2.
117
118fn marks(s: &str, raw: &[u8]) -> Outcome<String> {
119 let h = res!(res!(res!(matcher(&res!(parse(s)))).matches(&on(raw))).ok_or_else(|| err!("{:?}: no hit", s; Missing)));
120 let mut v: Vec<String> = h.parts.iter().flat_map(|p| p.snippets.iter())
121 .flat_map(|s| s.spans.iter().map(|x| s.text.get(x.start..x.end).unwrap_or("?").to_lowercase()).collect::<Vec<_>>())
122 .collect();
123 v.sort();
124 v.dedup();
125 Ok(v.join(" "))
126}
127
128// A scope takes one operand; a scope straight after a scope is refused in words, at any length.
129// Each length aborted the process before, so run each alone.
130fn key_chain(n: usize) {
131 for s in ["from:".repeat(n) + "a", "subject:to:".repeat(n / 2) + "a"] {
132 match parse(&s) {
133 Ok(_) => panic!("a chain of {} scopes accepted", n),
134 Err(e) => assert!(e.to_string().contains("takes text"), "refused in other words: {}", e),
135 }
136 }
137}
138
139#[test]
140fn f2_key_chain_1000() { key_chain(1_000); }
141
142#[test]
143fn f2_key_chain_10000() { key_chain(10_000); }
144
145#[test]
146fn f2_scope_one_operand() {
147 for s in ["from:ada", "from:(ada bob)", "from:{ada bob}", "from:-ada", "subject:\"two words\"", "to:/b.b/"] {
148 assert!(parse(s).is_ok(), "{:?} refused", s);
149 }
150 for s in ["from:to:ada", "from:is:read", "subject:from:(a)"] {
151 assert!(parse(s).is_err(), "{:?} accepted", s);
152 }
153}
154
155// Every alternative of an any-of that holds is marked, whatever the order typed; the answer is
156// the same as before.
157#[test]
158fn f2_any_of_marks() -> Outcome<()> {
159 let raw = msg("alpha one beta two");
160 for (s, want) in [("alpha OR beta", "alpha beta"), ("beta OR alpha", "alpha beta"), ("{alpha beta}", "alpha beta"),
161 ("zeta OR alpha OR beta", "alpha beta"), ("(alpha OR zeta) beta", "alpha beta"), ("alpha OR (beta zeta)", "alpha"),
162 ("alpha OR -beta", "alpha"), ("{-zeta beta}", "beta")] {
163 assert_eq!(res!(marks(s, &raw)), want, "marks of {:?}", s);
164 assert_eq!(res!(ans(s, &on(&raw))), Some(true), "answer of {:?}", s);
165 }
166 Ok(())
167}
168
169// A query far over the terms bound is refused before it builds its leaves: 2 MB of words in
170// about the time of a short query, not seconds.
171#[test]
172fn f2_terms_refused_early() {
173 let s = "a ".repeat(1_000_000);
174 let at = Instant::now();
175 refused(parse(&s), MAX_TERMS);
176 let t = at.elapsed();
177 eprintln!("F2 2 MB of words refused in {:?}", t);
178 assert!(t < Duration::from_millis(300), "refused only after {:?}", t);
179}