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