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);
39 my $ro = PublicInbox::Search->new($git_dir);
41 $rw->{xdb}->commit_transaction if $rw && $rw->{xdb};
42 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
43 $rw->_xdb_acquire->begin_transaction;
47 # git repository perms
48 is(PublicInbox::SearchIdx->_git_config_perm(undef),
49 &PublicInbox::SearchIdx::PERM_GROUP,
50 "undefined permission is group");
51 is(PublicInbox::SearchIdx::_umask_for(
52 PublicInbox::SearchIdx->_git_config_perm('0644')),
53 0022, "644 => umask(0022)");
54 is(PublicInbox::SearchIdx::_umask_for(
55 PublicInbox::SearchIdx->_git_config_perm('0600')),
56 0077, "600 => umask(0077)");
57 is(PublicInbox::SearchIdx::_umask_for(
58 PublicInbox::SearchIdx->_git_config_perm('0640')),
59 0027, "640 => umask(0027)");
60 is(PublicInbox::SearchIdx::_umask_for(
61 PublicInbox::SearchIdx->_git_config_perm('group')),
62 0007, 'group => umask(0007)');
63 is(PublicInbox::SearchIdx::_umask_for(
64 PublicInbox::SearchIdx->_git_config_perm('everybody')),
65 0002, 'everybody => umask(0002)');
66 is(PublicInbox::SearchIdx::_umask_for(
67 PublicInbox::SearchIdx->_git_config_perm('umask')),
68 umask, 'umask => existing umask');
72 my $root = Email::MIME->create(
74 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
75 Subject => 'Hello world',
76 'Message-ID' => '<root@s>',
77 From => 'John Smith <js@example.com>',
78 To => 'list@example.com',
81 my $last = Email::MIME->create(
83 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
84 Subject => 'Re: Hello world',
85 'In-Reply-To' => '<root@s>',
86 'Message-ID' => '<last@s>',
87 From => 'John Smith <js@example.com>',
88 To => 'list@example.com',
90 body => "goodbye forever :<\n");
94 $root_id = $rw->add_message($root);
95 is($root_id, int($root_id), "root_id is an integer: $root_id");
96 $last_id = $rw->add_message($last);
97 is($last_id, int($last_id), "last_id is an integer: $last_id");
102 sort(map { $_->mid } @{$res->{msgs}});
108 my $found = $ro->lookup_message('<root@s>');
109 ok($found, "message found");
110 is($root_id, $found->{doc_id}, 'doc_id set correctly');
111 $found->ensure_metadata;
112 is($found->mid, 'root@s', 'mid set correctly');
113 ok(int($found->thread_id) > 0, 'thread_id is an integer');
115 my @exp = sort qw(root@s last@s);
116 my $res = $ro->query("path:hello_world");
117 my @res = filter_mids($res);
118 is_deeply(\@res, \@exp, 'got expected results for path: match');
120 foreach my $p (qw(hello hello_ hello_world2 hello_world_)) {
121 $res = $ro->query("path:$p");
122 is($res->{total}, 0, "path variant `$p' does not match");
125 $res = $ro->query('subject:(Hello world)');
126 @res = filter_mids($res);
127 is_deeply(\@res, \@exp, 'got expected results for subject:() match');
129 $res = $ro->query('subject:"Hello world"');
130 @res = filter_mids($res);
131 is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
133 $res = $ro->query('subject:"Hello world"', {limit => 1});
134 is(scalar @{$res->{msgs}}, 1, "limit works");
135 my $first = $res->{msgs}->[0];
137 $res = $ro->query('subject:"Hello world"', {offset => 1});
138 is(scalar @{$res->{msgs}}, 1, "offset works");
139 my $second = $res->{msgs}->[0];
141 isnt($first, $second, "offset returned different result from limit");
147 my $rmid = '<ghost-message@s>';
148 my $reply_to_ghost = Email::MIME->create(
150 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
151 Subject => 'Re: ghosts',
152 'Message-ID' => '<ghost-reply@s>',
153 'In-Reply-To' => $rmid,
154 From => 'Time Traveler <tt@example.com>',
155 To => 'list@example.com',
160 my $reply_id = $rw->add_message($reply_to_ghost);
161 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
163 my $was_ghost = Email::MIME->create(
165 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
167 'Message-ID' => $rmid,
168 From => 'Laggy Sender <lag@example.com>',
169 To => 'list@example.com',
171 body => "are real\n");
173 my $ghost_id = $rw->add_message($was_ghost);
174 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
175 ok($ghost_id < $reply_id, "ghost vivified from earlier message");
178 # search thread on ghost
184 my $res = $ro->query('ghost');
185 my @exp = sort qw(ghost-message@s ghost-reply@s);
186 my @res = filter_mids($res);
187 is_deeply(\@res, \@exp, 'got expected results for Subject match');
190 $res = $ro->query('goodbye');
191 is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
198 my $long_mid = 'last' . ('x' x 60). '@s';
200 my $long = Email::MIME->create(
202 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
203 Subject => 'long message ID',
204 'References' => '<root@s> <last@s>',
205 'In-Reply-To' => '<last@s>',
206 'Message-ID' => "<$long_mid>",
207 From => '"Long I.D." <long-id@example.com>',
208 To => 'list@example.com',
211 my $long_id = $rw->add_message($long);
212 is($long_id, int($long_id), "long_id is an integer: $long_id");
219 my $long_reply_mid = 'reply-to-long@1';
220 my $long_reply = Email::MIME->create(
222 Subject => 'I break references',
223 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
224 'Message-ID' => "<$long_reply_mid>",
226 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
227 'In-Reply-To' => "<$long_mid>",
228 From => '"no1 <no1@example.com>',
229 To => 'list@example.com',
231 body => "no References\n");
232 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
236 my $t = $ro->get_thread('root@s');
237 is($t->{total}, 4, "got all 4 mesages in thread");
238 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
239 @res = filter_mids($t);
240 is_deeply(\@res, \@exp, "get_thread works");
243 # quote prioritization
246 $rw->add_message(Email::MIME->create(
248 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
250 'Message-ID' => '<quote@a>',
251 From => 'Quoter <quoter@example.com>',
252 To => 'list@example.com',
254 body => "> theatre illusions\nfade\n"));
256 $rw->add_message(Email::MIME->create(
258 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
260 'Message-ID' => '<nquote@a>',
261 From => 'Non-Quoter<non-quoter@example.com>',
262 To => 'list@example.com',
264 body => "theatre\nfade\n"));
265 my $res = $rw->query("theatre");
266 is($res->{total}, 2, "got both matches");
267 is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
268 is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
270 $res = $rw->query("illusions");
271 is($res->{total}, 1, "got a match for quoted text");
272 is($res->{msgs}->[0]->mid, 'quote@a',
273 "quoted result returned if nothing else");
276 # circular references
278 my $s = 'foo://'. ('Circle' x 15).'/foo';
279 my $doc_id = $rw->add_message(Email::MIME->create(
280 header => [ Subject => $s ],
282 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
283 'Message-ID' => '<circle@a>',
284 'References' => '<circle@a>',
285 'In-Reply-To' => '<circle@a>',
286 From => 'Circle <circle@example.com>',
287 To => 'list@example.com',
290 ok($doc_id > 0, "doc_id defined with circular reference");
291 my $smsg = $rw->lookup_message('circle@a');
292 $smsg->ensure_metadata;
293 is($smsg->references, '', "no references created");
294 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
295 is($s, $msg->mini_mime->header('Subject'), 'long subject not rewritten');
300 my $mbox = 't/utf8.mbox';
301 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
305 $str =~ s/\AFrom [^\n]+\n//s;
306 my $mime = Email::MIME->new($str);
307 my $doc_id = $rw->add_message($mime);
308 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
309 my $smsg = $rw->lookup_message('testmessage@example.com');
310 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
312 # mini_mime technically not valid (I think),
313 # but good enough for displaying HTML:
314 is($mime->header('Subject'), $msg->mini_mime->header('Subject'),
315 'UTF-8 subject preserved');