1 # Copyright (C) 2015, all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
6 eval { require PublicInbox::Search; };
7 plan skip_all => "Xapian missing for search" if $@;
8 use File::Temp qw/tempdir/;
11 my $tmpdir = tempdir(CLEANUP => 1);
12 my $git_dir = "$tmpdir/a.git";
13 my ($root_id, $last_id);
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");
19 my $rw = PublicInbox::Search->new($git_dir, 1);
20 my $ro = PublicInbox::Search->new($git_dir);
23 $rw = PublicInbox::Search->new($git_dir, 1);
27 my $root = Email::MIME->create(
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',
36 my $last = Email::MIME->create(
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',
45 body => "goodbye forever :<\n");
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");
56 sort(map { $_->mid } @{$res->{msgs}});
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');
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');
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");
79 $res = $ro->query('subject:(Hello world)');
80 @res = filter_mids($res);
81 is_deeply(\@res, \@exp, 'got expected results for subject:() match');
83 $res = $ro->query('subject:"Hello world"');
84 @res = filter_mids($res);
85 is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
87 $res = $ro->query('subject:"Hello world"', {limit => 1});
88 is(scalar @{$res->{msgs}}, 1, "limit works");
89 my $first = $res->{msgs}->[0];
91 $res = $ro->query('subject:"Hello world"', {offset => 1});
92 is(scalar @{$res->{msgs}}, 1, "offset works");
93 my $second = $res->{msgs}->[0];
95 isnt($first, $second, "offset returned different result from limit");
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");
110 my $rmid = '<ghost-message@s>';
111 my $reply_to_ghost = Email::MIME->create(
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',
123 my $reply_id = $rw->add_message($reply_to_ghost);
124 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
126 my $was_ghost = Email::MIME->create(
128 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
130 'Message-ID' => $rmid,
131 From => 'Laggy Sender <lag@example.com>',
132 To => 'list@example.com',
134 body => "are real\n");
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");
141 # search thread on ghost
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');
153 $res = $ro->query('goodbye');
154 is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
161 my $long_mid = 'last' . ('x' x 60). '@s';
162 my $long_midc = Digest::SHA::sha1_hex($long_mid);
164 my $long = Email::MIME->create(
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',
175 my $long_id = $rw->add_message($long);
176 is($long_id, int($long_id), "long_id is an integer: $long_id");
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");
185 my $followups = $ro->get_followups('root@s');
186 $followups = [ filter_mids($followups) ];
187 is_deeply($followups, [ filter_mids($res) ], "get_followups matches");
189 my $long_reply_mid = 'reply-to-long@1';
190 my $long_reply = Email::MIME->create(
192 Subject => 'I break references',
193 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
194 'Message-ID' => "<$long_reply_mid>",
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',
201 body => "no References\n");
202 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
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");
213 # quote prioritization
216 $rw->add_message(Email::MIME->create(
218 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
220 'Message-ID' => '<quote@a>',
221 From => 'Quoter <quoter@example.com>',
222 To => 'list@example.com',
224 body => "> theatre illusions\nfade\n"));
226 $rw->add_message(Email::MIME->create(
228 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
230 'Message-ID' => '<nquote@a>',
231 From => 'Non-Quoter<non-quoter@example.com>',
232 To => 'list@example.com',
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");
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");
246 # circular references
248 my $doc_id = $rw->add_message(Email::MIME->create(
250 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
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',
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");