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