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');
58 { # TODO: -extindex should write this to config
59 open $fh, '>>', $cfg_path or BAIL_OUT $!;
60 print $fh <<EOF or BAIL_OUT $!;
63 topdir = $home/extindex
65 close $fh or BAIL_OUT $!;
67 my $pi_cfg = PublicInbox::Config->new;
69 ok($pi_cfg->ALL, '->ALL');
70 my $ibx = $pi_cfg->{-by_newsgroup}->{'v2.example'};
71 my $ret = $pi_cfg->ALL->nntp_xref_for($ibx, $ibx->over->get_art(1));
72 is_deeply($ret, ['v1.example:1'], '->nntp_xref_for');
76 require_mods(qw(Net::NNTP), 1);
77 my ($out, $err) = ("$home/nntpd.out.log", "$home/nntpd.err.log");
78 my $cmd = [ '-nntpd', '-W0', "--stdout=$out", "--stderr=$err" ];
79 my $td = start_script($cmd, undef, { 3 => $sock });
80 my $n = Net::NNTP->new($host_port);
81 my @xp = $n->xpath('<testmessage@example.com>');
82 is_deeply(\@xp, [ qw(v1.example/1 v2.example/1) ]);
83 $n->group('v1.example');
84 my $res = $n->head(1);
85 @$res = grep(/^Xref: /, @$res);
86 like($res->[0], qr/ v1\.example:1 v2\.example:1/, 'nntp_xref works');
89 my $es = PublicInbox::ExtSearch->new("$home/extindex");
91 my $smsg = $es->over->get_art(1);
92 ok($smsg, 'got first article');
93 is($es->over->get_art(2), undef, 'only one added');
94 my $xref3 = $es->over->get_xref3(1);
95 like($xref3->[0], qr/\A\Qv2.example\E:1:/, 'order preserved 1');
96 like($xref3->[1], qr/\A\Qv1.example\E:1:/, 'order preserved 2');
97 is(scalar(@$xref3), 2, 'only to entries');
101 my ($in, $out, $err);
102 $in = $out = $err = '';
103 my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
104 my $env = { MAIL_EDITOR => "$^X -i -p -e 's/test message/BEST MSG/'" };
105 my $cmd = [ qw(-edit -Ft/utf8.eml), "$home/v2test" ];
106 ok(run_script($cmd, $env, $opt), '-edit');
107 ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
109 like($err, qr/discontiguous range/, 'warned about discontiguous range');
110 my $msg1 = $es->over->get_art(1) or BAIL_OUT 'msg1 missing';
111 my $msg2 = $es->over->get_art(2) or BAIL_OUT 'msg2 missing';
112 is($msg1->{mid}, $msg2->{mid}, 'edited message indexed');
113 isnt($msg1->{blob}, $msg2->{blob}, 'blobs differ');
114 my $eml2 = $es->smsg_eml($msg2);
115 like($eml2->body, qr/BEST MSG/, 'edited body in #2');
116 unlike($eml2->body, qr/test message/, 'old body discarded in #2');
117 my $eml1 = $es->smsg_eml($msg1);
118 like($eml1->body, qr/test message/, 'original body in #1');
119 my $x1 = $es->over->get_xref3(1);
120 my $x2 = $es->over->get_xref3(2);
121 is(scalar(@$x1), 1, 'original only has one xref3');
122 is(scalar(@$x2), 1, 'new message has one xref3');
123 isnt($x1->[0], $x2->[0], 'xref3 differs');
125 my $mset = $es->mset('b:"BEST MSG"');
126 is($mset->size, 1, 'new message found');
127 $mset = $es->mset('b:"test message"');
128 is($mset->size, 1, 'old message found');
130 delete @$es{qw(git over xdb)}; # fork preparation
132 open my $rmfh, '+>', undef or BAIL_OUT $!;
134 print $rmfh $eml2->as_string or BAIL_OUT $!;
135 seek($rmfh, 0, SEEK_SET) or BAIL_OUT $!;
137 ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm');
139 ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef),
140 'extindex after rm');
141 is($es->over->get_art(2), undef, 'doc #2 gone');
142 $mset = $es->mset('b:"BEST MSG"');
143 is($mset->size, 0, 'new message gone');
146 my $misc = $es->misc;
147 my @it = $misc->mset('')->items;
148 is(scalar(@it), 2, 'two inboxes');
149 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
150 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
152 if ('remove v1test and test gc') {
153 xsys([qw(git config --unset publicinbox.v1test.inboxdir)],
154 { GIT_CONFIG => $cfg_path });
155 my $opt = { 2 => \(my $err = '') };
156 ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $opt),
158 like($err, qr/^I: remove #1 v1\.example /ms, 'removed v1 message');
159 is(scalar(grep(!/^I:/, split(/^/m, $err))), 0,
160 'no non-informational messages');
161 $misc->{xdb}->reopen;
162 @it = $misc->mset('')->items;
163 is(scalar(@it), 1, 'only one inbox left');