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