]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
update copyright headers and email addresses
[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 {
20         my $orig = "FOO " x 30;
21         my $summ = PublicInbox::Search::subject_summary($orig);
22
23         $summ = length($summ);
24         $orig = length($orig);
25         ok($summ < $orig && $summ > 0, "summary shortened ($orig => $summ)");
26
27         $orig = "FOO" x 30;
28         $summ = PublicInbox::Search::subject_summary($orig);
29
30         $summ = length($summ);
31         $orig = length($orig);
32         ok($summ < $orig && $summ > 0,
33            "summary shortened but not empty: $summ");
34 }
35
36 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
37 my $ro = PublicInbox::Search->new($git_dir);
38 my $rw_commit = sub {
39         $rw = undef;
40         $rw = PublicInbox::SearchIdx->new($git_dir, 1);
41 };
42
43 {
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');
66 }
67
68 {
69         my $root = Email::MIME->create(
70                 header_str => [
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',
76                 ],
77                 body => "\\m/\n");
78         my $last = Email::MIME->create(
79                 header_str => [
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',
86                 ],
87                 body => "goodbye forever :<\n");
88
89         my $rv;
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");
94 }
95
96 sub filter_mids {
97         my ($res) = @_;
98         sort(map { $_->mid } @{$res->{msgs}});
99 }
100
101 {
102         $rw_commit->();
103         $ro->reopen;
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');
110
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');
115
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");
119         }
120
121         $res = $ro->query('subject:(Hello world)');
122         @res = filter_mids($res);
123         is_deeply(\@res, \@exp, 'got expected results for subject:() match');
124
125         $res = $ro->query('subject:"Hello world"');
126         @res = filter_mids($res);
127         is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
128
129         $res = $ro->query('subject:"Hello world"', {limit => 1});
130         is(scalar @{$res->{msgs}}, 1, "limit works");
131         my $first = $res->{msgs}->[0];
132
133         $res = $ro->query('subject:"Hello world"', {offset => 1});
134         is(scalar @{$res->{msgs}}, 1, "offset works");
135         my $second = $res->{msgs}->[0];
136
137         isnt($first, $second, "offset returned different result from limit");
138 }
139
140 # ghost vivication
141 {
142         $rw_commit->();
143         my $rmid = '<ghost-message@s>';
144         my $reply_to_ghost = Email::MIME->create(
145                 header_str => [
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',
152                 ],
153                 body => "-_-\n");
154
155         my $rv;
156         my $reply_id = $rw->add_message($reply_to_ghost);
157         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
158
159         my $was_ghost = Email::MIME->create(
160                 header_str => [
161                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
162                         Subject => 'ghosts',
163                         'Message-ID' => $rmid,
164                         From => 'Laggy Sender <lag@example.com>',
165                         To => 'list@example.com',
166                 ],
167                 body => "are real\n");
168
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");
172 }
173
174 # search thread on ghost
175 {
176         $rw_commit->();
177         $ro->reopen;
178
179         # Subject:
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');
184
185         # body
186         $res = $ro->query('goodbye');
187         is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
188 }
189
190 # long message-id
191 {
192         $rw_commit->();
193         $ro->reopen;
194         my $long_mid = 'last' . ('x' x 60). '@s';
195
196         my $long = Email::MIME->create(
197                 header_str => [
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',
205                 ],
206                 body => "wut\n");
207         my $long_id = $rw->add_message($long);
208         is($long_id, int($long_id), "long_id is an integer: $long_id");
209
210         $rw_commit->();
211         $ro->reopen;
212         my $res;
213         my @res;
214
215         my $long_reply_mid = 'reply-to-long@1';
216         my $long_reply = Email::MIME->create(
217                 header_str => [
218                         Subject => 'I break references',
219                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
220                         'Message-ID' => "<$long_reply_mid>",
221                         # No References:
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',
226                 ],
227                 body => "no References\n");
228         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
229
230         $rw_commit->();
231         $ro->reopen;
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");
237 }
238
239 # quote prioritization
240 {
241         $rw_commit->();
242         $rw->add_message(Email::MIME->create(
243                 header_str => [
244                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
245                         Subject => 'Hello',
246                         'Message-ID' => '<quote@a>',
247                         From => 'Quoter <quoter@example.com>',
248                         To => 'list@example.com',
249                 ],
250                 body => "> theatre illusions\nfade\n"));
251
252         $rw->add_message(Email::MIME->create(
253                 header_str => [
254                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
255                         Subject => 'Hello',
256                         'Message-ID' => '<nquote@a>',
257                         From => 'Non-Quoter<non-quoter@example.com>',
258                         To => 'list@example.com',
259                 ],
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");
265
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");
270 }
271
272 # circular references
273 {
274         my $doc_id = $rw->add_message(Email::MIME->create(
275                 header_str => [
276                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
277                         Subject => 'Circle',
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',
283                 ],
284                 body => "LOOP!\n"));
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_sorted, '', "no references created");
289 }
290
291 done_testing();
292
293 1;