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 File::Temp qw/tempdir/;
9 require './t/common.perl';
11 my $this = (split('/', __FILE__))[-1];
13 # TODO: remove Search::Xapian as a requirement for basic
14 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
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 $cls = "PublicInbox::V${v}Writable";
44 my $im = $cls->new($ibx, {nproc=>1});
45 $mime->header_set('Message-ID', '<m@1>');
46 ok($im->add($mime), 'first message added');
49 # index master (required for v1)
50 is(system($index, $ibx->{mainrepo}), 0, 'index master OK');
51 my $ro_master = PublicInbox::Inbox->new({mainrepo => $ibx->{mainrepo}});
52 my ($nr, $msgs) = $ro_master->recent;
53 is($nr, 1, 'only one message in master, so far');
54 is($msgs->[0]->{mid}, 'm@1', 'first message in master indexed');
57 my @cmd = (qw(git clone --mirror -q));
58 my $mirror = "$tmpdir/mirror-$v";
60 push @cmd, $ibx->{mainrepo}, $mirror;
62 push @cmd, "$ibx->{mainrepo}/git/0.git", "$mirror/git/0.git";
64 my $fetch_dir = $cmd[-1];
65 is(system(@cmd), 0, "v$v clone OK");
68 local $ENV{PI_CONFIG} = "$tmpdir/.picfg";
69 @cmd = ("$path/public-inbox-init", '-L', $level,
70 'mirror', $mirror, '//example.com/test', 'test@example.com');
71 push @cmd, '-V2' if $v == 2;
72 is(system(@cmd), 0, "v$v init OK");
75 is(system($index, $mirror), 0, "v$v index mirror OK");
78 my $ro_mirror = PublicInbox::Inbox->new({mainrepo => $mirror});
79 ($nr, $msgs) = $ro_mirror->recent;
80 is($nr, 1, 'only one message, so far');
81 is($msgs->[0]->{mid}, 'm@1', 'read first message');
84 $mime->header_set('Message-ID', '<m@2>');
85 ok($im->add($mime), '2nd message added');
89 is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
90 is(system($index, $mirror), 0, "v$v index mirror again OK");
91 ($nr, $msgs) = $ro_mirror->recent;
92 is($nr, 2, '2nd message seen in mirror');
93 is_deeply([sort { $a cmp $b } map { $_->{mid} } @$msgs],
94 ['m@1','m@2'], 'got both messages in mirror');
96 # incremental index master (required for v1)
97 is(system($index, $ibx->{mainrepo}), 0, 'index master OK');
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 # remove message from master
104 ok($im->remove($mime), '2nd message removed');
108 is(system('git', "--git-dir=$fetch_dir", qw(fetch -q)), 0, 'fetch OK');
109 is(system($index, $mirror), 0, "v$v index mirror again OK");
110 ($nr, $msgs) = $ro_mirror->recent;
111 is($nr, 1, '2nd message gone from mirror');
112 is_deeply([map { $_->{mid} } @$msgs], ['m@1'],
113 'message unavailable in mirror');
116 # we can probably cull some other tests and put full/medium tests, here
117 for my $level (qw(basic)) {
119 subtest("v$v indexlevel=$level" => sub {
120 import_index_incremental($v, $level);