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");
20 my $orig = "FOO " x 30;
21 my $summ = PublicInbox::Search::subject_summary($orig);
23 $summ = length($summ);
24 $orig = length($orig);
25 ok($summ < $orig && $summ > 0, "summary shortened ($orig => $summ)");
28 $summ = PublicInbox::Search::subject_summary($orig);
30 $summ = length($summ);
31 $orig = length($orig);
32 ok($summ < $orig && $summ > 0,
33 "summary shortened but not empty: $summ");
36 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
37 my $ro = PublicInbox::Search->new($git_dir);
40 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
44 # git repository perms
45 is(PublicInbox::SearchIdx->_git_config_perm(undef),
46 &PublicInbox::SearchIdx::PERM_GROUP,
47 "undefined permission is group");
48 is(PublicInbox::SearchIdx::_umask_for(
49 PublicInbox::SearchIdx->_git_config_perm('0644')),
50 0022, "644 => umask(0022)");
51 is(PublicInbox::SearchIdx::_umask_for(
52 PublicInbox::SearchIdx->_git_config_perm('0600')),
53 0077, "600 => umask(0077)");
54 is(PublicInbox::SearchIdx::_umask_for(
55 PublicInbox::SearchIdx->_git_config_perm('0640')),
56 0027, "640 => umask(0027)");
57 is(PublicInbox::SearchIdx::_umask_for(
58 PublicInbox::SearchIdx->_git_config_perm('group')),
59 0007, 'group => umask(0007)');
60 is(PublicInbox::SearchIdx::_umask_for(
61 PublicInbox::SearchIdx->_git_config_perm('everybody')),
62 0002, 'everybody => umask(0002)');
63 is(PublicInbox::SearchIdx::_umask_for(
64 PublicInbox::SearchIdx->_git_config_perm('umask')),
65 umask, 'umask => existing umask');
69 my $root = Email::MIME->create(
71 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
72 Subject => 'Hello world',
73 'Message-ID' => '<root@s>',
74 From => 'John Smith <js@example.com>',
75 To => 'list@example.com',
78 my $last = Email::MIME->create(
80 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
81 Subject => 'Re: Hello world',
82 'In-Reply-To' => '<root@s>',
83 'Message-ID' => '<last@s>',
84 From => 'John Smith <js@example.com>',
85 To => 'list@example.com',
87 body => "goodbye forever :<\n");
90 $root_id = $rw->add_message($root);
91 is($root_id, int($root_id), "root_id is an integer: $root_id");
92 $last_id = $rw->add_message($last);
93 is($last_id, int($last_id), "last_id is an integer: $last_id");
98 sort(map { $_->mid } @{$res->{msgs}});
104 my $found = $ro->lookup_message('<root@s>');
105 ok($found, "message found");
106 is($root_id, $found->{doc_id}, 'doc_id set correctly');
107 $found->ensure_metadata;
108 is($found->mid, 'root@s', 'mid set correctly');
109 ok(int($found->thread_id) > 0, 'thread_id is an integer');
111 my @exp = sort qw(root@s last@s);
112 my $res = $ro->query("path:hello_world");
113 my @res = filter_mids($res);
114 is_deeply(\@res, \@exp, 'got expected results for path: match');
116 foreach my $p (qw(hello hello_ hello_world2 hello_world_)) {
117 $res = $ro->query("path:$p");
118 is($res->{total}, 0, "path variant `$p' does not match");
121 $res = $ro->query('subject:(Hello world)');
122 @res = filter_mids($res);
123 is_deeply(\@res, \@exp, 'got expected results for subject:() 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"', {limit => 1});
130 is(scalar @{$res->{msgs}}, 1, "limit works");
131 my $first = $res->{msgs}->[0];
133 $res = $ro->query('subject:"Hello world"', {offset => 1});
134 is(scalar @{$res->{msgs}}, 1, "offset works");
135 my $second = $res->{msgs}->[0];
137 isnt($first, $second, "offset returned different result from limit");
143 my $rmid = '<ghost-message@s>';
144 my $reply_to_ghost = Email::MIME->create(
146 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
147 Subject => 'Re: ghosts',
148 'Message-ID' => '<ghost-reply@s>',
149 'In-Reply-To' => $rmid,
150 From => 'Time Traveler <tt@example.com>',
151 To => 'list@example.com',
156 my $reply_id = $rw->add_message($reply_to_ghost);
157 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
159 my $was_ghost = Email::MIME->create(
161 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
163 'Message-ID' => $rmid,
164 From => 'Laggy Sender <lag@example.com>',
165 To => 'list@example.com',
167 body => "are real\n");
169 my $ghost_id = $rw->add_message($was_ghost);
170 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
171 ok($ghost_id < $reply_id, "ghost vivified from earlier message");
174 # search thread on ghost
180 my $res = $ro->query('ghost');
181 my @exp = sort qw(ghost-message@s ghost-reply@s);
182 my @res = filter_mids($res);
183 is_deeply(\@res, \@exp, 'got expected results for Subject match');
186 $res = $ro->query('goodbye');
187 is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
194 my $long_mid = 'last' . ('x' x 60). '@s';
196 my $long = Email::MIME->create(
198 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
199 Subject => 'long message ID',
200 'References' => '<root@s> <last@s>',
201 'In-Reply-To' => '<last@s>',
202 'Message-ID' => "<$long_mid>",
203 From => '"Long I.D." <long-id@example.com>',
204 To => 'list@example.com',
207 my $long_id = $rw->add_message($long);
208 is($long_id, int($long_id), "long_id is an integer: $long_id");
215 my $long_reply_mid = 'reply-to-long@1';
216 my $long_reply = Email::MIME->create(
218 Subject => 'I break references',
219 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
220 'Message-ID' => "<$long_reply_mid>",
222 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
223 'In-Reply-To' => "<$long_mid>",
224 From => '"no1 <no1@example.com>',
225 To => 'list@example.com',
227 body => "no References\n");
228 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
232 my $t = $ro->get_thread('root@s');
233 is($t->{total}, 4, "got all 4 mesages in thread");
234 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
235 @res = filter_mids($t);
236 is_deeply(\@res, \@exp, "get_thread works");
239 # quote prioritization
242 $rw->add_message(Email::MIME->create(
244 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
246 'Message-ID' => '<quote@a>',
247 From => 'Quoter <quoter@example.com>',
248 To => 'list@example.com',
250 body => "> theatre illusions\nfade\n"));
252 $rw->add_message(Email::MIME->create(
254 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
256 'Message-ID' => '<nquote@a>',
257 From => 'Non-Quoter<non-quoter@example.com>',
258 To => 'list@example.com',
260 body => "theatre\nfade\n"));
261 my $res = $rw->query("theatre");
262 is($res->{total}, 2, "got both matches");
263 is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
264 is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
266 $res = $rw->query("illusions");
267 is($res->{total}, 1, "got a match for quoted text");
268 is($res->{msgs}->[0]->mid, 'quote@a',
269 "quoted result returned if nothing else");
272 # circular references
274 my $doc_id = $rw->add_message(Email::MIME->create(
276 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
278 'Message-ID' => '<circle@a>',
279 'References' => '<circle@a>',
280 'In-Reply-To' => '<circle@a>',
281 From => 'Circle <circle@example.com>',
282 To => 'list@example.com',
285 ok($doc_id > 0, "doc_id defined with circular reference");
286 my $smsg = $rw->lookup_message('circle@a');
287 $smsg->ensure_metadata;
288 is($smsg->references, '', "no references created");