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 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::MIME->new(<<'EOF');
18 Subject: this is a subject
19 Date: Fri, 02 Oct 1993 00:00:00 +0000
24 sub import_index_incremental {
25 my ($v, $level, $mime) = @_;
26 my $this = "pi-$v-$level-indexlevels";
27 my ($tmpdir, $for_destroy) = tmpdir();
28 local $ENV{PI_CONFIG} = "$tmpdir/config";
29 my $ibx = PublicInbox::Inbox->new({
30 inboxdir => "$tmpdir/testbox",
33 -primary_address => 'test@example.com',
36 my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer;
37 $mime->header_set('Message-ID', '<m@1>');
38 ok($im->add($mime), 'first message added');
41 # index master (required for v1)
42 ok(run_script(['-index', $ibx->{inboxdir}, "-L$level"]),
44 my $ro_master = PublicInbox::Inbox->new({
45 inboxdir => $ibx->{inboxdir},
48 my ($nr, $msgs) = $ro_master->recent;
49 is($nr, 1, 'only one message in master, so far');
50 is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
53 my @cmd = (qw(git clone --mirror -q));
54 my $mirror = "$tmpdir/mirror-$v";
56 push @cmd, $ibx->{inboxdir}, $mirror;
58 push @cmd, "$ibx->{inboxdir}/git/0.git", "$mirror/git/0.git";
60 my $fetch_dir = $cmd[-1];
61 is(xsys(@cmd), 0, "v$v clone OK");
64 local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
65 @cmd = ('-init', '-L', $level,
66 'mirror', $mirror, '//example.com/test', 'test@example.com');
67 push @cmd, '-V2' if $v == 2;
68 ok(run_script(\@cmd), "v$v init OK");
71 ok(run_script(['-index', $mirror]), "v$v index mirror OK");
74 my $ro_mirror = PublicInbox::Inbox->new({
78 ($nr, $msgs) = $ro_mirror->recent;
79 is($nr, 1, 'only one message, so far');
80 is($msgs->[0]->{mid}, 'm@1', 'read first message');
83 $mime->header_set('Message-ID', '<m@2>');
84 ok($im->add($mime), '2nd message added');
88 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
89 ok(run_script(['-index', $mirror]), "v$v index mirror again OK");
90 ($nr, $msgs) = $ro_mirror->recent;
91 is($nr, 2, '2nd message seen in mirror');
92 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
93 ['m@1','m@2'], 'got both messages in mirror');
95 # incremental index master (required for v1)
96 ok(run_script(['-index', $ibx->{inboxdir}, "-L$level"]),
98 ($nr, $msgs) = $ro_master->recent;
99 is($nr, 2, '2nd message seen in master');
100 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
101 ['m@1','m@2'], 'got both messages in master');
103 my @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
104 is_deeply(\@rw_nums, [1, 2], 'master has expected NNTP articles');
106 my @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
107 is_deeply(\@ro_nums, [1, 2], 'mirror has expected NNTP articles');
109 # remove message from master
110 ok($im->remove($mime), '2nd message removed');
112 @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
113 is_deeply(\@rw_nums, [1], 'unindex NNTP article'.$v.$level);
115 if ($level ne 'basic') {
116 ok(run_script(['-xcpdb', '-q', $mirror]), "v$v xcpdb OK");
117 is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
118 'indexlevel detectable by Admin after xcpdb v' .$v.$level);
119 delete $ro_mirror->{$_} for (qw(over search));
120 ($nr, $msgs) = $ro_mirror->search->query('m:m@2');
121 is($nr, 1, "v$v found m\@2 via Xapian on $level");
125 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
126 ok(run_script(['-index', $mirror]), "v$v index mirror again OK");
127 ($nr, $msgs) = $ro_mirror->recent;
128 is($nr, 1, '2nd message gone from mirror');
129 is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
130 'message unavailable in mirror');
132 if ($v == 2 && $level eq 'basic') {
133 is_deeply([glob("$ibx->{inboxdir}/xap*/?/")], [],
134 'no Xapian shard directories for v2 basic');
136 if ($level ne 'basic') {
137 ($nr, $msgs) = $ro_mirror->search->reopen->query('m:m@2');
138 is($nr, 0, "v$v m\@2 gone from Xapian in mirror on $level");
141 # add another message to master and have the mirror
142 # sync and reindex it
143 my @expect = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
144 foreach my $i (3..5) {
145 $mime->header_set('Message-ID', "<m\@$i>");
146 ok($im->add($mime), "#$i message added");
150 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
151 ok(run_script(['-index', '--reindex', $mirror]),
152 "v$v index --reindex mirror OK");
153 @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
154 @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
155 is_deeply(\@rw_nums, \@expect, "v$v master has expected NNTP articles");
156 is_deeply(\@ro_nums, \@expect, "v$v mirror matches master articles");
158 is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
159 'indexlevel detectable by Admin '.$v.$level);
162 # we can probably cull some other tests
163 import_index_incremental($PI_TEST_VERSION, 'basic', $mime);
166 require PublicInbox::Search;
167 PublicInbox::Search::load_xapian() or
168 skip('Xapian perl binding missing', 2);
169 foreach my $l (qw(medium full)) {
170 import_index_incremental($PI_TEST_VERSION, $l, $mime);