lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / lei-index.t
blob2b28f1be5ae950cfd5aae59127d14430ba108238
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict; use v5.10.1; use PublicInbox::TestCommon;
5 use File::Spec;
6 require_mods(qw(lei));
7 my ($ro_home, $cfg_path) = setup_public_inboxes;
8 my ($tmpdir, $for_destroy) = tmpdir;
9 my $env = { PI_CONFIG => $cfg_path };
10 my $srv = {};
12 SKIP: {
13         require_mods(qw(-nntpd Net::NNTP), 1);
14         my $rdr = { 3 => tcp_server };
15         $srv->{nntpd} = start_script(
16                 [qw(-nntpd -W0), "--stdout=$tmpdir/n1", "--stderr=$tmpdir/n2"],
17                 $env, $rdr) or xbail "nntpd: $?";
18         $srv->{nntp_host_port} = tcp_host_port($rdr->{3});
21 SKIP: {
22         require_mods(qw(-imapd Mail::IMAPClient), 1);
23         my $rdr = { 3 => tcp_server };
24         $srv->{imapd} = start_script(
25                 [qw(-imapd -W0), "--stdout=$tmpdir/i1", "--stderr=$tmpdir/i2"],
26                 $env, $rdr) or xbail("-imapd $?");
27         $srv->{imap_host_port} = tcp_host_port($rdr->{3});
30 for ('', qw(cur new)) {
31         mkdir "$tmpdir/md/$_" or xbail "mkdir: $!";
32         mkdir "$tmpdir/md1/$_" or xbail "mkdir: $!";
34 symlink(File::Spec->rel2abs('t/plack-qp.eml'), "$tmpdir/md/cur/x:2,");
35 my $expect = do {
36         open my $fh, '<', 't/plack-qp.eml' or xbail $!;
37         local $/;
38         <$fh>;
41 # mbsync and offlineimap both put ":2," in "new/" files:
42 symlink(File::Spec->rel2abs('t/utf8.eml'), "$tmpdir/md/new/u:2,") or
43         xbail "symlink $!";
45 symlink(File::Spec->rel2abs('t/mda-mime.eml'), "$tmpdir/md1/cur/x:2,S") or
46         xbail "symlink $!";
48 test_lei({ tmpdir => $tmpdir }, sub {
49         my $store_path = "$ENV{HOME}/.local/share/lei/store/";
51         lei_ok qw(index +L:md), "$tmpdir/md";
52         lei_ok(qw(q mid:qp@example.com));
53         my $res_a = json_utf8->decode($lei_out);
54         is_deeply $res_a->[0]->{L}, [ 'md' ], 'label set on index';
55         my $blob = $res_a->[0]->{'blob'};
56         like($blob, qr/\A[0-9a-f]{40,}\z/, 'got blob from qp@example');
57         lei_ok(qw(-C / blob), $blob);
58         is($lei_out, $expect, 'got expected blob via Maildir');
59         lei_ok(qw(q mid:qp@example.com -f text));
60         like($lei_out, qr/^hi = bye/sm, 'lei2mail fallback');
62         lei_ok(qw(q mid:testmessage@example.com -f text));
63         lei_ok(qw(-C / blob --mail 9bf1002c49eb075df47247b74d69bcd555e23422));
65         my $all_obj = ['git', "--git-dir=$store_path/ALL.git",
66                         qw(cat-file --batch-check --batch-all-objects)];
67         is_deeply([xqx($all_obj)], [], 'no git objects');
68         lei_ok('import', 't/plack-qp.eml');
69         ok(grep(/\A$blob blob /, my @objs = xqx($all_obj)),
70                 'imported blob');
71         lei_ok(qw(q m:qp@example.com --dedupe=none));
72         my $res_b = json_utf8->decode($lei_out);
73         is_deeply($res_b, $res_a, 'no extra DB entries');
75         # ensure tag works on index-only messages:
76         lei_ok(qw(tag +kw:seen t/utf8.eml));
77         lei_ok(qw(q mid:testmessage@example.com));
78         is_deeply(json_utf8->decode($lei_out)->[0]->{kw},
79                 ['seen'], 'seen kw can be set on index-only message');
81         lei_ok(qw(q z:0.. -o), "$tmpdir/all-results") for (1..2);
82         is_deeply([xqx($all_obj)], \@objs,
83                 'no new objects after 2x q to trigger implicit import');
85         lei_ok 'index', "$tmpdir/md1/cur/x:2,S";
86         lei_ok qw(q m:multipart-html-sucks@11);
87         is_deeply(json_utf8->decode($lei_out)->[0]->{'kw'},
88                 ['seen'], 'keyword set');
89         lei_ok 'reindex';
90         lei_ok qw(q m:multipart-html-sucks@11);
91         is_deeply(json_utf8->decode($lei_out)->[0]->{'kw'},
92                 ['seen'], 'keyword still set after reindex');
94         $srv->{nntpd} and
95                 lei_ok('index', "nntp://$srv->{nntp_host_port}/t.v2");
96         $srv->{imapd} and
97                 lei_ok('index', "imap://$srv->{imap_host_port}/t.v2.0");
98         is_deeply([xqx($all_obj)], \@objs, 'no new objects from NNTP+IMAP');
100         lei_ok qw(q m:multipart-html-sucks@11);
101         $res_a = json_utf8->decode($lei_out)->[0];
102         is_deeply($res_a->{'kw'}, ['seen'],
103                 'keywords still set after NNTP + IMAP import');
105         # ensure import works after lms->local_blob fallback in lei/store
106         lei_ok('import', 't/mda-mime.eml');
107         lei_ok qw(q m:multipart-html-sucks@11);
108         $res_b = json_utf8->decode($lei_out)->[0];
109         my $t = xqx(['git', "--git-dir=$store_path/ALL.git",
110                         qw(cat-file -t), $res_b->{blob}]);
111         is($t, "blob\n", 'got blob');
113         lei_ok('reindex');
114         lei_ok qw(q m:multipart-html-sucks@11);
115         $res_a = json_utf8->decode($lei_out)->[0];
116         is_deeply($res_a->{'kw'}, ['seen'],
117                 'keywords still set after reindex');
120 done_testing;