2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use PublicInbox::Config;
8 use PublicInbox::InboxWritable;
11 require_mods(qw(json DBD::SQLite Search::Xapian));
12 require PublicInbox::Search;
13 use_ok 'PublicInbox::ExtSearch';
14 use_ok 'PublicInbox::ExtSearchIdx';
15 use_ok 'PublicInbox::OverIdx';
16 my $sock = tcp_server();
17 my $host_port = tcp_host_port($sock);
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 $!;
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');
35 $eml->header_set('List-Id', '<v2.example.com>');
36 open($fh, '+>', undef) or BAIL_OUT $!;
38 print $fh $eml->as_string or BAIL_OUT $!;
39 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
41 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
43 ok(run_script([qw(-init -V1 v1test --newsgroup v1.example), "$home/v1test",
44 'http://example.com/v1test', $v1addr ]), 'v1test init');
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 $!;
52 $env = { ORIGINAL_RECIPIENT => $v1addr };
53 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
55 run_script([qw(-index -Lbasic), "$home/v1test"]) or BAIL_OUT "index $?";
57 ok(run_script([qw(-extindex --all), "$home/extindex"]), 'extindex init');
59 my $es = PublicInbox::ExtSearch->new("$home/extindex");
60 ok($es->has_threadid, '->has_threadid');
64 xsys([qw(git config publicinbox.v1test.boost), 10],
65 { GIT_CONFIG => $cfg_path });
66 ok(run_script([qw(-extindex --all), "$home/extindex-b"]),
67 'extindex init with boost');
68 my $es = PublicInbox::ExtSearch->new("$home/extindex-b");
69 my $smsg = $es->over->get_art(1);
70 ok($smsg, 'got first article');
71 my $xref3 = $es->over->get_xref3($smsg->{num});
72 my @v1 = grep(/\Av1/, @$xref3);
73 my @v2 = grep(/\Av2/, @$xref3);
74 like($v1[0], qr/\Av1\.example.*?\b\Q$smsg->{blob}\E\b/,
75 'smsg->{blob} respected boost');
76 is(scalar(@$xref3), 2, 'only to entries');
79 xsys([qw(git config publicinbox.v2test.boost), 20],
80 { GIT_CONFIG => $cfg_path });
81 ok(run_script([qw(-extindex --all --reindex), "$home/extindex-b"]),
82 'extindex --reindex with altered boost');
84 $es = PublicInbox::ExtSearch->new("$home/extindex-b");
85 $smsg = $es->over->get_art(1);
86 like($v2[0], qr/\Av2\.example.*?\b\Q$smsg->{blob}\E\b/,
87 'smsg->{blob} respects boost after reindex');
89 xsys([qw(git config --unset publicinbox.v1test.boost)],
90 { GIT_CONFIG => $cfg_path });
91 xsys([qw(git config --unset publicinbox.v2test.boost)],
92 { GIT_CONFIG => $cfg_path });
95 { # TODO: -extindex should write this to config
96 open $fh, '>>', $cfg_path or BAIL_OUT $!;
97 print $fh <<EOF or BAIL_OUT $!;
100 topdir = $home/extindex
102 close $fh or BAIL_OUT $!;
104 my $pi_cfg = PublicInbox::Config->new;
106 ok($pi_cfg->ALL, '->ALL');
107 my $ibx = $pi_cfg->{-by_newsgroup}->{'v2.example'};
108 my $ret = $pi_cfg->ALL->nntp_xref_for($ibx, $ibx->over->get_art(1));
109 is_deeply($ret, { 'v1.example' => 1, 'v2.example' => 1 },
114 require_mods(qw(Net::NNTP), 1);
115 my ($out, $err) = ("$home/nntpd.out.log", "$home/nntpd.err.log");
116 my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
117 my $td = start_script($cmd, undef, { 3 => $sock });
118 my $n = Net::NNTP->new($host_port);
119 my @xp = $n->xpath('<testmessage@example.com>');
120 is_deeply(\@xp, [ qw(v1.example/1 v2.example/1) ]);
121 $n->group('v1.example');
122 my $res = $n->head(1);
123 @$res = grep(/^Xref: /, @$res);
124 like($res->[0], qr/ v1\.example:1 v2\.example:1/, 'nntp_xref works');
127 my $es = PublicInbox::ExtSearch->new("$home/extindex");
129 my $smsg = $es->over->get_art(1);
130 ok($smsg, 'got first article');
131 is($es->over->get_art(2), undef, 'only one added');
132 my $xref3 = $es->over->get_xref3(1);
133 like($xref3->[0], qr/\A\Qv2.example\E:1:/, 'order preserved 1');
134 like($xref3->[1], qr/\A\Qv1.example\E:1:/, 'order preserved 2');
135 is(scalar(@$xref3), 2, 'only to entries');
138 if ('inbox edited') {
139 my ($in, $out, $err);
140 $in = $out = $err = '';
141 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
142 my $env = { MAIL_EDITOR => "$^X -i -p -e 's/test message/BEST MSG/'" };
143 my $cmd = [ qw(-edit -Ft/utf8.eml), "$home/v2test" ];
144 ok(run_script($cmd, $env, $opt), '-edit');
145 ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
147 like($err, qr/discontiguous range/, 'warned about discontiguous range');
148 my $msg1 = $es->over->get_art(1) or BAIL_OUT 'msg1 missing';
149 my $msg2 = $es->over->get_art(2) or BAIL_OUT 'msg2 missing';
150 is($msg1->{mid}, $msg2->{mid}, 'edited message indexed');
151 isnt($msg1->{blob}, $msg2->{blob}, 'blobs differ');
152 my $eml2 = $es->smsg_eml($msg2);
153 like($eml2->body, qr/BEST MSG/, 'edited body in #2');
154 unlike($eml2->body, qr/test message/, 'old body discarded in #2');
155 my $eml1 = $es->smsg_eml($msg1);
156 like($eml1->body, qr/test message/, 'original body in #1');
157 my $x1 = $es->over->get_xref3(1);
158 my $x2 = $es->over->get_xref3(2);
159 is(scalar(@$x1), 1, 'original only has one xref3');
160 is(scalar(@$x2), 1, 'new message has one xref3');
161 isnt($x1->[0], $x2->[0], 'xref3 differs');
163 my $mset = $es->mset('b:"BEST MSG"');
164 is($mset->size, 1, 'new message found');
165 $mset = $es->mset('b:"test message"');
166 is($mset->size, 1, 'old message found');
167 delete @$es{qw(git over xdb)}; # fork preparation
169 my $pi_cfg = PublicInbox::Config->new;
171 is(scalar($pi_cfg->ALL->mset('s:Testing')->items), 2,
172 '2 results in ->ALL');
175 $pi_cfg->each_inbox(sub {
178 local $SIG{__WARN__} = sub {}; # FIXME support --reindex
179 my $mset = $ibx->isrch->mset('s:Testing');
180 $res->{$ibx->eidx_key} = $ibx->isrch->mset_to_smsg($ibx, $mset);
182 is($nr, 2, 'two inboxes');
184 for my $v (qw(v1 v2)) {
185 my $ibx = $pi_cfg->lookup_newsgroup("$v.example");
186 my $smsg = $ibx->over->get_art(1);
188 $exp->{"$v.example"} = [ $smsg ];
190 is_deeply($res, $exp, 'isearch limited results');
191 $pi_cfg = $res = $exp = undef;
193 open my $rmfh, '+>', undef or BAIL_OUT $!;
195 print $rmfh $eml2->as_string or BAIL_OUT $!;
196 seek($rmfh, 0, SEEK_SET) or BAIL_OUT $!;
198 ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm');
200 ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef),
201 'extindex after rm');
202 is($es->over->get_art(2), undef, 'doc #2 gone');
203 $mset = $es->mset('b:"BEST MSG"');
204 is($mset->size, 0, 'new message gone');
207 my $misc = $es->misc;
208 my @it = $misc->mset('')->items;
209 is(scalar(@it), 2, 'two inboxes');
210 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
211 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
213 my $cfg = PublicInbox::Config->new;
214 my $schema_version = PublicInbox::Search::SCHEMA_VERSION();
215 my $f = "$home/extindex/ei$schema_version/over.sqlite3";
216 my $oidx = PublicInbox::OverIdx->new($f);
217 if ('inject w/o indexing') {
218 use PublicInbox::Import;
219 my $v1ibx = $cfg->lookup_name('v1test');
220 my $last_v1_commit = $v1ibx->mm->last_commit;
221 my $v2ibx = $cfg->lookup_name('v2test');
222 my $last_v2_commit = $v2ibx->mm->last_commit_xap($schema_version, 0);
223 my $git0 = PublicInbox::Git->new("$v2ibx->{inboxdir}/git/0.git");
224 chomp(my $cmt = $git0->qx(qw(rev-parse HEAD^0)));
225 is($last_v2_commit, $cmt, 'v2 index up-to-date');
227 my $v2im = PublicInbox::Import->new($git0, undef, undef, $v2ibx);
228 $v2im->{lock_path} = undef;
229 $v2im->{path_type} = 'v2';
230 $v2im->add(eml_load('t/mda-mime.eml'));
232 chomp(my $tip = $git0->qx(qw(rev-parse HEAD^0)));
233 isnt($tip, $cmt, '0.git v2 updated');
235 # inject a message w/o updating index
236 rename("$home/v1test/public-inbox", "$home/v1test/skip-index") or
238 open(my $eh, '<', 't/iso-2202-jp.eml') or BAIL_OUT $!;
239 run_script(['-mda', '--no-precheck'], $env, { 0 => $eh}) or
241 rename("$home/v1test/skip-index", "$home/v1test/public-inbox") or
244 my ($in, $out, $err);
245 $in = $out = $err = '';
246 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
247 ok(run_script([qw(-extindex -v -v --all), "$home/extindex"],
248 undef, undef), 'extindex noop');
250 my $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
251 is($mset->size, 0, 'did not attempt to index unindexed v1 message');
252 $mset = $es->mset('mid:multipart-html-sucks@11');
253 is($mset->size, 0, 'did not attempt to index unindexed v2 message');
254 ok(run_script([qw(-index --all)]), 'indexed v1 and v2 inboxes');
256 isnt($v1ibx->mm->last_commit, $last_v1_commit, '-index v1 worked');
257 isnt($v2ibx->mm->last_commit_xap($schema_version, 0),
258 $last_v2_commit, '-index v2 worked');
259 ok(run_script([qw(-extindex --all), "$home/extindex"]),
263 $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
264 is($mset->size, 1, 'got v1 message');
265 $mset = $es->mset('mid:multipart-html-sucks@11');
266 is($mset->size, 1, 'got v2 message');
269 if ('reindex catches missed messages') {
270 my $v2ibx = $cfg->lookup_name('v2test');
271 $v2ibx->{-no_fsync} = 1;
272 my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
273 my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
274 my $eml = eml_load('t/data/0001.patch');
277 my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
278 isnt($cmt_a, $cmt_b, 'v2 0.git HEAD updated');
280 my $uv = $v2ibx->uidvalidity;
281 my $lc_key = "lc-v2:v2.example//$uv;0";
282 is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
283 'update lc-v2 meta, old is as expected');
284 my $max = $oidx->max;
286 ok(run_script([qw(-extindex), "$home/extindex", $v2ibx->{inboxdir}]),
288 is($oidx->max, $max, '->max unchanged');
289 is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 unchanged');
291 my $opt = { 2 => \(my $err = '') };
292 ok(run_script([qw(-extindex --reindex), "$home/extindex",
293 $v2ibx->{inboxdir}], undef, $opt),
294 '--reindex for unseen');
295 is($oidx->max, $max + 1, '->max bumped');
296 is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 stays unchanged');
297 my @err = split(/^/, $err);
298 is(scalar(@err), 1, 'only one warning') or diag "err=$err";
299 like($err[0], qr/I: reindex_unseen/, 'got reindex_unseen message');
300 my $new = $oidx->get_art($max + 1);
301 is($new->{subject}, $eml->header('Subject'), 'new message added');
304 my $mset = $es->mset("mid:$new->{mid}");
305 is($mset->size, 1, 'previously unseen, now indexed in Xapian');
307 ok($im->remove($eml), 'remove new message from v2 inbox');
309 my $cmt_c = $v2ibx->mm->last_commit_xap($schema_version, 0);
310 is($oidx->eidx_meta($lc_key, $cmt_c), $cmt_b,
311 'bump lc-v2 meta again to skip v2 remove');
314 ok(run_script([qw(-extindex --reindex), "$home/extindex",
315 $v2ibx->{inboxdir}], undef, $opt),
316 '--reindex for stale');
317 @err = split(/^/, $err);
318 is(scalar(@err), 1, 'only one warning') or diag "err=$err";
319 like($err[0], qr/\(#$new->{num}\): stale/, 'got stale message warning');
320 is($oidx->get_art($new->{num}), undef,
321 'stale message gone from over');
322 is_deeply($oidx->get_xref3($new->{num}), [],
323 'stale message has no xref3');
325 $mset = $es->mset("mid:$new->{mid}");
326 is($mset->size, 0, 'stale mid gone Xapian');
329 if ('reindex catches content bifurcation') {
330 use PublicInbox::MID qw(mids);
331 my $v2ibx = $cfg->lookup_name('v2test');
332 $v2ibx->{-no_fsync} = 1;
333 my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
334 my $eml = eml_load('t/data/message_embed.eml');
335 my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
338 my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
339 my $uv = $v2ibx->uidvalidity;
340 my $lc_key = "lc-v2:v2.example//$uv;0";
342 is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
343 'update lc-v2 meta, old is as expected');
344 my $mid = mids($eml)->[0];
345 my $smsg = $v2ibx->over->next_by_mid($mid, \(my $id), \(my $prev));
346 my $oldmax = $oidx->max;
347 my $x3_orig = $oidx->get_xref3(3);
348 is(scalar(@$x3_orig), 1, '#3 has one xref');
349 $oidx->add_xref3(3, $smsg->{num}, $smsg->{blob}, 'v2.example');
350 my $x3 = $oidx->get_xref3(3);
351 is(scalar(@$x3), 2, 'injected xref3');
353 my $opt = { 2 => \(my $err = '') };
354 ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
355 'extindex --all is noop');
356 is($err, '', 'no warnings in index');
358 is($oidx->max, $oldmax, 'oidx->max unchanged');
360 ok(run_script([qw(-extindex --reindex --all), "$home/extindex"],
361 undef, $opt), 'extindex --reindex');
363 ok($oidx->max > $oldmax, 'oidx->max bumped');
364 like($err, qr/split into 2 due to deduplication change/,
365 'bifurcation noted');
366 my $added = $oidx->get_art($oidx->max);
367 is($added->{blob}, $smsg->{blob}, 'new blob indexed');
368 is_deeply(["v2.example:$smsg->{num}:$smsg->{blob}"],
369 $oidx->get_xref3($added->{num}),
370 'xref3 corrected for bifurcated message');
371 is_deeply($oidx->get_xref3(3), $x3_orig, 'xref3 restored for #3');
374 if ('--reindex --rethread') {
375 my $before = $oidx->dbh->selectrow_array(<<'');
376 SELECT MAX(tid) FROM over WHERE num > 0
379 ok(run_script([qw(-extindex --reindex --rethread --all),
380 "$home/extindex"], undef, $opt),
382 my $after = $oidx->dbh->selectrow_array(<<'');
383 SELECT MIN(tid) FROM over WHERE num > 0
385 # actual rethread logic is identical to v1/v2 and tested elsewhere
386 ok($after > $before, '--rethread updates MIN(tid)');
389 if ('remove v1test and test gc') {
390 xsys([qw(git config --unset publicinbox.v1test.inboxdir)],
391 { GIT_CONFIG => $cfg_path });
392 my $opt = { 2 => \(my $err = '') };
393 ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $opt),
395 like($err, qr/^I: remove #1 v1\.example /ms, 'removed v1 message');
396 is(scalar(grep(!/^I:/, split(/^/m, $err))), 0,
397 'no non-informational messages');
398 $misc->{xdb}->reopen;
399 @it = $misc->mset('')->items;
400 is(scalar(@it), 1, 'only one inbox left');
403 if ('dedupe + dry-run') {
404 my @cmd = ('-extindex', "$home/extindex");
405 my $opt = { 2 => \(my $err = '') };
406 ok(run_script([@cmd, '--dedupe'], undef, $opt), '--dedupe');
407 ok(run_script([@cmd, qw(--dedupe --dry-run)], undef, $opt),
408 '--dry-run --dedupe');
409 is $err, '', 'no errors';
410 ok(!run_script([@cmd, qw(--dry-run)], undef, $opt),
411 '--dry-run alone fails');