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->query('m:root@s');
93 is(scalar(@$found), 1, "message found");
94 is($found->[0]->mid, 'root@s', 'mid set correctly');
97 my @exp = sort qw(root@s last@s);
99 $res = $ro->query('s:(Hello world)');
100 @res = filter_mids($res);
101 is_deeply(\@res, \@exp, 'got expected results for s:() match');
103 $res = $ro->query('s:"Hello world"');
104 @res = filter_mids($res);
105 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
107 $res = $ro->query('s:"Hello world"', {limit => 1});
108 is(scalar @$res, 1, "limit works");
109 my $first = $res->[0];
111 $res = $ro->query('s:"Hello world"', {offset => 1});
112 is(scalar @$res, 1, "offset works");
113 my $second = $res->[0];
115 isnt($first, $second, "offset returned different result from limit");
121 my $rmid = '<ghost-message@s>';
122 my $reply_to_ghost = Email::MIME->create(
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',
134 my $reply_id = $rw->add_message($reply_to_ghost);
135 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
137 my $was_ghost = Email::MIME->create(
139 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
141 'Message-ID' => $rmid,
142 From => 'Laggy Sender <lag@example.com>',
143 To => 'list@example.com',
145 body => "are real\n");
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");
154 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
155 ok($_->{num} > 0, 'positive art num') foreach @$msgs
158 # search thread on ghost
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');
170 $res = $ro->query('goodbye');
171 is($res->[0]->mid, 'last@s', 'got goodbye message body');
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');
185 my $long_mid = 'last' . ('x' x 60). '@s';
187 my $long = Email::MIME->create(
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',
198 my $long_id = $rw->add_message($long);
199 is($long_id, int($long_id), "long_id is an integer: $long_id");
206 my $long_reply_mid = 'reply-to-long@1';
207 my $long_reply = Email::MIME->create(
209 Subject => 'I break references',
210 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
211 'Message-ID' => "<$long_reply_mid>",
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',
218 body => "no References\n");
219 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
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");
230 # quote prioritization
233 $rw->add_message(Email::MIME->create(
235 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
237 'Message-ID' => '<quote@a>',
238 From => 'Quoter <quoter@example.com>',
239 To => 'list@example.com',
241 body => "> theatre illusions\nfade\n"));
243 $rw->add_message(Email::MIME->create(
245 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
247 'Message-ID' => '<nquote@a>',
248 From => 'Non-Quoter<non-quoter@example.com>',
249 To => 'list@example.com',
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");
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");
263 # circular references
265 my $s = 'foo://'. ('Circle' x 15).'/foo';
266 my $doc_id = $rw->add_message(Email::MIME->create(
267 header => [ Subject => $s ],
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',
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');
285 my $mbox = 't/utf8.mbox';
286 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
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');
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');
307 # names and addresses
309 my $mset = $ro->query('t:list@example.com', {mset => 1});
310 is($mset->size, 6, 'searched To: successfully');
311 foreach my $m ($mset->items) {
312 my $smsg = $ro->lookup_article($m->get_docid);
313 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
316 $mset = $ro->query('tc:list@example.com', {mset => 1});
317 is($mset->size, 6, 'searched To+Cc: successfully');
318 foreach my $m ($mset->items) {
319 my $smsg = $ro->lookup_article($m->get_docid);
320 my $tocc = join("\n", $smsg->to, $smsg->cc);
321 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
324 foreach my $pfx ('tcf:', 'c:') {
325 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
326 is($mset->items, 1, "searched $pfx successfully for Cc:");
327 foreach my $m ($mset->items) {
328 my $smsg = $ro->lookup_article($m->get_docid);
329 like($smsg->cc, qr/\bfoo\@example\.com\b/,
334 foreach my $pfx ('', 'tcf:', 'f:') {
335 my $res = $ro->query($pfx . 'Laggy');
337 "searched $pfx successfully for From:");
338 foreach my $smsg (@$res) {
339 like($smsg->from, qr/Laggy Sender/,
340 "From appears with $pfx");
348 my $res = $ro->query('b:hello');
349 is(scalar(@$res), 0, 'no match on body search only');
350 $res = $ro->query('bs:smith');
352 'no match on body+subject search for From');
354 $res = $ro->query('q:theatre');
355 is(scalar(@$res), 1, 'only one quoted body');
356 like($res->[0]->from, qr/\AQuoter/, 'got quoted body');
358 $res = $ro->query('nq:theatre');
359 is(scalar @$res, 1, 'only one non-quoted body');
360 like($res->[0]->from, qr/\ANon-Quoter/, 'got non-quoted body');
362 foreach my $pfx (qw(b: bs:)) {
363 $res = $ro->query($pfx . 'theatre');
364 is(scalar @$res, 2, "searched both bodies for $pfx");
365 like($res->[0]->from, qr/\ANon-Quoter/,
366 "non-quoter first for $pfx");
371 my $part1 = Email::MIME->create(
373 content_type => 'text/plain',
374 disposition => 'attachment',
375 charset => 'US-ASCII',
376 encoding => 'quoted-printable',
377 filename => 'attached_fart.txt',
379 body_str => 'inside the attachment',
381 my $part2 = Email::MIME->create(
383 content_type => 'text/plain',
384 disposition => 'attachment',
385 charset => 'US-ASCII',
386 encoding => 'quoted-printable',
387 filename => 'part_deux.txt',
389 body_str => 'inside another',
391 my $amsg = Email::MIME->create(
393 Subject => 'see attachment',
394 'Message-ID' => '<file@attached>',
395 From => 'John Smith <js@example.com>',
396 To => 'list@example.com',
398 parts => [ $part1, $part2 ],
400 ok($rw->add_message($amsg), 'added attachment');
403 my $n = $ro->query('n:attached_fart.txt');
404 is(scalar @$n, 1, 'got result for n:');
405 my $res = $ro->query('part_deux.txt');
406 is(scalar @$res, 1, 'got result without n:');
407 is($n->[0]->mid, $res->[0]->mid,
408 'same result with and without');
409 my $txt = $ro->query('"inside another"');
410 is($txt->[0]->mid, $res->[0]->mid,
411 'search inside text attachments works');
413 my $mid = $n->[0]->mid;
415 my $art = $ro->next_by_mid($mid, \$id, \$prev);
416 ok($art, 'article exists in OVER DB');
417 $rw->unindex_blob($amsg);
418 $rw->commit_txn_lazy;
419 is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB');