]> Sergey Matveev's repositories - public-inbox.git/blob - t/extsearch.t
ffbc10e2f3522b585dcab4f0d78efcae4054f423
[public-inbox.git] / t / extsearch.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Config;
8 use PublicInbox::Search;
9 use PublicInbox::InboxWritable;
10 use Fcntl qw(:seek);
11 require_git(2.6);
12 require_mods(qw(json DBD::SQLite Search::Xapian));
13 use_ok 'PublicInbox::ExtSearch';
14 use_ok 'PublicInbox::ExtSearchIdx';
15 use_ok 'PublicInbox::OverIdx';
16 my $sock = tcp_server();
17 my $host_port = $sock->sockhost . ':' . $sock->sockport;
18 my ($home, $for_destroy) = tmpdir();
19 local $ENV{HOME} = $home;
20 mkdir "$home/.public-inbox" or BAIL_OUT $!;
21 my $cfg_path = "$home/.public-inbox/config";
22 open my $fh, '>', $cfg_path or BAIL_OUT $!;
23 print $fh <<EOF or BAIL_OUT $!;
24 [publicinboxMda]
25         spamcheck = none
26 EOF
27 close $fh or BAIL_OUT $!;
28 my $v2addr = 'v2test@example.com';
29 my $v1addr = 'v1test@example.com';
30 ok(run_script([qw(-init -Lbasic -V2 v2test --newsgroup v2.example),
31         "$home/v2test", 'http://example.com/v2test', $v2addr ]), 'v2test init');
32 my $env = { ORIGINAL_RECIPIENT => $v2addr };
33 my $eml = eml_load('t/utf8.eml');
34
35 $eml->header_set('List-Id', '<v2.example.com>');
36 open($fh, '+>', undef) or BAIL_OUT $!;
37 $fh->autoflush(1);
38 print $fh $eml->as_string or BAIL_OUT $!;
39 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
40
41 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
42
43 ok(run_script([qw(-init -V1 v1test --newsgroup v1.example), "$home/v1test",
44         'http://example.com/v1test', $v1addr ]), 'v1test init');
45
46 $eml->header_set('List-Id', '<v1.example.com>');
47 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
48 truncate($fh, 0) or BAIL_OUT $!;
49 print $fh $eml->as_string or BAIL_OUT $!;
50 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
51
52 $env = { ORIGINAL_RECIPIENT => $v1addr };
53 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
54
55 run_script([qw(-index -Lbasic), "$home/v1test"]) or BAIL_OUT "index $?";
56
57 ok(run_script([qw(-extindex --all), "$home/extindex"]), 'extindex init');
58 {
59         my $es = PublicInbox::ExtSearch->new("$home/extindex");
60         ok($es->has_threadid, '->has_threadid');
61 }
62
63 { # TODO: -extindex should write this to config
64         open $fh, '>>', $cfg_path or BAIL_OUT $!;
65         print $fh <<EOF or BAIL_OUT $!;
66 ; for ->ALL
67 [extindex "all"]
68         topdir = $home/extindex
69 EOF
70         close $fh or BAIL_OUT $!;
71
72         my $pi_cfg = PublicInbox::Config->new;
73         $pi_cfg->fill_all;
74         ok($pi_cfg->ALL, '->ALL');
75         my $ibx = $pi_cfg->{-by_newsgroup}->{'v2.example'};
76         my $ret = $pi_cfg->ALL->nntp_xref_for($ibx, $ibx->over->get_art(1));
77         is_deeply($ret, { 'v1.example' => 1, 'v2.example' => 1 },
78                 '->nntp_xref_for');
79 }
80
81 SKIP: {
82         require_mods(qw(Net::NNTP), 1);
83         my ($out, $err) = ("$home/nntpd.out.log", "$home/nntpd.err.log");
84         my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
85         my $td = start_script($cmd, undef, { 3 => $sock });
86         my $n = Net::NNTP->new($host_port);
87         my @xp = $n->xpath('<testmessage@example.com>');
88         is_deeply(\@xp, [ qw(v1.example/1 v2.example/1) ]);
89         $n->group('v1.example');
90         my $res = $n->head(1);
91         @$res = grep(/^Xref: /, @$res);
92         like($res->[0], qr/ v1\.example:1 v2\.example:1/, 'nntp_xref works');
93 }
94
95 my $es = PublicInbox::ExtSearch->new("$home/extindex");
96 {
97         my $smsg = $es->over->get_art(1);
98         ok($smsg, 'got first article');
99         is($es->over->get_art(2), undef, 'only one added');
100         my $xref3 = $es->over->get_xref3(1);
101         like($xref3->[0], qr/\A\Qv2.example\E:1:/, 'order preserved 1');
102         like($xref3->[1], qr/\A\Qv1.example\E:1:/, 'order preserved 2');
103         is(scalar(@$xref3), 2, 'only to entries');
104 }
105
106 if ('inbox edited') {
107         my ($in, $out, $err);
108         $in = $out = $err = '';
109         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
110         my $env = { MAIL_EDITOR => "$^X -i -p -e 's/test message/BEST MSG/'" };
111         my $cmd = [ qw(-edit -Ft/utf8.eml), "$home/v2test" ];
112         ok(run_script($cmd, $env, $opt), '-edit');
113         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
114                 'extindex again');
115         like($err, qr/discontiguous range/, 'warned about discontiguous range');
116         my $msg1 = $es->over->get_art(1) or BAIL_OUT 'msg1 missing';
117         my $msg2 = $es->over->get_art(2) or BAIL_OUT 'msg2 missing';
118         is($msg1->{mid}, $msg2->{mid}, 'edited message indexed');
119         isnt($msg1->{blob}, $msg2->{blob}, 'blobs differ');
120         my $eml2 = $es->smsg_eml($msg2);
121         like($eml2->body, qr/BEST MSG/, 'edited body in #2');
122         unlike($eml2->body, qr/test message/, 'old body discarded in #2');
123         my $eml1 = $es->smsg_eml($msg1);
124         like($eml1->body, qr/test message/, 'original body in #1');
125         my $x1 = $es->over->get_xref3(1);
126         my $x2 = $es->over->get_xref3(2);
127         is(scalar(@$x1), 1, 'original only has one xref3');
128         is(scalar(@$x2), 1, 'new message has one xref3');
129         isnt($x1->[0], $x2->[0], 'xref3 differs');
130
131         my $mset = $es->mset('b:"BEST MSG"');
132         is($mset->size, 1, 'new message found');
133         $mset = $es->mset('b:"test message"');
134         is($mset->size, 1, 'old message found');
135         delete @$es{qw(git over xdb)}; # fork preparation
136
137         my $pi_cfg = PublicInbox::Config->new;
138         $pi_cfg->fill_all;
139         is(scalar($pi_cfg->ALL->mset('s:Testing')->items), 2,
140                 '2 results in ->ALL');
141         my $res = {};
142         my $nr = 0;
143         $pi_cfg->each_inbox(sub {
144                 $nr++;
145                 my ($ibx) = @_;
146                 local $SIG{__WARN__} = sub {}; # FIXME support --reindex
147                 my $mset = $ibx->isrch->mset('s:Testing');
148                 $res->{$ibx->eidx_key} = $ibx->isrch->mset_to_smsg($ibx, $mset);
149         });
150         is($nr, 2, 'two inboxes');
151         my $exp = {};
152         for my $v (qw(v1 v2)) {
153                 my $ibx = $pi_cfg->lookup_newsgroup("$v.example");
154                 my $smsg = $ibx->over->get_art(1);
155                 $smsg->psgi_cull;
156                 $exp->{"$v.example"} = [ $smsg ];
157         }
158         is_deeply($res, $exp, 'isearch limited results');
159         $pi_cfg = $res = $exp = undef;
160
161         open my $rmfh, '+>', undef or BAIL_OUT $!;
162         $rmfh->autoflush(1);
163         print $rmfh $eml2->as_string or BAIL_OUT $!;
164         seek($rmfh, 0, SEEK_SET) or BAIL_OUT $!;
165         $opt->{0} = $rmfh;
166         ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm');
167
168         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef),
169                 'extindex after rm');
170         is($es->over->get_art(2), undef, 'doc #2 gone');
171         $mset = $es->mset('b:"BEST MSG"');
172         is($mset->size, 0, 'new message gone');
173 }
174
175 my $misc = $es->misc;
176 my @it = $misc->mset('')->items;
177 is(scalar(@it), 2, 'two inboxes');
178 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
179 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
180
181 my $cfg = PublicInbox::Config->new;
182 my $schema_version = PublicInbox::Search::SCHEMA_VERSION();
183 my $f = "$home/extindex/ei$schema_version/over.sqlite3";
184 my $oidx = PublicInbox::OverIdx->new($f);
185 if ('inject w/o indexing') {
186         use PublicInbox::Import;
187         my $v1ibx = $cfg->lookup_name('v1test');
188         my $last_v1_commit = $v1ibx->mm->last_commit;
189         my $v2ibx = $cfg->lookup_name('v2test');
190         my $last_v2_commit = $v2ibx->mm->last_commit_xap($schema_version, 0);
191         my $git0 = PublicInbox::Git->new("$v2ibx->{inboxdir}/git/0.git");
192         chomp(my $cmt = $git0->qx(qw(rev-parse HEAD^0)));
193         is($last_v2_commit, $cmt, 'v2 index up-to-date');
194
195         my $v2im = PublicInbox::Import->new($git0, undef, undef, $v2ibx);
196         $v2im->{lock_path} = undef;
197         $v2im->{path_type} = 'v2';
198         $v2im->add(eml_load('t/mda-mime.eml'));
199         $v2im->done;
200         chomp(my $tip = $git0->qx(qw(rev-parse HEAD^0)));
201         isnt($tip, $cmt, '0.git v2 updated');
202
203         # inject a message w/o updating index
204         rename("$home/v1test/public-inbox", "$home/v1test/skip-index") or
205                 BAIL_OUT $!;
206         open(my $eh, '<', 't/iso-2202-jp.eml') or BAIL_OUT $!;
207         run_script(['-mda', '--no-precheck'], $env, { 0 => $eh}) or
208                 BAIL_OUT '-mda';
209         rename("$home/v1test/skip-index", "$home/v1test/public-inbox") or
210                 BAIL_OUT $!;
211
212         my ($in, $out, $err);
213         $in = $out = $err = '';
214         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
215         ok(run_script([qw(-extindex -v -v --all), "$home/extindex"],
216                 undef, undef), 'extindex noop');
217         $es->{xdb}->reopen;
218         my $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
219         is($mset->size, 0, 'did not attempt to index unindexed v1 message');
220         $mset = $es->mset('mid:multipart-html-sucks@11');
221         is($mset->size, 0, 'did not attempt to index unindexed v2 message');
222         ok(run_script([qw(-index --all)]), 'indexed v1 and v2 inboxes');
223
224         isnt($v1ibx->mm->last_commit, $last_v1_commit, '-index v1 worked');
225         isnt($v2ibx->mm->last_commit_xap($schema_version, 0),
226                 $last_v2_commit, '-index v2 worked');
227         ok(run_script([qw(-extindex --all), "$home/extindex"]),
228                 'extindex updates');
229
230         $es->{xdb}->reopen;
231         $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
232         is($mset->size, 1, 'got v1 message');
233         $mset = $es->mset('mid:multipart-html-sucks@11');
234         is($mset->size, 1, 'got v2 message');
235 }
236
237 if ('reindex catches missed messages') {
238         my $v2ibx = $cfg->lookup_name('v2test');
239         my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
240         my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
241         my $eml = eml_load('t/data/0001.patch');
242         $im->add($eml);
243         $im->done;
244         my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
245         isnt($cmt_a, $cmt_b, 'v2 0.git HEAD updated');
246         $oidx->dbh;
247         my $uv = $v2ibx->uidvalidity;
248         my $lc_key = "lc-v2:v2.example//$uv;0";
249         is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
250                 'update lc-v2 meta, old is as expected');
251         my $max = $oidx->max;
252         $oidx->dbh_close;
253         ok(run_script([qw(-extindex), "$home/extindex", $v2ibx->{inboxdir}]),
254                 '-extindex noop');
255         is($oidx->max, $max, '->max unchanged');
256         is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 unchanged');
257         $oidx->dbh_close;
258         my $opt = { 2 => \(my $err = '') };
259         ok(run_script([qw(-extindex --reindex), "$home/extindex",
260                         $v2ibx->{inboxdir}], undef, $opt),
261                         '--reindex for unseen');
262         is($oidx->max, $max + 1, '->max bumped');
263         is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 stays unchanged');
264         my @err = split(/^/, $err);
265         is(scalar(@err), 1, 'only one warning') or diag "err=$err";
266         like($err[0], qr/I: reindex_unseen/, 'got reindex_unseen message');
267         my $new = $oidx->get_art($max + 1);
268         is($new->{subject}, $eml->header('Subject'), 'new message added');
269
270         $es->{xdb}->reopen;
271         my $mset = $es->mset("mid:$new->{mid}");
272         is($mset->size, 1, 'previously unseen, now indexed in Xapian');
273
274         ok($im->remove($eml), 'remove new message from v2 inbox');
275         $im->done;
276         my $cmt_c = $v2ibx->mm->last_commit_xap($schema_version, 0);
277         is($oidx->eidx_meta($lc_key, $cmt_c), $cmt_b,
278                 'bump lc-v2 meta again to skip v2 remove');
279         $err = '';
280         $oidx->dbh_close;
281         ok(run_script([qw(-extindex --reindex), "$home/extindex",
282                         $v2ibx->{inboxdir}], undef, $opt),
283                         '--reindex for stale');
284         @err = split(/^/, $err);
285         is(scalar(@err), 1, 'only one warning') or diag "err=$err";
286         like($err[0], qr/\(#$new->{num}\): stale/, 'got stale message warning');
287         is($oidx->get_art($new->{num}), undef,
288                 'stale message gone from over');
289         is_deeply($oidx->get_xref3($new->{num}), [],
290                 'stale message has no xref3');
291         $es->{xdb}->reopen;
292         $mset = $es->mset("mid:$new->{mid}");
293         is($mset->size, 0, 'stale mid gone Xapian');
294 }
295
296 if ('reindex catches content bifurcation') {
297         use PublicInbox::MID qw(mids);
298         my $v2ibx = $cfg->lookup_name('v2test');
299         my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
300         my $eml = eml_load('t/data/message_embed.eml');
301         my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
302         $im->add($eml);
303         $im->done;
304         my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
305         my $uv = $v2ibx->uidvalidity;
306         my $lc_key = "lc-v2:v2.example//$uv;0";
307         $oidx->dbh;
308         is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
309                 'update lc-v2 meta, old is as expected');
310         my $mid = mids($eml)->[0];
311         my $smsg = $v2ibx->over->next_by_mid($mid, \(my $id), \(my $prev));
312         my $oldmax = $oidx->max;
313         my $x3_orig = $oidx->get_xref3(3);
314         is(scalar(@$x3_orig), 1, '#3 has one xref');
315         $oidx->add_xref3(3, $smsg->{num}, $smsg->{blob}, 'v2.example');
316         my $x3 = $oidx->get_xref3(3);
317         is(scalar(@$x3), 2, 'injected xref3');
318         $oidx->commit_lazy;
319         my $opt = { 2 => \(my $err = '') };
320         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
321                 'extindex --all is noop');
322         is($err, '', 'no warnings in index');
323         $oidx->dbh;
324         is($oidx->max, $oldmax, 'oidx->max unchanged');
325         $oidx->dbh_close;
326         ok(run_script([qw(-extindex --reindex --all), "$home/extindex"],
327                 undef, $opt), 'extindex --reindex');
328         $oidx->dbh;
329         ok($oidx->max > $oldmax, 'oidx->max bumped');
330         like($err, qr/split into 2 due to deduplication change/,
331                 'bifurcation noted');
332         my $added = $oidx->get_art($oidx->max);
333         is($added->{blob}, $smsg->{blob}, 'new blob indexed');
334         is_deeply(["v2.example:$smsg->{num}:$smsg->{blob}"],
335                 $oidx->get_xref3($added->{num}),
336                 'xref3 corrected for bifurcated message');
337         is_deeply($oidx->get_xref3(3), $x3_orig, 'xref3 restored for #3');
338 }
339
340 if ('--reindex --rethread') {
341         my $before = $oidx->dbh->selectrow_array(<<'');
342 SELECT MAX(tid) FROM over WHERE num > 0
343
344         my $opt = {};
345         ok(run_script([qw(-extindex --reindex --rethread --all),
346                         "$home/extindex"], undef, $opt),
347                         '--rethread');
348         my $after = $oidx->dbh->selectrow_array(<<'');
349 SELECT MIN(tid) FROM over WHERE num > 0
350
351         # actual rethread logic is identical to v1/v2 and tested elsewhere
352         ok($after > $before, '--rethread updates MIN(tid)');
353 }
354
355 if ('remove v1test and test gc') {
356         xsys([qw(git config --unset publicinbox.v1test.inboxdir)],
357                 { GIT_CONFIG => $cfg_path });
358         my $opt = { 2 => \(my $err = '') };
359         ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $opt),
360                 'extindex --gc');
361         like($err, qr/^I: remove #1 v1\.example /ms, 'removed v1 message');
362         is(scalar(grep(!/^I:/, split(/^/m, $err))), 0,
363                 'no non-informational messages');
364         $misc->{xdb}->reopen;
365         @it = $misc->mset('')->items;
366         is(scalar(@it), 1, 'only one inbox left');
367 }
368
369 done_testing;