]> Sergey Matveev's repositories - public-inbox.git/blob - t/extsearch.t
dfc190e2d69e8a3d2628361a62db45d1102c86f2
[public-inbox.git] / t / extsearch.t
1 #!perl -w
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>
4 use strict;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Config;
8 use PublicInbox::InboxWritable;
9 use Fcntl qw(:seek);
10 require_git(2.6);
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 $!;
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 if ('with boost') {
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');
77         undef $es;
78
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');
83
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');
88
89         # high boost added later
90         my $b2 = "$home/extindex-bb";
91         ok(run_script([qw(-extindex), $b2, "$home/v1test"]),
92                 'extindex with low boost inbox only');
93         ok(run_script([qw(-extindex), $b2, "$home/v2test"]),
94                 'extindex with high boost inbox only');
95         $es = PublicInbox::ExtSearch->new($b2);
96         $smsg = $es->over->get_art(1);
97         $xref3 = $es->over->get_xref3($smsg->{num});
98         like($v2[0], qr/\Av2\.example.*?\b\Q$smsg->{blob}\E\b/,
99                 'smsg->{blob} respected boost across 2 index runs');
100
101         xsys([qw(git config --unset publicinbox.v1test.boost)],
102                 { GIT_CONFIG => $cfg_path });
103         xsys([qw(git config --unset publicinbox.v2test.boost)],
104                 { GIT_CONFIG => $cfg_path });
105 }
106
107 { # TODO: -extindex should write this to config
108         open $fh, '>>', $cfg_path or BAIL_OUT $!;
109         print $fh <<EOF or BAIL_OUT $!;
110 ; for ->ALL
111 [extindex "all"]
112         topdir = $home/extindex
113 EOF
114         close $fh or BAIL_OUT $!;
115
116         my $pi_cfg = PublicInbox::Config->new;
117         $pi_cfg->fill_all;
118         ok($pi_cfg->ALL, '->ALL');
119         my $ibx = $pi_cfg->{-by_newsgroup}->{'v2.example'};
120         my $ret = $pi_cfg->ALL->nntp_xref_for($ibx, $ibx->over->get_art(1));
121         is_deeply($ret, { 'v1.example' => 1, 'v2.example' => 1 },
122                 '->nntp_xref_for');
123 }
124
125 SKIP: {
126         require_mods(qw(Net::NNTP), 1);
127         my ($out, $err) = ("$home/nntpd.out.log", "$home/nntpd.err.log");
128         my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
129         my $td = start_script($cmd, undef, { 3 => $sock });
130         my $n = Net::NNTP->new($host_port);
131         my @xp = $n->xpath('<testmessage@example.com>');
132         is_deeply(\@xp, [ qw(v1.example/1 v2.example/1) ]);
133         $n->group('v1.example');
134         my $res = $n->head(1);
135         @$res = grep(/^Xref: /, @$res);
136         like($res->[0], qr/ v1\.example:1 v2\.example:1/, 'nntp_xref works');
137 }
138
139 my $es = PublicInbox::ExtSearch->new("$home/extindex");
140 {
141         my $smsg = $es->over->get_art(1);
142         ok($smsg, 'got first article');
143         is($es->over->get_art(2), undef, 'only one added');
144         my $xref3 = $es->over->get_xref3(1);
145         like($xref3->[0], qr/\A\Qv2.example\E:1:/, 'order preserved 1');
146         like($xref3->[1], qr/\A\Qv1.example\E:1:/, 'order preserved 2');
147         is(scalar(@$xref3), 2, 'only to entries');
148 }
149
150 if ('inbox edited') {
151         my ($in, $out, $err);
152         $in = $out = $err = '';
153         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
154         my $env = { MAIL_EDITOR => "$^X -i -p -e 's/test message/BEST MSG/'" };
155         my $cmd = [ qw(-edit -Ft/utf8.eml), "$home/v2test" ];
156         ok(run_script($cmd, $env, $opt), '-edit');
157         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
158                 'extindex again');
159         like($err, qr/discontiguous range/, 'warned about discontiguous range');
160         my $msg1 = $es->over->get_art(1) or BAIL_OUT 'msg1 missing';
161         my $msg2 = $es->over->get_art(2) or BAIL_OUT 'msg2 missing';
162         is($msg1->{mid}, $msg2->{mid}, 'edited message indexed');
163         isnt($msg1->{blob}, $msg2->{blob}, 'blobs differ');
164         my $eml2 = $es->smsg_eml($msg2);
165         like($eml2->body, qr/BEST MSG/, 'edited body in #2');
166         unlike($eml2->body, qr/test message/, 'old body discarded in #2');
167         my $eml1 = $es->smsg_eml($msg1);
168         like($eml1->body, qr/test message/, 'original body in #1');
169         my $x1 = $es->over->get_xref3(1);
170         my $x2 = $es->over->get_xref3(2);
171         is(scalar(@$x1), 1, 'original only has one xref3');
172         is(scalar(@$x2), 1, 'new message has one xref3');
173         isnt($x1->[0], $x2->[0], 'xref3 differs');
174
175         my $mset = $es->mset('b:"BEST MSG"');
176         is($mset->size, 1, 'new message found');
177         $mset = $es->mset('b:"test message"');
178         is($mset->size, 1, 'old message found');
179         delete @$es{qw(git over xdb qp)}; # fork preparation
180
181         my $pi_cfg = PublicInbox::Config->new;
182         $pi_cfg->fill_all;
183         is(scalar($pi_cfg->ALL->mset('s:Testing')->items), 2,
184                 '2 results in ->ALL');
185         my $res = {};
186         my $nr = 0;
187         $pi_cfg->each_inbox(sub {
188                 $nr++;
189                 my ($ibx) = @_;
190                 local $SIG{__WARN__} = sub {}; # FIXME support --reindex
191                 my $mset = $ibx->isrch->mset('s:Testing');
192                 $res->{$ibx->eidx_key} = $ibx->isrch->mset_to_smsg($ibx, $mset);
193         });
194         is($nr, 2, 'two inboxes');
195         my $exp = {};
196         for my $v (qw(v1 v2)) {
197                 my $ibx = $pi_cfg->lookup_newsgroup("$v.example");
198                 my $smsg = $ibx->over->get_art(1);
199                 $smsg->psgi_cull;
200                 $exp->{"$v.example"} = [ $smsg ];
201         }
202         is_deeply($res, $exp, 'isearch limited results');
203         $pi_cfg = $res = $exp = undef;
204
205         open my $rmfh, '+>', undef or BAIL_OUT $!;
206         $rmfh->autoflush(1);
207         print $rmfh $eml2->as_string or BAIL_OUT $!;
208         seek($rmfh, 0, SEEK_SET) or BAIL_OUT $!;
209         $opt->{0} = $rmfh;
210         ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm');
211
212         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef),
213                 'extindex after rm');
214         is($es->over->get_art(2), undef, 'doc #2 gone');
215         $mset = $es->mset('b:"BEST MSG"');
216         is($mset->size, 0, 'new message gone');
217 }
218
219 my $misc = $es->misc;
220 my @it = $misc->mset('')->items;
221 is(scalar(@it), 2, 'two inboxes');
222 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
223 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
224
225 my $cfg = PublicInbox::Config->new;
226 my $schema_version = PublicInbox::Search::SCHEMA_VERSION();
227 my $f = "$home/extindex/ei$schema_version/over.sqlite3";
228 my $oidx = PublicInbox::OverIdx->new($f);
229 if ('inject w/o indexing') {
230         use PublicInbox::Import;
231         my $v1ibx = $cfg->lookup_name('v1test');
232         my $last_v1_commit = $v1ibx->mm->last_commit;
233         my $v2ibx = $cfg->lookup_name('v2test');
234         my $last_v2_commit = $v2ibx->mm->last_commit_xap($schema_version, 0);
235         my $git0 = PublicInbox::Git->new("$v2ibx->{inboxdir}/git/0.git");
236         chomp(my $cmt = $git0->qx(qw(rev-parse HEAD^0)));
237         is($last_v2_commit, $cmt, 'v2 index up-to-date');
238
239         my $v2im = PublicInbox::Import->new($git0, undef, undef, $v2ibx);
240         $v2im->{lock_path} = undef;
241         $v2im->{path_type} = 'v2';
242         $v2im->add(eml_load('t/mda-mime.eml'));
243         $v2im->done;
244         chomp(my $tip = $git0->qx(qw(rev-parse HEAD^0)));
245         isnt($tip, $cmt, '0.git v2 updated');
246
247         # inject a message w/o updating index
248         rename("$home/v1test/public-inbox", "$home/v1test/skip-index") or
249                 BAIL_OUT $!;
250         open(my $eh, '<', 't/iso-2202-jp.eml') or BAIL_OUT $!;
251         run_script(['-mda', '--no-precheck'], $env, { 0 => $eh}) or
252                 BAIL_OUT '-mda';
253         rename("$home/v1test/skip-index", "$home/v1test/public-inbox") or
254                 BAIL_OUT $!;
255
256         my ($in, $out, $err);
257         $in = $out = $err = '';
258         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
259         ok(run_script([qw(-extindex -v -v --all), "$home/extindex"],
260                 undef, undef), 'extindex noop');
261         $es->{xdb}->reopen;
262         my $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
263         is($mset->size, 0, 'did not attempt to index unindexed v1 message');
264         $mset = $es->mset('mid:multipart-html-sucks@11');
265         is($mset->size, 0, 'did not attempt to index unindexed v2 message');
266         ok(run_script([qw(-index --all)]), 'indexed v1 and v2 inboxes');
267
268         isnt($v1ibx->mm->last_commit, $last_v1_commit, '-index v1 worked');
269         isnt($v2ibx->mm->last_commit_xap($schema_version, 0),
270                 $last_v2_commit, '-index v2 worked');
271         ok(run_script([qw(-extindex --all), "$home/extindex"]),
272                 'extindex updates');
273
274         $es->{xdb}->reopen;
275         $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
276         is($mset->size, 1, 'got v1 message');
277         $mset = $es->mset('mid:multipart-html-sucks@11');
278         is($mset->size, 1, 'got v2 message');
279 }
280
281 if ('reindex catches missed messages') {
282         my $v2ibx = $cfg->lookup_name('v2test');
283         $v2ibx->{-no_fsync} = 1;
284         my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
285         my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
286         my $eml = eml_load('t/data/0001.patch');
287         $im->add($eml);
288         $im->done;
289         my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
290         isnt($cmt_a, $cmt_b, 'v2 0.git HEAD updated');
291         $oidx->dbh;
292         my $uv = $v2ibx->uidvalidity;
293         my $lc_key = "lc-v2:v2.example//$uv;0";
294         is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
295                 'update lc-v2 meta, old is as expected');
296         my $max = $oidx->max;
297         $oidx->dbh_close;
298         ok(run_script([qw(-extindex), "$home/extindex", $v2ibx->{inboxdir}]),
299                 '-extindex noop');
300         is($oidx->max, $max, '->max unchanged');
301         is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 unchanged');
302         $oidx->dbh_close;
303         my $opt = { 2 => \(my $err = '') };
304         ok(run_script([qw(-extindex --reindex), "$home/extindex",
305                         $v2ibx->{inboxdir}], undef, $opt),
306                         '--reindex for unseen');
307         is($oidx->max, $max + 1, '->max bumped');
308         is($oidx->eidx_meta($lc_key), $cmt_b, 'lc-v2 stays unchanged');
309         my @err = split(/^/, $err);
310         is(scalar(@err), 1, 'only one warning') or diag "err=$err";
311         like($err[0], qr/I: reindex_unseen/, 'got reindex_unseen message');
312         my $new = $oidx->get_art($max + 1);
313         is($new->{subject}, $eml->header('Subject'), 'new message added');
314
315         $es->{xdb}->reopen;
316         my $mset = $es->mset("mid:$new->{mid}");
317         is($mset->size, 1, 'previously unseen, now indexed in Xapian');
318
319         ok($im->remove($eml), 'remove new message from v2 inbox');
320         $im->done;
321         my $cmt_c = $v2ibx->mm->last_commit_xap($schema_version, 0);
322         is($oidx->eidx_meta($lc_key, $cmt_c), $cmt_b,
323                 'bump lc-v2 meta again to skip v2 remove');
324         $err = '';
325         $oidx->dbh_close;
326         ok(run_script([qw(-extindex --reindex), "$home/extindex",
327                         $v2ibx->{inboxdir}], undef, $opt),
328                         '--reindex for stale');
329         @err = split(/^/, $err);
330         is(scalar(@err), 1, 'only one warning') or diag "err=$err";
331         like($err[0], qr/\(#$new->{num}\): stale/, 'got stale message warning');
332         is($oidx->get_art($new->{num}), undef,
333                 'stale message gone from over');
334         is_deeply($oidx->get_xref3($new->{num}), [],
335                 'stale message has no xref3');
336         $es->{xdb}->reopen;
337         $mset = $es->mset("mid:$new->{mid}");
338         is($mset->size, 0, 'stale mid gone Xapian');
339
340         ok(run_script([qw(-extindex --reindex --all --fast), "$home/extindex"],
341                         undef, $opt), '--reindex w/ --fast');
342         ok(!run_script([qw(-extindex --all --fast), "$home/extindex"],
343                         undef, $opt), '--fast alone makes no sense');
344 }
345
346 if ('reindex catches content bifurcation') {
347         use PublicInbox::MID qw(mids);
348         my $v2ibx = $cfg->lookup_name('v2test');
349         $v2ibx->{-no_fsync} = 1;
350         my $im = PublicInbox::InboxWritable->new($v2ibx)->importer(0);
351         my $eml = eml_load('t/data/message_embed.eml');
352         my $cmt_a = $v2ibx->mm->last_commit_xap($schema_version, 0);
353         $im->add($eml);
354         $im->done;
355         my $cmt_b = $v2ibx->mm->last_commit_xap($schema_version, 0);
356         my $uv = $v2ibx->uidvalidity;
357         my $lc_key = "lc-v2:v2.example//$uv;0";
358         $oidx->dbh;
359         is($oidx->eidx_meta($lc_key, $cmt_b), $cmt_a,
360                 'update lc-v2 meta, old is as expected');
361         my $mid = mids($eml)->[0];
362         my $smsg = $v2ibx->over->next_by_mid($mid, \(my $id), \(my $prev));
363         my $oldmax = $oidx->max;
364         my $x3_orig = $oidx->get_xref3(3);
365         is(scalar(@$x3_orig), 1, '#3 has one xref');
366         $oidx->add_xref3(3, $smsg->{num}, $smsg->{blob}, 'v2.example');
367         my $x3 = $oidx->get_xref3(3);
368         is(scalar(@$x3), 2, 'injected xref3');
369         $oidx->commit_lazy;
370         my $opt = { 2 => \(my $err = '') };
371         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
372                 'extindex --all is noop');
373         is($err, '', 'no warnings in index');
374         $oidx->dbh;
375         is($oidx->max, $oldmax, 'oidx->max unchanged');
376         $oidx->dbh_close;
377         ok(run_script([qw(-extindex --reindex --all), "$home/extindex"],
378                 undef, $opt), 'extindex --reindex') or diag explain($opt);
379         $oidx->dbh;
380         ok($oidx->max > $oldmax, 'oidx->max bumped');
381         like($err, qr/split into 2 due to deduplication change/,
382                 'bifurcation noted');
383         my $added = $oidx->get_art($oidx->max);
384         is($added->{blob}, $smsg->{blob}, 'new blob indexed');
385         is_deeply(["v2.example:$smsg->{num}:$smsg->{blob}"],
386                 $oidx->get_xref3($added->{num}),
387                 'xref3 corrected for bifurcated message');
388         is_deeply($oidx->get_xref3(3), $x3_orig, 'xref3 restored for #3');
389 }
390
391 if ('--reindex --rethread') {
392         my $before = $oidx->dbh->selectrow_array(<<'');
393 SELECT MAX(tid) FROM over WHERE num > 0
394
395         my $opt = {};
396         ok(run_script([qw(-extindex --reindex --rethread --all),
397                         "$home/extindex"], undef, $opt),
398                         '--rethread');
399         my $after = $oidx->dbh->selectrow_array(<<'');
400 SELECT MIN(tid) FROM over WHERE num > 0
401
402         # actual rethread logic is identical to v1/v2 and tested elsewhere
403         ok($after > $before, '--rethread updates MIN(tid)');
404 }
405
406 if ('remove v1test and test gc') {
407         xsys([qw(git config --unset publicinbox.v1test.inboxdir)],
408                 { GIT_CONFIG => $cfg_path });
409         my $opt = { 2 => \(my $err = '') };
410         ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $opt),
411                 'extindex --gc');
412         like($err, qr/^I: remove #1 v1\.example /ms, 'removed v1 message');
413         is(scalar(grep(!/^I:/, split(/^/m, $err))), 0,
414                 'no non-informational messages');
415         $misc->{xdb}->reopen;
416         @it = $misc->mset('')->items;
417         is(scalar(@it), 1, 'only one inbox left');
418 }
419
420 if ('dedupe + dry-run') {
421         my @cmd = ('-extindex', "$home/extindex");
422         my $opt = { 2 => \(my $err = '') };
423         ok(run_script([@cmd, '--dedupe'], undef, $opt), '--dedupe');
424         ok(run_script([@cmd, qw(--dedupe --dry-run)], undef, $opt),
425                 '--dry-run --dedupe');
426         is $err, '', 'no errors';
427         ok(!run_script([@cmd, qw(--dry-run)], undef, $opt),
428                 '--dry-run alone fails');
429 }
430
431 # chmod 0755, $home or xbail "chmod: $!";
432 for my $j (1, 3, 6) {
433         my $o = { 2 => \(my $err = '') };
434         my $d = "$home/extindex-j$j";
435         ok(run_script(['-extindex', "-j$j", '--all', $d], undef, $o),
436                 "init with -j$j");
437         my $max = $j - 2;
438         $max = 0 if $max < 0;
439         my @dirs = glob("$d/ei*/?");
440         like($dirs[-1], qr!/ei[0-9]+/$max\z!, '-j works');
441 }
442
443 SKIP: {
444         my $d = "$home/extindex-j1";
445         my $es = PublicInbox::ExtSearch->new($d);
446         ok(my $nresult0 = $es->mset('z:0..')->size, 'got results');
447         ok(ref($es->{xdb}), '{xdb} created');
448         my $nshards1 = $es->{nshard};
449         is($nshards1, 1, 'correct shard count');
450
451         my @ei_dir = glob("$d/ei*/");
452         chmod 0755, $ei_dir[0] or xbail "chmod: $!";
453         my $mode = sprintf('%04o', 07777 & (stat($ei_dir[0]))[2]);
454         is($mode, '0755', 'mode set on ei*/ dir');
455         my $o = { 2 => \(my $err = '') };
456         ok(run_script([qw(-xcpdb -R4), $d]), 'xcpdb R4');
457         my @dirs = glob("$d/ei*/?");
458         for my $i (0..3) {
459                 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 1, "shard [$i] created");
460                 my $m = sprintf('%04o', 07777 & (stat($dirs[$i]))[2]);
461                 is($m, $mode, "shard [$i] mode");
462         }
463         delete @$es{qw(xdb qp)};
464         is($es->mset('z:0..')->size, $nresult0, 'new shards, same results');
465
466         for my $i (4..5) {
467                 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 0, "no shard [$i]");
468         }
469
470         ok(run_script([qw(-xcpdb -R2), $d]), 'xcpdb -R2');
471         @dirs = glob("$d/ei*/?");
472         for my $i (0..1) {
473                 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 1, "shard [$i] kept");
474         }
475         for my $i (2..3) {
476                 is(grep(m!/ei[0-9]+/$i\z!, @dirs), 0, "no shard [$i]");
477         }
478         skip 'xapian-compact missing', 4 unless have_xapian_compact;
479         ok(run_script([qw(-compact), $d], undef, $o), 'compact');
480         # n.b. stderr contains xapian-compact output
481
482         my @d2 = glob("$d/ei*/?");
483         is_deeply(\@d2, \@dirs, 'dirs consistent after compact');
484         ok(run_script([qw(-extindex --dedupe --all), $d]),
485                 '--dedupe works after compact');
486         ok(run_script([qw(-extindex --gc), $d], undef, $o),
487                 '--gc works after compact');
488 }
489
490 { # ensure --gc removes non-xposted messages
491         my $old_size = -s $cfg_path // xbail "stat $cfg_path $!";
492         my $tmp_addr = 'v2tmp@example.com';
493         run_script([qw(-init v2tmp --indexlevel basic
494                 --newsgroup v2tmp.example),
495                 "$home/v2tmp", 'http://example.com/v2tmp', $tmp_addr ])
496                 or xbail '-init';
497         $env = { ORIGINAL_RECIPIENT => $tmp_addr };
498         open $fh, '+>', undef or xbail "open $!";
499         $fh->autoflush(1);
500         my $mid = 'tmpmsg@example.com';
501         print $fh <<EOM or xbail "print $!";
502 From: b\@z
503 To: b\@r
504 Message-Id: <$mid>
505 Subject: tmpmsg
506 Date: Tue, 19 Jan 2038 03:14:07 +0000
507
508 EOM
509         seek $fh, 0, SEEK_SET or xbail "seek $!";
510         run_script([qw(-mda --no-precheck)], $env, {0 => $fh}) or xbail '-mda';
511         ok(run_script([qw(-extindex --all), "$home/extindex"]), 'update');
512         my $nr;
513         {
514                 my $es = PublicInbox::ExtSearch->new("$home/extindex");
515                 my ($id, $prv);
516                 my $smsg = $es->over->next_by_mid($mid, \$id, \$prv);
517                 ok($smsg, 'tmpmsg indexed');
518                 my $mset = $es->search->mset("mid:$mid");
519                 is($mset->size, 1, 'new message found');
520                 $mset = $es->search->mset('z:0..');
521                 $nr = $mset->size;
522         }
523         truncate($cfg_path, $old_size) or xbail "truncate $!";
524         my $rdr = { 2 => \(my $err) };
525         ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $rdr),
526                 'gc to get rid of removed inbox');
527         is_deeply([ grep(!/^(?:I:|#)/, split(/^/m, $err)) ], [],
528                 'no non-informational errors in stderr');
529
530         my $es = PublicInbox::ExtSearch->new("$home/extindex");
531         my $mset = $es->search->mset("mid:$mid");
532         is($mset->size, 0, 'tmpmsg gone from search');
533         my ($id, $prv);
534         is($es->over->next_by_mid($mid, \$id, \$prv), undef,
535                 'tmpmsg gone from over');
536         $id = $prv = undef;
537         is($es->over->next_by_mid('testmessage@example.com', \$id, \$prv),
538                 undef, 'remaining message not indavderover');
539         $mset = $es->search->mset('z:0..');
540         is($mset->size, $nr - 1, 'existing messages not clobbered from search');
541         my $o = $es->over->{dbh}->selectall_arrayref(<<EOM);
542 SELECT num FROM over ORDER BY num
543 EOM
544         is(scalar(@$o), $mset->size, 'over row count matches Xapian');
545         my $x = $es->over->{dbh}->selectall_arrayref(<<EOM);
546 SELECT DISTINCT(docid) FROM xref3 ORDER BY docid
547 EOM
548         is_deeply($x, $o, 'xref3 and over docids match');
549 }
550
551 done_testing;