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