1 # Copyright (C) 2019 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 File::Temp qw/tempdir/;
10 require './t/common.perl';
12 my $this = (split('/', __FILE__))[-1];
14 foreach my $mod (qw(DBD::SQLite)) {
16 plan skip_all => "$mod missing for $this" if $@;
19 my $path = 'blib/script';
20 my $index = "$path/public-inbox-index";
21 my @xcpdb = ("$path/public-inbox-xcpdb", '-q');
23 my $mime = PublicInbox::MIME->create(
25 From => 'a@example.com',
26 To => 'test@example.com',
27 Subject => 'this is a subject',
28 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
30 body => "hello world\n",
33 sub import_index_incremental {
35 my $tmpdir = tempdir("pi-$this-tmp-XXXXXX", TMPDIR => 1, CLEANUP => 1);
36 my $ibx = PublicInbox::Inbox->new({
37 mainrepo => "$tmpdir/testbox",
40 -primary_address => 'test@example.com',
43 my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer;
44 $mime->header_set('Message-ID', '<m@1>');
45 ok($im->add($mime), 'first message added');
48 # index master (required for v1)
49 is(system($index, $ibx->{mainrepo}, "-L$level"), 0, 'index master OK');
50 my $ro_master = PublicInbox::Inbox->new({
51 mainrepo => $ibx->{mainrepo},
54 my ($nr, $msgs) = $ro_master->recent;
55 is($nr, 1, 'only one message in master, so far');
56 is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
59 my @cmd = (qw(git clone --mirror -q));
60 my $mirror = "$tmpdir/mirror-$v";
62 push @cmd, $ibx->{mainrepo}, $mirror;
64 push @cmd, "$ibx->{mainrepo}/git/0.git", "$mirror/git/0.git";
66 my $fetch_dir = $cmd[-1];
67 is(system(@cmd), 0, "v$v clone OK");
70 local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
71 @cmd = ("$path/public-inbox-init", '-L', $level,
72 'mirror', $mirror, '//example.com/test', 'test@example.com');
73 push @cmd, '-V2' if $v == 2;
74 is(system(@cmd), 0, "v$v init OK");
77 is(system($index, $mirror), 0, "v$v index mirror OK");
80 my $ro_mirror = PublicInbox::Inbox->new({
84 ($nr, $msgs) = $ro_mirror->recent;
85 is($nr, 1, 'only one message, so far');
86 is($msgs->[0]->{mid}, 'm@1', 'read first message');
89 $mime->header_set('Message-ID', '<m@2>');
90 ok($im->add($mime), '2nd message added');
94 is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
95 is(system($index, $mirror), 0, "v$v index mirror again OK");
96 ($nr, $msgs) = $ro_mirror->recent;
97 is($nr, 2, '2nd message seen in mirror');
98 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
99 ['m@1','m@2'], 'got both messages in mirror');
101 # incremental index master (required for v1)
102 is(system($index, $ibx->{mainrepo}, "-L$level"), 0, 'index master OK');
103 ($nr, $msgs) = $ro_master->recent;
104 is($nr, 2, '2nd message seen in master');
105 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
106 ['m@1','m@2'], 'got both messages in master');
108 my @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
109 is_deeply(\@rw_nums, [1, 2], 'master has expected NNTP articles');
111 my @ro_nums = map { $_->{num} } @{$ro_mirror->over->query_ts(0, 0)};
112 is_deeply(\@ro_nums, [1, 2], 'mirror has expected NNTP articles');
114 # remove message from master
115 ok($im->remove($mime), '2nd message removed');
117 @rw_nums = map { $_->{num} } @{$ibx->over->query_ts(0, 0)};
118 is_deeply(\@rw_nums, [1], 'unindex NNTP article'.$v.$level);
120 if ($level ne 'basic') {
121 is(system(@xcpdb, $mirror), 0, "v$v xcpdb OK");
122 delete $ro_mirror->{$_} for (qw(over search));
123 ($nr, $msgs) = $ro_mirror->search->query('m:m@2');
124 is($nr, 1, "v$v found m\@2 via Xapian on $level");
128 is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
129 is(system($index, $mirror), 0, "v$v index mirror again OK");
130 ($nr, $msgs) = $ro_mirror->recent;
131 is($nr, 1, '2nd message gone from mirror');
132 is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
133 'message unavailable in mirror');
135 if ($v == 2 && $level eq 'basic') {
136 is_deeply([glob("$ibx->{mainrepo}/xap*/?/")], [],
137 'no Xapian partition directories for v2 basic');
139 if ($level ne 'basic') {
140 ($nr, $msgs) = $ro_mirror->search->reopen->query('m:m@2');
141 is($nr, 0, "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(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
154 is(system($index, '--reindex', $mirror), 0,
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");
162 # we can probably cull some other tests and put full/medium tests, here
163 for my $level (qw(basic)) {
165 subtest("v$v indexlevel=$level" => sub {
166 import_index_incremental($v, $level);
172 require PublicInbox::Search;
173 PublicInbox::Search::load_xapian() or skip 'Search::Xapian missing', 2;
175 subtest("v$v indexlevel=medium" => sub {
176 import_index_incremental($v, 'medium');