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/;
11 my $tmpdir = tempdir(CLEANUP => 1);
12 my $git_dir = "$tmpdir/a.git";
13 my ($root_id, $last_id);
15 is(0, system(qw(git init -q --bare), $git_dir), "git init (main)");
16 eval { PublicInbox::Search->new($git_dir) };
17 ok($@, "exception raised on non-existent DB");
19 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
20 my $ro = PublicInbox::Search->new($git_dir);
23 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
27 # git repository perms
28 is(PublicInbox::SearchIdx->_git_config_perm(undef),
29 &PublicInbox::SearchIdx::PERM_GROUP,
30 "undefined permission is group");
31 is(PublicInbox::SearchIdx::_umask_for(
32 PublicInbox::SearchIdx->_git_config_perm('0644')),
33 0022, "644 => umask(0022)");
34 is(PublicInbox::SearchIdx::_umask_for(
35 PublicInbox::SearchIdx->_git_config_perm('0600')),
36 0077, "600 => umask(0077)");
37 is(PublicInbox::SearchIdx::_umask_for(
38 PublicInbox::SearchIdx->_git_config_perm('0640')),
39 0027, "640 => umask(0027)");
40 is(PublicInbox::SearchIdx::_umask_for(
41 PublicInbox::SearchIdx->_git_config_perm('group')),
42 0007, 'group => umask(0007)');
43 is(PublicInbox::SearchIdx::_umask_for(
44 PublicInbox::SearchIdx->_git_config_perm('everybody')),
45 0002, 'everybody => umask(0002)');
46 is(PublicInbox::SearchIdx::_umask_for(
47 PublicInbox::SearchIdx->_git_config_perm('umask')),
48 umask, 'umask => existing umask');
52 my $root = Email::MIME->create(
54 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
55 Subject => 'Hello world',
56 'Message-ID' => '<root@s>',
57 From => 'John Smith <js@example.com>',
58 To => 'list@example.com',
61 my $last = Email::MIME->create(
63 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
64 Subject => 'Re: Hello world',
65 'In-Reply-To' => '<root@s>',
66 'Message-ID' => '<last@s>',
67 From => 'John Smith <js@example.com>',
68 To => 'list@example.com',
70 body => "goodbye forever :<\n");
73 $root_id = $rw->add_message($root);
74 is($root_id, int($root_id), "root_id is an integer: $root_id");
75 $last_id = $rw->add_message($last);
76 is($last_id, int($last_id), "last_id is an integer: $last_id");
81 sort(map { $_->mid } @{$res->{msgs}});
87 my $found = $ro->lookup_message('<root@s>');
88 ok($found, "message found");
89 is($root_id, $found->{doc_id}, 'doc_id set correctly');
90 $found->ensure_metadata;
91 is($found->mid, 'root@s', 'mid set correctly');
92 ok(int($found->thread_id) > 0, 'thread_id is an integer');
94 my @exp = sort qw(root@s last@s);
95 my $res = $ro->query("path:hello_world");
96 my @res = filter_mids($res);
97 is_deeply(\@res, \@exp, 'got expected results for path: match');
99 foreach my $p (qw(hello hello_ hello_world2 hello_world_)) {
100 $res = $ro->query("path:$p");
101 is($res->{total}, 0, "path variant `$p' does not match");
104 $res = $ro->query('subject:(Hello world)');
105 @res = filter_mids($res);
106 is_deeply(\@res, \@exp, 'got expected results for subject:() match');
108 $res = $ro->query('subject:"Hello world"');
109 @res = filter_mids($res);
110 is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
112 $res = $ro->query('subject:"Hello world"', {limit => 1});
113 is(scalar @{$res->{msgs}}, 1, "limit works");
114 my $first = $res->{msgs}->[0];
116 $res = $ro->query('subject:"Hello world"', {offset => 1});
117 is(scalar @{$res->{msgs}}, 1, "offset works");
118 my $second = $res->{msgs}->[0];
120 isnt($first, $second, "offset returned different result from limit");
122 foreach my $f (qw(inreplyto references)) {
123 $res = $ro->query($f . ':root@s');
124 @res = filter_mids($res);
125 is_deeply(\@res, [ 'last@s' ],
126 "got expected results for $f: match");
127 $res = $ro->query($f . ':root');
128 is($res->{total}, 0, "no partial mid match");
135 my $rmid = '<ghost-message@s>';
136 my $reply_to_ghost = Email::MIME->create(
138 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
139 Subject => 'Re: ghosts',
140 'Message-ID' => '<ghost-reply@s>',
141 'In-Reply-To' => $rmid,
142 From => 'Time Traveler <tt@example.com>',
143 To => 'list@example.com',
148 my $reply_id = $rw->add_message($reply_to_ghost);
149 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
151 my $was_ghost = Email::MIME->create(
153 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
155 'Message-ID' => $rmid,
156 From => 'Laggy Sender <lag@example.com>',
157 To => 'list@example.com',
159 body => "are real\n");
161 my $ghost_id = $rw->add_message($was_ghost);
162 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
163 ok($ghost_id < $reply_id, "ghost vivified from earlier message");
166 # search thread on ghost
172 my $res = $ro->query('ghost');
173 my @exp = sort qw(ghost-message@s ghost-reply@s);
174 my @res = filter_mids($res);
175 is_deeply(\@res, \@exp, 'got expected results for Subject match');
178 $res = $ro->query('goodbye');
179 is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
186 my $long_mid = 'last' . ('x' x 60). '@s';
187 my $long_midc = Digest::SHA::sha1_hex($long_mid);
189 my $long = Email::MIME->create(
191 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
192 Subject => 'long message ID',
193 'References' => '<root@s> <last@s>',
194 'In-Reply-To' => '<last@s>',
195 'Message-ID' => "<$long_mid>",
196 From => '"Long I.D." <long-id@example.com>',
197 To => 'list@example.com',
200 my $long_id = $rw->add_message($long);
201 is($long_id, int($long_id), "long_id is an integer: $long_id");
205 my $res = $ro->query('references:root@s');
206 my @res = filter_mids($res);
207 is_deeply(\@res, [ sort('last@s', $long_midc) ],
208 "got expected results for references: match");
210 my $followups = $ro->get_followups('root@s');
211 $followups = [ filter_mids($followups) ];
212 is_deeply($followups, [ filter_mids($res) ], "get_followups matches");
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_midc);
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}, undef, "no references created");