2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
8 use PublicInbox::Inbox;
9 require PublicInbox::Admin;
10 my $PI_TEST_VERSION = $ENV{PI_TEST_VERSION} || 2;
11 require_git('2.6') if $PI_TEST_VERSION == 2;
12 require_mods(qw(DBD::SQLite));
14 my $mime = PublicInbox::Eml->new(<<'EOF');
17 Subject: this is a subject
18 Date: Fri, 02 Oct 1993 00:00:00 +0000
23 my $import_index_incremental = sub {
24 my ($v, $level, $mime) = @_;
26 my $this = "pi-$v-$level-indexlevels";
27 my ($tmpdir, $for_destroy) = tmpdir();
28 my $ibx = create_inbox "testbox$v", indexlevel => $level,
29 version => $v, tmpdir => "$tmpdir/v$v", sub {
30 $mime->header_set('Message-ID', '<m@1>');
31 $_[0]->add($mime) or BAIL_OUT;
33 my $im = $ibx->importer(0);
34 local $ENV{PI_CONFIG} = "$tmpdir/config";
36 # index master (required for v1)
37 my @cmd = (qw(-index -j0 --dangerous), $ibx->{inboxdir}, "-L$level");
38 push @cmd, '-c' if have_xapian_compact;
39 ok(run_script(\@cmd, undef, { 2 => \$err }), 'index master');
40 my $ro_master = PublicInbox::Inbox->new({
41 inboxdir => $ibx->{inboxdir},
44 my $msgs = $ro_master->over->recent;
45 is(scalar(@$msgs), 1, 'only one message in master, so far');
46 is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
49 @cmd = (qw(git clone --mirror -q));
50 my $mirror = "$tmpdir/mirror-$v";
52 push @cmd, $ibx->{inboxdir}, $mirror;
54 push @cmd, "$ibx->{inboxdir}/git/0.git", "$mirror/git/0.git";
56 my $fetch_dir = $cmd[-1];
57 is(xsys(@cmd), 0, "v$v clone OK");
60 local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
61 @cmd = ('-init', '-L', $level,
62 'mirror', $mirror, '//example.com/test', 'test@example.com');
63 push @cmd, '-V2' if $v == 2;
64 ok(run_script(\@cmd), "v$v init OK");
67 ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror OK");
70 my $ro_mirror = PublicInbox::Inbox->new({
74 $msgs = $ro_mirror->over->recent;
75 is(scalar(@$msgs), 1, 'only one message, so far');
76 is($msgs->[0]->{mid}, 'm@1', 'read first message');
79 $mime->header_set('Message-ID', '<m@2>');
80 ok($im->add($mime), '2nd message added');
84 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
85 ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror again OK");
86 $msgs = $ro_mirror->over->recent;
87 is(scalar(@$msgs), 2, '2nd message seen in mirror');
88 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
89 ['m@1','m@2'], 'got both messages in mirror');
91 # incremental index master (required for v1)
92 ok(run_script([qw(-index -j0), $ibx->{inboxdir}, "-L$level"]),
94 $msgs = $ro_master->over->recent;
95 is(scalar(@$msgs), 2, '2nd message seen in master');
96 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
97 ['m@1','m@2'], 'got both messages in master');
99 my @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
100 is_deeply(\@rw_nums, [1, 2], 'master has expected NNTP articles');
102 my @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
103 is_deeply(\@ro_nums, [1, 2], 'mirror has expected NNTP articles');
105 # remove message from master
106 ok($im->remove($mime), '2nd message removed');
108 @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
109 is_deeply(\@rw_nums, [1], 'unindex NNTP article'.$v.$level);
111 if ($level ne 'basic') {
112 ok(run_script(['-xcpdb', '-q', $mirror]), "v$v xcpdb OK");
113 is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
114 'indexlevel detectable by Admin after xcpdb v' .$v.$level);
115 delete $ro_mirror->{$_} for (qw(over search));
116 my $mset = $ro_mirror->search->mset('m:m@2');
117 is($mset->size, 1, "v$v found m\@2 via Xapian on $level");
121 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
122 ok(run_script([qw(-index -j0), $mirror]), "v$v index mirror again OK");
123 $msgs = $ro_mirror->over->recent;
124 is(scalar(@$msgs), 1, '2nd message gone from mirror');
125 is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
126 'message unavailable in mirror');
128 if ($v == 2 && $level eq 'basic') {
129 is_deeply([glob("$ibx->{inboxdir}/xap*/?/")], [],
130 'no Xapian shard directories for v2 basic');
132 if ($level ne 'basic') {
133 my $mset = $ro_mirror->search->reopen->mset('m:m@2');
135 "v$v m\@2 gone from Xapian in mirror on $level");
138 # add another message to master and have the mirror
139 # sync and reindex it
140 my @expect = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
141 foreach my $i (3..5) {
142 $mime->header_set('Message-ID', "<m\@$i>");
143 ok($im->add($mime), "#$i message added");
147 is(xsys('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
148 ok(run_script([qw(-index -j0 --reindex), $mirror]),
149 "v$v index --reindex mirror OK");
150 @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
151 @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
152 is_deeply(\@rw_nums, \@expect, "v$v master has expected NNTP articles");
153 is_deeply(\@ro_nums, \@expect, "v$v mirror matches master articles");
155 is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
156 'indexlevel detectable by Admin '.$v.$level);
159 skip 'xapian-compact missing', 1 if !have_xapian_compact;
160 my $cmd = [ qw(-compact), $mirror ];
161 ok(run_script($cmd, undef, { 2 => \$err}), "compact $level")
166 # we can probably cull some other tests
167 $import_index_incremental->($PI_TEST_VERSION, 'basic', $mime);
170 require_mods(qw(Search::Xapian), 2);
171 foreach my $l (qw(medium full)) {
172 $import_index_incremental->($PI_TEST_VERSION, $l, $mime);