]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
extmsg: use Xapian only for partial matches
[public-inbox.git] / t / search.t
1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
19 my $ibx = $rw->{-inbox};
20 $ibx->with_umask(sub {
21         $rw->_xdb_acquire;
22         $rw->_xdb_release;
23 });
24 $rw = undef;
25 my $ro = PublicInbox::Search->new($git_dir);
26 my $rw_commit = sub {
27         $rw->commit_txn_lazy if $rw;
28         $rw = PublicInbox::SearchIdx->new($git_dir, 1);
29         $rw->begin_txn_lazy;
30 };
31
32 {
33         # git repository perms
34         is($ibx->_git_config_perm(), &PublicInbox::InboxWritable::PERM_GROUP,
35            "undefined permission is group");
36         is(PublicInbox::InboxWritable::_umask_for(
37              PublicInbox::InboxWritable->_git_config_perm('0644')),
38            0022, "644 => umask(0022)");
39         is(PublicInbox::InboxWritable::_umask_for(
40              PublicInbox::InboxWritable->_git_config_perm('0600')),
41            0077, "600 => umask(0077)");
42         is(PublicInbox::InboxWritable::_umask_for(
43              PublicInbox::InboxWritable->_git_config_perm('0640')),
44            0027, "640 => umask(0027)");
45         is(PublicInbox::InboxWritable::_umask_for(
46              PublicInbox::InboxWritable->_git_config_perm('group')),
47            0007, 'group => umask(0007)');
48         is(PublicInbox::InboxWritable::_umask_for(
49              PublicInbox::InboxWritable->_git_config_perm('everybody')),
50            0002, 'everybody => umask(0002)');
51         is(PublicInbox::InboxWritable::_umask_for(
52              PublicInbox::InboxWritable->_git_config_perm('umask')),
53            umask, 'umask => existing umask');
54 }
55
56 $ibx->with_umask(sub {
57         my $root = Email::MIME->create(
58                 header_str => [
59                         Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
60                         Subject => 'Hello world',
61                         'Message-ID' => '<root@s>',
62                         From => 'John Smith <js@example.com>',
63                         To => 'list@example.com',
64                 ],
65                 body => "\\m/\n");
66         my $last = Email::MIME->create(
67                 header_str => [
68                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
69                         Subject => 'Re: Hello world',
70                         'In-Reply-To' => '<root@s>',
71                         'Message-ID' => '<last@s>',
72                         From => 'John Smith <js@example.com>',
73                         To => 'list@example.com',
74                         Cc => 'foo@example.com',
75                 ],
76                 body => "goodbye forever :<\n");
77
78         my $rv;
79         $rw_commit->();
80         $root_id = $rw->add_message($root);
81         is($root_id, int($root_id), "root_id is an integer: $root_id");
82         $last_id = $rw->add_message($last);
83         is($last_id, int($last_id), "last_id is an integer: $last_id");
84 });
85
86 sub filter_mids {
87         my ($msgs) = @_;
88         sort(map { $_->mid } @$msgs);
89 }
90
91 {
92         $rw_commit->();
93         $ro->reopen;
94         my $found = $ro->query('m:root@s');
95         is(scalar(@$found), 1, "message found");
96         is($found->[0]->mid, 'root@s', 'mid set correctly');
97
98         my ($res, @res);
99         my @exp = sort qw(root@s last@s);
100
101         $res = $ro->query('s:(Hello world)');
102         @res = filter_mids($res);
103         is_deeply(\@res, \@exp, 'got expected results for s:() match');
104
105         $res = $ro->query('s:"Hello world"');
106         @res = filter_mids($res);
107         is_deeply(\@res, \@exp, 'got expected results for s:"" match');
108
109         $res = $ro->query('s:"Hello world"', {limit => 1});
110         is(scalar @$res, 1, "limit works");
111         my $first = $res->[0];
112
113         $res = $ro->query('s:"Hello world"', {offset => 1});
114         is(scalar @$res, 1, "offset works");
115         my $second = $res->[0];
116
117         isnt($first, $second, "offset returned different result from limit");
118 }
119
120 # ghost vivication
121 $ibx->with_umask(sub {
122         $rw_commit->();
123         my $rmid = '<ghost-message@s>';
124         my $reply_to_ghost = Email::MIME->create(
125                 header_str => [
126                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
127                         Subject => 'Re: ghosts',
128                         'Message-ID' => '<ghost-reply@s>',
129                         'In-Reply-To' => $rmid,
130                         From => 'Time Traveler <tt@example.com>',
131                         To => 'list@example.com',
132                 ],
133                 body => "-_-\n");
134
135         my $rv;
136         my $reply_id = $rw->add_message($reply_to_ghost);
137         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
138
139         my $was_ghost = Email::MIME->create(
140                 header_str => [
141                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
142                         Subject => 'ghosts',
143                         'Message-ID' => $rmid,
144                         From => 'Laggy Sender <lag@example.com>',
145                         To => 'list@example.com',
146                 ],
147                 body => "are real\n");
148
149         my $ghost_id = $rw->add_message($was_ghost);
150         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
151         my $msgs = $rw->{over}->get_thread('ghost-message@s');
152         is(scalar(@$msgs), 2, 'got both messages in ghost thread');
153         foreach (qw(sid tid)) {
154                 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
155         }
156         isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
157         ok($_->{num} > 0, 'positive art num') foreach @$msgs
158 });
159
160 # search thread on ghost
161 {
162         $rw_commit->();
163         $ro->reopen;
164
165         # subject
166         my $res = $ro->query('ghost');
167         my @exp = sort qw(ghost-message@s ghost-reply@s);
168         my @res = filter_mids($res);
169         is_deeply(\@res, \@exp, 'got expected results for Subject match');
170
171         # body
172         $res = $ro->query('goodbye');
173         is($res->[0]->mid, 'last@s', 'got goodbye message body');
174
175         # datestamp
176         $res = $ro->query('dt:20101002000001..20101002000001');
177         @res = filter_mids($res);
178         is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
179         $res = $ro->query('dt:20101002000002..20101002000002');
180         is_deeply($res, [], 'exact Date: match down to the second');
181 }
182
183 # long message-id
184 $ibx->with_umask(sub {
185         $rw_commit->();
186         $ro->reopen;
187         my $long_mid = 'last' . ('x' x 60). '@s';
188
189         my $long = Email::MIME->create(
190                 header_str => [
191                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
192                         Subject => 'long message ID',
193                         'References' => '<root@s> <last@s>',
194                         'In-Reply-To' => '<last@s>',
195                         'Message-ID' => "<$long_mid>",
196                         From => '"Long I.D." <long-id@example.com>',
197                         To => 'list@example.com',
198                 ],
199                 body => "wut\n");
200         my $long_id = $rw->add_message($long);
201         is($long_id, int($long_id), "long_id is an integer: $long_id");
202
203         $rw_commit->();
204         $ro->reopen;
205         my $res;
206         my @res;
207
208         my $long_reply_mid = 'reply-to-long@1';
209         my $long_reply = Email::MIME->create(
210                 header_str => [
211                         Subject => 'I break references',
212                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
213                         'Message-ID' => "<$long_reply_mid>",
214                         # No References:
215                         # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
216                         'In-Reply-To' => "<$long_mid>",
217                         From => '"no1 <no1@example.com>',
218                         To => 'list@example.com',
219                 ],
220                 body => "no References\n");
221         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
222
223         $rw_commit->();
224         $ro->reopen;
225         my $t = $ro->get_thread('root@s');
226         is(scalar(@$t), 4, "got all 4 mesages in thread");
227         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
228         @res = filter_mids($t);
229         is_deeply(\@res, \@exp, "get_thread works");
230 });
231
232 # quote prioritization
233 $ibx->with_umask(sub {
234         $rw_commit->();
235         $rw->add_message(Email::MIME->create(
236                 header_str => [
237                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
238                         Subject => 'Hello',
239                         'Message-ID' => '<quote@a>',
240                         From => 'Quoter <quoter@example.com>',
241                         To => 'list@example.com',
242                 ],
243                 body => "> theatre illusions\nfade\n"));
244
245         $rw->add_message(Email::MIME->create(
246                 header_str => [
247                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
248                         Subject => 'Hello',
249                         'Message-ID' => '<nquote@a>',
250                         From => 'Non-Quoter<non-quoter@example.com>',
251                         To => 'list@example.com',
252                 ],
253                 body => "theatre\nfade\n"));
254         my $res = $rw->query("theatre");
255         is(scalar(@$res), 2, "got both matches");
256         is($res->[0]->mid, 'nquote@a', "non-quoted scores higher");
257         is($res->[1]->mid, 'quote@a', "quoted result still returned");
258
259         $res = $rw->query("illusions");
260         is(scalar(@$res), 1, "got a match for quoted text");
261         is($res->[0]->mid, 'quote@a',
262                 "quoted result returned if nothing else");
263 });
264
265 # circular references
266 $ibx->with_umask(sub {
267         my $s = 'foo://'. ('Circle' x 15).'/foo';
268         my $doc_id = $rw->add_message(Email::MIME->create(
269                 header => [ Subject => $s ],
270                 header_str => [
271                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
272                         'Message-ID' => '<circle@a>',
273                         'References' => '<circle@a>',
274                         'In-Reply-To' => '<circle@a>',
275                         From => 'Circle <circle@example.com>',
276                         To => 'list@example.com',
277                 ],
278                 body => "LOOP!\n"));
279         ok($doc_id > 0, "doc_id defined with circular reference");
280         my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
281         is($smsg->references, '', "no references created");
282         is($s, $smsg->subject, 'long subject not rewritten');
283 });
284
285 $ibx->with_umask(sub {
286         my $str = eval {
287                 my $mbox = 't/utf8.mbox';
288                 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
289                 local $/;
290                 <$fh>
291         };
292         $str =~ s/\AFrom [^\n]+\n//s;
293         my $mime = Email::MIME->new($str);
294         my $doc_id = $rw->add_message($mime);
295         ok($doc_id > 0, 'message indexed doc_id with UTF-8');
296         my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
297         is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved');
298 });
299
300 {
301         my $msgs = $ro->query('d:19931002..20101002');
302         ok(scalar(@$msgs) > 0, 'got results within range');
303         $msgs = $ro->query('d:20101003..');
304         is(scalar(@$msgs), 0, 'nothing after 20101003');
305         $msgs = $ro->query('d:..19931001');
306         is(scalar(@$msgs), 0, 'nothing before 19931001');
307 }
308
309 # names and addresses
310 {
311         my $mset = $ro->query('t:list@example.com', {mset => 1});
312         is($mset->size, 6, 'searched To: successfully');
313         foreach my $m ($mset->items) {
314                 my $smsg = $ro->lookup_article($m->get_docid);
315                 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
316         }
317
318         $mset = $ro->query('tc:list@example.com', {mset => 1});
319         is($mset->size, 6, 'searched To+Cc: successfully');
320         foreach my $m ($mset->items) {
321                 my $smsg = $ro->lookup_article($m->get_docid);
322                 my $tocc = join("\n", $smsg->to, $smsg->cc);
323                 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
324         }
325
326         foreach my $pfx ('tcf:', 'c:') {
327                 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
328                 is($mset->items, 1, "searched $pfx successfully for Cc:");
329                 foreach my $m ($mset->items) {
330                         my $smsg = $ro->lookup_article($m->get_docid);
331                         like($smsg->cc, qr/\bfoo\@example\.com\b/,
332                                 'cc appears');
333                 }
334         }
335
336         foreach my $pfx ('', 'tcf:', 'f:') {
337                 my $res = $ro->query($pfx . 'Laggy');
338                 is(scalar(@$res), 1,
339                         "searched $pfx successfully for From:");
340                 foreach my $smsg (@$res) {
341                         like($smsg->from, qr/Laggy Sender/,
342                                 "From appears with $pfx");
343                 }
344         }
345 }
346
347 {
348         $rw_commit->();
349         $ro->reopen;
350         my $res = $ro->query('b:hello');
351         is(scalar(@$res), 0, 'no match on body search only');
352         $res = $ro->query('bs:smith');
353         is(scalar(@$res), 0,
354                 'no match on body+subject search for From');
355
356         $res = $ro->query('q:theatre');
357         is(scalar(@$res), 1, 'only one quoted body');
358         like($res->[0]->from, qr/\AQuoter/, 'got quoted body');
359
360         $res = $ro->query('nq:theatre');
361         is(scalar @$res, 1, 'only one non-quoted body');
362         like($res->[0]->from, qr/\ANon-Quoter/, 'got non-quoted body');
363
364         foreach my $pfx (qw(b: bs:)) {
365                 $res = $ro->query($pfx . 'theatre');
366                 is(scalar @$res, 2, "searched both bodies for $pfx");
367                 like($res->[0]->from, qr/\ANon-Quoter/,
368                         "non-quoter first for $pfx");
369         }
370 }
371
372 $ibx->with_umask(sub {
373         my $part1 = Email::MIME->create(
374                  attributes => {
375                      content_type => 'text/plain',
376                      disposition  => 'attachment',
377                      charset => 'US-ASCII',
378                      encoding => 'quoted-printable',
379                      filename => 'attached_fart.txt',
380                  },
381                  body_str => 'inside the attachment',
382         );
383         my $part2 = Email::MIME->create(
384                  attributes => {
385                      content_type => 'text/plain',
386                      disposition  => 'attachment',
387                      charset => 'US-ASCII',
388                      encoding => 'quoted-printable',
389                      filename => 'part_deux.txt',
390                  },
391                  body_str => 'inside another',
392         );
393         my $amsg = Email::MIME->create(
394                 header_str => [
395                         Subject => 'see attachment',
396                         'Message-ID' => '<file@attached>',
397                         From => 'John Smith <js@example.com>',
398                         To => 'list@example.com',
399                 ],
400                 parts => [ $part1, $part2 ],
401         );
402         ok($rw->add_message($amsg), 'added attachment');
403         $rw_commit->();
404         $ro->reopen;
405         my $n = $ro->query('n:attached_fart.txt');
406         is(scalar @$n, 1, 'got result for n:');
407         my $res = $ro->query('part_deux.txt');
408         is(scalar @$res, 1, 'got result without n:');
409         is($n->[0]->mid, $res->[0]->mid,
410                 'same result with and without');
411         my $txt = $ro->query('"inside another"');
412         is($txt->[0]->mid, $res->[0]->mid,
413                 'search inside text attachments works');
414
415         my $mid = $n->[0]->mid;
416         my ($id, $prev);
417         my $art = $ro->next_by_mid($mid, \$id, \$prev);
418         ok($art, 'article exists in OVER DB');
419         $rw->unindex_blob($amsg);
420         $rw->commit_txn_lazy;
421         is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB');
422 });
423
424 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
425                 glob("$git_dir/public-inbox/xapian*/"),
426                 glob("$git_dir/public-inbox/xapian*/*")) {
427         my @st = stat($f);
428         my ($bn) = (split(m!/!, $f))[-1];
429         is($st[2] & 07777, -f _ ? 0660 : 0770,
430                 "sharedRepository respected for $bn");
431 }
432
433 $ibx->with_umask(sub {
434         $rw_commit->();
435         my $digits = '10010260936330';
436         my $ua = 'Pine.LNX.4.10';
437         my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
438         is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
439                 'no results yet');
440         my $pine = Email::MIME->create(
441                 header_str => [
442                         Subject => 'blah',
443                         'Message-ID' => "<$mid>",
444                         From => 'torvalds@transmeta',
445                         To => 'list@example.com',
446                 ],
447                 body => ""
448         );
449         my $x = $rw->add_message($pine);
450         $rw->commit_txn_lazy;
451         is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
452                 'searching only digit yielded result');
453
454         my $wild = $digits;
455         for my $i (1..6) {
456                 chop($wild);
457                 is($ro->query("m:$wild*", { mset => 1})->size, 1,
458                         "searching chopped($i) digit yielded result $wild ");
459         }
460         is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
461 });
462
463 done_testing();
464
465 1;