]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-index.t
lei_mirror: simplify clone_v2_prep
[public-inbox.git] / t / lei-index.t
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 = {};
11
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});
19 }
20
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});
28 }
29
30 for ('', qw(cur new)) {
31         mkdir "$tmpdir/md/$_" or xbail "mkdir: $!";
32         mkdir "$tmpdir/md1/$_" or xbail "mkdir: $!";
33 }
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>;
39 };
40
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 $!";
44
45 symlink(File::Spec->rel2abs('t/mda-mime.eml'), "$tmpdir/md1/cur/x:2,S") or
46         xbail "symlink $!";
47
48 test_lei({ tmpdir => $tmpdir }, sub {
49         my $store_path = "$ENV{HOME}/.local/share/lei/store/";
50
51         lei_ok('index', "$tmpdir/md");
52         lei_ok(qw(q mid:qp@example.com));
53         my $res_a = json_utf8->decode($lei_out);
54         my $blob = $res_a->[0]->{'blob'};
55         like($blob, qr/\A[0-9a-f]{40,}\z/, 'got blob from qp@example');
56         lei_ok(qw(-C / blob), $blob);
57         is($lei_out, $expect, 'got expected blob via Maildir');
58         lei_ok(qw(q mid:qp@example.com -f text));
59         like($lei_out, qr/^hi = bye/sm, 'lei2mail fallback');
60
61         lei_ok(qw(q mid:testmessage@example.com -f text));
62         lei_ok(qw(-C / blob --mail 9bf1002c49eb075df47247b74d69bcd555e23422));
63
64         my $all_obj = ['git', "--git-dir=$store_path/ALL.git",
65                         qw(cat-file --batch-check --batch-all-objects)];
66         is_deeply([xqx($all_obj)], [], 'no git objects');
67         lei_ok('import', 't/plack-qp.eml');
68         ok(grep(/\A$blob blob /, my @objs = xqx($all_obj)),
69                 'imported blob');
70         lei_ok(qw(q m:qp@example.com --dedupe=none));
71         my $res_b = json_utf8->decode($lei_out);
72         is_deeply($res_b, $res_a, 'no extra DB entries');
73
74         # ensure tag works on index-only messages:
75         lei_ok(qw(tag +kw:seen t/utf8.eml));
76         lei_ok(qw(q mid:testmessage@example.com));
77         is_deeply(json_utf8->decode($lei_out)->[0]->{kw},
78                 ['seen'], 'seen kw can be set on index-only message');
79
80         lei_ok(qw(q z:0.. -o), "$tmpdir/all-results") for (1..2);
81         is_deeply([xqx($all_obj)], \@objs,
82                 'no new objects after 2x q to trigger implicit import');
83
84         lei_ok 'index', "$tmpdir/md1/cur/x:2,S";
85         lei_ok qw(q m:multipart-html-sucks@11);
86         is_deeply(json_utf8->decode($lei_out)->[0]->{'kw'},
87                 ['seen'], 'keyword set');
88         lei_ok 'reindex';
89         lei_ok qw(q m:multipart-html-sucks@11);
90         is_deeply(json_utf8->decode($lei_out)->[0]->{'kw'},
91                 ['seen'], 'keyword still set after reindex');
92
93         $srv->{nntpd} and
94                 lei_ok('index', "nntp://$srv->{nntp_host_port}/t.v2");
95         $srv->{imapd} and
96                 lei_ok('index', "imap://$srv->{imap_host_port}/t.v2.0");
97         is_deeply([xqx($all_obj)], \@objs, 'no new objects from NNTP+IMAP');
98
99         lei_ok qw(q m:multipart-html-sucks@11);
100         $res_a = json_utf8->decode($lei_out)->[0];
101         is_deeply($res_a->{'kw'}, ['seen'],
102                 'keywords still set after NNTP + IMAP import');
103
104         # ensure import works after lms->local_blob fallback in lei/store
105         lei_ok('import', 't/mda-mime.eml');
106         lei_ok qw(q m:multipart-html-sucks@11);
107         $res_b = json_utf8->decode($lei_out)->[0];
108         my $t = xqx(['git', "--git-dir=$store_path/ALL.git",
109                         qw(cat-file -t), $res_b->{blob}]);
110         is($t, "blob\n", 'got blob');
111
112         lei_ok('reindex');
113         lei_ok qw(q m:multipart-html-sucks@11);
114         $res_a = json_utf8->decode($lei_out)->[0];
115         is_deeply($res_a->{'kw'}, ['seen'],
116                 'keywords still set after reindex');
117 });
118
119 done_testing;