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 use File::Temp qw/tempdir/;
7 use PublicInbox::Search;
10 my $tmpdir = tempdir(CLEANUP => 1);
11 my $git_dir = "$tmpdir/a.git";
12 my ($root_id, $last_id);
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");
18 my $rw = PublicInbox::Search->new($git_dir, 1);
19 my $ro = PublicInbox::Search->new($git_dir);
22 my $root = Email::MIME->create(
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',
31 my $last = Email::MIME->create(
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',
40 body => "goodbye forever :<\n");
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");
51 sort(map { (split(/\n/, $_))[0] } @{$res->{msgs}});
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');
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');
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");
73 $res = $ro->query('subject:(hello world)');
74 @res = filter_mids($res);
75 is_deeply(\@res, \@exp, 'got expected results for subject:() match');
77 $res = $ro->query('subject:"hello world"');
78 @res = filter_mids($res);
79 is_deeply(\@res, \@exp, 'got expected results for subject:"" match');
81 $res = $ro->query('subject:"hello world"', {limit => 1});
82 is(scalar @{$res->{msgs}}, 1, "limit works");
83 my $first = $res->{msgs}->[0];
85 $res = $ro->query('subject:"hello world"', {offset => 1});
86 is(scalar @{$res->{msgs}}, 1, "offset works");
87 my $second = $res->{msgs}->[0];
89 isnt($first, $second, "offset returned different result from limit");
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");
104 my $rmid = '<ghost-message@s>';
105 my $reply_to_ghost = Email::MIME->create(
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',
117 my $reply_id = $rw->add_message($reply_to_ghost);
118 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
120 my $was_ghost = Email::MIME->create(
122 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
124 'Message-ID' => $rmid,
125 From => 'Laggy Sender <lag@example.com>',
126 To => 'list@example.com',
128 body => "are real\n");
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");
135 # search thread on ghost
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');
146 $res = $ro->query('goodbye');
147 is((split(/\n/, $res->{msgs}->[0]))[0], 'last@s',
148 'got goodbye message body');
155 my $long_mid = 'last' . ('x' x 60). '@s';
156 my $long_midc = Digest::SHA::sha1_hex($long_mid);
158 my $long = Email::MIME->create(
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',
169 my $long_id = $rw->add_message($long);
170 is($long_id, int($long_id), "long_id is an integer: $long_id");
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");
178 my $replies = $ro->get_replies('root@s');
179 $replies = [ filter_mids($replies) ];
180 is_deeply($replies, [ filter_mids($res) ], "get_replies matches");
182 my $long_reply_mid = 'reply-to-long@1';
183 my $long_reply = Email::MIME->create(
185 Subject => 'I break references',
186 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
187 'Message-ID' => "<$long_reply_mid>",
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',
194 body => "no References\n");
195 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
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");
205 # quote prioritization
208 $rw->add_message(Email::MIME->create(
210 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
212 'Message-ID' => '<quote@a>',
213 From => 'Quoter <quoter@example.com>',
214 To => 'list@example.com',
216 body => "> theatre illusions\nfade\n"));
218 $rw->add_message(Email::MIME->create(
220 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
222 'Message-ID' => '<nquote@a>',
223 From => 'Non-Quoter<non-quoter@example.com>',
224 To => 'list@example.com',
226 body => "theatre\nfade\n"));
227 my $res = $rw->query("theatre");
228 is($res->{count}, 2, "got both matches");
229 like($res->{msgs}->[0], qr/\Anquote\@a/, "non-quoted scores higher");
230 like($res->{msgs}->[1], qr/\Aquote\@a/, "quoted result still returned");
232 $res = $rw->query("illusions");
233 is($res->{count}, 1, "got a match for quoted text");
234 like($res->{msgs}->[0], qr/\Aquote\@a/,
235 "quoted result returned if nothing else");