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(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 = PublicInbox::SearchIdx->new($git_dir, 1);
43 # git repository perms
44 is(PublicInbox::SearchIdx->_git_config_perm(undef),
45 &PublicInbox::SearchIdx::PERM_GROUP,
46 "undefined permission is group");
47 is(PublicInbox::SearchIdx::_umask_for(
48 PublicInbox::SearchIdx->_git_config_perm('0644')),
49 0022, "644 => umask(0022)");
50 is(PublicInbox::SearchIdx::_umask_for(
51 PublicInbox::SearchIdx->_git_config_perm('0600')),
52 0077, "600 => umask(0077)");
53 is(PublicInbox::SearchIdx::_umask_for(
54 PublicInbox::SearchIdx->_git_config_perm('0640')),
55 0027, "640 => umask(0027)");
56 is(PublicInbox::SearchIdx::_umask_for(
57 PublicInbox::SearchIdx->_git_config_perm('group')),
58 0007, 'group => umask(0007)');
59 is(PublicInbox::SearchIdx::_umask_for(
60 PublicInbox::SearchIdx->_git_config_perm('everybody')),
61 0002, 'everybody => umask(0002)');
62 is(PublicInbox::SearchIdx::_umask_for(
63 PublicInbox::SearchIdx->_git_config_perm('umask')),
64 umask, 'umask => existing umask');
68 my $root = Email::MIME->create(
70 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
71 Subject => 'Hello world',
72 'Message-ID' => '<root@s>',
73 From => 'John Smith <js@example.com>',
74 To => 'list@example.com',
77 my $last = Email::MIME->create(
79 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
80 Subject => 'Re: Hello world',
81 'In-Reply-To' => '<root@s>',
82 'Message-ID' => '<last@s>',
83 From => 'John Smith <js@example.com>',
84 To => 'list@example.com',
86 body => "goodbye forever :<\n");
89 $root_id = $rw->add_message($root);
90 is($root_id, int($root_id), "root_id is an integer: $root_id");
91 $last_id = $rw->add_message($last);
92 is($last_id, int($last_id), "last_id is an integer: $last_id");
97 sort(map { $_->mid } @{$res->{msgs}});
103 my $found = $ro->lookup_message('<root@s>');
104 ok($found, "message found");
105 is($root_id, $found->{doc_id}, 'doc_id set correctly');
106 $found->ensure_metadata;
107 is($found->mid, 'root@s', 'mid set correctly');
108 ok(int($found->thread_id) > 0, 'thread_id is an integer');
110 my @exp = sort qw(root@s last@s);
111 my $res = $ro->query("path:hello_world");
112 my @res = filter_mids($res);
113 is_deeply(\@res, \@exp, 'got expected results for path: match');
115 foreach my $p (qw(hello hello_ hello_world2 hello_world_)) {
116 $res = $ro->query("path:$p");
117 is($res->{total}, 0, "path variant `$p' does not match");
120 $res = $ro->query('subject:(Hello world)');
121 @res = filter_mids($res);
122 is_deeply(\@res, \@exp, 'got expected results for subject:() 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"', {limit => 1});
129 is(scalar @{$res->{msgs}}, 1, "limit works");
130 my $first = $res->{msgs}->[0];
132 $res = $ro->query('subject:"Hello world"', {offset => 1});
133 is(scalar @{$res->{msgs}}, 1, "offset works");
134 my $second = $res->{msgs}->[0];
136 isnt($first, $second, "offset returned different result from limit");
142 my $rmid = '<ghost-message@s>';
143 my $reply_to_ghost = Email::MIME->create(
145 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
146 Subject => 'Re: ghosts',
147 'Message-ID' => '<ghost-reply@s>',
148 'In-Reply-To' => $rmid,
149 From => 'Time Traveler <tt@example.com>',
150 To => 'list@example.com',
155 my $reply_id = $rw->add_message($reply_to_ghost);
156 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
158 my $was_ghost = Email::MIME->create(
160 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
162 'Message-ID' => $rmid,
163 From => 'Laggy Sender <lag@example.com>',
164 To => 'list@example.com',
166 body => "are real\n");
168 my $ghost_id = $rw->add_message($was_ghost);
169 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
170 ok($ghost_id < $reply_id, "ghost vivified from earlier message");
173 # search thread on ghost
179 my $res = $ro->query('ghost');
180 my @exp = sort qw(ghost-message@s ghost-reply@s);
181 my @res = filter_mids($res);
182 is_deeply(\@res, \@exp, 'got expected results for Subject match');
185 $res = $ro->query('goodbye');
186 is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
193 my $long_mid = 'last' . ('x' x 60). '@s';
195 my $long = Email::MIME->create(
197 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
198 Subject => 'long message ID',
199 'References' => '<root@s> <last@s>',
200 'In-Reply-To' => '<last@s>',
201 'Message-ID' => "<$long_mid>",
202 From => '"Long I.D." <long-id@example.com>',
203 To => 'list@example.com',
206 my $long_id = $rw->add_message($long);
207 is($long_id, int($long_id), "long_id is an integer: $long_id");
214 my $long_reply_mid = 'reply-to-long@1';
215 my $long_reply = Email::MIME->create(
217 Subject => 'I break references',
218 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
219 'Message-ID' => "<$long_reply_mid>",
221 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
222 'In-Reply-To' => "<$long_mid>",
223 From => '"no1 <no1@example.com>',
224 To => 'list@example.com',
226 body => "no References\n");
227 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
231 my $t = $ro->get_thread('root@s');
232 is($t->{total}, 4, "got all 4 mesages in thread");
233 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
234 @res = filter_mids($t);
235 is_deeply(\@res, \@exp, "get_thread works");
238 # quote prioritization
241 $rw->add_message(Email::MIME->create(
243 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
245 'Message-ID' => '<quote@a>',
246 From => 'Quoter <quoter@example.com>',
247 To => 'list@example.com',
249 body => "> theatre illusions\nfade\n"));
251 $rw->add_message(Email::MIME->create(
253 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
255 'Message-ID' => '<nquote@a>',
256 From => 'Non-Quoter<non-quoter@example.com>',
257 To => 'list@example.com',
259 body => "theatre\nfade\n"));
260 my $res = $rw->query("theatre");
261 is($res->{total}, 2, "got both matches");
262 is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
263 is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
265 $res = $rw->query("illusions");
266 is($res->{total}, 1, "got a match for quoted text");
267 is($res->{msgs}->[0]->mid, 'quote@a',
268 "quoted result returned if nothing else");
271 # circular references
273 my $doc_id = $rw->add_message(Email::MIME->create(
275 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
277 'Message-ID' => '<circle@a>',
278 'References' => '<circle@a>',
279 'In-Reply-To' => '<circle@a>',
280 From => 'Circle <circle@example.com>',
281 To => 'list@example.com',
284 ok($doc_id > 0, "doc_id defined with circular reference");
285 my $smsg = $rw->lookup_message('circle@a');
286 $smsg->ensure_metadata;
287 is($smsg->references, '', "no references created");