2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use PublicInbox::Config;
9 my $json = PublicInbox::Config::json() or plan skip_all => 'JSON missing';
11 require_mods(qw(DBD::SQLite Search::Xapian));
12 use_ok 'PublicInbox::ExtSearch';
13 use_ok 'PublicInbox::ExtSearchIdx';
14 my $sock = tcp_server();
15 my $host_port = $sock->sockhost . ':' . $sock->sockport;
16 my ($home, $for_destroy) = tmpdir();
17 local $ENV{HOME} = $home;
18 mkdir "$home/.public-inbox" or BAIL_OUT $!;
19 my $cfg_path = "$home/.public-inbox/config";
20 open my $fh, '>', $cfg_path or BAIL_OUT $!;
21 print $fh <<EOF or BAIL_OUT $!;
25 close $fh or BAIL_OUT $!;
26 my $v2addr = 'v2test@example.com';
27 my $v1addr = 'v1test@example.com';
28 ok(run_script([qw(-init -V2 v2test --newsgroup v2.example), "$home/v2test",
29 'http://example.com/v2test', $v2addr ]), 'v2test init');
30 my $env = { ORIGINAL_RECIPIENT => $v2addr };
31 my $eml = eml_load('t/utf8.eml');
33 $eml->header_set('List-Id', '<v2.example.com>');
34 open($fh, '+>', undef) or BAIL_OUT $!;
36 print $fh $eml->as_string or BAIL_OUT $!;
37 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
39 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
41 ok(run_script([qw(-init -V1 v1test --newsgroup v1.example), "$home/v1test",
42 'http://example.com/v1test', $v1addr ]), 'v1test init');
44 $eml->header_set('List-Id', '<v1.example.com>');
45 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
46 truncate($fh, 0) or BAIL_OUT $!;
47 print $fh $eml->as_string or BAIL_OUT $!;
48 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
50 $env = { ORIGINAL_RECIPIENT => $v1addr };
51 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
53 run_script(['-index', "$home/v1test"]) or BAIL_OUT "index $?";
55 ok(run_script([qw(-extindex --all), "$home/extindex"]), 'extindex init');
57 my $es = PublicInbox::ExtSearch->new("$home/extindex");
58 ok($es->has_threadid, '->has_threadid');
61 { # TODO: -extindex should write this to config
62 open $fh, '>>', $cfg_path or BAIL_OUT $!;
63 print $fh <<EOF or BAIL_OUT $!;
66 topdir = $home/extindex
68 close $fh or BAIL_OUT $!;
70 my $pi_cfg = PublicInbox::Config->new;
72 ok($pi_cfg->ALL, '->ALL');
73 my $ibx = $pi_cfg->{-by_newsgroup}->{'v2.example'};
74 my $ret = $pi_cfg->ALL->nntp_xref_for($ibx, $ibx->over->get_art(1));
75 is_deeply($ret, { 'v1.example' => 1, 'v2.example' => 1 },
80 require_mods(qw(Net::NNTP), 1);
81 my ($out, $err) = ("$home/nntpd.out.log", "$home/nntpd.err.log");
82 my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
83 my $td = start_script($cmd, undef, { 3 => $sock });
84 my $n = Net::NNTP->new($host_port);
85 my @xp = $n->xpath('<testmessage@example.com>');
86 is_deeply(\@xp, [ qw(v1.example/1 v2.example/1) ]);
87 $n->group('v1.example');
88 my $res = $n->head(1);
89 @$res = grep(/^Xref: /, @$res);
90 like($res->[0], qr/ v1\.example:1 v2\.example:1/, 'nntp_xref works');
93 my $es = PublicInbox::ExtSearch->new("$home/extindex");
95 my $smsg = $es->over->get_art(1);
96 ok($smsg, 'got first article');
97 is($es->over->get_art(2), undef, 'only one added');
98 my $xref3 = $es->over->get_xref3(1);
99 like($xref3->[0], qr/\A\Qv2.example\E:1:/, 'order preserved 1');
100 like($xref3->[1], qr/\A\Qv1.example\E:1:/, 'order preserved 2');
101 is(scalar(@$xref3), 2, 'only to entries');
105 my ($in, $out, $err);
106 $in = $out = $err = '';
107 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
108 my $env = { MAIL_EDITOR => "$^X -i -p -e 's/test message/BEST MSG/'" };
109 my $cmd = [ qw(-edit -Ft/utf8.eml), "$home/v2test" ];
110 ok(run_script($cmd, $env, $opt), '-edit');
111 ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
113 like($err, qr/discontiguous range/, 'warned about discontiguous range');
114 my $msg1 = $es->over->get_art(1) or BAIL_OUT 'msg1 missing';
115 my $msg2 = $es->over->get_art(2) or BAIL_OUT 'msg2 missing';
116 is($msg1->{mid}, $msg2->{mid}, 'edited message indexed');
117 isnt($msg1->{blob}, $msg2->{blob}, 'blobs differ');
118 my $eml2 = $es->smsg_eml($msg2);
119 like($eml2->body, qr/BEST MSG/, 'edited body in #2');
120 unlike($eml2->body, qr/test message/, 'old body discarded in #2');
121 my $eml1 = $es->smsg_eml($msg1);
122 like($eml1->body, qr/test message/, 'original body in #1');
123 my $x1 = $es->over->get_xref3(1);
124 my $x2 = $es->over->get_xref3(2);
125 is(scalar(@$x1), 1, 'original only has one xref3');
126 is(scalar(@$x2), 1, 'new message has one xref3');
127 isnt($x1->[0], $x2->[0], 'xref3 differs');
129 my $mset = $es->mset('b:"BEST MSG"');
130 is($mset->size, 1, 'new message found');
131 $mset = $es->mset('b:"test message"');
132 is($mset->size, 1, 'old message found');
134 delete @$es{qw(git over xdb)}; # fork preparation
136 open my $rmfh, '+>', undef or BAIL_OUT $!;
138 print $rmfh $eml2->as_string or BAIL_OUT $!;
139 seek($rmfh, 0, SEEK_SET) or BAIL_OUT $!;
141 ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm');
143 ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef),
144 'extindex after rm');
145 is($es->over->get_art(2), undef, 'doc #2 gone');
146 $mset = $es->mset('b:"BEST MSG"');
147 is($mset->size, 0, 'new message gone');
150 my $misc = $es->misc;
151 my @it = $misc->mset('')->items;
152 is(scalar(@it), 2, 'two inboxes');
153 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
154 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
156 if ('remove v1test and test gc') {
157 xsys([qw(git config --unset publicinbox.v1test.inboxdir)],
158 { GIT_CONFIG => $cfg_path });
159 my $opt = { 2 => \(my $err = '') };
160 ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $opt),
162 like($err, qr/^I: remove #1 v1\.example /ms, 'removed v1 message');
163 is(scalar(grep(!/^I:/, split(/^/m, $err))), 0,
164 'no non-informational messages');
165 $misc->{xdb}->reopen;
166 @it = $misc->mset('')->items;
167 is(scalar(@it), 1, 'only one inbox left');