1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::Inbox;
8 use PublicInbox::InboxWritable;
9 use PublicInbox::Spawn qw(which);
10 require PublicInbox::Admin;
11 use PublicInbox::TestCommon;
12 my $PI_TEST_VERSION = $ENV{PI_TEST_VERSION} || 2;
13 require_git('2.6') if $PI_TEST_VERSION == 2;
14 require_mods(qw(DBD::SQLite));
15 my $have_xapian_compact = which($ENV{XAPIAN_COMPACT} || 'xapian-compact');
17 my $mime = PublicInbox::Eml->new(<<'EOF');
20 Subject: this is a subject
21 Date: Fri, 02 Oct 1993 00:00:00 +0000
26 my $import_index_incremental = sub {
27 my ($v, $level, $mime) = @_;
29 my $this = "pi-$v-$level-indexlevels";
30 my ($tmpdir, $for_destroy) = tmpdir();
31 local $ENV{PI_CONFIG} = "$tmpdir/config";
32 my $ibx = PublicInbox::Inbox->new({
33 inboxdir => "$tmpdir/testbox",
36 -primary_address => 'test@example.com',
39 my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer(0);
40 $mime->header_set('Message-ID', '<m@1>');
41 ok($im->add($mime), 'first message added');
44 # index master (required for v1)
45 my @cmd = (qw(-index -j0), $ibx->{inboxdir}, "-L$level");
46 push @cmd, '-c' if $have_xapian_compact;
47 ok(run_script(\@cmd, undef, { 2 => \$err }), 'index master');
48 my $ro_master = PublicInbox::Inbox->new({
49 inboxdir => $ibx->{inboxdir},
52 my $msgs = $ro_master->recent;
53 is(scalar(@$msgs), 1, 'only one message in master, so far');
54 is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
57 @cmd = (qw(git clone --mirror -q));
58 my $mirror = "$tmpdir/mirror-$v";
60 push @cmd, $ibx->{inboxdir}, $mirror;
62 push @cmd, "$ibx->{inboxdir}/git/0.git", "$mirror/git/0.git";
64 my $fetch_dir = $cmd[-1];
65 is(xsys(@cmd), 0, "v$v clone OK");
68 local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
69 @cmd = ('-init', '-L', $level,
70 'mirror', $mirror, '//example.com/test', 'test@example.com');
71 push @cmd, '-V2' if $v == 2;
72 ok(run_script(\@cmd), "v$v init OK");
75 ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror OK");
78 my $ro_mirror = PublicInbox::Inbox->new({
82 $msgs = $ro_mirror->recent;
83 is(scalar(@$msgs), 1, 'only one message, so far');
84 is($msgs->[0]->{mid}, 'm@1', 'read first message');
87 $mime->header_set('Message-ID', '<m@2>');
88 ok($im->add($mime), '2nd message added');
92 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
93 ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror again OK");
94 $msgs = $ro_mirror->recent;
95 is(scalar(@$msgs), 2, '2nd message seen in mirror');
96 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
97 ['m@1','m@2'], 'got both messages in mirror');
99 # incremental index master (required for v1)
100 ok(run_script([qw(-index -j0), $ibx->{inboxdir}, "-L$level"]),
102 $msgs = $ro_master->recent;
103 is(scalar(@$msgs), 2, '2nd message seen in master');
104 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
105 ['m@1','m@2'], 'got both messages in master');
107 my @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
108 is_deeply(\@rw_nums, [1, 2], 'master has expected NNTP articles');
110 my @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
111 is_deeply(\@ro_nums, [1, 2], 'mirror has expected NNTP articles');
113 # remove message from master
114 ok($im->remove($mime), '2nd message removed');
116 @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
117 is_deeply(\@rw_nums, [1], 'unindex NNTP article'.$v.$level);
119 if ($level ne 'basic') {
120 ok(run_script(['-xcpdb', '-q', $mirror]), "v$v xcpdb OK");
121 is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
122 'indexlevel detectable by Admin after xcpdb v' .$v.$level);
123 delete $ro_mirror->{$_} for (qw(over search));
124 my $mset = $ro_mirror->search->mset('m:m@2');
125 is($mset->size, 1, "v$v found m\@2 via Xapian on $level");
129 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
130 ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror again OK");
131 $msgs = $ro_mirror->recent;
132 is(scalar(@$msgs), 1, '2nd message gone from mirror');
133 is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
134 'message unavailable in mirror');
136 if ($v == 2 && $level eq 'basic') {
137 is_deeply([glob("$ibx->{inboxdir}/xap*/?/")], [],
138 'no Xapian shard directories for v2 basic');
140 if ($level ne 'basic') {
141 my $mset = $ro_mirror->search->reopen->mset('m:m@2');
143 "v$v m\@2 gone from Xapian in mirror on $level");
146 # add another message to master and have the mirror
147 # sync and reindex it
148 my @expect = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
149 foreach my $i (3..5) {
150 $mime->header_set('Message-ID', "<m\@$i>");
151 ok($im->add($mime), "#$i message added");
155 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
156 ok(run_script([qw(-index -j0 --reindex), $mirror]),
157 "v$v index --reindex mirror OK");
158 @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
159 @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
160 is_deeply(\@rw_nums, \@expect, "v$v master has expected NNTP articles");
161 is_deeply(\@ro_nums, \@expect, "v$v mirror matches master articles");
163 is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
164 'indexlevel detectable by Admin '.$v.$level);
167 skip 'xapian-compact missing', 1 if !$have_xapian_compact;
168 my $cmd = [ qw(-compact), $mirror ];
169 ok(run_script($cmd, undef, { 2 => \$err}), "compact $level");
173 # we can probably cull some other tests
174 $import_index_incremental->($PI_TEST_VERSION, 'basic', $mime);
177 require PublicInbox::Search;
178 PublicInbox::Search::load_xapian() or
179 skip('Xapian perl binding missing', 2);
180 foreach my $l (qw(medium full)) {
181 $import_index_incremental->($PI_TEST_VERSION, $l, $mime);