]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
17e9eaf43aea8e764de4e618539d7108e0947e43
[public-inbox.git] / t / search.t
1 # Copyright (C) 2015, all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 use strict;
4 use warnings;
5 use Test::More;
6 eval { require PublicInbox::SearchIdx; };
7 plan skip_all => "Xapian missing for search" if $@;
8 use File::Temp qw/tempdir/;
9 use Email::MIME;
10 use Data::Dumper;
11 my $tmpdir = tempdir(CLEANUP => 1);
12 my $git_dir = "$tmpdir/a.git";
13 my ($root_id, $last_id);
14
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");
18
19 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
20 my $ro = PublicInbox::Search->new($git_dir);
21 my $rw_commit = sub {
22         $rw = undef;
23         $rw = PublicInbox::SearchIdx->new($git_dir, 1);
24 };
25
26 {
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');
49 }
50
51 {
52         my $root = Email::MIME->create(
53                 header_str => [
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',
59                 ],
60                 body => "\\m/\n");
61         my $last = Email::MIME->create(
62                 header_str => [
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',
69                 ],
70                 body => "goodbye forever :<\n");
71
72         my $rv;
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");
77 }
78
79 sub filter_mids {
80         my ($res) = @_;
81         sort(map { $_->mid } @{$res->{msgs}});
82 }
83
84 {
85         $rw_commit->();
86         $ro->reopen;
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');
93
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');
98
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");
102         }
103
104         $res = $ro->query('subject:(Hello world)');
105         @res = filter_mids($res);
106         is_deeply(\@res, \@exp, 'got expected results for subject:() match');
107
108         $res = $ro->query('subject:"Hello world"');
109         @res = filter_mids($res);
110         is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
111
112         $res = $ro->query('subject:"Hello world"', {limit => 1});
113         is(scalar @{$res->{msgs}}, 1, "limit works");
114         my $first = $res->{msgs}->[0];
115
116         $res = $ro->query('subject:"Hello world"', {offset => 1});
117         is(scalar @{$res->{msgs}}, 1, "offset works");
118         my $second = $res->{msgs}->[0];
119
120         isnt($first, $second, "offset returned different result from limit");
121
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");
129         }
130 }
131
132 # ghost vivication
133 {
134         $rw_commit->();
135         my $rmid = '<ghost-message@s>';
136         my $reply_to_ghost = Email::MIME->create(
137                 header_str => [
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',
144                 ],
145                 body => "-_-\n");
146
147         my $rv;
148         my $reply_id = $rw->add_message($reply_to_ghost);
149         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
150
151         my $was_ghost = Email::MIME->create(
152                 header_str => [
153                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
154                         Subject => 'ghosts',
155                         'Message-ID' => $rmid,
156                         From => 'Laggy Sender <lag@example.com>',
157                         To => 'list@example.com',
158                 ],
159                 body => "are real\n");
160
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");
164 }
165
166 # search thread on ghost
167 {
168         $rw_commit->();
169         $ro->reopen;
170
171         # Subject:
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');
176
177         # body
178         $res = $ro->query('goodbye');
179         is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
180 }
181
182 # long message-id
183 {
184         $rw_commit->();
185         $ro->reopen;
186         my $long_mid = 'last' . ('x' x 60). '@s';
187         my $long_midc = Digest::SHA::sha1_hex($long_mid);
188
189         my $long = Email::MIME->create(
190                 header_str => [
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',
198                 ],
199                 body => "wut\n");
200         my $long_id = $rw->add_message($long);
201         is($long_id, int($long_id), "long_id is an integer: $long_id");
202
203         $rw_commit->();
204         $ro->reopen;
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");
209
210         my $followups = $ro->get_followups('root@s');
211         $followups = [ filter_mids($followups) ];
212         is_deeply($followups, [ filter_mids($res) ], "get_followups matches");
213
214         my $long_reply_mid = 'reply-to-long@1';
215         my $long_reply = Email::MIME->create(
216                 header_str => [
217                         Subject => 'I break references',
218                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
219                         'Message-ID' => "<$long_reply_mid>",
220                         # No References:
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',
225                 ],
226                 body => "no References\n");
227         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
228
229         $rw_commit->();
230         $ro->reopen;
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");
236 }
237
238 # quote prioritization
239 {
240         $rw_commit->();
241         $rw->add_message(Email::MIME->create(
242                 header_str => [
243                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
244                         Subject => 'Hello',
245                         'Message-ID' => '<quote@a>',
246                         From => 'Quoter <quoter@example.com>',
247                         To => 'list@example.com',
248                 ],
249                 body => "> theatre illusions\nfade\n"));
250
251         $rw->add_message(Email::MIME->create(
252                 header_str => [
253                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
254                         Subject => 'Hello',
255                         'Message-ID' => '<nquote@a>',
256                         From => 'Non-Quoter<non-quoter@example.com>',
257                         To => 'list@example.com',
258                 ],
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");
264
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");
269 }
270
271 # circular references
272 {
273         my $doc_id = $rw->add_message(Email::MIME->create(
274                 header_str => [
275                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
276                         Subject => 'Circle',
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',
282                 ],
283                 body => "LOOP!\n"));
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");
288 }
289
290 done_testing();
291
292 1;