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