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