summaryrefslogtreecommitdiff
path: root/t/Extract.t
blob: 3a18395908b4b3be3b2e650af61caf24c5ee1e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use v5.22;
use warnings;
use utf8;

use Test::More;

use JSON::PP 'decode_json';
use List::Util 'min';


my $EXTRACT = {
    perl_mail_box  => 'perl script/qmauth.pl',
    python_mailbox => 'python script/qmauth.py',
    rust_maildir   => 'extract/target/debug/jwebmail-extract',
}->{python_mailbox};
my $MAILDIR = 't/';
my $SYS_USER = $ENV{USER};
my $MAIL_USER = 'maildir';

my $PROG = "$EXTRACT $MAILDIR $SYS_USER $MAIL_USER";

my $MAIL_COUNT = do {
    opendir(my $dh, "$MAILDIR/$MAIL_USER/cur");
    my @all = grep { /^[^.]/ } readdir($dh);
    closedir($dh);
    local $" = ' ';
    note "@all";
    @all
};

subtest start => sub {
    my @res = `$PROG invalid 2>/dev/null`;

    isnt $? >> 8, 0;

    if ($? >> 8 == 3) {
        is scalar(@res), 1;
        my $result = decode_json @res;
        ok exists $result->{error};
    }
};

subtest folders => sub {
    my @res = `$PROG folders`;

    is($? >> 8, 0);
    is @res, 1;
    my $result = decode_json $res[0];

    is(@$result, 2);
};

subtest count => sub {
    my @res = `$PROG count ''`;

    is($? >> 8, 0);
    is @res, 1;
    my $result = decode_json $res[0];

    is($result->{total_mails}, $MAIL_COUNT);
};

subtest list => sub {
    my $s = "$PROG list '' 0 4 date";
    my @res = `$s`;

    is($? >> 8, 0);
    is @res, 1;
    my $result = decode_json $res[0];

    is(@$result, min($MAIL_COUNT, 4));
    ok($result->[0]{message_handle});
    ok($result->[0]{head}{from}) or diag $s;
    ok($result->[0]{head}{to});
};

subtest read => sub {
    my $folder = '';
    my @pre_res = `$PROG list '$folder' 0 10 ''`;

    is($? >> 8, 0);
    is @pre_res, 1;
    my $pre_result = decode_json $pre_res[0];
    ok(my $mid = $pre_result->[0]{message_handle});

    my @res = `$PROG read '$folder' '$mid'`;

    is($? >> 8, 0, "read exit code") or (diag @res, return);
    is @res, 1;
    my $result = decode_json $res[0];

    is_deeply(
        $result->{head}{from},
        [{address => 'xy@example.com', display_name => 'Moderator-Address'}],
    );
    ok(exists $result->{head}{date});
    is($result->{head}{date}, '1994-03-22T13:34:51+00:00');
};

done_testing;