]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
searchmsg: ensure long subject lines are not broken
[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('pi-search-XXXXXX', TMPDIR => 1, 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 $rw = undef;
38 my $rw_commit = sub {
39         $rw->{xdb}->commit_transaction if $rw;
40         $rw = undef;
41         $rw = PublicInbox::SearchIdx->new($git_dir, 1);
42         $rw->{xdb}->begin_transaction;
43 };
44
45 {
46         # git repository perms
47         is(PublicInbox::SearchIdx->_git_config_perm(undef),
48            &PublicInbox::SearchIdx::PERM_GROUP,
49            "undefined permission is group");
50         is(PublicInbox::SearchIdx::_umask_for(
51              PublicInbox::SearchIdx->_git_config_perm('0644')),
52            0022, "644 => umask(0022)");
53         is(PublicInbox::SearchIdx::_umask_for(
54              PublicInbox::SearchIdx->_git_config_perm('0600')),
55            0077, "600 => umask(0077)");
56         is(PublicInbox::SearchIdx::_umask_for(
57              PublicInbox::SearchIdx->_git_config_perm('0640')),
58            0027, "640 => umask(0027)");
59         is(PublicInbox::SearchIdx::_umask_for(
60              PublicInbox::SearchIdx->_git_config_perm('group')),
61            0007, 'group => umask(0007)');
62         is(PublicInbox::SearchIdx::_umask_for(
63              PublicInbox::SearchIdx->_git_config_perm('everybody')),
64            0002, 'everybody => umask(0002)');
65         is(PublicInbox::SearchIdx::_umask_for(
66              PublicInbox::SearchIdx->_git_config_perm('umask')),
67            umask, 'umask => existing umask');
68 }
69
70 {
71         my $root = Email::MIME->create(
72                 header_str => [
73                         Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
74                         Subject => 'Hello world',
75                         'Message-ID' => '<root@s>',
76                         From => 'John Smith <js@example.com>',
77                         To => 'list@example.com',
78                 ],
79                 body => "\\m/\n");
80         my $last = Email::MIME->create(
81                 header_str => [
82                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
83                         Subject => 'Re: Hello world',
84                         'In-Reply-To' => '<root@s>',
85                         'Message-ID' => '<last@s>',
86                         From => 'John Smith <js@example.com>',
87                         To => 'list@example.com',
88                 ],
89                 body => "goodbye forever :<\n");
90
91         my $rv;
92         $rw_commit->();
93         $root_id = $rw->add_message($root);
94         is($root_id, int($root_id), "root_id is an integer: $root_id");
95         $last_id = $rw->add_message($last);
96         is($last_id, int($last_id), "last_id is an integer: $last_id");
97 }
98
99 sub filter_mids {
100         my ($res) = @_;
101         sort(map { $_->mid } @{$res->{msgs}});
102 }
103
104 {
105         $rw_commit->();
106         $ro->reopen;
107         my $found = $ro->lookup_message('<root@s>');
108         ok($found, "message found");
109         is($root_id, $found->{doc_id}, 'doc_id set correctly');
110         $found->ensure_metadata;
111         is($found->mid, 'root@s', 'mid set correctly');
112         ok(int($found->thread_id) > 0, 'thread_id is an integer');
113
114         my @exp = sort qw(root@s last@s);
115         my $res = $ro->query("path:hello_world");
116         my @res = filter_mids($res);
117         is_deeply(\@res, \@exp, 'got expected results for path: match');
118
119         foreach my $p (qw(hello hello_ hello_world2 hello_world_)) {
120                 $res = $ro->query("path:$p");
121                 is($res->{total}, 0, "path variant `$p' does not match");
122         }
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"');
129         @res = filter_mids($res);
130         is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
131
132         $res = $ro->query('subject:"Hello world"', {limit => 1});
133         is(scalar @{$res->{msgs}}, 1, "limit works");
134         my $first = $res->{msgs}->[0];
135
136         $res = $ro->query('subject:"Hello world"', {offset => 1});
137         is(scalar @{$res->{msgs}}, 1, "offset works");
138         my $second = $res->{msgs}->[0];
139
140         isnt($first, $second, "offset returned different result from limit");
141 }
142
143 # ghost vivication
144 {
145         $rw_commit->();
146         my $rmid = '<ghost-message@s>';
147         my $reply_to_ghost = Email::MIME->create(
148                 header_str => [
149                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
150                         Subject => 'Re: ghosts',
151                         'Message-ID' => '<ghost-reply@s>',
152                         'In-Reply-To' => $rmid,
153                         From => 'Time Traveler <tt@example.com>',
154                         To => 'list@example.com',
155                 ],
156                 body => "-_-\n");
157
158         my $rv;
159         my $reply_id = $rw->add_message($reply_to_ghost);
160         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
161
162         my $was_ghost = Email::MIME->create(
163                 header_str => [
164                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
165                         Subject => 'ghosts',
166                         'Message-ID' => $rmid,
167                         From => 'Laggy Sender <lag@example.com>',
168                         To => 'list@example.com',
169                 ],
170                 body => "are real\n");
171
172         my $ghost_id = $rw->add_message($was_ghost);
173         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
174         ok($ghost_id < $reply_id, "ghost vivified from earlier message");
175 }
176
177 # search thread on ghost
178 {
179         $rw_commit->();
180         $ro->reopen;
181
182         # Subject:
183         my $res = $ro->query('ghost');
184         my @exp = sort qw(ghost-message@s ghost-reply@s);
185         my @res = filter_mids($res);
186         is_deeply(\@res, \@exp, 'got expected results for Subject match');
187
188         # body
189         $res = $ro->query('goodbye');
190         is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
191 }
192
193 # long message-id
194 {
195         $rw_commit->();
196         $ro->reopen;
197         my $long_mid = 'last' . ('x' x 60). '@s';
198
199         my $long = Email::MIME->create(
200                 header_str => [
201                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
202                         Subject => 'long message ID',
203                         'References' => '<root@s> <last@s>',
204                         'In-Reply-To' => '<last@s>',
205                         'Message-ID' => "<$long_mid>",
206                         From => '"Long I.D." <long-id@example.com>',
207                         To => 'list@example.com',
208                 ],
209                 body => "wut\n");
210         my $long_id = $rw->add_message($long);
211         is($long_id, int($long_id), "long_id is an integer: $long_id");
212
213         $rw_commit->();
214         $ro->reopen;
215         my $res;
216         my @res;
217
218         my $long_reply_mid = 'reply-to-long@1';
219         my $long_reply = Email::MIME->create(
220                 header_str => [
221                         Subject => 'I break references',
222                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
223                         'Message-ID' => "<$long_reply_mid>",
224                         # No References:
225                         # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
226                         'In-Reply-To' => "<$long_mid>",
227                         From => '"no1 <no1@example.com>',
228                         To => 'list@example.com',
229                 ],
230                 body => "no References\n");
231         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
232
233         $rw_commit->();
234         $ro->reopen;
235         my $t = $ro->get_thread('root@s');
236         is($t->{total}, 4, "got all 4 mesages in thread");
237         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
238         @res = filter_mids($t);
239         is_deeply(\@res, \@exp, "get_thread works");
240 }
241
242 # quote prioritization
243 {
244         $rw_commit->();
245         $rw->add_message(Email::MIME->create(
246                 header_str => [
247                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
248                         Subject => 'Hello',
249                         'Message-ID' => '<quote@a>',
250                         From => 'Quoter <quoter@example.com>',
251                         To => 'list@example.com',
252                 ],
253                 body => "> theatre illusions\nfade\n"));
254
255         $rw->add_message(Email::MIME->create(
256                 header_str => [
257                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
258                         Subject => 'Hello',
259                         'Message-ID' => '<nquote@a>',
260                         From => 'Non-Quoter<non-quoter@example.com>',
261                         To => 'list@example.com',
262                 ],
263                 body => "theatre\nfade\n"));
264         my $res = $rw->query("theatre");
265         is($res->{total}, 2, "got both matches");
266         is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
267         is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
268
269         $res = $rw->query("illusions");
270         is($res->{total}, 1, "got a match for quoted text");
271         is($res->{msgs}->[0]->mid, 'quote@a',
272                 "quoted result returned if nothing else");
273 }
274
275 # circular references
276 {
277         my $s = 'foo://'. ('Circle' x 15).'/foo';
278         my $doc_id = $rw->add_message(Email::MIME->create(
279                 header => [ Subject => $s ],
280                 header_str => [
281                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
282                         'Message-ID' => '<circle@a>',
283                         'References' => '<circle@a>',
284                         'In-Reply-To' => '<circle@a>',
285                         From => 'Circle <circle@example.com>',
286                         To => 'list@example.com',
287                 ],
288                 body => "LOOP!\n"));
289         ok($doc_id > 0, "doc_id defined with circular reference");
290         my $smsg = $rw->lookup_message('circle@a');
291         $smsg->ensure_metadata;
292         is($smsg->references, '', "no references created");
293         my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
294         is($s, $msg->mini_mime->header('Subject'), 'long subject not rewritten');
295 }
296
297 {
298         my $str = eval {
299                 my $mbox = 't/utf8.mbox';
300                 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
301                 local $/;
302                 <$fh>
303         };
304         $str =~ s/\AFrom [^\n]+\n//s;
305         my $mime = Email::MIME->new($str);
306         my $doc_id = $rw->add_message($mime);
307         ok($doc_id > 0, 'message indexed doc_id with UTF-8');
308         my $smsg = $rw->lookup_message('testmessage@example.com');
309         my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
310
311         # mini_mime technically not valid (I think),
312         # but good enough for displaying HTML:
313         is($mime->header('Subject'), $msg->mini_mime->header('Subject'),
314                 'UTF-8 subject preserved');
315 }
316
317 done_testing();
318
319 1;