]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
t/: remove unnecessary Dumper use
[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 my $tmpdir = tempdir(CLEANUP => 1);
11 my $git_dir = "$tmpdir/a.git";
12 my ($root_id, $last_id);
13
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");
17
18 {
19         my $orig = "FOO " x 30;
20         my $summ = PublicInbox::Search::subject_summary($orig);
21
22         $summ = length($summ);
23         $orig = length($orig);
24         ok($summ < $orig && $summ > 0, "summary shortened ($orig => $summ)");
25
26         $orig = "FOO" x 30;
27         $summ = PublicInbox::Search::subject_summary($orig);
28
29         $summ = length($summ);
30         $orig = length($orig);
31         ok($summ < $orig && $summ > 0,
32            "summary shortened but not empty: $summ");
33 }
34
35 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
36 my $ro = PublicInbox::Search->new($git_dir);
37 my $rw_commit = sub {
38         $rw = undef;
39         $rw = PublicInbox::SearchIdx->new($git_dir, 1);
40 };
41
42 {
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');
65 }
66
67 {
68         my $root = Email::MIME->create(
69                 header_str => [
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',
75                 ],
76                 body => "\\m/\n");
77         my $last = Email::MIME->create(
78                 header_str => [
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',
85                 ],
86                 body => "goodbye forever :<\n");
87
88         my $rv;
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");
93 }
94
95 sub filter_mids {
96         my ($res) = @_;
97         sort(map { $_->mid } @{$res->{msgs}});
98 }
99
100 {
101         $rw_commit->();
102         $ro->reopen;
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');
109
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');
114
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");
118         }
119
120         $res = $ro->query('subject:(Hello world)');
121         @res = filter_mids($res);
122         is_deeply(\@res, \@exp, 'got expected results for subject:() match');
123
124         $res = $ro->query('subject:"Hello world"');
125         @res = filter_mids($res);
126         is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
127
128         $res = $ro->query('subject:"Hello world"', {limit => 1});
129         is(scalar @{$res->{msgs}}, 1, "limit works");
130         my $first = $res->{msgs}->[0];
131
132         $res = $ro->query('subject:"Hello world"', {offset => 1});
133         is(scalar @{$res->{msgs}}, 1, "offset works");
134         my $second = $res->{msgs}->[0];
135
136         isnt($first, $second, "offset returned different result from limit");
137 }
138
139 # ghost vivication
140 {
141         $rw_commit->();
142         my $rmid = '<ghost-message@s>';
143         my $reply_to_ghost = Email::MIME->create(
144                 header_str => [
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',
151                 ],
152                 body => "-_-\n");
153
154         my $rv;
155         my $reply_id = $rw->add_message($reply_to_ghost);
156         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
157
158         my $was_ghost = Email::MIME->create(
159                 header_str => [
160                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
161                         Subject => 'ghosts',
162                         'Message-ID' => $rmid,
163                         From => 'Laggy Sender <lag@example.com>',
164                         To => 'list@example.com',
165                 ],
166                 body => "are real\n");
167
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");
171 }
172
173 # search thread on ghost
174 {
175         $rw_commit->();
176         $ro->reopen;
177
178         # Subject:
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');
183
184         # body
185         $res = $ro->query('goodbye');
186         is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
187 }
188
189 # long message-id
190 {
191         $rw_commit->();
192         $ro->reopen;
193         my $long_mid = 'last' . ('x' x 60). '@s';
194
195         my $long = Email::MIME->create(
196                 header_str => [
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',
204                 ],
205                 body => "wut\n");
206         my $long_id = $rw->add_message($long);
207         is($long_id, int($long_id), "long_id is an integer: $long_id");
208
209         $rw_commit->();
210         $ro->reopen;
211         my $res;
212         my @res;
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_mid);
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, '', "no references created");
288 }
289
290 done_testing();
291
292 1;