]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-import.t
No ext_urls
[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/\bis `eml', not --in-format/, '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 lei_ok('blob', $res->[0]->{blob});
58 is($lei_out, "From: a\@b\nMessage-ID: <x\@y>\n", 'got blob back');
59
60
61 $eml->header_set('Message-ID', '<v@y>');
62 $eml->header_set('Status', 'RO');
63 $in = 'From v@y Fri Oct  2 00:00:00 1993'."\n".$eml->as_string;
64 lei_ok([qw(import --no-kw -F mboxrd -)], undef, { %$lei_opt, 0 => \$in },
65         \'import single file with --no-kw from stdin');
66 lei(qw(q m:v@y));
67 $res = json_utf8->decode($lei_out);
68 is($res->[1], undef, 'only one result');
69 is($res->[0]->{'m'}, 'v@y', 'got expected message');
70 is($res->[0]->{kw}, undef, 'no keywords set');
71
72 $eml->header_set('Message-ID', '<k@y>');
73 $in = 'From k@y Fri Oct  2 00:00:00 1993'."\n".$eml->as_string;
74 lei_ok([qw(import -F mboxrd /dev/fd/0)], undef, { %$lei_opt, 0 => \$in },
75         \'import single file with --kw (default) from stdin');
76 lei(qw(q m:k@y));
77 $res = json_utf8->decode($lei_out);
78 is($res->[1], undef, 'only one result');
79 is($res->[0]->{'m'}, 'k@y', 'got expected message');
80 is_deeply($res->[0]->{kw}, ['seen'], "`seen' keywords set");
81
82 # no From, Sender, or Message-ID
83 $eml_str = <<'EOM';
84 Subject: draft message with no sender
85 References: <y@y>
86 Resent-Message-ID: <resent-test@example>
87
88 No use for a name
89 EOM
90 lei_ok([qw(import -F eml -)], undef, { %$lei_opt, 0 => \$eml_str });
91 lei_ok(['q', 's:draft message with no sender']);
92 my $draft_a = json_utf8->decode($lei_out);
93 ok(!exists $draft_a->[0]->{'m'}, 'no fake mid stored or exposed');
94 lei_ok([qw(tag -F eml - +kw:draft)], undef, { %$lei_opt, 0 => \$eml_str });
95 lei_ok(['q', 's:draft message with no sender']);
96 my $draft_b = json_utf8->decode($lei_out);
97 my $kw = delete $draft_b->[0]->{kw};
98 is_deeply($kw, ['draft'], 'draft kw set');
99 is_deeply($draft_a, $draft_b, 'fake Message-ID lookup') or
100                                 diag explain($draft_a, $draft_b);
101 lei_ok('blob', '--mail', $draft_b->[0]->{blob});
102 is($lei_out, $eml_str, 'draft retrieved by blob');
103
104
105 $eml_str = "Message-ID: <inbox\@example.com>\nSubject: label-this\n\n";
106 lei_ok([qw(import -F eml - +kw:seen +L:inbox)],
107         undef, { %$lei_opt, 0 => \$eml_str });
108 lei_ok(qw(q m:inbox@example.com));
109 $res = json_utf8->decode($lei_out);
110 is_deeply($res->[0]->{kw}, ['seen'], 'keyword set');
111 is_deeply($res->[0]->{L}, ['inbox'], 'label set');
112
113
114 # see t/lei_to_mail.t for "import -F mbox*"
115 });
116 done_testing;