]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-import.t
fa40ad016b3cbad0864a0cf9f4b1f4426567e555
[public-inbox.git] / t / lei-import.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; use v5.10.1; use PublicInbox::TestCommon;
5 test_lei(sub {
6 ok(!lei(qw(import -F bogus), 't/plack-qp.eml'), 'fails with bogus format');
7 like($lei_err, qr/\bbogus unrecognized/, 'gave error message');
8
9 lei_ok(qw(q s:boolean), \'search miss before import');
10 unlike($lei_out, qr/boolean/i, 'no results, yet');
11 open my $fh, '<', 't/data/0001.patch' or BAIL_OUT $!;
12 lei_ok([qw(import -F eml -)], undef, { %$lei_opt, 0 => $fh },
13         \'import single file from stdin') or diag $lei_err;
14 close $fh;
15 lei_ok(qw(q s:boolean), \'search hit after import');
16 lei_ok(qw(q s:boolean -f mboxrd), \'blob accessible after import');
17 {
18         my $expect = [ eml_load('t/data/0001.patch') ];
19         require PublicInbox::MboxReader;
20         my @cmp;
21         open my $fh, '<', \$lei_out or BAIL_OUT "open :scalar: $!";
22         PublicInbox::MboxReader->mboxrd($fh, sub {
23                 my ($eml) = @_;
24                 $eml->header_set('Status');
25                 push @cmp, $eml;
26         });
27         is_deeply(\@cmp, $expect, 'got expected message in mboxrd');
28 }
29 lei_ok(qw(import -F eml), 't/data/message_embed.eml',
30         \'import single file by path');
31
32 lei_ok(qw(q m:testmessage@example.com));
33 is($lei_out, "[null]\n", 'no results, yet');
34 my $oid = '9bf1002c49eb075df47247b74d69bcd555e23422';
35 my $eml = eml_load('t/utf8.eml');
36 my $in = 'From x@y Fri Oct  2 00:00:00 1993'."\n".$eml->as_string;
37 lei_ok([qw(import -F eml -)], undef, { %$lei_opt, 0 => \$in });
38 lei_ok(qw(q m:testmessage@example.com));
39 is(json_utf8->decode($lei_out)->[0]->{'blob'}, $oid,
40         'got expected OID w/o From');
41
42 my $eml_str = <<'';
43 From: a@b
44 Message-ID: <x@y>
45 Status: RO
46
47 my $opt = { %$lei_opt, 0 => \$eml_str };
48 lei_ok([qw(import -F eml -)], undef, $opt,
49         \'import single file with keywords from stdin');
50 lei_ok(qw(q m:x@y));
51 my $res = json_utf8->decode($lei_out);
52 is($res->[1], undef, 'only one result');
53 is($res->[0]->{'m'}, 'x@y', 'got expected message');
54 is($res->[0]->{kw}, undef, 'Status ignored for eml');
55 lei_ok(qw(q -f mboxrd m:x@y));
56 unlike($lei_out, qr/^Status:/, 'no Status: in imported message');
57
58
59 $eml->header_set('Message-ID', '<v@y>');
60 $eml->header_set('Status', 'RO');
61 $in = 'From v@y Fri Oct  2 00:00:00 1993'."\n".$eml->as_string;
62 lei_ok([qw(import --no-kw -F mboxrd -)], undef, { %$lei_opt, 0 => \$in },
63         \'import single file with --no-kw from stdin');
64 lei(qw(q m:v@y));
65 $res = json_utf8->decode($lei_out);
66 is($res->[1], undef, 'only one result');
67 is($res->[0]->{'m'}, 'v@y', 'got expected message');
68 is($res->[0]->{kw}, undef, 'no keywords set');
69
70 $eml->header_set('Message-ID', '<k@y>');
71 $in = 'From k@y Fri Oct  2 00:00:00 1993'."\n".$eml->as_string;
72 lei_ok([qw(import -F mboxrd /dev/fd/0)], undef, { %$lei_opt, 0 => \$in },
73         \'import single file with --kw (default) from stdin');
74 lei(qw(q m:k@y));
75 $res = json_utf8->decode($lei_out);
76 is($res->[1], undef, 'only one result');
77 is($res->[0]->{'m'}, 'k@y', 'got expected message');
78 is_deeply($res->[0]->{kw}, ['seen'], "`seen' keywords set");
79
80 # see t/lei_to_mail.t for "import -F mbox*"
81 });
82 done_testing;