]> Sergey Matveev's repositories - public-inbox.git/blob - t/extsearch.t
extsearchidx: enforce -index before -extindex
[public-inbox.git] / t / extsearch.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 use PublicInbox::Config;
8 use Fcntl qw(:seek);
9 my $json = PublicInbox::Config::json() or plan skip_all => 'JSON missing';
10 require_git(2.6);
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 $!;
22 [publicinboxMda]
23         spamcheck = none
24 EOF
25 close $fh or BAIL_OUT $!;
26 my $v2addr = 'v2test@example.com';
27 my $v1addr = 'v1test@example.com';
28 ok(run_script([qw(-init -Lbasic -V2 v2test --newsgroup v2.example),
29         "$home/v2test", 'http://example.com/v2test', $v2addr ]), 'v2test init');
30 my $env = { ORIGINAL_RECIPIENT => $v2addr };
31 my $eml = eml_load('t/utf8.eml');
32
33 $eml->header_set('List-Id', '<v2.example.com>');
34 open($fh, '+>', undef) or BAIL_OUT $!;
35 $fh->autoflush(1);
36 print $fh $eml->as_string or BAIL_OUT $!;
37 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
38
39 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
40
41 ok(run_script([qw(-init -V1 v1test --newsgroup v1.example), "$home/v1test",
42         'http://example.com/v1test', $v1addr ]), 'v1test init');
43
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 $!;
49
50 $env = { ORIGINAL_RECIPIENT => $v1addr };
51 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
52
53 run_script([qw(-index -Lbasic), "$home/v1test"]) or BAIL_OUT "index $?";
54
55 ok(run_script([qw(-extindex --all), "$home/extindex"]), 'extindex init');
56 {
57         my $es = PublicInbox::ExtSearch->new("$home/extindex");
58         ok($es->has_threadid, '->has_threadid');
59 }
60
61 { # TODO: -extindex should write this to config
62         open $fh, '>>', $cfg_path or BAIL_OUT $!;
63         print $fh <<EOF or BAIL_OUT $!;
64 ; for ->ALL
65 [extindex "all"]
66         topdir = $home/extindex
67 EOF
68         close $fh or BAIL_OUT $!;
69
70         my $pi_cfg = PublicInbox::Config->new;
71         $pi_cfg->fill_all;
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 },
76                 '->nntp_xref_for');
77 }
78
79 SKIP: {
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');
91 }
92
93 my $es = PublicInbox::ExtSearch->new("$home/extindex");
94 {
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');
102 }
103
104 {
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),
112                 'extindex again');
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');
128
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');
133         delete @$es{qw(git over xdb)}; # fork preparation
134
135         my $pi_cfg = PublicInbox::Config->new;
136         $pi_cfg->fill_all;
137         is(scalar($pi_cfg->ALL->mset('s:Testing')->items), 2,
138                 '2 results in ->ALL');
139         my $res = {};
140         my $nr = 0;
141         $pi_cfg->each_inbox(sub {
142                 $nr++;
143                 my ($ibx) = @_;
144                 local $SIG{__WARN__} = sub {}; # FIXME support --reindex
145                 my $mset = $ibx->isrch->mset('s:Testing');
146                 $res->{$ibx->eidx_key} = $ibx->isrch->mset_to_smsg($ibx, $mset);
147         });
148         is($nr, 2, 'two inboxes');
149         my $exp = {};
150         for my $v (qw(v1 v2)) {
151                 my $ibx = $pi_cfg->lookup_newsgroup("$v.example");
152                 my $smsg = $ibx->over->get_art(1);
153                 $smsg->psgi_cull;
154                 $exp->{"$v.example"} = [ $smsg ];
155         }
156         is_deeply($res, $exp, 'isearch limited results');
157         $pi_cfg = $res = $exp = undef;
158
159         open my $rmfh, '+>', undef or BAIL_OUT $!;
160         $rmfh->autoflush(1);
161         print $rmfh $eml2->as_string or BAIL_OUT $!;
162         seek($rmfh, 0, SEEK_SET) or BAIL_OUT $!;
163         $opt->{0} = $rmfh;
164         ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm');
165
166         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef),
167                 'extindex after rm');
168         is($es->over->get_art(2), undef, 'doc #2 gone');
169         $mset = $es->mset('b:"BEST MSG"');
170         is($mset->size, 0, 'new message gone');
171 }
172
173 my $misc = $es->misc;
174 my @it = $misc->mset('')->items;
175 is(scalar(@it), 2, 'two inboxes');
176 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
177 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
178
179 if ('inject w/o indexing') {
180         use PublicInbox::Import;
181         use PublicInbox::Search;
182         my $schema_version = PublicInbox::Search::SCHEMA_VERSION();
183         my $v1ibx = PublicInbox::Config->new->lookup_name('v1test');
184         my $last_v1_commit = $v1ibx->mm->last_commit;
185         my $v2ibx = PublicInbox::Config->new->lookup_name('v2test');
186         my $last_v2_commit = $v2ibx->mm->last_commit_xap($schema_version, 0);
187         my $git0 = PublicInbox::Git->new("$v2ibx->{inboxdir}/git/0.git");
188         chomp(my $cmt = $git0->qx(qw(rev-parse HEAD^0)));
189         is($last_v2_commit, $cmt, 'v2 index up-to-date');
190
191         my $v2im = PublicInbox::Import->new($git0, undef, undef, $v2ibx);
192         $v2im->{lock_path} = undef;
193         $v2im->{path_type} = 'v2';
194         $v2im->add(eml_load('t/mda-mime.eml'));
195         $v2im->done;
196         chomp(my $tip = $git0->qx(qw(rev-parse HEAD^0)));
197         isnt($tip, $cmt, '0.git v2 updated');
198
199         # inject a message w/o updating index
200         rename("$home/v1test/public-inbox", "$home/v1test/skip-index") or
201                 BAIL_OUT $!;
202         open(my $eh, '<', 't/iso-2202-jp.eml') or BAIL_OUT $!;
203         run_script(['-mda', '--no-precheck'], $env, { 0 => $eh}) or
204                 BAIL_OUT '-mda';
205         rename("$home/v1test/skip-index", "$home/v1test/public-inbox") or
206                 BAIL_OUT $!;
207
208         my ($in, $out, $err);
209         $in = $out = $err = '';
210         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
211         ok(run_script([qw(-extindex -v -v --all), "$home/extindex"],
212                 undef, undef), 'extindex noop');
213         $es->{xdb}->reopen;
214         my $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
215         is($mset->size, 0, 'did not attempt to index unindexed v1 message');
216         $mset = $es->mset('mid:multipart-html-sucks@11');
217         is($mset->size, 0, 'did not attempt to index unindexed v2 message');
218         ok(run_script([qw(-index --all)]), 'indexed v1 and v2 inboxes');
219
220         isnt($v1ibx->mm->last_commit, $last_v1_commit, '-index v1 worked');
221         isnt($v2ibx->mm->last_commit_xap($schema_version, 0),
222                 $last_v2_commit, '-index v2 worked');
223         ok(run_script([qw(-extindex --all), "$home/extindex"]),
224                 'extindex updates');
225
226         $es->{xdb}->reopen;
227         $mset = $es->mset('mid:199707281508.AAA24167@hoyogw.example');
228         is($mset->size, 1, 'got v1 message');
229         $mset = $es->mset('mid:multipart-html-sucks@11');
230         is($mset->size, 1, 'got v2 message');
231 }
232
233 if ('remove v1test and test gc') {
234         xsys([qw(git config --unset publicinbox.v1test.inboxdir)],
235                 { GIT_CONFIG => $cfg_path });
236         my $opt = { 2 => \(my $err = '') };
237         ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $opt),
238                 'extindex --gc');
239         like($err, qr/^I: remove #1 v1\.example /ms, 'removed v1 message');
240         is(scalar(grep(!/^I:/, split(/^/m, $err))), 0,
241                 'no non-informational messages');
242         $misc->{xdb}->reopen;
243         @it = $misc->mset('')->items;
244         is(scalar(@it), 1, 'only one inbox left');
245 }
246
247 done_testing;