]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
search: implement subject summarization
[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         foreach my $f (qw(inreplyto references)) {
140                 $res = $ro->query($f . ':root@s');
141                 @res = filter_mids($res);
142                 is_deeply(\@res, [ 'last@s' ],
143                           "got expected results for $f: match");
144                 $res = $ro->query($f . ':root');
145                 is($res->{total}, 0, "no partial mid match");
146         }
147 }
148
149 # ghost vivication
150 {
151         $rw_commit->();
152         my $rmid = '<ghost-message@s>';
153         my $reply_to_ghost = Email::MIME->create(
154                 header_str => [
155                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
156                         Subject => 'Re: ghosts',
157                         'Message-ID' => '<ghost-reply@s>',
158                         'In-Reply-To' => $rmid,
159                         From => 'Time Traveler <tt@example.com>',
160                         To => 'list@example.com',
161                 ],
162                 body => "-_-\n");
163
164         my $rv;
165         my $reply_id = $rw->add_message($reply_to_ghost);
166         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
167
168         my $was_ghost = Email::MIME->create(
169                 header_str => [
170                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
171                         Subject => 'ghosts',
172                         'Message-ID' => $rmid,
173                         From => 'Laggy Sender <lag@example.com>',
174                         To => 'list@example.com',
175                 ],
176                 body => "are real\n");
177
178         my $ghost_id = $rw->add_message($was_ghost);
179         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
180         ok($ghost_id < $reply_id, "ghost vivified from earlier message");
181 }
182
183 # search thread on ghost
184 {
185         $rw_commit->();
186         $ro->reopen;
187
188         # Subject:
189         my $res = $ro->query('ghost');
190         my @exp = sort qw(ghost-message@s ghost-reply@s);
191         my @res = filter_mids($res);
192         is_deeply(\@res, \@exp, 'got expected results for Subject match');
193
194         # body
195         $res = $ro->query('goodbye');
196         is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
197 }
198
199 # long message-id
200 {
201         $rw_commit->();
202         $ro->reopen;
203         my $long_mid = 'last' . ('x' x 60). '@s';
204         my $long_midc = Digest::SHA::sha1_hex($long_mid);
205
206         my $long = Email::MIME->create(
207                 header_str => [
208                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
209                         Subject => 'long message ID',
210                         'References' => '<root@s> <last@s>',
211                         'In-Reply-To' => '<last@s>',
212                         'Message-ID' => "<$long_mid>",
213                         From => '"Long I.D." <long-id@example.com>',
214                         To => 'list@example.com',
215                 ],
216                 body => "wut\n");
217         my $long_id = $rw->add_message($long);
218         is($long_id, int($long_id), "long_id is an integer: $long_id");
219
220         $rw_commit->();
221         $ro->reopen;
222         my $res = $ro->query('references:root@s');
223         my @res = filter_mids($res);
224         is_deeply(\@res, [ sort('last@s', $long_midc) ],
225                   "got expected results for references: match");
226
227         my $followups = $ro->get_followups('root@s');
228         $followups = [ filter_mids($followups) ];
229         is_deeply($followups, [ filter_mids($res) ], "get_followups matches");
230
231         my $long_reply_mid = 'reply-to-long@1';
232         my $long_reply = Email::MIME->create(
233                 header_str => [
234                         Subject => 'I break references',
235                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
236                         'Message-ID' => "<$long_reply_mid>",
237                         # No References:
238                         # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
239                         'In-Reply-To' => "<$long_mid>",
240                         From => '"no1 <no1@example.com>',
241                         To => 'list@example.com',
242                 ],
243                 body => "no References\n");
244         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
245
246         $rw_commit->();
247         $ro->reopen;
248         my $t = $ro->get_thread('root@s');
249         is($t->{total}, 4, "got all 4 mesages in thread");
250         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_midc);
251         @res = filter_mids($t);
252         is_deeply(\@res, \@exp, "get_thread works");
253 }
254
255 # quote prioritization
256 {
257         $rw_commit->();
258         $rw->add_message(Email::MIME->create(
259                 header_str => [
260                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
261                         Subject => 'Hello',
262                         'Message-ID' => '<quote@a>',
263                         From => 'Quoter <quoter@example.com>',
264                         To => 'list@example.com',
265                 ],
266                 body => "> theatre illusions\nfade\n"));
267
268         $rw->add_message(Email::MIME->create(
269                 header_str => [
270                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
271                         Subject => 'Hello',
272                         'Message-ID' => '<nquote@a>',
273                         From => 'Non-Quoter<non-quoter@example.com>',
274                         To => 'list@example.com',
275                 ],
276                 body => "theatre\nfade\n"));
277         my $res = $rw->query("theatre");
278         is($res->{total}, 2, "got both matches");
279         is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
280         is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
281
282         $res = $rw->query("illusions");
283         is($res->{total}, 1, "got a match for quoted text");
284         is($res->{msgs}->[0]->mid, 'quote@a',
285                 "quoted result returned if nothing else");
286 }
287
288 # circular references
289 {
290         my $doc_id = $rw->add_message(Email::MIME->create(
291                 header_str => [
292                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
293                         Subject => 'Circle',
294                         'Message-ID' => '<circle@a>',
295                         'References' => '<circle@a>',
296                         'In-Reply-To' => '<circle@a>',
297                         From => 'Circle <circle@example.com>',
298                         To => 'list@example.com',
299                 ],
300                 body => "LOOP!\n"));
301         ok($doc_id > 0, "doc_id defined with circular reference");
302         my $smsg = $rw->lookup_message('circle@a');
303         $smsg->ensure_metadata;
304         is($smsg->{references}, undef, "no references created");
305 }
306
307 done_testing();
308
309 1;