1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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");
19 my $orig = "FOO " x 30;
20 my $summ = PublicInbox::Search::subject_summary($orig);
22 $summ = length($summ);
23 $orig = length($orig);
24 ok($summ < $orig && $summ > 0, "summary shortened ($orig => $summ)");
27 $summ = PublicInbox::Search::subject_summary($orig);
29 $summ = length($summ);
30 $orig = length($orig);
31 ok($summ < $orig && $summ > 0,
32 "summary shortened but not empty: $summ");
35 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
36 my $ro = PublicInbox::Search->new($git_dir);
39 $rw->{xdb}->commit_transaction if $rw;
41 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
42 $rw->{xdb}->begin_transaction;
46 # git repository perms
47 is(PublicInbox::SearchIdx->_git_config_perm(undef),
48 &PublicInbox::SearchIdx::PERM_GROUP,
49 "undefined permission is group");
50 is(PublicInbox::SearchIdx::_umask_for(
51 PublicInbox::SearchIdx->_git_config_perm('0644')),
52 0022, "644 => umask(0022)");
53 is(PublicInbox::SearchIdx::_umask_for(
54 PublicInbox::SearchIdx->_git_config_perm('0600')),
55 0077, "600 => umask(0077)");
56 is(PublicInbox::SearchIdx::_umask_for(
57 PublicInbox::SearchIdx->_git_config_perm('0640')),
58 0027, "640 => umask(0027)");
59 is(PublicInbox::SearchIdx::_umask_for(
60 PublicInbox::SearchIdx->_git_config_perm('group')),
61 0007, 'group => umask(0007)');
62 is(PublicInbox::SearchIdx::_umask_for(
63 PublicInbox::SearchIdx->_git_config_perm('everybody')),
64 0002, 'everybody => umask(0002)');
65 is(PublicInbox::SearchIdx::_umask_for(
66 PublicInbox::SearchIdx->_git_config_perm('umask')),
67 umask, 'umask => existing umask');
71 my $root = Email::MIME->create(
73 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
74 Subject => 'Hello world',
75 'Message-ID' => '<root@s>',
76 From => 'John Smith <js@example.com>',
77 To => 'list@example.com',
80 my $last = Email::MIME->create(
82 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
83 Subject => 'Re: Hello world',
84 'In-Reply-To' => '<root@s>',
85 'Message-ID' => '<last@s>',
86 From => 'John Smith <js@example.com>',
87 To => 'list@example.com',
89 body => "goodbye forever :<\n");
93 $root_id = $rw->add_message($root);
94 is($root_id, int($root_id), "root_id is an integer: $root_id");
95 $last_id = $rw->add_message($last);
96 is($last_id, int($last_id), "last_id is an integer: $last_id");
101 sort(map { $_->mid } @{$res->{msgs}});
107 my $found = $ro->lookup_message('<root@s>');
108 ok($found, "message found");
109 is($root_id, $found->{doc_id}, 'doc_id set correctly');
110 $found->ensure_metadata;
111 is($found->mid, 'root@s', 'mid set correctly');
112 ok(int($found->thread_id) > 0, 'thread_id is an integer');
114 my @exp = sort qw(root@s last@s);
115 my $res = $ro->query("path:hello_world");
116 my @res = filter_mids($res);
117 is_deeply(\@res, \@exp, 'got expected results for path: match');
119 foreach my $p (qw(hello hello_ hello_world2 hello_world_)) {
120 $res = $ro->query("path:$p");
121 is($res->{total}, 0, "path variant `$p' does not match");
124 $res = $ro->query('subject:(Hello world)');
125 @res = filter_mids($res);
126 is_deeply(\@res, \@exp, 'got expected results for subject:() match');
128 $res = $ro->query('subject:"Hello world"');
129 @res = filter_mids($res);
130 is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
132 $res = $ro->query('subject:"Hello world"', {limit => 1});
133 is(scalar @{$res->{msgs}}, 1, "limit works");
134 my $first = $res->{msgs}->[0];
136 $res = $ro->query('subject:"Hello world"', {offset => 1});
137 is(scalar @{$res->{msgs}}, 1, "offset works");
138 my $second = $res->{msgs}->[0];
140 isnt($first, $second, "offset returned different result from limit");
146 my $rmid = '<ghost-message@s>';
147 my $reply_to_ghost = Email::MIME->create(
149 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
150 Subject => 'Re: ghosts',
151 'Message-ID' => '<ghost-reply@s>',
152 'In-Reply-To' => $rmid,
153 From => 'Time Traveler <tt@example.com>',
154 To => 'list@example.com',
159 my $reply_id = $rw->add_message($reply_to_ghost);
160 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
162 my $was_ghost = Email::MIME->create(
164 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
166 'Message-ID' => $rmid,
167 From => 'Laggy Sender <lag@example.com>',
168 To => 'list@example.com',
170 body => "are real\n");
172 my $ghost_id = $rw->add_message($was_ghost);
173 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
174 ok($ghost_id < $reply_id, "ghost vivified from earlier message");
177 # search thread on ghost
183 my $res = $ro->query('ghost');
184 my @exp = sort qw(ghost-message@s ghost-reply@s);
185 my @res = filter_mids($res);
186 is_deeply(\@res, \@exp, 'got expected results for Subject match');
189 $res = $ro->query('goodbye');
190 is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
197 my $long_mid = 'last' . ('x' x 60). '@s';
199 my $long = Email::MIME->create(
201 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
202 Subject => 'long message ID',
203 'References' => '<root@s> <last@s>',
204 'In-Reply-To' => '<last@s>',
205 'Message-ID' => "<$long_mid>",
206 From => '"Long I.D." <long-id@example.com>',
207 To => 'list@example.com',
210 my $long_id = $rw->add_message($long);
211 is($long_id, int($long_id), "long_id is an integer: $long_id");
218 my $long_reply_mid = 'reply-to-long@1';
219 my $long_reply = Email::MIME->create(
221 Subject => 'I break references',
222 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
223 'Message-ID' => "<$long_reply_mid>",
225 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
226 'In-Reply-To' => "<$long_mid>",
227 From => '"no1 <no1@example.com>',
228 To => 'list@example.com',
230 body => "no References\n");
231 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
235 my $t = $ro->get_thread('root@s');
236 is($t->{total}, 4, "got all 4 mesages in thread");
237 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
238 @res = filter_mids($t);
239 is_deeply(\@res, \@exp, "get_thread works");
242 # quote prioritization
245 $rw->add_message(Email::MIME->create(
247 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
249 'Message-ID' => '<quote@a>',
250 From => 'Quoter <quoter@example.com>',
251 To => 'list@example.com',
253 body => "> theatre illusions\nfade\n"));
255 $rw->add_message(Email::MIME->create(
257 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
259 'Message-ID' => '<nquote@a>',
260 From => 'Non-Quoter<non-quoter@example.com>',
261 To => 'list@example.com',
263 body => "theatre\nfade\n"));
264 my $res = $rw->query("theatre");
265 is($res->{total}, 2, "got both matches");
266 is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
267 is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
269 $res = $rw->query("illusions");
270 is($res->{total}, 1, "got a match for quoted text");
271 is($res->{msgs}->[0]->mid, 'quote@a',
272 "quoted result returned if nothing else");
275 # circular references
277 my $s = 'foo://'. ('Circle' x 15).'/foo';
278 my $doc_id = $rw->add_message(Email::MIME->create(
279 header => [ Subject => $s ],
281 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
282 'Message-ID' => '<circle@a>',
283 'References' => '<circle@a>',
284 'In-Reply-To' => '<circle@a>',
285 From => 'Circle <circle@example.com>',
286 To => 'list@example.com',
289 ok($doc_id > 0, "doc_id defined with circular reference");
290 my $smsg = $rw->lookup_message('circle@a');
291 $smsg->ensure_metadata;
292 is($smsg->references, '', "no references created");
293 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
294 is($s, $msg->mini_mime->header('Subject'), 'long subject not rewritten');
299 my $mbox = 't/utf8.mbox';
300 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
304 $str =~ s/\AFrom [^\n]+\n//s;
305 my $mime = Email::MIME->new($str);
306 my $doc_id = $rw->add_message($mime);
307 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
308 my $smsg = $rw->lookup_message('testmessage@example.com');
309 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
311 # mini_mime technically not valid (I think),
312 # but good enough for displaying HTML:
313 is($mime->header('Subject'), $msg->mini_mime->header('Subject'),
314 'UTF-8 subject preserved');