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";
22 my $mime = PublicInbox::MIME->create(
24 From => 'a@example.com',
25 To => 'test@example.com',
26 Subject => 'this is a subject',
27 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
29 body => "hello world\n",
32 sub import_index_incremental {
34 my $tmpdir = tempdir("pi-$this-tmp-XXXXXX", TMPDIR => 1, CLEANUP => 1);
35 my $ibx = PublicInbox::Inbox->new({
36 mainrepo => "$tmpdir/testbox",
39 -primary_address => 'test@example.com',
42 my $im = PublicInbox::InboxWritable->new($ibx, {nproc=>1})->importer;
43 $mime->header_set('Message-ID', '<m@1>');
44 ok($im->add($mime), 'first message added');
47 # index master (required for v1)
48 is(system($index, $ibx->{mainrepo}, "-L$level"), 0, 'index master OK');
49 my $ro_master = PublicInbox::Inbox->new({
50 mainrepo => $ibx->{mainrepo},
53 my ($nr, $msgs) = $ro_master->recent;
54 is($nr, 1, 'only one message in master, so far');
55 is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
58 my @cmd = (qw(git clone --mirror -q));
59 my $mirror = "$tmpdir/mirror-$v";
61 push @cmd, $ibx->{mainrepo}, $mirror;
63 push @cmd, "$ibx->{mainrepo}/git/0.git", "$mirror/git/0.git";
65 my $fetch_dir = $cmd[-1];
66 is(system(@cmd), 0, "v$v clone OK");
69 local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
70 @cmd = ("$path/public-inbox-init", '-L', $level,
71 'mirror', $mirror, '//example.com/test', 'test@example.com');
72 push @cmd, '-V2' if $v == 2;
73 is(system(@cmd), 0, "v$v init OK");
76 is(system($index, $mirror), 0, "v$v index mirror OK");
79 my $ro_mirror = PublicInbox::Inbox->new({
83 ($nr, $msgs) = $ro_mirror->recent;
84 is($nr, 1, 'only one message, so far');
85 is($msgs->[0]->{mid}, 'm@1', 'read first message');
88 $mime->header_set('Message-ID', '<m@2>');
89 ok($im->add($mime), '2nd message added');
93 is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
94 is(system($index, $mirror), 0, "v$v index mirror again OK");
95 ($nr, $msgs) = $ro_mirror->recent;
96 is($nr, 2, '2nd message seen in mirror');
97 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
98 ['m@1','m@2'], 'got both messages in mirror');
100 # incremental index master (required for v1)
101 is(system($index, $ibx->{mainrepo}, "-L$level"), 0, 'index master OK');
102 ($nr, $msgs) = $ro_master->recent;
103 is($nr, 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 # remove message from master
108 ok($im->remove($mime), '2nd message removed');
112 is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
113 is(system($index, $mirror), 0, "v$v index mirror again OK");
114 ($nr, $msgs) = $ro_mirror->recent;
115 is($nr, 1, '2nd message gone from mirror');
116 is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
117 'message unavailable in mirror');
119 if ($v == 2 && $level eq 'basic') {
120 is_deeply([glob("$ibx->{mainrepo}/xap*/?/")], [],
121 'no Xapian partition directories for v2 basic');
125 # we can probably cull some other tests and put full/medium tests, here
126 for my $level (qw(basic)) {
128 subtest("v$v indexlevel=$level" => sub {
129 import_index_incremental($v, $level);