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