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>
6 eval { require PublicInbox::SearchIdx; };
7 plan skip_all => "Xapian missing for search" if $@;
8 use File::Temp qw/tempdir/;
10 my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
11 my $git_dir = "$tmpdir/a.git";
12 my ($root_id, $last_id);
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");
18 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
21 my $ibx = $rw->{-inbox};
23 my $ro = PublicInbox::Search->new($git_dir);
25 $rw->commit_txn_lazy if $rw;
26 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
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');
55 my $root = Email::MIME->create(
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',
64 my $last = Email::MIME->create(
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',
74 body => "goodbye forever :<\n");
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");
86 sort(map { $_->mid } @$msgs);
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');
98 my @exp = sort qw(root@s last@s);
100 $res = $ro->query('s:(Hello world)');
101 @res = filter_mids($res);
102 is_deeply(\@res, \@exp, 'got expected results for s:() match');
104 $res = $ro->query('s:"Hello world"');
105 @res = filter_mids($res);
106 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
108 $res = $ro->query('s:"Hello world"', {limit => 1});
109 is(scalar @$res, 1, "limit works");
110 my $first = $res->[0];
112 $res = $ro->query('s:"Hello world"', {offset => 1});
113 is(scalar @$res, 1, "offset works");
114 my $second = $res->[0];
116 isnt($first, $second, "offset returned different result from limit");
122 my $rmid = '<ghost-message@s>';
123 my $reply_to_ghost = Email::MIME->create(
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',
135 my $reply_id = $rw->add_message($reply_to_ghost);
136 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
138 my $was_ghost = Email::MIME->create(
140 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
142 'Message-ID' => $rmid,
143 From => 'Laggy Sender <lag@example.com>',
144 To => 'list@example.com',
146 body => "are real\n");
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');
151 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
152 foreach (qw(sid tid)) {
153 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
155 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
156 ok($_->{num} > 0, 'positive art num') foreach @$msgs
159 # search thread on ghost
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');
171 $res = $ro->query('goodbye');
172 is($res->[0]->mid, 'last@s', 'got goodbye message body');
175 $res = $ro->query('dt:20101002000001..20101002000001');
176 @res = filter_mids($res);
177 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
178 $res = $ro->query('dt:20101002000002..20101002000002');
179 is_deeply($res, [], 'exact Date: match down to the second');
186 my $long_mid = 'last' . ('x' x 60). '@s';
188 my $long = Email::MIME->create(
190 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
191 Subject => 'long message ID',
192 'References' => '<root@s> <last@s>',
193 'In-Reply-To' => '<last@s>',
194 'Message-ID' => "<$long_mid>",
195 From => '"Long I.D." <long-id@example.com>',
196 To => 'list@example.com',
199 my $long_id = $rw->add_message($long);
200 is($long_id, int($long_id), "long_id is an integer: $long_id");
207 my $long_reply_mid = 'reply-to-long@1';
208 my $long_reply = Email::MIME->create(
210 Subject => 'I break references',
211 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
212 'Message-ID' => "<$long_reply_mid>",
214 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
215 'In-Reply-To' => "<$long_mid>",
216 From => '"no1 <no1@example.com>',
217 To => 'list@example.com',
219 body => "no References\n");
220 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
224 my $t = $ro->get_thread('root@s');
225 is(scalar(@$t), 4, "got all 4 mesages in thread");
226 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
227 @res = filter_mids($t);
228 is_deeply(\@res, \@exp, "get_thread works");
231 # quote prioritization
234 $rw->add_message(Email::MIME->create(
236 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
238 'Message-ID' => '<quote@a>',
239 From => 'Quoter <quoter@example.com>',
240 To => 'list@example.com',
242 body => "> theatre illusions\nfade\n"));
244 $rw->add_message(Email::MIME->create(
246 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
248 'Message-ID' => '<nquote@a>',
249 From => 'Non-Quoter<non-quoter@example.com>',
250 To => 'list@example.com',
252 body => "theatre\nfade\n"));
253 my $res = $rw->query("theatre");
254 is(scalar(@$res), 2, "got both matches");
255 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher");
256 is($res->[1]->mid, 'quote@a', "quoted result still returned");
258 $res = $rw->query("illusions");
259 is(scalar(@$res), 1, "got a match for quoted text");
260 is($res->[0]->mid, 'quote@a',
261 "quoted result returned if nothing else");
264 # circular references
266 my $s = 'foo://'. ('Circle' x 15).'/foo';
267 my $doc_id = $rw->add_message(Email::MIME->create(
268 header => [ Subject => $s ],
270 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
271 'Message-ID' => '<circle@a>',
272 'References' => '<circle@a>',
273 'In-Reply-To' => '<circle@a>',
274 From => 'Circle <circle@example.com>',
275 To => 'list@example.com',
278 ok($doc_id > 0, "doc_id defined with circular reference");
279 my $smsg = $rw->first_smsg_by_mid('circle@a');
280 is($smsg->references, '', "no references created");
281 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
282 is($s, $msg->subject, 'long subject not rewritten');
287 my $mbox = 't/utf8.mbox';
288 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
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 $smsg = $rw->first_smsg_by_mid('testmessage@example.com');
297 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
299 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved');
303 my $msgs = $ro->query('d:19931002..20101002');
304 ok(scalar(@$msgs) > 0, 'got results within range');
305 $msgs = $ro->query('d:20101003..');
306 is(scalar(@$msgs), 0, 'nothing after 20101003');
307 $msgs = $ro->query('d:..19931001');
308 is(scalar(@$msgs), 0, 'nothing before 19931001');
311 # names and addresses
313 my $res = $ro->query('t:list@example.com');
314 is(scalar @$res, 6, 'searched To: successfully');
315 foreach my $smsg (@$res) {
316 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
319 $res = $ro->query('tc:list@example.com');
320 is(scalar @$res, 6, 'searched To+Cc: successfully');
321 foreach my $smsg (@$res) {
322 my $tocc = join("\n", $smsg->to, $smsg->cc);
323 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
326 foreach my $pfx ('tcf:', 'c:') {
327 $res = $ro->query($pfx . 'foo@example.com');
329 "searched $pfx successfully for Cc:");
330 foreach my $smsg (@$res) {
331 like($smsg->cc, qr/\bfoo\@example\.com\b/,
336 foreach my $pfx ('', 'tcf:', 'f:') {
337 $res = $ro->query($pfx . 'Laggy');
339 "searched $pfx successfully for From:");
340 foreach my $smsg (@$res) {
341 like($smsg->from, qr/Laggy Sender/,
342 "From appears with $pfx");
350 my $res = $ro->query('b:hello');
351 is(scalar(@$res), 0, 'no match on body search only');
352 $res = $ro->query('bs:smith');
354 'no match on body+subject search for From');
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');
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');
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");
373 my $part1 = Email::MIME->create(
375 content_type => 'text/plain',
376 disposition => 'attachment',
377 charset => 'US-ASCII',
378 encoding => 'quoted-printable',
379 filename => 'attached_fart.txt',
381 body_str => 'inside the attachment',
383 my $part2 = Email::MIME->create(
385 content_type => 'text/plain',
386 disposition => 'attachment',
387 charset => 'US-ASCII',
388 encoding => 'quoted-printable',
389 filename => 'part_deux.txt',
391 body_str => 'inside another',
393 my $amsg = Email::MIME->create(
395 Subject => 'see attachment',
396 'Message-ID' => '<file@attached>',
397 From => 'John Smith <js@example.com>',
398 To => 'list@example.com',
400 parts => [ $part1, $part2 ],
402 ok($rw->add_message($amsg), 'added attachment');
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');
415 $rw->commit_txn_lazy;