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