1 # Copyright (C) 2019-2021 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 require PublicInbox::Admin;
10 use PublicInbox::TestCommon;
11 my $PI_TEST_VERSION = $ENV{PI_TEST_VERSION} || 2;
12 require_git('2.6') if $PI_TEST_VERSION == 2;
13 require_mods(qw(DBD::SQLite));
15 my $mime = PublicInbox::Eml->new(<<'EOF');
18 Subject: this is a subject
19 Date: Fri, 02 Oct 1993 00:00:00 +0000
24 my $import_index_incremental = sub {
25 my ($v, $level, $mime) = @_;
27 my $this = "pi-$v-$level-indexlevels";
28 my ($tmpdir, $for_destroy) = tmpdir();
29 local $ENV{PI_CONFIG} = "$tmpdir/config";
30 my $ibx = PublicInbox::Inbox->new({
31 inboxdir => "$tmpdir/testbox",
34 -primary_address => 'test@example.com',
37 my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer(0);
38 $mime->header_set('Message-ID', '<m@1>');
39 ok($im->add($mime), 'first message added');
42 # index master (required for v1)
43 my @cmd = (qw(-index -j0), $ibx->{inboxdir}, "-L$level");
44 push @cmd, '-c' if have_xapian_compact;
45 ok(run_script(\@cmd, undef, { 2 => \$err }), 'index master');
46 my $ro_master = PublicInbox::Inbox->new({
47 inboxdir => $ibx->{inboxdir},
50 my $msgs = $ro_master->recent;
51 is(scalar(@$msgs), 1, 'only one message in master, so far');
52 is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
55 @cmd = (qw(git clone --mirror -q));
56 my $mirror = "$tmpdir/mirror-$v";
58 push @cmd, $ibx->{inboxdir}, $mirror;
60 push @cmd, "$ibx->{inboxdir}/git/0.git", "$mirror/git/0.git";
62 my $fetch_dir = $cmd[-1];
63 is(xsys(@cmd), 0, "v$v clone OK");
66 local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
67 @cmd = ('-init', '-L', $level,
68 'mirror', $mirror, '//example.com/test', 'test@example.com');
69 push @cmd, '-V2' if $v == 2;
70 ok(run_script(\@cmd), "v$v init OK");
73 ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror OK");
76 my $ro_mirror = PublicInbox::Inbox->new({
80 $msgs = $ro_mirror->recent;
81 is(scalar(@$msgs), 1, 'only one message, so far');
82 is($msgs->[0]->{mid}, 'm@1', 'read first message');
85 $mime->header_set('Message-ID', '<m@2>');
86 ok($im->add($mime), '2nd message added');
90 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
91 ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror again OK");
92 $msgs = $ro_mirror->recent;
93 is(scalar(@$msgs), 2, '2nd message seen in mirror');
94 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
95 ['m@1','m@2'], 'got both messages in mirror');
97 # incremental index master (required for v1)
98 ok(run_script([qw(-index -j0), $ibx->{inboxdir}, "-L$level"]),
100 $msgs = $ro_master->recent;
101 is(scalar(@$msgs), 2, '2nd message seen in master');
102 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
103 ['m@1','m@2'], 'got both messages in master');
105 my @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
106 is_deeply(\@rw_nums, [1, 2], 'master has expected NNTP articles');
108 my @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
109 is_deeply(\@ro_nums, [1, 2], 'mirror has expected NNTP articles');
111 # remove message from master
112 ok($im->remove($mime), '2nd message removed');
114 @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
115 is_deeply(\@rw_nums, [1], 'unindex NNTP article'.$v.$level);
117 if ($level ne 'basic') {
118 ok(run_script(['-xcpdb', '-q', $mirror]), "v$v xcpdb OK");
119 is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
120 'indexlevel detectable by Admin after xcpdb v' .$v.$level);
121 delete $ro_mirror->{$_} for (qw(over search));
122 my $mset = $ro_mirror->search->mset('m:m@2');
123 is($mset->size, 1, "v$v found m\@2 via Xapian on $level");
127 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
128 ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror again OK");
129 $msgs = $ro_mirror->recent;
130 is(scalar(@$msgs), 1, '2nd message gone from mirror');
131 is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
132 'message unavailable in mirror');
134 if ($v == 2 && $level eq 'basic') {
135 is_deeply([glob("$ibx->{inboxdir}/xap*/?/")], [],
136 'no Xapian shard directories for v2 basic');
138 if ($level ne 'basic') {
139 my $mset = $ro_mirror->search->reopen->mset('m:m@2');
141 "v$v m\@2 gone from Xapian in mirror on $level");
144 # add another message to master and have the mirror
145 # sync and reindex it
146 my @expect = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
147 foreach my $i (3..5) {
148 $mime->header_set('Message-ID', "<m\@$i>");
149 ok($im->add($mime), "#$i message added");
153 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
154 ok(run_script([qw(-index -j0 --reindex), $mirror]),
155 "v$v index --reindex mirror OK");
156 @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
157 @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
158 is_deeply(\@rw_nums, \@expect, "v$v master has expected NNTP articles");
159 is_deeply(\@ro_nums, \@expect, "v$v mirror matches master articles");
161 is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
162 'indexlevel detectable by Admin '.$v.$level);
165 skip 'xapian-compact missing', 1 if !have_xapian_compact;
166 my $cmd = [ qw(-compact), $mirror ];
167 ok(run_script($cmd, undef, { 2 => \$err}), "compact $level");
171 # we can probably cull some other tests
172 $import_index_incremental->($PI_TEST_VERSION, 'basic', $mime);
175 require_mods(qw(Search::Xapian), 2);
176 foreach my $l (qw(medium full)) {
177 $import_index_incremental->($PI_TEST_VERSION, $l, $mime);