]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
search: split search indexing to a separate file
[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         my $root = Email::MIME->create(
28                 header_str => [
29                         Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
30                         Subject => 'Hello world',
31                         'Message-ID' => '<root@s>',
32                         From => 'John Smith <js@example.com>',
33                         To => 'list@example.com',
34                 ],
35                 body => "\\m/\n");
36         my $last = Email::MIME->create(
37                 header_str => [
38                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
39                         Subject => 'Re: Hello world',
40                         'In-Reply-To' => '<root@s>',
41                         'Message-ID' => '<last@s>',
42                         From => 'John Smith <js@example.com>',
43                         To => 'list@example.com',
44                 ],
45                 body => "goodbye forever :<\n");
46
47         my $rv;
48         $root_id = $rw->add_message($root);
49         is($root_id, int($root_id), "root_id is an integer: $root_id");
50         $last_id = $rw->add_message($last);
51         is($last_id, int($last_id), "last_id is an integer: $last_id");
52 }
53
54 sub filter_mids {
55         my ($res) = @_;
56         sort(map { $_->mid } @{$res->{msgs}});
57 }
58
59 {
60         $rw_commit->();
61         $ro->reopen;
62         my $found = $ro->lookup_message('<root@s>');
63         ok($found, "message found");
64         is($root_id, $found->{doc_id}, 'doc_id set correctly');
65         $found->ensure_metadata;
66         is($found->mid, 'root@s', 'mid set correctly');
67         ok(int($found->thread_id) > 0, 'thread_id is an integer');
68
69         my @exp = sort qw(root@s last@s);
70         my $res = $ro->query("path:hello_world");
71         my @res = filter_mids($res);
72         is_deeply(\@res, \@exp, 'got expected results for path: match');
73
74         foreach my $p (qw(hello hello_ hello_world2 hello_world_)) {
75                 $res = $ro->query("path:$p");
76                 is($res->{total}, 0, "path variant `$p' does not match");
77         }
78
79         $res = $ro->query('subject:(Hello world)');
80         @res = filter_mids($res);
81         is_deeply(\@res, \@exp, 'got expected results for subject:() match');
82
83         $res = $ro->query('subject:"Hello world"');
84         @res = filter_mids($res);
85         is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
86
87         $res = $ro->query('subject:"Hello world"', {limit => 1});
88         is(scalar @{$res->{msgs}}, 1, "limit works");
89         my $first = $res->{msgs}->[0];
90
91         $res = $ro->query('subject:"Hello world"', {offset => 1});
92         is(scalar @{$res->{msgs}}, 1, "offset works");
93         my $second = $res->{msgs}->[0];
94
95         isnt($first, $second, "offset returned different result from limit");
96
97         foreach my $f (qw(inreplyto references)) {
98                 $res = $ro->query($f . ':root@s');
99                 @res = filter_mids($res);
100                 is_deeply(\@res, [ 'last@s' ],
101                           "got expected results for $f: match");
102                 $res = $ro->query($f . ':root');
103                 is($res->{total}, 0, "no partial mid match");
104         }
105 }
106
107 # ghost vivication
108 {
109         $rw_commit->();
110         my $rmid = '<ghost-message@s>';
111         my $reply_to_ghost = Email::MIME->create(
112                 header_str => [
113                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
114                         Subject => 'Re: ghosts',
115                         'Message-ID' => '<ghost-reply@s>',
116                         'In-Reply-To' => $rmid,
117                         From => 'Time Traveler <tt@example.com>',
118                         To => 'list@example.com',
119                 ],
120                 body => "-_-\n");
121
122         my $rv;
123         my $reply_id = $rw->add_message($reply_to_ghost);
124         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
125
126         my $was_ghost = Email::MIME->create(
127                 header_str => [
128                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
129                         Subject => 'ghosts',
130                         'Message-ID' => $rmid,
131                         From => 'Laggy Sender <lag@example.com>',
132                         To => 'list@example.com',
133                 ],
134                 body => "are real\n");
135
136         my $ghost_id = $rw->add_message($was_ghost);
137         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
138         ok($ghost_id < $reply_id, "ghost vivified from earlier message");
139 }
140
141 # search thread on ghost
142 {
143         $rw_commit->();
144         $ro->reopen;
145
146         # Subject:
147         my $res = $ro->query('ghost');
148         my @exp = sort qw(ghost-message@s ghost-reply@s);
149         my @res = filter_mids($res);
150         is_deeply(\@res, \@exp, 'got expected results for Subject match');
151
152         # body
153         $res = $ro->query('goodbye');
154         is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
155 }
156
157 # long message-id
158 {
159         $rw_commit->();
160         $ro->reopen;
161         my $long_mid = 'last' . ('x' x 60). '@s';
162         my $long_midc = Digest::SHA::sha1_hex($long_mid);
163
164         my $long = Email::MIME->create(
165                 header_str => [
166                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
167                         Subject => 'long message ID',
168                         'References' => '<root@s> <last@s>',
169                         'In-Reply-To' => '<last@s>',
170                         'Message-ID' => "<$long_mid>",
171                         From => '"Long I.D." <long-id@example.com>',
172                         To => 'list@example.com',
173                 ],
174                 body => "wut\n");
175         my $long_id = $rw->add_message($long);
176         is($long_id, int($long_id), "long_id is an integer: $long_id");
177
178         $rw_commit->();
179         $ro->reopen;
180         my $res = $ro->query('references:root@s');
181         my @res = filter_mids($res);
182         is_deeply(\@res, [ sort('last@s', $long_midc) ],
183                   "got expected results for references: match");
184
185         my $followups = $ro->get_followups('root@s');
186         $followups = [ filter_mids($followups) ];
187         is_deeply($followups, [ filter_mids($res) ], "get_followups matches");
188
189         my $long_reply_mid = 'reply-to-long@1';
190         my $long_reply = Email::MIME->create(
191                 header_str => [
192                         Subject => 'I break references',
193                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
194                         'Message-ID' => "<$long_reply_mid>",
195                         # No References:
196                         # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
197                         'In-Reply-To' => "<$long_mid>",
198                         From => '"no1 <no1@example.com>',
199                         To => 'list@example.com',
200                 ],
201                 body => "no References\n");
202         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
203
204         $rw_commit->();
205         $ro->reopen;
206         my $t = $ro->get_thread('root@s');
207         is($t->{total}, 4, "got all 4 mesages in thread");
208         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_midc);
209         @res = filter_mids($t);
210         is_deeply(\@res, \@exp, "get_thread works");
211 }
212
213 # quote prioritization
214 {
215         $rw_commit->();
216         $rw->add_message(Email::MIME->create(
217                 header_str => [
218                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
219                         Subject => 'Hello',
220                         'Message-ID' => '<quote@a>',
221                         From => 'Quoter <quoter@example.com>',
222                         To => 'list@example.com',
223                 ],
224                 body => "> theatre illusions\nfade\n"));
225
226         $rw->add_message(Email::MIME->create(
227                 header_str => [
228                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
229                         Subject => 'Hello',
230                         'Message-ID' => '<nquote@a>',
231                         From => 'Non-Quoter<non-quoter@example.com>',
232                         To => 'list@example.com',
233                 ],
234                 body => "theatre\nfade\n"));
235         my $res = $rw->query("theatre");
236         is($res->{total}, 2, "got both matches");
237         is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
238         is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
239
240         $res = $rw->query("illusions");
241         is($res->{total}, 1, "got a match for quoted text");
242         is($res->{msgs}->[0]->mid, 'quote@a',
243                 "quoted result returned if nothing else");
244 }
245
246 # circular references
247 {
248         my $doc_id = $rw->add_message(Email::MIME->create(
249                 header_str => [
250                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
251                         Subject => 'Circle',
252                         'Message-ID' => '<circle@a>',
253                         'References' => '<circle@a>',
254                         'In-Reply-To' => '<circle@a>',
255                         From => 'Circle <circle@example.com>',
256                         To => 'list@example.com',
257                 ],
258                 body => "LOOP!\n"));
259         ok($doc_id > 0, "doc_id defined with circular reference");
260         my $smsg = $rw->lookup_message('circle@a');
261         $smsg->ensure_metadata;
262         is($smsg->{references}, undef, "no references created");
263 }
264
265 done_testing();
266
267 1;