]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
search: more granular message body searching
[public-inbox.git] / t / search.t
1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 use strict;
4 use warnings;
5 use Test::More;
6 eval { require PublicInbox::SearchIdx; };
7 plan skip_all => "Xapian missing for search" if $@;
8 use File::Temp qw/tempdir/;
9 use Email::MIME;
10 my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
11 my $git_dir = "$tmpdir/a.git";
12 my ($root_id, $last_id);
13
14 is(0, system(qw(git init -q --bare), $git_dir), "git init (main)");
15 eval { PublicInbox::Search->new($git_dir) };
16 ok($@, "exception raised on non-existent DB");
17
18 {
19         my $orig = "FOO " x 30;
20         my $summ = PublicInbox::Search::subject_summary($orig);
21
22         $summ = length($summ);
23         $orig = length($orig);
24         ok($summ < $orig && $summ > 0, "summary shortened ($orig => $summ)");
25
26         $orig = "FOO" x 30;
27         $summ = PublicInbox::Search::subject_summary($orig);
28
29         $summ = length($summ);
30         $orig = length($orig);
31         ok($summ < $orig && $summ > 0,
32            "summary shortened but not empty: $summ");
33 }
34
35 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
36 $rw->_xdb_acquire;
37 $rw->_xdb_release;
38 $rw = undef;
39 my $ro = PublicInbox::Search->new($git_dir);
40 my $rw_commit = sub {
41         $rw->{xdb}->commit_transaction if $rw && $rw->{xdb};
42         $rw = PublicInbox::SearchIdx->new($git_dir, 1);
43         $rw->_xdb_acquire->begin_transaction;
44 };
45
46 {
47         # git repository perms
48         is(PublicInbox::SearchIdx->_git_config_perm(undef),
49            &PublicInbox::SearchIdx::PERM_GROUP,
50            "undefined permission is group");
51         is(PublicInbox::SearchIdx::_umask_for(
52              PublicInbox::SearchIdx->_git_config_perm('0644')),
53            0022, "644 => umask(0022)");
54         is(PublicInbox::SearchIdx::_umask_for(
55              PublicInbox::SearchIdx->_git_config_perm('0600')),
56            0077, "600 => umask(0077)");
57         is(PublicInbox::SearchIdx::_umask_for(
58              PublicInbox::SearchIdx->_git_config_perm('0640')),
59            0027, "640 => umask(0027)");
60         is(PublicInbox::SearchIdx::_umask_for(
61              PublicInbox::SearchIdx->_git_config_perm('group')),
62            0007, 'group => umask(0007)');
63         is(PublicInbox::SearchIdx::_umask_for(
64              PublicInbox::SearchIdx->_git_config_perm('everybody')),
65            0002, 'everybody => umask(0002)');
66         is(PublicInbox::SearchIdx::_umask_for(
67              PublicInbox::SearchIdx->_git_config_perm('umask')),
68            umask, 'umask => existing umask');
69 }
70
71 {
72         my $root = Email::MIME->create(
73                 header_str => [
74                         Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
75                         Subject => 'Hello world',
76                         'Message-ID' => '<root@s>',
77                         From => 'John Smith <js@example.com>',
78                         To => 'list@example.com',
79                 ],
80                 body => "\\m/\n");
81         my $last = Email::MIME->create(
82                 header_str => [
83                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
84                         Subject => 'Re: Hello world',
85                         'In-Reply-To' => '<root@s>',
86                         'Message-ID' => '<last@s>',
87                         From => 'John Smith <js@example.com>',
88                         To => 'list@example.com',
89                         Cc => 'foo@example.com',
90                 ],
91                 body => "goodbye forever :<\n");
92
93         my $rv;
94         $rw_commit->();
95         $root_id = $rw->add_message($root);
96         is($root_id, int($root_id), "root_id is an integer: $root_id");
97         $last_id = $rw->add_message($last);
98         is($last_id, int($last_id), "last_id is an integer: $last_id");
99 }
100
101 sub filter_mids {
102         my ($res) = @_;
103         sort(map { $_->mid } @{$res->{msgs}});
104 }
105
106 {
107         $rw_commit->();
108         $ro->reopen;
109         my $found = $ro->lookup_message('<root@s>');
110         ok($found, "message found");
111         is($root_id, $found->{doc_id}, 'doc_id set correctly');
112         $found->ensure_metadata;
113         is($found->mid, 'root@s', 'mid set correctly');
114         ok(int($found->thread_id) > 0, 'thread_id is an integer');
115
116         my @exp = sort qw(root@s last@s);
117         my $res = $ro->query("path:hello_world");
118         my @res = filter_mids($res);
119         is_deeply(\@res, \@exp, 'got expected results for path: match');
120
121         foreach my $p (qw(hello hello_ hello_world2 hello_world_)) {
122                 $res = $ro->query("path:$p");
123                 is($res->{total}, 0, "path variant `$p' does not match");
124         }
125
126         $res = $ro->query('s:(Hello world)');
127         @res = filter_mids($res);
128         is_deeply(\@res, \@exp, 'got expected results for s:() match');
129
130         $res = $ro->query('s:"Hello world"');
131         @res = filter_mids($res);
132         is_deeply(\@res, \@exp, 'got expected results for s:"" match');
133
134         $res = $ro->query('s:"Hello world"', {limit => 1});
135         is(scalar @{$res->{msgs}}, 1, "limit works");
136         my $first = $res->{msgs}->[0];
137
138         $res = $ro->query('s:"Hello world"', {offset => 1});
139         is(scalar @{$res->{msgs}}, 1, "offset works");
140         my $second = $res->{msgs}->[0];
141
142         isnt($first, $second, "offset returned different result from limit");
143 }
144
145 # ghost vivication
146 {
147         $rw_commit->();
148         my $rmid = '<ghost-message@s>';
149         my $reply_to_ghost = Email::MIME->create(
150                 header_str => [
151                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
152                         Subject => 'Re: ghosts',
153                         'Message-ID' => '<ghost-reply@s>',
154                         'In-Reply-To' => $rmid,
155                         From => 'Time Traveler <tt@example.com>',
156                         To => 'list@example.com',
157                 ],
158                 body => "-_-\n");
159
160         my $rv;
161         my $reply_id = $rw->add_message($reply_to_ghost);
162         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
163
164         my $was_ghost = Email::MIME->create(
165                 header_str => [
166                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
167                         Subject => 'ghosts',
168                         'Message-ID' => $rmid,
169                         From => 'Laggy Sender <lag@example.com>',
170                         To => 'list@example.com',
171                 ],
172                 body => "are real\n");
173
174         my $ghost_id = $rw->add_message($was_ghost);
175         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
176         ok($ghost_id < $reply_id, "ghost vivified from earlier message");
177 }
178
179 # search thread on ghost
180 {
181         $rw_commit->();
182         $ro->reopen;
183
184         # subject
185         my $res = $ro->query('ghost');
186         my @exp = sort qw(ghost-message@s ghost-reply@s);
187         my @res = filter_mids($res);
188         is_deeply(\@res, \@exp, 'got expected results for Subject match');
189
190         # body
191         $res = $ro->query('goodbye');
192         is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
193 }
194
195 # long message-id
196 {
197         $rw_commit->();
198         $ro->reopen;
199         my $long_mid = 'last' . ('x' x 60). '@s';
200
201         my $long = Email::MIME->create(
202                 header_str => [
203                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
204                         Subject => 'long message ID',
205                         'References' => '<root@s> <last@s>',
206                         'In-Reply-To' => '<last@s>',
207                         'Message-ID' => "<$long_mid>",
208                         From => '"Long I.D." <long-id@example.com>',
209                         To => 'list@example.com',
210                 ],
211                 body => "wut\n");
212         my $long_id = $rw->add_message($long);
213         is($long_id, int($long_id), "long_id is an integer: $long_id");
214
215         $rw_commit->();
216         $ro->reopen;
217         my $res;
218         my @res;
219
220         my $long_reply_mid = 'reply-to-long@1';
221         my $long_reply = Email::MIME->create(
222                 header_str => [
223                         Subject => 'I break references',
224                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
225                         'Message-ID' => "<$long_reply_mid>",
226                         # No References:
227                         # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
228                         'In-Reply-To' => "<$long_mid>",
229                         From => '"no1 <no1@example.com>',
230                         To => 'list@example.com',
231                 ],
232                 body => "no References\n");
233         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
234
235         $rw_commit->();
236         $ro->reopen;
237         my $t = $ro->get_thread('root@s');
238         is($t->{total}, 4, "got all 4 mesages in thread");
239         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
240         @res = filter_mids($t);
241         is_deeply(\@res, \@exp, "get_thread works");
242 }
243
244 # quote prioritization
245 {
246         $rw_commit->();
247         $rw->add_message(Email::MIME->create(
248                 header_str => [
249                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
250                         Subject => 'Hello',
251                         'Message-ID' => '<quote@a>',
252                         From => 'Quoter <quoter@example.com>',
253                         To => 'list@example.com',
254                 ],
255                 body => "> theatre illusions\nfade\n"));
256
257         $rw->add_message(Email::MIME->create(
258                 header_str => [
259                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
260                         Subject => 'Hello',
261                         'Message-ID' => '<nquote@a>',
262                         From => 'Non-Quoter<non-quoter@example.com>',
263                         To => 'list@example.com',
264                 ],
265                 body => "theatre\nfade\n"));
266         my $res = $rw->query("theatre");
267         is($res->{total}, 2, "got both matches");
268         is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
269         is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
270
271         $res = $rw->query("illusions");
272         is($res->{total}, 1, "got a match for quoted text");
273         is($res->{msgs}->[0]->mid, 'quote@a',
274                 "quoted result returned if nothing else");
275 }
276
277 # circular references
278 {
279         my $s = 'foo://'. ('Circle' x 15).'/foo';
280         my $doc_id = $rw->add_message(Email::MIME->create(
281                 header => [ Subject => $s ],
282                 header_str => [
283                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
284                         'Message-ID' => '<circle@a>',
285                         'References' => '<circle@a>',
286                         'In-Reply-To' => '<circle@a>',
287                         From => 'Circle <circle@example.com>',
288                         To => 'list@example.com',
289                 ],
290                 body => "LOOP!\n"));
291         ok($doc_id > 0, "doc_id defined with circular reference");
292         my $smsg = $rw->lookup_message('circle@a');
293         $smsg->ensure_metadata;
294         is($smsg->references, '', "no references created");
295         my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
296         is($s, $msg->mini_mime->header('Subject'), 'long subject not rewritten');
297 }
298
299 {
300         my $str = eval {
301                 my $mbox = 't/utf8.mbox';
302                 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
303                 local $/;
304                 <$fh>
305         };
306         $str =~ s/\AFrom [^\n]+\n//s;
307         my $mime = Email::MIME->new($str);
308         my $doc_id = $rw->add_message($mime);
309         ok($doc_id > 0, 'message indexed doc_id with UTF-8');
310         my $smsg = $rw->lookup_message('testmessage@example.com');
311         my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
312
313         # mini_mime technically not valid (I think),
314         # but good enough for displaying HTML:
315         is($mime->header('Subject'), $msg->mini_mime->header('Subject'),
316                 'UTF-8 subject preserved');
317 }
318
319 {
320         my $res = $ro->query('d:19931002..20101002');
321         ok(scalar @{$res->{msgs}} > 0, 'got results within range');
322         $res = $ro->query('d:20101003..');
323         is(scalar @{$res->{msgs}}, 0, 'nothing after 20101003');
324         $res = $ro->query('d:..19931001');
325         is(scalar @{$res->{msgs}}, 0, 'nothing before 19931001');
326 }
327
328 # names and addresses
329 {
330         my $res = $ro->query('t:list@example.com');
331         is(scalar @{$res->{msgs}}, 6, 'searched To: successfully');
332         foreach my $smsg (@{$res->{msgs}}) {
333                 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
334         }
335
336         $res = $ro->query('tc:list@example.com');
337         is(scalar @{$res->{msgs}}, 6, 'searched To+Cc: successfully');
338         foreach my $smsg (@{$res->{msgs}}) {
339                 my $tocc = join("\n", $smsg->to, $smsg->cc);
340                 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
341         }
342
343         foreach my $pfx ('tcf:', 'c:') {
344                 $res = $ro->query($pfx . 'foo@example.com');
345                 is(scalar @{$res->{msgs}}, 1,
346                         "searched $pfx successfully for Cc:");
347                 foreach my $smsg (@{$res->{msgs}}) {
348                         like($smsg->cc, qr/\bfoo\@example\.com\b/,
349                                 'cc appears');
350                 }
351         }
352
353         foreach my $pfx ('', 'tcf:', 'f:') {
354                 $res = $ro->query($pfx . 'Laggy');
355                 is(scalar @{$res->{msgs}}, 1,
356                         "searched $pfx successfully for From:");
357                 foreach my $smsg (@{$res->{msgs}}) {
358                         like($smsg->from, qr/Laggy Sender/,
359                                 "From appears with $pfx");
360                 }
361         }
362 }
363
364 {
365         $rw_commit->();
366         $ro->reopen;
367         my $res = $ro->query('b:hello');
368         is(scalar @{$res->{msgs}}, 0, 'no match on body search only');
369         $res = $ro->query('bs:smith');
370         is(scalar @{$res->{msgs}}, 0,
371                 'no match on body+subject search for From');
372
373         $res = $ro->query('q:theatre');
374         is(scalar @{$res->{msgs}}, 1, 'only one quoted body');
375         like($res->{msgs}->[0]->from, qr/\AQuoter/, 'got quoted body');
376
377         $res = $ro->query('nq:theatre');
378         is(scalar @{$res->{msgs}}, 1, 'only one non-quoted body');
379         like($res->{msgs}->[0]->from, qr/\ANon-Quoter/, 'got non-quoted body');
380
381         foreach my $pfx (qw(b: bs:)) {
382                 $res = $ro->query($pfx . 'theatre');
383                 is(scalar @{$res->{msgs}}, 2, "searched both bodies for $pfx");
384                 like($res->{msgs}->[0]->from, qr/\ANon-Quoter/,
385                         "non-quoter first for $pfx");
386         }
387 }
388
389 done_testing();
390
391 1;