]> Sergey Matveev's repositories - public-inbox.git/blob - t/extsearch.t
t/extsearch: show a more realistic case
[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 ($home, $for_destroy) = tmpdir();
15 local $ENV{HOME} = $home;
16 mkdir "$home/.public-inbox" or BAIL_OUT $!;
17 open my $fh, '>', "$home/.public-inbox/config" or BAIL_OUT $!;
18 print $fh <<EOF or BAIL_OUT $!;
19 [publicinboxMda]
20         spamcheck = none
21 EOF
22 close $fh or BAIL_OUT $!;
23 my $v2addr = 'v2test@example.com';
24 my $v1addr = 'v1test@example.com';
25 ok(run_script([qw(-init -V2 v2test --newsgroup v2.example), "$home/v2test",
26         'http://example.com/v2test', $v2addr ]), 'v2test init');
27 my $env = { ORIGINAL_RECIPIENT => $v2addr };
28 my $eml = eml_load('t/utf8.eml');
29
30 $eml->header_set('List-Id', '<v2.example.com>');
31 open($fh, '+>', undef) or BAIL_OUT $!;
32 $fh->autoflush(1);
33 print $fh $eml->as_string or BAIL_OUT $!;
34 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
35
36 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
37
38 ok(run_script([qw(-init -V1 v1test), "$home/v1test",
39         'http://example.com/v1test', $v1addr ]), 'v1test init');
40
41 $eml->header_set('List-Id', '<v1.example.com>');
42 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
43 truncate($fh, 0) or BAIL_OUT $!;
44 print $fh $eml->as_string or BAIL_OUT $!;
45 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
46
47 $env = { ORIGINAL_RECIPIENT => $v1addr };
48 run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda';
49
50 run_script(['-index', "$home/v1test"]) or BAIL_OUT "index $?";
51
52 ok(run_script([qw(-extindex --all), "$home/extindex"]), 'extindex init');
53
54 my $es = PublicInbox::ExtSearch->new("$home/extindex");
55 {
56         my $smsg = $es->over->get_art(1);
57         ok($smsg, 'got first article');
58         is($es->over->get_art(2), undef, 'only one added');
59         my $xref3 = $es->over->get_xref3(1);
60         like($xref3->[0], qr/\A\Qv2.example\E:1:/, 'order preserved 1');
61         like($xref3->[1], qr!\A\Q$home/v1test\E:1:!, 'order preserved 2');
62         is(scalar(@$xref3), 2, 'only to entries');
63 }
64
65 {
66         my ($in, $out, $err);
67         $in = $out = $err = '';
68         my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
69         my $env = { MAIL_EDITOR => "$^X -i -p -e 's/test message/BEST MSG/'" };
70         my $cmd = [ qw(-edit -Ft/utf8.eml), "$home/v2test" ];
71         ok(run_script($cmd, $env, $opt), '-edit');
72         ok(run_script([qw(-extindex --all), "$home/extindex"], undef, $opt),
73                 'extindex again');
74         like($err, qr/discontiguous range/, 'warned about discontiguous range');
75         my $msg1 = $es->over->get_art(1) or BAIL_OUT 'msg1 missing';
76         my $msg2 = $es->over->get_art(2) or BAIL_OUT 'msg2 missing';
77         is($msg1->{mid}, $msg2->{mid}, 'edited message indexed');
78         isnt($msg1->{blob}, $msg2->{blob}, 'blobs differ');
79         my $eml2 = $es->smsg_eml($msg2);
80         like($eml2->body, qr/BEST MSG/, 'edited body in #2');
81         unlike($eml2->body, qr/test message/, 'old body discarded in #2');
82         my $eml1 = $es->smsg_eml($msg1);
83         like($eml1->body, qr/test message/, 'original body in #1');
84         my $x1 = $es->over->get_xref3(1);
85         my $x2 = $es->over->get_xref3(2);
86         is(scalar(@$x1), 1, 'original only has one xref3');
87         is(scalar(@$x2), 1, 'new message has one xref3');
88         isnt($x1->[0], $x2->[0], 'xref3 differs');
89 }
90
91 my $misc = $es->misc;
92 my @it = $misc->mset('')->items;
93 is(scalar(@it), 2, 'two inboxes');
94 like($it[0]->get_document->get_data, qr/v2test/, 'docdata matched v2');
95 like($it[1]->get_document->get_data, qr/v1test/, 'docdata matched v1');
96 my $pi_cfg = PublicInbox::Config->new;
97 $pi_cfg->fill_all;
98 my $ret = $misc->newsgroup_matches('', $pi_cfg);
99 is_deeply($pi_cfg->{-by_newsgroup}, $ret, '->newsgroup_matches');
100
101 done_testing;