]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei_xsearch.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / lei_xsearch.t
1 #!perl -w
2 # Copyright (C) 2020-2021 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 v5.10.1;
6 use List::Util qw(shuffle);
7 use PublicInbox::TestCommon;
8 use PublicInbox::Eml;
9 require_mods(qw(DBD::SQLite Search::Xapian));
10 require PublicInbox::ExtSearchIdx;
11 require_git 2.6;
12 require_ok 'PublicInbox::LeiXSearch';
13 require_ok 'PublicInbox::LeiALE';
14 require_ok 'PublicInbox::LEI';
15 my ($home, $for_destroy) = tmpdir();
16 my @ibx;
17 for my $V (1..2) {
18         for my $i (3..6) {
19                 push @ibx, create_inbox("v$V-$i", indexlevel => 'full',
20                                         version => $V, sub {
21                         my ($im, $ibx) = @_;
22                         for my $j (0..9) {
23                                 my $eml = PublicInbox::Eml->new(<<EOM);
24 From: x\@example.com
25 To: $ibx->{-primary_address}
26 Date: Fri, 02 Oct 1993 0$V:0$i:0$j +0000
27 Subject: v${V}i${i}j$j
28 Message-ID: <v${V}i${i}j$j\@example>
29
30 ${V}er ${i}on j$j
31 EOM
32                                 $im->add($eml) or BAIL_OUT '->add';
33                         }
34                 }); # create_inbox
35         }
36 }
37 my $first = shift @ibx; is($first->{name}, 'v1-3', 'first plucked');
38 my $last = pop @ibx; is($last->{name}, 'v2-6', 'last plucked');
39 my $eidx = PublicInbox::ExtSearchIdx->new("$home/eidx");
40 $eidx->attach_inbox($first);
41 $eidx->attach_inbox($last);
42 $eidx->eidx_sync({fsync => 0});
43 my $es = PublicInbox::ExtSearch->new("$home/eidx");
44 my $lxs = PublicInbox::LeiXSearch->new;
45 for my $ibxish (shuffle($es, @ibx)) {
46         $lxs->prepare_external($ibxish);
47 }
48 for my $loc ($lxs->locals) {
49         $lxs->attach_external($loc);
50 }
51 my $nr = $lxs->xdb->get_doccount;
52 my $mset = $lxs->mset('d:19931002..19931003', { limit => $nr });
53 is($mset->size, $nr, 'got all messages');
54 my @msgs;
55 for my $mi ($mset->items) {
56         if (my $smsg = $lxs->smsg_for($mi)) {
57                 push @msgs, $smsg;
58         } else {
59                 diag "E: ${\$mi->get_docid} missing";
60         }
61 }
62 is(scalar(@msgs), $nr, 'smsgs retrieved for all');
63
64 $mset = $lxs->recent(undef, { limit => 1 });
65 is($mset->size, 1, 'one result');
66
67 my @ibxish = $lxs->locals;
68 is(scalar(@ibxish), scalar(@ibx) + 1, 'got locals back');
69 is($lxs->search, $lxs, '->search works');
70 is($lxs->over, undef, '->over fails');
71
72 {
73         $lxs = PublicInbox::LeiXSearch->new;
74         my $v2ibx = create_inbox 'v2full', version => 2, sub {
75                 $_[0]->add(eml_load('t/plack-qp.eml'));
76         };
77         my $v1ibx = create_inbox 'v1medium', indexlevel => 'medium',
78                                 tmpdir => "$home/v1tmp", sub {
79                 $_[0]->add(eml_load('t/utf8.eml'));
80         };
81         $lxs->prepare_external($v1ibx);
82         $lxs->prepare_external($v2ibx);
83         for my $loc ($lxs->locals) {
84                 $lxs->attach_external($loc);
85         }
86         my $mset = $lxs->mset('m:testmessage@example.com');
87         is($mset->size, 1, 'got m: match on medium+full XSearch mix');
88         my $mitem = ($mset->items)[0];
89         my $smsg = $lxs->smsg_for($mitem) or BAIL_OUT 'smsg_for broken';
90
91         my $ale = PublicInbox::LeiALE::_new("$home/ale");
92         my $lei = bless {}, 'PublicInbox::LEI';
93         $ale->refresh_externals($lxs, $lei);
94         my $exp = [ $smsg->{blob}, 'blob', -s 't/utf8.eml' ];
95         is_deeply([ $ale->git->check($smsg->{blob}) ], $exp, 'ale->git->check');
96
97         $lxs = PublicInbox::LeiXSearch->new;
98         $lxs->prepare_external($v2ibx);
99         $ale->refresh_externals($lxs, $lei);
100         is_deeply([ $ale->git->check($smsg->{blob}) ], $exp,
101                         'ale->git->check remembered inactive external');
102
103         rename("$home/v1tmp", "$home/v1moved") or BAIL_OUT "rename: $!";
104         $ale->refresh_externals($lxs, $lei);
105         is($ale->git->check($smsg->{blob}), undef,
106                         'missing after directory gone');
107 }
108
109 done_testing;