]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
c16811d8d462806886c5d6f06e54d09c8dd0f6ba
[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         is($found->mid, 'root@s', 'mid set correctly');
113         ok(int($found->thread_id) > 0, 'thread_id is an integer');
114
115         my @exp = sort qw(root@s last@s);
116         my $res = $ro->query("path:hello_world");
117         my @res = filter_mids($res);
118         is_deeply(\@res, \@exp, 'got expected results for path: match');
119
120         foreach my $p (qw(hello hello_ hello_world2 hello_world_)) {
121                 $res = $ro->query("path:$p");
122                 is($res->{total}, 0, "path variant `$p' does not match");
123         }
124
125         $res = $ro->query('s:(Hello world)');
126         @res = filter_mids($res);
127         is_deeply(\@res, \@exp, 'got expected results for s:() match');
128
129         $res = $ro->query('s:"Hello world"');
130         @res = filter_mids($res);
131         is_deeply(\@res, \@exp, 'got expected results for s:"" match');
132
133         $res = $ro->query('s:"Hello world"', {limit => 1});
134         is(scalar @{$res->{msgs}}, 1, "limit works");
135         my $first = $res->{msgs}->[0];
136
137         $res = $ro->query('s:"Hello world"', {offset => 1});
138         is(scalar @{$res->{msgs}}, 1, "offset works");
139         my $second = $res->{msgs}->[0];
140
141         isnt($first, $second, "offset returned different result from limit");
142 }
143
144 # ghost vivication
145 {
146         $rw_commit->();
147         my $rmid = '<ghost-message@s>';
148         my $reply_to_ghost = Email::MIME->create(
149                 header_str => [
150                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
151                         Subject => 'Re: ghosts',
152                         'Message-ID' => '<ghost-reply@s>',
153                         'In-Reply-To' => $rmid,
154                         From => 'Time Traveler <tt@example.com>',
155                         To => 'list@example.com',
156                 ],
157                 body => "-_-\n");
158
159         my $rv;
160         my $reply_id = $rw->add_message($reply_to_ghost);
161         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
162
163         my $was_ghost = Email::MIME->create(
164                 header_str => [
165                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
166                         Subject => 'ghosts',
167                         'Message-ID' => $rmid,
168                         From => 'Laggy Sender <lag@example.com>',
169                         To => 'list@example.com',
170                 ],
171                 body => "are real\n");
172
173         my $ghost_id = $rw->add_message($was_ghost);
174         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
175         ok($ghost_id < $reply_id, "ghost vivified from earlier message");
176 }
177
178 # search thread on ghost
179 {
180         $rw_commit->();
181         $ro->reopen;
182
183         # subject
184         my $res = $ro->query('ghost');
185         my @exp = sort qw(ghost-message@s ghost-reply@s);
186         my @res = filter_mids($res);
187         is_deeply(\@res, \@exp, 'got expected results for Subject match');
188
189         # body
190         $res = $ro->query('goodbye');
191         is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
192 }
193
194 # long message-id
195 {
196         $rw_commit->();
197         $ro->reopen;
198         my $long_mid = 'last' . ('x' x 60). '@s';
199
200         my $long = Email::MIME->create(
201                 header_str => [
202                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
203                         Subject => 'long message ID',
204                         'References' => '<root@s> <last@s>',
205                         'In-Reply-To' => '<last@s>',
206                         'Message-ID' => "<$long_mid>",
207                         From => '"Long I.D." <long-id@example.com>',
208                         To => 'list@example.com',
209                 ],
210                 body => "wut\n");
211         my $long_id = $rw->add_message($long);
212         is($long_id, int($long_id), "long_id is an integer: $long_id");
213
214         $rw_commit->();
215         $ro->reopen;
216         my $res;
217         my @res;
218
219         my $long_reply_mid = 'reply-to-long@1';
220         my $long_reply = Email::MIME->create(
221                 header_str => [
222                         Subject => 'I break references',
223                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
224                         'Message-ID' => "<$long_reply_mid>",
225                         # No References:
226                         # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
227                         'In-Reply-To' => "<$long_mid>",
228                         From => '"no1 <no1@example.com>',
229                         To => 'list@example.com',
230                 ],
231                 body => "no References\n");
232         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
233
234         $rw_commit->();
235         $ro->reopen;
236         my $t = $ro->get_thread('root@s');
237         is($t->{total}, 4, "got all 4 mesages in thread");
238         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
239         @res = filter_mids($t);
240         is_deeply(\@res, \@exp, "get_thread works");
241 }
242
243 # quote prioritization
244 {
245         $rw_commit->();
246         $rw->add_message(Email::MIME->create(
247                 header_str => [
248                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
249                         Subject => 'Hello',
250                         'Message-ID' => '<quote@a>',
251                         From => 'Quoter <quoter@example.com>',
252                         To => 'list@example.com',
253                 ],
254                 body => "> theatre illusions\nfade\n"));
255
256         $rw->add_message(Email::MIME->create(
257                 header_str => [
258                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
259                         Subject => 'Hello',
260                         'Message-ID' => '<nquote@a>',
261                         From => 'Non-Quoter<non-quoter@example.com>',
262                         To => 'list@example.com',
263                 ],
264                 body => "theatre\nfade\n"));
265         my $res = $rw->query("theatre");
266         is($res->{total}, 2, "got both matches");
267         is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
268         is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
269
270         $res = $rw->query("illusions");
271         is($res->{total}, 1, "got a match for quoted text");
272         is($res->{msgs}->[0]->mid, 'quote@a',
273                 "quoted result returned if nothing else");
274 }
275
276 # circular references
277 {
278         my $s = 'foo://'. ('Circle' x 15).'/foo';
279         my $doc_id = $rw->add_message(Email::MIME->create(
280                 header => [ Subject => $s ],
281                 header_str => [
282                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
283                         'Message-ID' => '<circle@a>',
284                         'References' => '<circle@a>',
285                         'In-Reply-To' => '<circle@a>',
286                         From => 'Circle <circle@example.com>',
287                         To => 'list@example.com',
288                 ],
289                 body => "LOOP!\n"));
290         ok($doc_id > 0, "doc_id defined with circular reference");
291         my $smsg = $rw->lookup_message('circle@a');
292         is($smsg->references, '', "no references created");
293         my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
294         is($s, $msg->subject, 'long subject not rewritten');
295 }
296
297 {
298         my $str = eval {
299                 my $mbox = 't/utf8.mbox';
300                 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
301                 local $/;
302                 <$fh>
303         };
304         $str =~ s/\AFrom [^\n]+\n//s;
305         my $mime = Email::MIME->new($str);
306         my $doc_id = $rw->add_message($mime);
307         ok($doc_id > 0, 'message indexed doc_id with UTF-8');
308         my $smsg = $rw->lookup_message('testmessage@example.com');
309         my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
310
311         is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved');
312 }
313
314 {
315         my $res = $ro->query('d:19931002..20101002');
316         ok(scalar @{$res->{msgs}} > 0, 'got results within range');
317         $res = $ro->query('d:20101003..');
318         is(scalar @{$res->{msgs}}, 0, 'nothing after 20101003');
319         $res = $ro->query('d:..19931001');
320         is(scalar @{$res->{msgs}}, 0, 'nothing before 19931001');
321 }
322
323 # names and addresses
324 {
325         my $res = $ro->query('t:list@example.com');
326         is(scalar @{$res->{msgs}}, 6, 'searched To: successfully');
327         foreach my $smsg (@{$res->{msgs}}) {
328                 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
329         }
330
331         $res = $ro->query('tc:list@example.com');
332         is(scalar @{$res->{msgs}}, 6, 'searched To+Cc: successfully');
333         foreach my $smsg (@{$res->{msgs}}) {
334                 my $tocc = join("\n", $smsg->to, $smsg->cc);
335                 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
336         }
337
338         foreach my $pfx ('tcf:', 'c:') {
339                 $res = $ro->query($pfx . 'foo@example.com');
340                 is(scalar @{$res->{msgs}}, 1,
341                         "searched $pfx successfully for Cc:");
342                 foreach my $smsg (@{$res->{msgs}}) {
343                         like($smsg->cc, qr/\bfoo\@example\.com\b/,
344                                 'cc appears');
345                 }
346         }
347
348         foreach my $pfx ('', 'tcf:', 'f:') {
349                 $res = $ro->query($pfx . 'Laggy');
350                 is(scalar @{$res->{msgs}}, 1,
351                         "searched $pfx successfully for From:");
352                 foreach my $smsg (@{$res->{msgs}}) {
353                         like($smsg->from, qr/Laggy Sender/,
354                                 "From appears with $pfx");
355                 }
356         }
357 }
358
359 {
360         $rw_commit->();
361         $ro->reopen;
362         my $res = $ro->query('b:hello');
363         is(scalar @{$res->{msgs}}, 0, 'no match on body search only');
364         $res = $ro->query('bs:smith');
365         is(scalar @{$res->{msgs}}, 0,
366                 'no match on body+subject search for From');
367
368         $res = $ro->query('q:theatre');
369         is(scalar @{$res->{msgs}}, 1, 'only one quoted body');
370         like($res->{msgs}->[0]->from, qr/\AQuoter/, 'got quoted body');
371
372         $res = $ro->query('nq:theatre');
373         is(scalar @{$res->{msgs}}, 1, 'only one non-quoted body');
374         like($res->{msgs}->[0]->from, qr/\ANon-Quoter/, 'got non-quoted body');
375
376         foreach my $pfx (qw(b: bs:)) {
377                 $res = $ro->query($pfx . 'theatre');
378                 is(scalar @{$res->{msgs}}, 2, "searched both bodies for $pfx");
379                 like($res->{msgs}->[0]->from, qr/\ANon-Quoter/,
380                         "non-quoter first for $pfx");
381         }
382 }
383
384 {
385         my $part1 = Email::MIME->create(
386                  attributes => {
387                      content_type => 'text/plain',
388                      disposition  => 'attachment',
389                      charset => 'US-ASCII',
390                      encoding => 'quoted-printable',
391                      filename => 'attached_fart.txt',
392                  },
393                  body_str => 'inside the attachment',
394         );
395         my $part2 = Email::MIME->create(
396                  attributes => {
397                      content_type => 'text/plain',
398                      disposition  => 'attachment',
399                      charset => 'US-ASCII',
400                      encoding => 'quoted-printable',
401                      filename => 'part_deux.txt',
402                  },
403                  body_str => 'inside another',
404         );
405         my $amsg = Email::MIME->create(
406                 header_str => [
407                         Subject => 'see attachment',
408                         'Message-ID' => '<file@attached>',
409                         From => 'John Smith <js@example.com>',
410                         To => 'list@example.com',
411                 ],
412                 parts => [ $part1, $part2 ],
413         );
414         ok($rw->add_message($amsg), 'added attachment');
415         $rw_commit->();
416         $ro->reopen;
417         my $n = $ro->query('n:attached_fart.txt');
418         is(scalar @{$n->{msgs}}, 1, 'got result for n:');
419         my $res = $ro->query('part_deux.txt');
420         is(scalar @{$res->{msgs}}, 1, 'got result without n:');
421         is($n->{msgs}->[0]->mid, $res->{msgs}->[0]->mid,
422                 'same result with and without');
423         my $txt = $ro->query('"inside another"');
424         is($txt->{msgs}->[0]->mid, $res->{msgs}->[0]->mid,
425                 'search inside text attachments works');
426 }
427
428 done_testing();
429
430 1;